Wednesday 25 July 2012

valarray Library

<valarray>




A valarray is like a fixed sized array or vector that supports arithmetic operations on all the elements at once. For instance, if x and y are valarrays of the same size, then x+y is a valarray containing the sums of the corresponding elements. Likewise, y=sqrt(x) assigns y[i]=sqrt(x[i]) to each element of y. In mixed type expressions, a scalar (element of type T) is promoted to a valarray of the same size by duplicating it, e.g. x+1 adds 1 to all elements of x.
valarray<T> v(n); // n elements of type T, initially T() or 0
valarray<T> v(x, n); // n copies of x (note arguments are backwards)
valarray<T> v(a, n); // Initialize from array a[0]..a[n-1]
valarray<T> v; // size is 0
v.size() // Number of elements, n
v[i] // i'th element, 0 <= i < n, not checked
v+=x, v+=v // Add x or v[i] to all v[i], also = -= *= /= %= ^= &= |= <<= >>=
v+v, v+x, x+v // Also - * / % ^ & | << >> and unary + - ~ !
sqrt(v) // Also all functions in cmath
x=v.sum() // Sum of all elements
v.shift(n) // Move all v[i] to v[i+n], shift in 0
v.cshift(n) // Move v[i] to v[(i+n) % v.size()]
v.resize(n) // Change size to n, but reset all elements to 0
v.resize(n, x) // Change size to n, set all elements to x

No comments:

Post a Comment