44#ifndef NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
45#define NEKTAR_LIB_LIBUTILITIES_BASSICUTILS_VECTORMATH_HPP
54template <
class T>
inline void Fill(
int n,
const T alpha, T *x,
const int incx)
64template <
class T> T
ran2(
long *idum);
68void FillWhiteNoise(
int n,
const T eps, T *x,
const int incx,
int seed = 9999);
72inline void Vmul(
int n,
const T *x,
const int incx,
const T *y,
const int incy,
76 if (incx == 1 && incy == 1 && incz == 1)
100inline void Smul(
int n,
const T alpha,
const T *x,
const int incx, T *y,
104 if (incx == 1 && incy == 1)
126inline void Vdiv(
int n,
const T *x,
const int incx,
const T *y,
const int incy,
127 T *
z,
const int incz)
130 if (incx == 1 && incy == 1)
154inline void Sdiv(
int n,
const T alpha,
const T *x,
const int incx, T *y,
158 if (incx == 1 && incy == 1)
180inline void Vadd(
int n,
const T *x,
const int incx,
const T *y,
const int incy,
181 T *
z,
const int incz)
194inline void Sadd(
int n,
const T alpha,
const T *x,
const int incx, T *y,
198 if (incx == 1 && incy == 1)
220inline void Vsub(
int n,
const T *x,
const int incx,
const T *y,
const int incy,
221 T *
z,
const int incz)
224 if (incx == 1 && incy == 1 && incz == 1)
248inline void Ssub(
int n,
const T alpha,
const T *x,
const int incx, T *y,
252 if (incx == 1 && incy == 1)
273template <
class T>
inline void Zero(
int n, T *x,
const int incx)
277 std::memset(x,
'\0', n *
sizeof(T));
292template <
class T>
inline void Neg(
int n, T *x,
const int incx)
303inline void Vlog(
int n,
const T *x,
const int incx, T *y,
const int incy)
315inline void Vexp(
int n,
const T *x,
const int incx, T *y,
const int incy)
327inline void Vpow(
int n,
const T *x,
const int incx,
const T f, T *y,
340inline void Vsqrt(
int n,
const T *x,
const int incx, T *y,
const int incy)
352inline void Vabs(
int n,
const T *x,
const int incx, T *y,
const int incy)
356 *y = (*x > 0) ? *x : -(*x);
366inline void Vvtvp(
int n,
const T *
w,
const int incw,
const T *x,
const int incx,
367 const T *y,
const int incy, T *
z,
const int incz)
371 *
z = (*w) * (*x) + (*y);
381inline void Vvtvm(
int n,
const T *
w,
const int incw,
const T *x,
const int incx,
382 const T *y,
const int incy, T *
z,
const int incz)
386 *
z = (*w) * (*x) - (*y);
396inline void Svtvp(
int n,
const T alpha,
const T *x,
const int incx,
const T *y,
397 const int incy, T *
z,
const int incz)
400 if (incx == 1 && incy == 1 && incz == 1)
404 *
z = alpha * (*x) + (*y);
414 *
z = alpha * (*x) + (*y);
424inline void Svtvm(
int n,
const T alpha,
const T *x,
const int incx,
const T *y,
425 const int incy, T *
z,
const int incz)
429 *
z = alpha * (*x) - (*y);
439inline void Vvtvvtp(
int n,
const T *v,
int incv,
const T *
w,
int incw,
440 const T *x,
int incx,
const T *y,
int incy, T *
z,
int incz)
444 *
z = (*v) * (*w) + (*x) * (*y);
456inline void Vvtvvtm(
int n,
const T *v,
int incv,
const T *
w,
int incw,
457 const T *x,
int incx,
const T *y,
int incy, T *
z,
int incz)
461 *
z = (*v) * (*w) - (*x) * (*y);
473inline void Svtsvtp(
int n,
const T alpha,
const T *x,
int incx,
const T
beta,
474 const T *y,
int incy, T *
z,
int incz)
478 *
z = alpha * (*x) +
beta * (*y);
488inline void Vstvpp(
int n,
const T alpha,
const T *v,
int incv,
const T *
w,
489 int incw,
const T *x,
int incx, T *
z,
int incz)
493 *
z = alpha * (*v) + (*w) + (*x);
504template <
class T,
class I,
505 typename =
typename std::enable_if<std::is_floating_point<T>::value &&
506 std::is_integral<I>::value>::type>
507inline void Gathr(I n,
const T *x,
const I *y, T *
z)
518inline void Gathr(
int n,
const T
sign,
const T *x,
const int *y, T *
z)
522 *
z++ =
sign * (*(x + *y++));
529inline void Gathr(
int n,
const T *
sign,
const T *x,
const int *y, T *
z)
533 *
z++ = *(
sign++) * (*(x + *y++));
539template <
class T>
inline void Scatr(
int n,
const T *x,
const int *y, T *
z)
543 *(
z + *(y++)) = *(x++);
549inline void Scatr(
int n,
const T
sign,
const T *x,
const int *y, T *
z)
553 *(
z + *(y++)) =
sign * (*(x++));
559inline void Scatr(
int n,
const T *
sign,
const T *x,
const int *y, T *
z)
565 *(
z + *(y++)) = *(
sign++) * (*(x++));
577template <
class T>
inline void Assmb(
int n,
const T *x,
const int *y, T *
z)
581 *(
z + *(y++)) += *(x++);
587inline void Assmb(
int n,
const T
sign,
const T *x,
const int *y, T *
z)
591 *(
z + *(y++)) +=
sign * (*(x++));
597inline void Assmb(
int n,
const T *
sign,
const T *x,
const int *y, T *
z)
601 *(
z + *(y++)) += *(
sign++) * (*(x++));
608template <
class T>
inline T
Vsum(
int n,
const T *x,
const int incx)
623template <
class T>
inline int Imax(
int n,
const T *x,
const int incx)
626 int i, indx = (n > 0) ? 0 : -1;
629 for (i = 0; i < n; i++)
644template <
class T>
inline T
Vmax(
int n,
const T *x,
const int incx)
662template <
class T>
inline int Iamax(
int n,
const T *x,
const int incx)
665 int i, indx = (n > 0) ? 0 : -1;
669 for (i = 0; i < n; i++)
671 xm = (*x > 0) ? *x : -*x;
685template <
class T>
inline T
Vamax(
int n,
const T *x,
const int incx)
693 xm = (*x > 0) ? *x : -*x;
704template <
class T>
inline int Imin(
int n,
const T *x,
const int incx)
707 int i, indx = (n > 0) ? 0 : -1;
710 for (i = 0; i < n; i++)
725template <
class T>
inline T
Vmin(
int n,
const T *x,
const int incx)
743template <
class T>
inline int Nnan(
int n,
const T *x,
const int incx)
761template <
class T>
inline T
Dot(
int n,
const T *
w,
const T *x)
776inline T
Dot(
int n,
const T *
w,
const int incw,
const T *x,
const int incx)
790template <
class T>
inline T
Dot2(
int n,
const T *
w,
const T *x,
const int *y)
796 sum += (*y == 1 ? (*w) * (*x) : 0);
806inline T
Dot2(
int n,
const T *
w,
const int incw,
const T *x,
const int incx,
807 const int *y,
const int incy)
813 sum += (*y == 1 ? (*w) * (*x) : 0.0);
825inline void Vcopy(
int n,
const T *x,
const int incx, T *y,
const int incy)
827 if (incx == 1 && incy == 1)
829 memcpy(y, x, n *
sizeof(T));
844inline void Reverse(
int n,
const T *x,
const int incx, T *y,
const int incy)
856 const T *x_end = x + (n - 1) * incx;
857 T *y_end = y + (n - 1) * incy;
858 for (i = 0; i < nloop; ++i)
#define sign(a, b)
return the sign(b)*a
@ beta
Gauss Radau pinned at x=-1,.
std::vector< double > w(NPUPPER)
std::vector< double > z(NPUPPER)
void Vsqrt(int n, const T *x, const int incx, T *y, const int incy)
sqrt y = sqrt(x)
void Ssub(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Substract vector y = alpha - x.
void Svtsvtp(int n, const T alpha, const T *x, int incx, const T beta, const T *y, int incy, T *z, int incz)
Svtsvtp (scalar times vector plus scalar times vector):
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.
void Gathr(I n, const T *x, const I *y, T *z)
Gather vector z[i] = x[y[i]].
void Vlog(int n, const T *x, const int incx, T *y, const int incy)
log y = log(x)
void Vexp(int n, const T *x, const int incx, T *y, const int incy)
exp y = exp(x)
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.
void Vabs(int n, const T *x, const int incx, T *y, const int incy)
vabs: y = |x|
T Dot2(int n, const T *w, const T *x, const int *y)
dot product
void Neg(int n, T *x, const int incx)
Negate x = -x.
T Vmin(int n, const T *x, const int incx)
Return the minimum element in x - called vmin to avoid conflict with min.
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
T Vsum(int n, const T *x, const int incx)
Subtract return sum(x)
void Scatr(int n, const T *x, const int *y, T *z)
Scatter vector z[y[i]] = x[i].
T Dot(int n, const T *w, const T *x)
dot product
void Assmb(int n, const T *x, const int *y, T *z)
Assemble z[y[i]] += x[i]; z should be zero'd first.
void Svtvm(int n, const T alpha, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Svtvm (scalar times vector minus vector): z = alpha*x - y.
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.
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
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):
void Smul(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha*x.
void Sdiv(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Scalar multiply y = alpha/x.
int Imax(int n, const T *x, const int incx)
Return the index of the maximum element in x.
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.
int Imin(int n, const T *x, const int incx)
Return the index of the minimum element in x.
void Zero(int n, T *x, const int incx)
Zero vector.
void Fill(int n, const T alpha, T *x, const int incx)
Fill a vector with a constant value.
T ran2(long *idum)
Generates a number from ~Normal(0,1)
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):
void FillWhiteNoise(int n, const T eps, T *x, const int incx, int outseed)
Fills a vector with white noise.
int Nnan(int n, const T *x, const int incx)
Return number of NaN elements of x.
T Vamax(int n, const T *x, const int incx)
Return the maximum absolute element in x called vamax to avoid conflict with max.
void Sadd(int n, const T alpha, const T *x, const int incx, T *y, const int incy)
Add vector y = alpha + x.
void Reverse(int n, const T *x, const int incx, T *y, const int incy)
T Vmax(int n, const T *x, const int incx)
Return the maximum element in x – called vmax to avoid conflict with max.
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):
void Vcopy(int n, const T *x, const int incx, T *y, const int incy)
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.
void Vpow(int n, const T *x, const int incx, const T f, T *y, const int incy)
pow y = pow(x, f)
int Iamax(int n, const T *x, const int incx)
Return the index of the maximum absolute element in x.
scalarT< T > log(scalarT< T > in)
scalarT< T > sqrt(scalarT< T > in)