function template
<valarray>

std::pow

template<class T> valarray<T> pow (const valarray<T>& x, const valarray<T>& y);template<class T> valarray<T> pow (const valarray<T>& x, const T& y);template<class T> valarray<T> pow (const T& x, const valarray<T>& y);
Compute power of valarray elements
Returns a valarray containing the results of the power operation on all the elements, in the same order. The results calculated are x raised to the power y (xy).

The function calls pow (unqualified) once for each element in both x and y; If either is a single T value, it is used for all calls.

This function overloads cmath's pow.

Parameters

x
valarray or element with the bases for the power operations.
y
valarray or element with the exponents for the power operations.
If both arguments are valarray objects and their sizes don't match, the behavior is undefined.

Return value

A valarray object with the values of x to the power of y.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// pow valarray example
#include <iostream>     // std::cout
#include <cstddef>      // std::size_t
#include <cmath>        // std::pow
#include <valarray>     // std::valarray, std::pow

int main ()
{
  std::valarray<double> val (5);
  std::valarray<double> results;

  for (int i=0; i<5; ++i) val[i]=i+1;
  std::cout << "val:";
  for (std::size_t i=0; i<val.size(); ++i) std::cout << ' ' << val[i];
  std::cout << '\n';

  results = std::pow (val,val);
  std::cout << "val^val:";
  for (std::size_t i=0; i<results.size(); ++i) std::cout << ' ' << results[i];
  std::cout << '\n';

  results = std::pow (val,2.0);
  std::cout << "val^2:";
  for (std::size_t i=0; i<results.size(); ++i) std::cout << ' ' << results[i];
  std::cout << '\n';

  results = std::pow (2.0,val);
  std::cout << "2^val:";
  for (std::size_t i=0; i<results.size(); ++i) std::cout << ' ' << results[i];
  std::cout << '\n';

  return 0;
}

Output:

val: 1 2 3 4 5
val^val: 1 4 27 256 3125
val^2: 1 4 9 16 25
2^val: 2 4 8 16 32


See also