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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Collection of templated functions for vector mathematics
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #ifndef NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
37 #define NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
38 
41 
42 namespace Vmath
43 {
44 
45  /***************** Math routines ***************/
46 
47  /// \brief Fill a vector with a constant value
48  template<class T> LIB_UTILITIES_EXPORT void Fill( int n, const T alpha, T *x, const int incx );
49 
50 
51 
52  /// \brief Generates a number from ~Normal(0,1)
53  template<class T> LIB_UTILITIES_EXPORT T ran2 (long* idum);
54 
55 
56  /// \brief Fills a vector with white noise.
57  template<class T> LIB_UTILITIES_EXPORT void FillWhiteNoise(
58  int n, const T eps, T *x, const int incx, int seed = 9999);
59 
60  /// \brief Multiply vector z = x*y
61  template<class T> LIB_UTILITIES_EXPORT void Vmul( int n, const T *x, const int incx, const T *y,
62  const int incy, T*z, const int incz);
63 
64  /// \brief Scalar multiply y = alpha*y
65 
66  template<class T> LIB_UTILITIES_EXPORT void Smul( int n, const T alpha, const T *x, const int incx,
67  T *y, const int incy);
68 
69  /// \brief Multiply vector z = x/y
70  template<class T> LIB_UTILITIES_EXPORT void Vdiv( int n, const T *x, const int incx, const T *y,
71  const int incy, T*z, const int incz);
72 
73  /// \brief Scalar multiply y = alpha/y
74  template<class T> LIB_UTILITIES_EXPORT void Sdiv( int n, const T alpha, const T *x,
75  const int incx, T *y, const int incy);
76 
77  /// \brief Add vector z = x+y
78  template<class T> LIB_UTILITIES_EXPORT void Vadd( int n, const T *x, const int incx, const T *y,
79  const int incy, T *z, const int incz);
80 
81  /// \brief Add vector y = alpha + x
82  template<class T> LIB_UTILITIES_EXPORT void Sadd( int n, const T alpha, const T *x,
83  const int incx, T *y, const int incy);
84 
85  /// \brief Subtract vector z = x-y
86  template<class T> LIB_UTILITIES_EXPORT void Vsub( int n, const T *x, const int incx, const T *y,
87  const int incy, T *z, const int incz);
88 
89  /// \brief Zero vector
90  template<class T> LIB_UTILITIES_EXPORT void Zero(int n, T *x, const int incx);
91 
92  /// \brief Negate x = -x
93  template<class T> LIB_UTILITIES_EXPORT void Neg( int n, T *x, const int incx);
94 
95 
96  template<class T> void Vlog(int n, const T *x, const int incx,
97  T *y, const int incy)
98  {
99  while (n--)
100  {
101  *y = log( *x );
102  x += incx;
103  y += incy;
104  }
105  }
106 
107 
108  template<class T> void Vexp(int n, const T *x, const int incx,
109  T *y, const int incy)
110  {
111  while (n--)
112  {
113  *y = exp( *x );
114  x += incx;
115  y += incy;
116  }
117  }
118 
119  template<class T> void Vpow(int n, const T *x, const int incx,
120  const T f, T *y, const int incy)
121  {
122  while (n--)
123  {
124  *y = pow( *x, f );
125  x += incx;
126  y += incy;
127  }
128  }
129 
130 
131  /// \brief sqrt y = sqrt(x)
132  template<class T> LIB_UTILITIES_EXPORT void Vsqrt(int n, const T *x, const int incx,
133  T *y, const int incy);
134 
135  /// \brief vabs: y = |x|
136  template<class T> LIB_UTILITIES_EXPORT void Vabs(int n, const T *x, const int incx,
137  T *y, const int incy);
138 
139  /********** Triad routines ***********************/
140 
141  /// \brief vvtvp (vector times vector plus vector): z = w*x + y
142  template<class T> LIB_UTILITIES_EXPORT void Vvtvp(int n,
143  const T *w, const int incw,
144  const T *x, const int incx,
145  const T *y, const int incy,
146  T *z, const int incz);
147 
148  /// \brief vvtvm (vector times vector plus vector): z = w*x - y
149  template<class T> LIB_UTILITIES_EXPORT void Vvtvm(int n, const T *w, const int incw, const T *x,
150  const int incx, const T *y, const int incy,
151  T *z, const int incz);
152 
153  /// \brief svtvp (scalar times vector plus vector): z = alpha*x + y
154  template<class T> LIB_UTILITIES_EXPORT void Svtvp(int n, const T alpha, const T *x,
155  const int incx, const T *y, const int incy,
156  T *z, const int incz);
157 
158  /// \brief svtvp (scalar times vector plus vector): z = alpha*x - y
159  template<class T> LIB_UTILITIES_EXPORT void Svtvm(int n, const T alpha, const T *x,
160  const int incx, const T *y, const int incy,
161  T *z, const int incz);
162 
163  /// \brief vvtvvtp (vector times vector plus vector times vector):
164  // z = v*w + x*y
165  template<class T> LIB_UTILITIES_EXPORT void Vvtvvtp (int n,
166  const T* v, int incv,
167  const T* w, int incw,
168  const T* x, int incx,
169  const T* y, int incy,
170  T* z, int incz);
171  /// \brief vvtvvtm (vector times vector minus vector times vector):
172  // z = v*w - x*y
173  template<class T> LIB_UTILITIES_EXPORT void Vvtvvtm (int n,
174  const T* v, int incv,
175  const T* w, int incw,
176  const T* x, int incx,
177  const T* y, int incy,
178  T* z, int incz);
179  /// \brief Svtsvtp (scalar times vector plus scalar times vector):
180  // z = alpha*x + beta*y
181  template<class T> LIB_UTILITIES_EXPORT void Svtsvtp (int n,
182  const T alpha,
183  const T* x, int incx,
184  const T beta,
185  const T* y, int incy,
186  T* z, int incz);
187 
188  /// \brief Vstvpp (scalar times vector plus vector plus vector):
189  // z = v*w + x*y
190  template<class T> LIB_UTILITIES_EXPORT void Vstvpp(int n,
191  const T alpha,
192  const T* v, int incv,
193  const T* w, int incw,
194  const T* x, int incx,
195  T* z, int incz);
196 
197  /************ Misc routine from Veclib (and extras) ************/
198 
199  /// \brief Gather vector z[i] = x[y[i]]
200  template<class T> LIB_UTILITIES_EXPORT void Gathr(int n, const T *x, const int *y,
201  T *z);
202 
203  /// \brief Gather vector z[i] = sign[i]*x[y[i]]
204  template<class T> LIB_UTILITIES_EXPORT void Gathr(int n, const T *sign, const T *x, const int *y,
205  T *z);
206 
207  /// \brief Scatter vector z[y[i]] = x[i]
208  template<class T> LIB_UTILITIES_EXPORT void Scatr(int n, const T *x, const int *y,
209  T *z);
210 
211  /// \brief Scatter vector z[y[i]] = sign[i]*x[i]
212  template<class T> LIB_UTILITIES_EXPORT void Scatr(int n, const T *sign, const T *x, const int *y,
213  T *z);
214 
215  /// \brief Assemble z[y[i]] += x[i]; z should be zero'd first
216  template<class T> LIB_UTILITIES_EXPORT void Assmb(int n, const T *x, const int *y,
217  T *z);
218 
219  /// \brief Assemble z[y[i]] += sign[i]*x[i]; z should be zero'd first
220  template<class T> LIB_UTILITIES_EXPORT void Assmb(int n, const T *sign, const T *x, const int *y,
221  T *z);
222 
223 
224  /************* Reduction routines *****************/
225 
226  /// \brief Subtract return sum(x)
227  template<class T> LIB_UTILITIES_EXPORT T Vsum( int n, const T *x, const int incx);
228 
229 
230  /// \brief Return the index of the maximum element in x
231  template<class T> LIB_UTILITIES_EXPORT int Imax( int n, const T *x, const int incx);
232 
233  /// \brief Return the maximum element in x -- called vmax to avoid
234  /// conflict with max
235  template<class T> LIB_UTILITIES_EXPORT T Vmax( int n, const T *x, const int incx);
236 
237  /// \brief Return the index of the maximum absolute element in x
238  template<class T> LIB_UTILITIES_EXPORT int Iamax( int n, const T *x, const int incx);
239 
240  /// \brief Return the maximum absolute element in x
241  /// called vamax to avoid conflict with max
242  template<class T> LIB_UTILITIES_EXPORT T Vamax( int n, const T *x, const int incx);
243 
244 
245  /// \brief Return the index of the minimum element in x
246  template<class T> LIB_UTILITIES_EXPORT int Imin( int n, const T *x, const int incx);
247 
248 
249  /// \brief Return the minimum element in x - called vmin to avoid
250  /// conflict with min
251  template<class T> LIB_UTILITIES_EXPORT T Vmin( int n, const T *x, const int incx);
252 
253  /// \brief Return number of NaN elements of x
254  template<class T> LIB_UTILITIES_EXPORT int Nnan(int n, const T *x, const int incx);
255 
256 
257  /// \brief vvtvp (vector times vector times vector): z = w*x*y
258  template<class T> LIB_UTILITIES_EXPORT T Dot( int n,
259  const T *w,
260  const T *x);
261 
262  /// \brief vvtvp (vector times vector times vector): z = w*x*y
263  template<class T> LIB_UTILITIES_EXPORT T Dot( int n,
264  const T *w, const int incw,
265  const T *x, const int incx);
266 
267  /// \brief vvtvp (vector times vector times vector): z = w*x*y
268  template<class T> LIB_UTILITIES_EXPORT T Dot2( int n,
269  const T *w,
270  const T *x,
271  const int *y);
272 
273  /// \brief vvtvp (vector times vector times vector): z = w*x*y
274  template<class T> LIB_UTILITIES_EXPORT T Dot2( int n,
275  const T *w, const int incw,
276  const T *x, const int incx,
277  const int *y, const int incy);
278 
279  /********** Memory routines ***********************/
280 
281  // \brief copy one vector to another
282  template<class T>
283  LIB_UTILITIES_EXPORT void Vcopy(int n, const T *x, const int incx,
284  T *y, const int incy);
285 
286  // \brief reverse the ordering of vector to another
287  template<class T> LIB_UTILITIES_EXPORT void Reverse( int n, const T *x, const int incx, T *y, const int incy);
288 
289 
290 }
291 #endif //VECTORMATH_HPP
292 
Definition: Vmath.cpp:40
void Vpow(int n, const T *x, const int incx, const T f, T *y, const int incy)
Definition: Vmath.hpp:119
void Gathr(int n, const T *x, const int *y, T *z)
Gather vector z[i] = x[y[i]].
Definition: Vmath.cpp:644
#define sign(a, b)
return the sign(b)*a
Definition: Polylib.cpp:27
void Vsqrt(int n, const T *x, const int incx, T *y, const int incy)
sqrt y = sqrt(x)
Definition: Vmath.cpp:408
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:779
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:871
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
Definition: Vmath.cpp:46
int Imin(int n, const T *x, const int incx)
Return the index of the minimum element in x.
Definition: Vmath.cpp:847
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:485
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:442
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:271
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:241
void Vabs(int n, const T *x, const int incx, T *y, const int incy)
vabs: y = |x|
Definition: Vmath.cpp:424
int Imax(int n, const T *x, const int incx)
Return the index of the maximum element in x.
Definition: Vmath.cpp:755
void Reverse(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1085
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*y.
Definition: Vmath.cpp:213
T ran2(long *idum)
Generates a number from ~Normal(0,1)
Definition: Vmath.cpp:73
#define LIB_UTILITIES_EXPORT
int Nnan(int n, const T *x, const int incx)
Return number of NaN elements of x.
Definition: Vmath.cpp:892
void Vexp(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:108
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:518
void Scatr(int n, const T *x, const int *y, T *z)
Scatter vector z[y[i]] = x[i].
Definition: Vmath.cpp:673
void Neg(int n, T *x, const int incx)
Negate x = -x.
Definition: Vmath.cpp:396
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:709
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:564
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:825
int Iamax(int n, const T *x, const int incx)
Return the index of the maximum absolute element in x.
Definition: Vmath.cpp:800
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:315
T Dot(int n, const T *w, const T *x)
vvtvp (vector times vector times vector): z = w*x*y
Definition: Vmath.cpp:914
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:343
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:617
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:537
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:465
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:954
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
Definition: Vmath.cpp:138
void Vlog(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.hpp:96
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:591
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
Definition: Vmath.cpp:737
void Zero(int n, T *x, const int incx)
Zero vector.
Definition: Vmath.cpp:373
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
Definition: Vmath.cpp:1061
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:299
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:183