Is like a vector, but is optimized to get good performance for the processing of value arrays

View versions (1)

Definition

The valarray template is defined in the standard header <valarray>, and in the nonstandard backward-compatibility header <valarray.h>.

namespace std {
       template <class T>
       class valarray;
   }

Description

A valarray is a representation of the mathematical concept of a linear sequence of values.

It is like a vector but is designed for high speed numerics at the expense of some programming ease and general purpose use.

A simple example of creating a valarray is:

std::valarray<int> val1(10);              // valarray of ten ints with value 0
std::valarray<float> val2(8.3, 10);       // valarray of ten floats with value 8.3

Performance

Valarray has many features that make it ideally suited for use with vector processors in traditional vector supercomputers and SIMD units in consumer-level scalar processors, and also ease vector mathematics programming even in scalar computers.

Valarray Operations

Create, Copy and Destroy Operations

OperationEffect
valarray()Default constructor, creates an empty valarray
valarray(size_t n)Creates a valarray that contains n elements
valarray(\c{const Tval,size_t n})Creates a valarray with n elements initialized by val
valarray(const T* a, size_t n)Creates a valarray with n elements initialized by the values of the elements in array a
valarray(\c{const valarrayva})Copy constructor
~valarray()Destroys all elements and frees the memory

Assignment Operations

OperationEffect
operator =(\c{const valarrayva})Assigns the elements of the valarray va
operator =(\c{const Tvalue})Assigns value to each element of the valarray

Member Functions

OperationEffect
size() constReturns the actual number of elements
resize(size_t n)Change the size of the valarray to n
min() constReturns the minimum value of all elements
max() constReturns the maximum value of all elements
sum() constReturns the sum of all elements
shift(int n) constReturns a new valarray in which all elements are shifted by n positions
cshift(int n) constReturns a new valarray in which all elements are shifted cyclically by n positions
apply(T op (T)) constReturns a new valarray with all elements processed by op()

Element Access

OperationEffect
operator [ ](size_t idx)Return the valarray element that has index idx

Transcendental Functions

OperationEffect
absAbsolute value of valarray elements
acosArc cosine of valarray elements
asinArc sine of valarray elements
atanArc tangent of valarray elements
atan2Atan2 of valarray elements
cosCosine of valarray elements
coshHyperbolic cosine of valarray elements
expExponential of valarray elements
logNatural logarithm of valarray elements
log10Common logarithm of valarray elements
powPower of valarray elements
sinSine of valarray elements
sinhHyperbolic sine of valarray elements
sqrtSquare root of valarray elements
tanTangent of valarray elements
tanhHyperbolic tangent of valarray elements

The header also overloads operators for the valarray class.

References

  • Nicolai M. Josuttis: "The C++ Standard Library"
Example 1
Problem

The following program illustrates a simple use of valarrays.

Workings
#include <iostream>
#include <valarray>

using namespace std;

// print valarray
template <class T>
void printValarray (const valarray<T>& va)
{
   for (int i=0; i<va.size(); i++) 
     {
     cout << va[i] << ' ';
     }
   cout << endl;
}

int main()
{
  // define two valarrays with ten elements
  valarray<double> va1(10), va2(10);

  // assign values 0.0, 1.1, up to 9.9 to the first valarray
  for (int i=0; i<10; i++) 
  {
     va1[i] = i * 1.1;
  }

  // assign -1 to all elements of the second valarray
  va2 = -1;

  // print both valarrays
  printValarray(va1);
  printValarray(va2);

  // print minimum, maximum, and sum of the first valarray
  cout << "min(): " << val.min() << endl;
  cout << "max(): " << val.max() << endl;
  cout << "sum(): " << val.sum() << endl;

  // assign values of the first to the second valarray
  va2 = va1;

  // remove all elements of the first valarray
  va1.resize (0);

  // print both valarrays again
  printValarray(va1);
  printValarray(va2);
}
Solution

Output:

0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
min():0
max(): 9.9
sum(): 49.5
0 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9