C++ Valarray::pow Function



The C++ Valarray::pow() function returns a valarray holding the results of the power operation on all the elements, in the same order. The calculations' outcomes are x raised to the power of y. (xy).

Every element in both x and y receives a single call to pow, which is utilized for all calls if either is a single T value. Cmath's pow is overloaded by this function.

Syntax

Following is the syntax for C++ Valarray::pow Function −

pow (const valarray<T>& x, const valarray<T>& y);
pow (const valarray<T>& x, const T& y);
pow (const T& x, const valarray<T>& y);

Parameters

  • x − It indicates the element with the bases for the power operations.

  • y − It indicates the element with the exponents for the power operations.

Examples

Example 1

Let's look into the following example, where we are going to use pow() function and retrieving the output.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   valarray<int> varr = { -2,0,-3,4,-5 };
   valarray<int> valarray1;
   valarray1 = pow(varr, 5);
   cout << "The New pow"
      << " Valarray is : "
      << endl;
   for (int& a : valarray1) {
      cout << a << " ";
   }
   cout << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

The New pow Valarray is : 
-32 0 -243 1024 -3125 

Example 2

Let's consider the following example, where we are going to use pow() function and retrieving output of both original valarray and pow valarray.

#include <iostream>
#include <valarray>
using namespace std;

int main() {
   valarray<double> myvalarray = { 2,4,0.6,-8,-9 };
   cout << "The Orignal Valarray is : ";
   
   for (double& ele : myvalarray)
      cout << ele << " ";
   valarray<double> powValarray;
   powValarray = pow(myvalarray, 2);
   cout << "\nThe pow Valarray is : ";
   
   for (double& ele : powValarray)
      cout << ele << " ";
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

The Orignal Valarray is : 2 4 0.6 -8 -9 
The pow Valarray is : 16 0.36 64 81

Example 3

In the following example, we are going to use pow() function with (const std::valarray<t>& base, const std::valarray<t>& exp) and retrieving the output.

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

class tutorial {
   friend std::ostream& operator<< (std::ostream& os, tutorial const& r) {
      constexpr char const* sup[] {
         "\u2070", "\u00B9", "\u00B2", "\u00B3", "\u2074",
         "\u2075", "\u2076", "\u2077", "\u2078", "\u2079",
      };
      for (std::size_t n = 0; n != r.base.size(); ++n) {
         os << std::left << r.base[n] << std::left;
         if (n < r.exponent.size())
            os << sup[r.exponent[n] % 10] << " ";
         else
            os << "  ";
      }
      if (r.results.size() != 0) {
         os << "=";
         for (std::size_t n = 0; n != r.results.size(); ++n)
            os << " " << r.results[n];
      }
      return os << '\n';
   }
   public:
   std::valarray<int> base{}, exponent{}, results{};
};
int main() {
   const std::valarray<int> base { 2,4,6,8,10};
   const std::valarray<int> exponent {1,3,5,7,9 };
   const std::valarray<int> power1 = std::pow(base, exponent);
   std::cout<< "pow  : " << tutorial{base, exponent, power1};
}

Output

Let us compile and run the above program, this will produce the following result −

pow  : 2¹ 4³ 6⁵ 8⁷ 10⁹ = 2 64 7776 2097152 1000000000
Advertisements