Nektar++
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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> LIB_UTILITIES_EXPORT void Fill( int n, const T alpha, T *x, const int incx );
56 
57  /// \brief Generates a number from ~Normal(0,1)
58  template<class T> LIB_UTILITIES_EXPORT T ran2 (long* idum);
59 
60  /// \brief Fills a vector with white noise.
61  template<class T> LIB_UTILITIES_EXPORT void FillWhiteNoise(
62  int n, const T eps, T *x, const int incx, int seed = 9999);
63 
64  /// \brief Multiply vector z = x*y
65  template<class T> LIB_UTILITIES_EXPORT void Vmul( int n, const T *x, const int incx, const T *y,
66  const int incy, T*z, const int incz);
67 
68  /// \brief Scalar multiply y = alpha*y
69 
70  template<class T> LIB_UTILITIES_EXPORT void Smul( int n, const T alpha, const T *x, const int incx,
71  T *y, const int incy);
72 
73  /// \brief Multiply vector z = x/y
74  template<class T> LIB_UTILITIES_EXPORT void Vdiv( int n, const T *x, const int incx, const T *y,
75  const int incy, T*z, const int incz);
76 
77  /// \brief Scalar multiply y = alpha/y
78  template<class T> LIB_UTILITIES_EXPORT void Sdiv( int n, const T alpha, const T *x,
79  const int incx, T *y, const int incy);
80 
81  /// \brief Add vector z = x+y
82  template<class T> LIB_UTILITIES_EXPORT void Vadd( int n, const T *x, const int incx, const T *y,
83  const int incy, T *z, const int incz);
84 
85  /// \brief Add vector y = alpha + x
86  template<class T> LIB_UTILITIES_EXPORT void Sadd( int n, const T alpha, const T *x,
87  const int incx, T *y, const int incy);
88 
89  /// \brief Subtract vector z = x-y
90  template<class T> LIB_UTILITIES_EXPORT void Vsub( int n, const T *x, const int incx, const T *y,
91  const int incy, T *z, const int incz);
92 
93  /// \brief Add vector y = alpha - x
94  template<class T> LIB_UTILITIES_EXPORT void Ssub( int n, const T alpha,
95  const T *x, const int incx, T *y, const int incy);
96 
97  /// \brief Zero vector
98  template<class T> LIB_UTILITIES_EXPORT void Zero(int n, T *x, const int incx);
99 
100  /// \brief Negate x = -x
101  template<class T> LIB_UTILITIES_EXPORT void Neg( int n, T *x, const int incx);
102 
103 
104  template<class T> void Vlog(int n, const T *x, const int incx,
105  T *y, const int incy)
106  {
107  while (n--)
108  {
109  *y = log( *x );
110  x += incx;
111  y += incy;
112  }
113  }
114 
115 
116  template<class T> void Vexp(int n, const T *x, const int incx,
117  T *y, const int incy)
118  {
119  while (n--)
120  {
121  *y = exp( *x );
122  x += incx;
123  y += incy;
124  }
125  }
126 
127  template<class T> void Vpow(int n, const T *x, const int incx,
128  const T f, T *y, const int incy)
129  {
130  while (n--)
131  {
132  *y = pow( *x, f );
133  x += incx;
134  y += incy;
135  }
136  }
137 
138 
139  /// \brief sqrt y = sqrt(x)
140  template<class T> LIB_UTILITIES_EXPORT void Vsqrt(int n, const T *x, const int incx,
141  T *y, const int incy);
142 
143  /// \brief vabs: y = |x|
144  template<class T> LIB_UTILITIES_EXPORT void Vabs(int n, const T *x, const int incx,
145  T *y, const int incy);
146 
147  /********** Triad routines ***********************/
148 
149  /// \brief vvtvp (vector times vector plus vector): z = w*x + y
150  template<class T> LIB_UTILITIES_EXPORT void Vvtvp( int n,
151  const T *w, const int incw,
152  const T *x, const int incx,
153  const T *y, const int incy,
154  T *z, const int incz );
155 
156  /// \brief vvtvm (vector times vector plus vector): z = w*x - y
157  template<class T> LIB_UTILITIES_EXPORT void Vvtvm(int n, const T *w, const int incw, const T *x,
158  const int incx, const T *y, const int incy,
159  T *z, const int incz);
160 
161  /// \brief Svtvp (scalar times vector plus vector): z = alpha*x + y
162  template<class T> LIB_UTILITIES_EXPORT void Svtvp(int n, const T alpha, const T *x,
163  const int incx, const T *y, const int incy,
164  T *z, const int incz);
165 
166  /// \brief Svtvm (scalar times vector minus vector): z = alpha*x - y
167  template<class T> LIB_UTILITIES_EXPORT void Svtvm ( int n, const T alpha, const T *x,
168  const int incx, const T *y, const int incy,
169  T *z, const int incz );
170 
171  /// \brief vvtvvtp (vector times vector plus vector times vector):
172  // z = v*w + x*y
173  //
174  // @param n Number of items in each vector.
175  template<class T> LIB_UTILITIES_EXPORT void Vvtvvtp ( int n,
176  const T* v, int incv,
177  const T* w, int incw,
178  const T* x, int incx,
179  const T* y, int incy,
180  T* z, int incz );
181 
182  /// \brief vvtvvtm (vector times vector minus vector times vector):
183  // z = v*w - x*y
184  template<class T> LIB_UTILITIES_EXPORT void Vvtvvtm (int n,
185  const T* v, int incv,
186  const T* w, int incw,
187  const T* x, int incx,
188  const T* y, int incy,
189  T* z, int incz);
190  /// \brief Svtsvtp (scalar times vector plus scalar times vector):
191  // z = alpha*x + beta*y
192  template<class T> LIB_UTILITIES_EXPORT void Svtsvtp (int n,
193  const T alpha,
194  const T* x, int incx,
195  const T beta,
196  const T* y, int incy,
197  T* z, int incz);
198 
199  /// \brief Vstvpp (scalar times vector plus vector plus vector):
200  // z = v*w + x*y
201  template<class T> LIB_UTILITIES_EXPORT void Vstvpp(int n,
202  const T alpha,
203  const T* v, int incv,
204  const T* w, int incw,
205  const T* x, int incx,
206  T* z, int incz);
207 
208  /************ Misc routine from Veclib (and extras) ************/
209 
210  /// \brief Gather vector z[i] = x[y[i]]
211  template<class T, class I, typename = typename std::enable_if
212  <
213  std::is_floating_point<T>::value &&
214  std::is_integral<I>::value
215  >::type
216  >
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> LIB_UTILITIES_EXPORT void Gathr(int n, const T *sign, const T *x, const int *y,
228  T *z);
229 
230  /// \brief Scatter vector z[y[i]] = x[i]
231  template<class T> LIB_UTILITIES_EXPORT void Scatr(int n, const T *x, const int *y,
232  T *z);
233 
234  /// \brief Scatter vector z[y[i]] = sign[i]*x[i]
235  template<class T> LIB_UTILITIES_EXPORT void Scatr(int n, const T *sign, const T *x, const int *y,
236  T *z);
237 
238  /// \brief Assemble z[y[i]] += x[i]; z should be zero'd first
239  template<class T> LIB_UTILITIES_EXPORT void Assmb(int n, const T *x, const int *y,
240  T *z);
241 
242  /// \brief Assemble z[y[i]] += sign[i]*x[i]; z should be zero'd first
243  template<class T> LIB_UTILITIES_EXPORT void Assmb(int n, const T *sign, const T *x, const int *y,
244  T *z);
245 
246 
247  /************* Reduction routines *****************/
248 
249  /// \brief Subtract return sum(x)
250  template<class T> LIB_UTILITIES_EXPORT T Vsum( int n, const T *x, const int incx);
251 
252 
253  /// \brief Return the index of the maximum element in x
254  template<class T> LIB_UTILITIES_EXPORT int Imax( int n, const T *x, const int incx);
255 
256  /// \brief Return the maximum element in x -- called vmax to avoid
257  /// conflict with max
258  template<class T> LIB_UTILITIES_EXPORT T Vmax( int n, const T *x, const int incx);
259 
260  /// \brief Return the index of the maximum absolute element in x
261  template<class T> LIB_UTILITIES_EXPORT int Iamax( int n, const T *x, const int incx);
262 
263  /// \brief Return the maximum absolute element in x
264  /// called vamax to avoid conflict with max
265  template<class T> LIB_UTILITIES_EXPORT T Vamax( int n, const T *x, const int incx);
266 
267 
268  /// \brief Return the index of the minimum element in x
269  template<class T> LIB_UTILITIES_EXPORT int Imin( int n, const T *x, const int incx);
270 
271 
272  /// \brief Return the minimum element in x - called vmin to avoid
273  /// conflict with min
274  template<class T> LIB_UTILITIES_EXPORT T Vmin( int n, const T *x, const int incx);
275 
276  /// \brief Return number of NaN elements of x
277  template<class T> LIB_UTILITIES_EXPORT int Nnan(int n, const T *x, const int incx);
278 
279 
280  /// \brief vvtvp (vector times vector times vector): z = w*x*y
281  template<class T> LIB_UTILITIES_EXPORT T Dot( int n,
282  const T *w,
283  const T *x);
284 
285  /// \brief vvtvp (vector times vector times vector): z = w*x*y
286  template<class T> LIB_UTILITIES_EXPORT T Dot( int n,
287  const T *w, const int incw,
288  const T *x, const int incx);
289 
290  /// \brief vvtvp (vector times vector times vector): z = w*x*y
291  template<class T> LIB_UTILITIES_EXPORT T Dot2( int n,
292  const T *w,
293  const T *x,
294  const int *y);
295 
296  /// \brief vvtvp (vector times vector times vector): z = w*x*y
297  template<class T> LIB_UTILITIES_EXPORT T Dot2( int n,
298  const T *w, const int incw,
299  const T *x, const int incx,
300  const int *y, const int incy);
301 
302  /********** Memory routines ***********************/
303 
304  // \brief copy one vector to another
305  template<class T>
306  LIB_UTILITIES_EXPORT void Vcopy(int n, const T *x, const int incx,
307  T *y, const int incy);
308 
309  // \brief reverse the ordering of vector to another
310  template<class T> LIB_UTILITIES_EXPORT void Reverse( int n, const T *x, const int incx, T *y, const int incy);
311 
312 
313 }
314 #endif //VECTORMATH_HPP
315 
#define LIB_UTILITIES_EXPORT
#define sign(a, b)
return the sign(b)*a
Definition: Polylib.cpp:15
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:475
void Ssub(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha - x.
Definition: Vmath.cpp:405
void Svtsvtp(int n, const T alpha, const T *x, int incx, const T beta, const T *y, int incy, T *z, int incz)
vvtvvtp (scalar times vector plus scalar times vector):
Definition: Vmath.cpp:691
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:192
void Vlog(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:104
void Vexp(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:116
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:565
void Vabs(int n, const T *x, const int incx, T *y, const int incy)
vabs: y = |x|
Definition: Vmath.cpp:493
T Dot2(int n, const T *w, const T *x, const int *y)
vvtvp (vector times vector times vector): z = w*x*y
Definition: Vmath.cpp:1084
void Neg(int n, T *x, const int incx)
Negate x = -x.
Definition: Vmath.cpp:461
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:992
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:513
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:846
void Scatr(int n, const T *x, const int *y, T *z)
Scatter vector z[y[i]] = x[i].
Definition: Vmath.cpp:772
T Dot(int n, const T *w, const T *x)
vvtvp (vector times vector times vector): z = w*x*y
Definition: Vmath.cpp:1038
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:756
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:813
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 plus vector): z = alpha*x - y
Definition: Vmath.cpp:602
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:322
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 plus vector): z = w*x - y
Definition: Vmath.cpp:541
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:658
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:225
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:291
int Imax(int n, const T *x, const int incx)
Return the index of the maximum element in x.
Definition: Vmath.cpp:866
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:257
int Imin(int n, const T *x, const int incx)
Return the index of the minimum element in x.
Definition: Vmath.cpp:966
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:436
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:74
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:723
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:142
int Nnan(int n, const T *x, const int incx)
Return number of NaN elements of x.
Definition: Vmath.cpp:1015
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:942
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha - x.
Definition: Vmath.cpp:341
void Reverse(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1226
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:892
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:625
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1199
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:372
void Vpow(int n, const T *x, const int incx, const T f, T *y, const int incy)
Definition: Vmath.hpp:127
int Iamax(int n, const T *x, const int incx)
Return the index of the maximum absolute element in x.
Definition: Vmath.cpp:915
scalarT< T > log(scalarT< T > in)
Definition: scalar.hpp:278