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;
}
|