Scroll to navigation

std::pow(std::valarray)(3) C++ Standard Libary std::pow(std::valarray)(3)

NAME

std::pow(std::valarray) - std::pow(std::valarray)

Synopsis


Defined in header <valarray>
template< class T >
std::valarray<T> pow( const std::valarray<T>& base, const std::valarray<T>& exp (1)
);
template< class T >


std::valarray<T> pow( const std::valarray<T>& base, (2)


const typename std::valarray<T>::value_type& vexp );
template< class T >


std::valarray<T> pow( const typename std::valarray<T>::value_type& vbase, (3)


const std::valarray<T>& exp );


Raises a value to a power.


1) Computes the values of each element in the numeric array base raised to the power
specified by the corresponding element from the numeric array exp.


The behavior is undefined if base.size() != exp.size().


2) Computes the values of each element in the numeric array base raised to the power
vexp.
3) Computes the values of vbase raised to the power defined by the elements in the
numeric array exp.

Parameters


base - numeric array containing the values of the base
exp - numeric array containing the values of the exponent
vbase - a value defining the base
vexp - a value defining the exponent

Return value


A numeric array containing the results of exponentiation.

Notes


Unqualified function (pow) is used to perform the computation. If such function is
not available, std::pow is used due to argument-dependent lookup.


The function can be implemented with the return type different from std::valarray.
In this case, the replacement type has the following properties:


* All const member functions of std::valarray are provided.
* std::valarray, std::slice_array, std::gslice_array, std::mask_array and
std::indirect_array can be constructed from the replacement type.
* All functions accepting an argument of type const std::valarray&
except begin() and end()
(since C++11) should also accept the replacement type.
* All functions accepting two arguments of type const std::valarray&
should accept every combination of const std::valarray& and the
replacement type.
* The return type does not add more than two levels of template nesting
over the most deeply-nested argument type.

Example

// Run this code


#include <cstddef>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <valarray>


void render(std::valarray<int> const& bases,
std::valarray<int> const& exponents = {},
std::valarray<int> const& results = {})
{
constexpr char const* sup[] {
"\u2070", "\u00B9", "\u00B2", "\u00B3", "\u2074",
"\u2075", "\u2076", "\u2077", "\u2078", "\u2079",
};


for (std::size_t n = 0; n != bases.size(); ++n) {
std::cout << std::left << bases[n] << std::left;
if (n < exponents.size())
std::cout << sup[exponents[n] % 10] << " ";
else
std::cout << " ";
}
if (results.size() != 0) {
std::cout << "=";
for (std::size_t n = 0; n != results.size(); ++n) {
std::cout << " " << results[n];
}
}
std::cout << '\n';
}


void render(int n) { std::cout << n << '\n'; }


int main()
{
constexpr int base { 2 };
constexpr int exponent { 5 };
const std::valarray<int> bases { 1, 2, 3, 4, 5, 6, 7 };
const std::valarray<int> exponents { 0, 1, 2, 3, 4, 5, 6 };


std::cout << "(1) pow( const std::valarray<T>& base, const std::valarray<T>& exp ); \n";
const std::valarray<int> powers1 = std::pow(bases, exponents);
std::cout << "base : "; render(bases);
std::cout << "exp : "; render(exponents);
std::cout << "pow : "; render(bases, exponents, powers1);


std::cout << "\n(2) pow( const std::valarray<T>& base, const value_type& vexp ); \n";
const std::valarray<int> powers2 = std::pow(bases, exponent);
std::cout << "base : "; render(bases);
std::cout << "vexp : "; render(exponent);
std::cout << "pow : "; render(bases, std::valarray<int>(exponent, bases.size()), powers2);


std::cout << "\n(3) pow( const value_type& vbase, const std::valarray<T>& exp ); \n";
const std::valarray<int> powers3 = std::pow(base, exponents);
std::cout << "vbase : "; render(base);
std::cout << "exp : "; render(exponents);
std::cout << "pow : "; render(std::valarray<int>(base, bases.size()), exponents, powers3);
}

Output:


(1) pow( const std::valarray<T>& base, const std::valarray<T>& exp );
base : 1 2 3 4 5 6 7
exp : 0 1 2 3 4 5 6
pow : 1⁰ 2¹ 3² 4³ 5⁴ 6⁵ 7⁶ = 1 2 9 64 625 7776 117649


(2) pow( const std::valarray<T>& base, const value_type& vexp );
base : 1 2 3 4 5 6 7
vexp : 5
pow : 1⁵ 2⁵ 3⁵ 4⁵ 5⁵ 6⁵ 7⁵ = 1 32 243 1024 3125 7776 16807


(3) pow( const value_type& vbase, const std::valarray<T>& exp );
vbase : 2
exp : 0 1 2 3 4 5 6
pow : 2⁰ 2¹ 2² 2³ 2⁴ 2⁵ 2⁶ = 1 2 4 8 16 32 64


Defect reports


The following behavior-changing defect reports were applied retroactively to
previously published C++ standards.


DR Applied to Behavior as published Correct behavior
T is deduced from both the scalar and the only deduce T from the
LWG 3074 C++98 valarray for (2-3), disallowing valarray
mixed-type calls

See also


sqrt(std::valarray) applies the function std::sqrt to each element of valarray
(function template)
pow
powf raises a number to the given power (\(\small{x^y}\)x^y)
powl (function)
(C++11)
(C++11)
pow(std::complex) complex power, one or both arguments may be a complex number
(function template)

2022.07.31 http://cppreference.com