Class ** valarray** provides

For example, continuing the example from Section 22.2:

v1[2] = 5; // change the element at position 2 using // the non-const index operator

Along with the ordinary subscript operators, ** valarray** provides four different subset operations. Each of these also has both

The four subset operations are the `slice` operation, the `gslice` operation, the `mask` operation, and the `indirect` operation. Both `slice` and `gslice` use a special auxiliary class to specify the intent of the operation. These classes are called ** slice** and

The `slice` operation provides a view into a ** valarray** that is like the same operation defined in Basic Linear Algebra Subprograms (BLAS). A

using std::slice; int a[9] = {1,2,3,4,5,6,7,8,9}; valarray<int> all(a,9); //all = {1,2,3,4,5,6,7,8,9} valarray<int> odd = all[slice(0,5,2)]; //odd = {1,3,5,7,9}

In this example, the subscript operation actually returns a ** slice_array**. Class

The `gslice` operation differs from the `slice` in that it defines a set of strides and an associated set of lengths. This set of lengths and strides allows a program to treat a single-dimensional ** valarray** as a multidimensional array and to pull multi-dimensional slices from that array. Note that the

Here is a simple example that uses ** gslice**s to represent a three-dimensional array. In this example, the first three numbers form the top row of a two-dimensional array whose center and bottom rows are completed with the next six numbers. The next nine numbers represent the second or

using std::gslice; using std::valarray; int a[27] = {0,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,9,2,3,4,5,6,7,8,9,10}; size_t lengths[2] = {3,3}; size_t strides[2] = {3,1}; valarray<int> v(a,27); // initial valarray valarray<size_t> len(lengths,2); // valarray of lengths valarray<size_t> stride(strides,2); // valarray of strides valarray<int> v2(0,9); // valarray to hold the // generalized slice v2 = (v[gslice(0,len,stride)]); // slice off the front = // {0,1,2,3,4,5,6,7,8} v2 = (v[gslice(9,len,stride)]); // slice off the middle // = {1,2,3,4,5,6,7,8,9} Back two-dimensional array = 2 3 4 5 6 7 8 9 10 Middle two-dimensional array = 1 2 3 4 5 6 7 8 9 Front two-dimensional array = 0 1 2 3 4 5 6 7 8

The third operation is a boolean `mask`, which selects certain elements of a ** valarray** based on whether or not the corresponding element of a boolean

int a[9] = {1,2,3,4,5,6,7,8,9}; std::valarray<int> all(a,9); // all = {1,2,3,4,5,6,7,8,9} bool b[9]= {true,false,true,false,true,false,true,false,true}; std::valarray<bool> vb(b,9); std::valarray<int> odd2 = all[vb]; // odd2 = {1,3,5,7,9}

Finally, the `indirect` operation selects elements of a ** valarray** dependent on whether or not a given index is present in a

int a[9] = {1,2,3,4,5,6,7,8,9}; std::valarray<int> all(a,9); // all = {1,2,3,4,5,6,7,8,9} std::size_t c[5] = {0,2,4,6,8}; std::valarray<size_t> in(c,5); std::valarray<int> oddity = all[in]; // oddity = {1,3,5,7}

Each of the auxiliary array classes, ** slice_array**,

Class ** valarray** provides four unary operations:

float a[4] = {1.0, -2.3, -4.5, 9.0}; std::valarray<float> v(a,4); std::valarray<float> neg = -v; // neg = {-1.0, 2.3, 4.5, -9.0}