C++ <valarray> - pow() Function
The C++ <valarray> pow() function returns a valarray containing the result of x raised to the power y (xy).
- If x and y both are valarrays of same size, the function computes the values of each element in x raised to the power specified by the corresponding element from y. The behavior is undefined if size of x and y are not equal.
- If x is a valarray and y is a value, the function computes the values of each element in x raised to the power y.
- If x is a value and y is a valarray, the function computes x raised to the power defined by the elements in y.
This function overloads with cmath's pow() function.
Syntax
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);
Parameters
x |
Specify a valarray or a value for the bases for the power operations. |
y |
Specify a valarray or a value for the exponent for the power operations. |
Return Value
Returns valarray containing the results of exponentiation.
Example:
The example below shows the usage of pow() function.
#include <iostream> #include <valarray> using namespace std; int main (){ valarray<double> va = {1, 2, 3, 4}; cout<<"va contains: "; for(int i = 0; i < va.size(); i++) cout<<va[i]<<" "; valarray<double> result1 = pow(va, va); cout<<"\nva^va returns: "; for(int i = 0; i < result1.size(); i++) cout<<result1[i]<<" "; valarray<double> result2 = pow(va, 2.0); cout<<"\nva^2.0 returns: "; for(int i = 0; i < result2.size(); i++) cout<<result2[i]<<" "; valarray<double> result3 = pow(2.0, va); cout<<"\n2.0^va returns: "; for(int i = 0; i < result3.size(); i++) cout<<result3[i]<<" "; return 0; }
The output of the above code will be:
va contains: 1 2 3 4 va^va returns: 1 4 27 256 va^2.0 returns: 1 4 9 16 2.0^va returns: 2 4 8 16
❮ C++ <valarray> Library