function template

std::pow

<valarray>
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 object containing the results of the power operation on all the elements. The results calculated are x raised to the power y (xy).

This function overloads cmath's pow function.

Parameters

x
valarray or element with the base for the power operations.
y
valarray or element with the exponent for the power operations.

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>
#include <cmath>
#include <valarray>
using namespace std;

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

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

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

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

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

  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