Nektar++
Vmath.hpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Vmath.hpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Collection of templated functions for vector mathematics
32 //
33 // Note: For those unfamiliar with the vector routines notation, it is
34 // reverse polish notation (RPN). For example:
35 //
36 // In the function "Vvtvp()", it is "Vvt" means vector vector times,
37 // which in infix notation is "v * v". So "Vvtvp" is:
38 //
39 // RPN: vector vector times vector plus
40 // Infix: ( vector * vector ) + vector
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
45 #define NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
46 
49 
50 namespace Vmath
51 {
52 /***************** Math routines ***************/
53 
54 /// \brief Fill a vector with a constant value
55 template <class T>
56 LIB_UTILITIES_EXPORT void Fill(int n, const T alpha, T *x, const int incx);
57 
58 /// \brief Generates a number from ~Normal(0,1)
59 template <class T> LIB_UTILITIES_EXPORT T ran2(long *idum);
60 
61 /// \brief Fills a vector with white noise.
62 template <class T>
63 LIB_UTILITIES_EXPORT void FillWhiteNoise(int n, const T eps, T *x,
64  const int incx, int seed = 9999);
65 
66 /// \brief Multiply vector z = x*y
67 template <class T>
68 LIB_UTILITIES_EXPORT void Vmul(int n, const T *x, const int incx, const T *y,
69  const int incy, T *z, const int incz);
70 
71 /// \brief Scalar multiply y = alpha*y
72 
73 template <class T>
74 LIB_UTILITIES_EXPORT void Smul(int n, const T alpha, const T *x, const int incx,
75  T *y, const int incy);
76 
77 /// \brief Multiply vector z = x/y
78 template <class T>
79 LIB_UTILITIES_EXPORT void Vdiv(int n, const T *x, const int incx, const T *y,
80  const int incy, T *z, const int incz);
81 
82 /// \brief Scalar multiply y = alpha/y
83 template <class T>
84 LIB_UTILITIES_EXPORT void Sdiv(int n, const T alpha, const T *x, const int incx,
85  T *y, const int incy);
86 
87 /// \brief Add vector z = x+y
88 template <class T>
89 LIB_UTILITIES_EXPORT void Vadd(int n, const T *x, const int incx, const T *y,
90  const int incy, T *z, const int incz);
91 
92 /// \brief Add vector y = alpha + x
93 template <class T>
94 LIB_UTILITIES_EXPORT void Sadd(int n, const T alpha, const T *x, const int incx,
95  T *y, const int incy);
96 
97 /// \brief Subtract vector z = x-y
98 template <class T>
99 LIB_UTILITIES_EXPORT void Vsub(int n, const T *x, const int incx, const T *y,
100  const int incy, T *z, const int incz);
101 
102 /// \brief Add vector y = alpha - x
103 template <class T>
104 LIB_UTILITIES_EXPORT void Ssub(int n, const T alpha, const T *x, const int incx,
105  T *y, const int incy);
106 
107 /// \brief Zero vector
108 template <class T> LIB_UTILITIES_EXPORT void Zero(int n, T *x, const int incx);
109 
110 /// \brief Negate x = -x
111 template <class T> LIB_UTILITIES_EXPORT void Neg(int n, T *x, const int incx);
112 
113 template <class T>
114 void Vlog(int n, const T *x, const int incx, T *y, const int incy)
115 {
116  while (n--)
117  {
118  *y = log(*x);
119  x += incx;
120  y += incy;
121  }
122 }
123 
124 template <class T>
125 void Vexp(int n, const T *x, const int incx, T *y, const int incy)
126 {
127  while (n--)
128  {
129  *y = exp(*x);
130  x += incx;
131  y += incy;
132  }
133 }
134 
135 template <class T>
136 void Vpow(int n, const T *x, const int incx, const T f, T *y, const int incy)
137 {
138  while (n--)
139  {
140  *y = pow(*x, f);
141  x += incx;
142  y += incy;
143  }
144 }
145 
146 /// \brief sqrt y = sqrt(x)
147 template <class T>
148 LIB_UTILITIES_EXPORT void Vsqrt(int n, const T *x, const int incx, T *y,
149  const int incy);
150 
151 /// \brief vabs: y = |x|
152 template <class T>
153 LIB_UTILITIES_EXPORT void Vabs(int n, const T *x, const int incx, T *y,
154  const int incy);
155 
156 /********** Triad routines ***********************/
157 
158 /// \brief vvtvp (vector times vector plus vector): z = w*x + y
159 template <class T>
160 LIB_UTILITIES_EXPORT void Vvtvp(int n, const T *w, const int incw, const T *x,
161  const int incx, const T *y, const int incy,
162  T *z, const int incz);
163 
164 /// \brief vvtvm (vector times vector plus vector): z = w*x - y
165 template <class T>
166 LIB_UTILITIES_EXPORT void Vvtvm(int n, const T *w, const int incw, const T *x,
167  const int incx, const T *y, const int incy,
168  T *z, const int incz);
169 
170 /// \brief Svtvp (scalar times vector plus vector): z = alpha*x + y
171 template <class T>
172 LIB_UTILITIES_EXPORT void Svtvp(int n, const T alpha, const T *x,
173  const int incx, const T *y, const int incy,
174  T *z, const int incz);
175 
176 /// \brief Svtvm (scalar times vector minus vector): z = alpha*x - y
177 template <class T>
178 LIB_UTILITIES_EXPORT void Svtvm(int n, const T alpha, const T *x,
179  const int incx, const T *y, const int incy,
180  T *z, const int incz);
181 
182 /// \brief vvtvvtp (vector times vector plus vector times vector):
183 // z = v*w + x*y
184 //
185 // @param n Number of items in each vector.
186 template <class T>
187 LIB_UTILITIES_EXPORT void Vvtvvtp(int n, const T *v, int incv, const T *w,
188  int incw, const T *x, int incx, const T *y,
189  int incy, T *z, int incz);
190 
191 /// \brief vvtvvtm (vector times vector minus vector times vector):
192 // z = v*w - x*y
193 template <class T>
194 LIB_UTILITIES_EXPORT void Vvtvvtm(int n, const T *v, int incv, const T *w,
195  int incw, const T *x, int incx, const T *y,
196  int incy, T *z, int incz);
197 /// \brief Svtsvtp (scalar times vector plus scalar times vector):
198 // z = alpha*x + beta*y
199 template <class T>
200 LIB_UTILITIES_EXPORT void Svtsvtp(int n, const T alpha, const T *x, int incx,
201  const T beta, const T *y, int incy, T *z,
202  int incz);
203 
204 /// \brief Vstvpp (scalar times vector plus vector plus vector):
205 // z = v*w + x*y
206 template <class T>
207 LIB_UTILITIES_EXPORT void Vstvpp(int n, const T alpha, const T *v, int incv,
208  const T *w, int incw, const T *x, int incx,
209  T *z, int incz);
210 
211 /************ Misc routine from Veclib (and extras) ************/
212 
213 /// \brief Gather vector z[i] = x[y[i]]
214 template <class T, class I,
215  typename = typename std::enable_if<std::is_floating_point<T>::value &&
216  std::is_integral<I>::value>::type>
217 void Gathr(I n, const T *x, const I *y, T *z)
218 {
219  while (n--)
220  {
221  *z++ = *(x + *y++);
222  }
223  return;
224 }
225 
226 /// \brief Gather vector z[i] = sign[i]*x[y[i]]
227 template <class T>
228 LIB_UTILITIES_EXPORT void Gathr(int n, const T *sign, const T *x, const int *y,
229  T *z);
230 
231 /// \brief Scatter vector z[y[i]] = x[i]
232 template <class T>
233 LIB_UTILITIES_EXPORT void Scatr(int n, const T *x, const int *y, T *z);
234 
235 /// \brief Scatter vector z[y[i]] = sign[i]*x[i]
236 template <class T>
237 LIB_UTILITIES_EXPORT void Scatr(int n, const T *sign, const T *x, const int *y,
238  T *z);
239 
240 /// \brief Assemble z[y[i]] += x[i]; z should be zero'd first
241 template <class T>
242 LIB_UTILITIES_EXPORT void Assmb(int n, const T *x, const int *y, T *z);
243 
244 /// \brief Assemble z[y[i]] += sign[i]*x[i]; z should be zero'd first
245 template <class T>
246 LIB_UTILITIES_EXPORT void Assmb(int n, const T *sign, const T *x, const int *y,
247  T *z);
248 
249 /************* Reduction routines *****************/
250 
251 /// \brief Subtract return sum(x)
252 template <class T>
253 LIB_UTILITIES_EXPORT T Vsum(int n, const T *x, const int incx);
254 
255 /// \brief Return the index of the maximum element in x
256 template <class T>
257 LIB_UTILITIES_EXPORT int Imax(int n, const T *x, const int incx);
258 
259 /// \brief Return the maximum element in x -- called vmax to avoid
260 /// conflict with max
261 template <class T>
262 LIB_UTILITIES_EXPORT T Vmax(int n, const T *x, const int incx);
263 
264 /// \brief Return the index of the maximum absolute element in x
265 template <class T>
266 LIB_UTILITIES_EXPORT int Iamax(int n, const T *x, const int incx);
267 
268 /// \brief Return the maximum absolute element in x
269 /// called vamax to avoid conflict with max
270 template <class T>
271 LIB_UTILITIES_EXPORT T Vamax(int n, const T *x, const int incx);
272 
273 /// \brief Return the index of the minimum element in x
274 template <class T>
275 LIB_UTILITIES_EXPORT int Imin(int n, const T *x, const int incx);
276 
277 /// \brief Return the minimum element in x - called vmin to avoid
278 /// conflict with min
279 template <class T>
280 LIB_UTILITIES_EXPORT T Vmin(int n, const T *x, const int incx);
281 
282 /// \brief Return number of NaN elements of x
283 template <class T>
284 LIB_UTILITIES_EXPORT int Nnan(int n, const T *x, const int incx);
285 
286 /// \brief vvtvp (vector times vector times vector): z = w*x*y
287 template <class T> LIB_UTILITIES_EXPORT T Dot(int n, const T *w, const T *x);
288 
289 /// \brief vvtvp (vector times vector times vector): z = w*x*y
290 template <class T>
291 LIB_UTILITIES_EXPORT T Dot(int n, const T *w, const int incw, const T *x,
292  const int incx);
293 
294 /// \brief vvtvp (vector times vector times vector): z = w*x*y
295 template <class T>
296 LIB_UTILITIES_EXPORT T Dot2(int n, const T *w, const T *x, const int *y);
297 
298 /// \brief vvtvp (vector times vector times vector): z = w*x*y
299 template <class T>
300 LIB_UTILITIES_EXPORT T Dot2(int n, const T *w, const int incw, const T *x,
301  const int incx, const int *y, const int incy);
302 
303 /********** Memory routines ***********************/
304 
305 // \brief copy one vector to another
306 template <class T>
307 LIB_UTILITIES_EXPORT void Vcopy(int n, const T *x, const int incx, T *y,
308  const int incy);
309 
310 // \brief reverse the ordering of vector to another
311 template <class T>
312 LIB_UTILITIES_EXPORT void Reverse(int n, const T *x, const int incx, T *y,
313  const int incy);
314 
315 } // namespace Vmath
316 #endif // VECTORMATH_HPP
#define LIB_UTILITIES_EXPORT
#define sign(a, b)
return the sign(b)*a
Definition: Polylib.cpp:49
@ beta
Gauss Radau pinned at x=-1,.
Definition: PointsType.h:61
Definition: Vmath.cpp:40
void Vsqrt(int n, const T *x, const int incx, T *y, const int incy)
sqrt y = sqrt(x)
Definition: Vmath.cpp:534
void Ssub(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Substract vector y = alpha - x.
Definition: Vmath.cpp:458
void Svtsvtp(int n, const T alpha, const T *x, int incx, const T beta, const T *y, int incy, T *z, int incz)
svtvvtp (scalar times vector plus scalar times vector):
Definition: Vmath.cpp:751
void Vmul(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x*y.
Definition: Vmath.cpp:209
void Vlog(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:114
void Vexp(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:125
void Svtvp(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
svtvp (scalar times vector plus vector): z = alpha*x + y
Definition: Vmath.cpp:622
void Vabs(int n, const T *x, const int incx, T *y, const int incy)
vabs: y = |x|
Definition: Vmath.cpp:553
T Dot2(int n, const T *w, const T *x, const int *y)
dot2 (vector times vector times vector): z = w*x*y
Definition: Vmath.cpp:1147
void Neg(int n, T *x, const int incx)
Negate x = -x.
Definition: Vmath.cpp:518
T Vmin(int n, const T *x, const int incx)
Return the minimum element in x - called vmin to avoid conflict with min.
Definition: Vmath.cpp:1050
void Vvtvp(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvp (vector times vector plus vector): z = w*x + y
Definition: Vmath.cpp:574
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:895
void Scatr(int n, const T *x, const int *y, T *z)
Scatter vector z[y[i]] = x[i].
Definition: Vmath.cpp:822
T Dot(int n, const T *w, const T *x)
dot (vector times vector): z = w*x
Definition: Vmath.cpp:1100
void Gathr(int n, const T *sign, const T *x, const int *y, T *z)
Gather vector z[i] = sign[i]*x[y[i]].
Definition: Vmath.cpp:805
void Assmb(int n, const T *x, const int *y, T *z)
Assemble z[y[i]] += x[i]; z should be zero'd first.
Definition: Vmath.cpp:862
void Svtvm(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
svtvp (scalar times vector minus vector): z = alpha*x - y
Definition: Vmath.cpp:664
void Vadd(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Add vector z = x+y.
Definition: Vmath.cpp:359
void Vvtvm(int n, const T *w, const int incw, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
vvtvm (vector times vector minus vector): z = w*x - y
Definition: Vmath.cpp:598
void Vvtvvtm(int n, const T *v, int incv, const T *w, int incw, const T *x, int incx, const T *y, int incy, T *z, int incz)
vvtvvtm (vector times vector minus vector times vector):
Definition: Vmath.cpp:721
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
Definition: Vmath.cpp:248
void Sdiv(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha/y.
Definition: Vmath.cpp:324
int Imax(int n, const T *x, const int incx)
Return the index of the maximum element in x.
Definition: Vmath.cpp:918
void Vdiv(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Multiply vector z = x/y.
Definition: Vmath.cpp:284
int Imin(int n, const T *x, const int incx)
Return the index of the minimum element in x.
Definition: Vmath.cpp:1023
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:492
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition: Vmath.cpp:45
T ran2(long *idum)
Generates a number from ~Normal(0,1)
Definition: Vmath.cpp:75
void Vstvpp(int n, const T alpha, const T *v, int incv, const T *w, int incw, const T *x, int incx, T *z, int incz)
Vstvpp (scalar times vector plus vector plus vector):
Definition: Vmath.cpp:777
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:155
int Nnan(int n, const T *x, const int incx)
Return number of NaN elements of x.
Definition: Vmath.cpp:1076
T Vamax(int n, const T *x, const int incx)
Return the maximum absolute element in x called vamax to avoid conflict with max.
Definition: Vmath.cpp:999
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add scalar y = alpha + x.
Definition: Vmath.cpp:384
void Reverse(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1286
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
Definition: Vmath.cpp:945
void Vvtvvtp(int n, const T *v, int incv, const T *w, int incw, const T *x, int incx, const T *y, int incy, T *z, int incz)
vvtvvtp (vector times vector plus vector times vector):
Definition: Vmath.cpp:692
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1255
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:419
void Vpow(int n, const T *x, const int incx, const T f, T *y, const int incy)
Definition: Vmath.hpp:136
int Iamax(int n, const T *x, const int incx)
Return the index of the maximum absolute element in x.
Definition: Vmath.cpp:971
scalarT< T > log(scalarT< T > in)
Definition: scalar.hpp:303