NeoPZ
pzvec_extras.h
Go to the documentation of this file.
1 
6 #ifndef PZVEC_EXTRAS_H
7 #define PZVEC_EXTRAS_H
8 
9 #include <algorithm>
10 #include "pzstack.h"
11 
12 
23 template< class T1, class T2, class Scalar >
24 void saxpy(TPZVec< T1 >& x, const TPZVec< T2 >& y, Scalar s) {
25  int size = x.NElements();
26 
27 #ifdef PZDEBUG
28  if (size != y.NElements()) {
29  PZError << "SAXPY error!" << std::endl
30  << "Vectors with different sizes #x = " << size
31  << ", #y = " << y.NElements() << std::endl;
32 
33  PZError.flush();
34  }
35 #endif
36 
37  for (int ii = 0; ii < size; ii++) {
38  x[ ii ] += s * y[ ii ];
39  }
40 }
41 
48 template< class T1, class Scalar >
49 void sscal(TPZVec< T1 > & x, const Scalar s) {
50 #ifdef BLAS
51  //The blas method's call shall be done here.
52 #endif //BLAS
53 
54  int size = x.NElements();
55 
56  for (int ii = 0; ii < size; ii++) {
57  x[ii] *= s;
58  }
59 }
60 
64 template <class T>
65 TPZVec<T> operator-(const TPZVec<T> &a, const TPZVec<T> &b) {
66  if (a.size() != b.size()) {
67  DebugStop();
68  }
69  TPZVec<T> result(a.size());
70  for (int i = 0; i < a.size(); i++) {
71  result[i] = a[i] - b[i];
72  }
73  return result;
74 }
75 
79 template <class T>
81  if (a.size() != b.size()) {
82  DebugStop();
83  }
84  for (int i = 0; i < a.size(); i++) {
85  a[i] -= b[i];
86  }
87  return a;
88 }
89 
97 template< class T1 >
98 double sdot(TPZVec< T1 > & x, TPZVec< T1 > & y) {
99 #ifdef BLAS
100  //The blas method's call shall be done here.
101 #endif //BLAS
102 
103  int size = x.NElements();
104  double sum = 0.0;
105 
106 #ifndef NODEBUG
107  if (size != y.NElements()) {
108  PZError << "SDOT error!" << std::endl
109  << "Vectors with different sizes #x = " << size
110  << ", #y = " << y.NElements() << std::endl;
111 
112  PZError.flush();
113  }
114 #endif
115 
116  for (int ii = 0; ii < size; ii++) {
117  sum += x[ii] * y[ii];
118  }
119 
120  return sum;
121 }
122 
123 template < class T1 >
124 REAL dist(TPZVec<T1> &vec1, TPZVec<T1> &vec2) {
125 #ifdef PZDEBUG
126  if (vec1.size() != vec2.size()) {
127  DebugStop();
128  }
129 #endif
130  REAL dist = 0.;
131  for (int i = 0; i < vec1.size(); i++) {
132  dist += (vec1[i] - vec2[i])*(vec1[i] - vec2[i]);
133  }
134  dist = sqrt(dist);
135  return dist;
136 }
137 
138 //--| SORTING |-----------------------------------------------------------------
139 
141 template< class T >
143  std::sort(static_cast<T*> (v.begin()), v.begin() + v.NElements());
144 
145  return v;
146 }
147 
149 template< class T >
150 int Find(TPZVec< T >& v, const T& e) {
151  T* found = std::find(static_cast<T*> (v), v + v.NElements(), e);
152 
153  int dist = distance(static_cast<T*> (v), found);
154 
155  return (dist == v.NElements() ? -1 : dist);
156 }
157 
159 template< class T >
161  int nel = v.NElements();
162 
163  T m = v[ 0 ];
164 
165  for (int ii = 1; ii < nel; ii++) {
166  m = min(m, v[ ii ]);
167  }
168 
169  return m;
170 }
171 
173 template< class T >
175  int nel = v.NElements();
176 
177  T m = v[ 0 ];
178 
179  for (int ii = 1; ii < nel; ii++) {
180  m = max(m, v[ ii ]);
181  }
182 
183  return m;
184 }
185 
187 template< class T, int N >
188 void Intersect(const TPZVec< T > &one, const TPZVec< T > &two, TPZStack< T, N > &result) {
189  int firstc, secondc, nfirst, nsecond;
190  nfirst = one.NElements();
191  nsecond = two.NElements();
192  firstc = 0;
193  secondc = 0;
194  while (firstc < nfirst && secondc < nsecond) {
195  while (firstc < nfirst && one[firstc] < two[secondc]) {
196  firstc++;
197  }
198  if (firstc == nfirst) break;
199  while (secondc < nsecond && two[secondc] < one[firstc]) {
200  secondc++;
201  }
202  if (firstc < nfirst && secondc < nsecond && one[firstc] == two[secondc]) {
203  result.Push(one[firstc]);
204  firstc++;
205  secondc++;
206  }
207  }
208 
209 }
210 
212 template< class T, int N >
213 void Intersect(const TPZVec< T > &one, const TPZVec< T > &two, const TPZVec< T > &three, TPZStack< T, N > &result) {
214  int firstc, secondc, thirdc, nfirst, nsecond, nthird;
215  nfirst = one.NElements();
216  nsecond = two.NElements();
217  nthird = three.NElements();
218  firstc = 0;
219  secondc = 0;
220  thirdc = 0;
221  while (firstc < nfirst && secondc < nsecond && thirdc < nthird) {
222  while (firstc < nfirst && (one[firstc] < two[secondc] || one[firstc] < three[thirdc])) {
223  firstc++;
224  }
225  if (firstc == nfirst)break;
226  while (secondc < nsecond && (two[secondc] < one[firstc] || two[secondc] < three[thirdc])) {
227  secondc++;
228  }
229  if (secondc == nsecond) break;
230  while (thirdc < nthird && (three[thirdc] < one[firstc] || three[thirdc] < two[secondc])) {
231  thirdc++;
232  }
233  if (firstc < nfirst && secondc < nsecond && thirdc < nthird && one[firstc] == two[secondc] && one[firstc] == three[thirdc]) {
234  result.Push(one[firstc]);
235  firstc++;
236  secondc++;
237  thirdc++;
238  }
239  }
240 
241 }
242 
244 template< class T>
245 T Norm(const TPZVec< T > &one) {
246  T res = 0.;
247  int size = one.NElements();
248  for (int i = 0; i < size; i++) {
249  res += one[i] * one[i];
250  }
251  return sqrt(res);
252 }
253 
254 template<class T>
255 void Cross(const TPZVec <T> &x1, const TPZVec<T> &x2, TPZVec<T> &result) {
256 #ifdef PZDEBUG
257  if (x1.size() != 3) {
258  DebugStop();
259  }
260  if (x2.size() != 3) {
261  DebugStop();
262  }
263  if (result.size() != 3) {
264  DebugStop();
265  }
266 #endif
267  result[0] = x1[1] * x2[2] - x2[1] * x1[2];
268  result[1] = x1[2] * x2[0] - x2[2] * x1[0];
269  result[2] = x1[0] * x2[1] - x2[0] * x1[1];
270 }
271 
272 template<class T>
273 T Dot(const TPZVec <T> &x1, const TPZVec<T> &x2) {
274  T result = T(0.);
275  int size = x1.NElements();
276 
277 #ifdef PZDEBUG
278  if (size != x2.NElements()) {
279  PZError << "Dot error!" << std::endl
280  << "Vectors with different sizes #x1 = " << size
281  << ", #x2 = " << x2.NElements() << std::endl;
282 
283  PZError.flush();
284  }
285 #endif
286 
287  for (int i = 0; i < size; ++i) {
288  result = result + x1[i] * x2[i];
289  }
290  return result;
291 }
292 
295 #endif //PZVEC_EXTRAS_H
296 
TPZVec< T > & Sort(TPZVec< T > &v)
Sorting the elements into v.
Definition: pzvec_extras.h:142
TPZVec< T > & operator-=(TPZVec< T > &a, const TPZVec< T > &b)
substracts two vectors
Definition: pzvec_extras.h:80
int Find(TPZVec< T > &v, const T &e)
Finds if exists the element e into vector v.
Definition: pzvec_extras.h:150
double sdot(TPZVec< T1 > &x, TPZVec< T1 > &y)
Performs a sdot operation: dot <- Transpose[x] * y.
Definition: pzvec_extras.h:98
This class implements a simple vector storage scheme for a templated class T. Utility.
Definition: pzgeopoint.h:19
void saxpy(TPZVec< T1 > &x, const TPZVec< T2 > &y, Scalar s)
Performs a saxpy operation: x <- x + s * y.
Definition: pzvec_extras.h:24
TPZVec< T > operator-(const TPZVec< T > &a, const TPZVec< T > &b)
substracts two vectors
Definition: pzvec_extras.h:65
T Norm(const TPZVec< T > &one)
Gets commom elements into the one and two vectors.
Definition: pzvec_extras.h:245
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
void Push(const T object)
Pushes a copy of the object on the stack.
Definition: pzstack.h:80
void Intersect(const TPZVec< T > &one, const TPZVec< T > &two, TPZStack< T, N > &result)
Gets commom elements into the one and two vectors.
Definition: pzvec_extras.h:188
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
T Max(TPZVec< T > &v)
Returns the maximum element into v.
Definition: pzvec_extras.h:174
void sscal(TPZVec< T1 > &x, const Scalar s)
Performs a sscal operation: x <- x * s.
Definition: pzvec_extras.h:49
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sqrt
Definition: tfadfunc.h:120
string res
Definition: test.py:151
T * begin() const
Casting operator. Returns The fStore pointer.
Definition: pzvec.h:450
A simple stack.
T Min(TPZVec< T > &v)
Returns the minimum element into v.
Definition: pzvec_extras.h:160
T Dot(const TPZVec< T > &x1, const TPZVec< T > &x2)
Definition: pzvec_extras.h:273
REAL dist(TPZVec< T1 > &vec1, TPZVec< T1 > &vec2)
Definition: pzvec_extras.h:124
This class implements a stack object. Utility.
Definition: pzcheckmesh.h:14
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
clarg::argString m("-m", "input matrix file name (text format)", "matrix.txt")
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15
void Cross(const TPZVec< T > &x1, const TPZVec< T > &x2, TPZVec< T > &result)
Definition: pzvec_extras.h:255