다음을 통해 공유


<valarray> functions

 

The latest version of this topic can be found at <valarray> functions.

abs acos asin
atan atan2 cos
cosh exp log
log10 pow sin
sinh sqrt swap
tan tanh

abs

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the absolute value of the elements of the input valarray.

template <class Type>  
valarray<Type> abs(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the absolute value of the elements of the input valarray.

Example

// valarray_abs.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   int i;  
  
   valarray<int> va1 ( 9 ), va2 ( 9 );  
   for ( i = 0 ; i < 4 ; i++ )  
      va1 [ i ] =  -i;  
   for ( i = 4 ; i < 9 ; i++ )  
      va1 [ i ] =  i;  
  
   cout << "The initial valarray is: ";  
      for (i = 0 ; i < 9 ; i++ )  
         cout << va1 [ i ] << " ";  
   cout << "." << endl;  
  
   va2 = abs ( va1 );  
   cout << "The absolute value of the initial valarray is: ";  
      for (i = 0 ; i < 9 ; i++ )  
         cout << va2 [ i ] << " ";  
   cout << "." << endl;  
}  
The initial valarray is: 0 -1 -2 -3 4 5 6 7 8 .  
The absolute value of the initial valarray is: 0 1 2 3 4 5 6 7 8 .  

acos

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the arccosine of the elements of the input valarray.

template <class Type>  
valarray<Type> acos(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the arccosine of the elements of the input valarray.

Remarks

The units of the returned elements are in radians.

The return value is a principal value between 0 and +pi that is consistent with the cosine value input.

Example

// valarray_acos.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  0.25 * i - 1;  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << " " << va1 [ i ];  
   cout << endl;  
  
   va2 = acos ( va1 );  
   cout << "The arccosine of the initial valarray is:\n";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << setw(10) << va2 [ i ]  
         << "  radians, which is  "  
         << setw(11) << (180/pi) * va2 [ i ]  
         << "  degrees" << endl;  
}  
The initial valarray is: -1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1  
The arccosine of the initial valarray is:  
   3.14159  radians, which is          180  degrees  
   2.41886  radians, which is       138.59  degrees  
    2.0944  radians, which is          120  degrees  
   1.82348  radians, which is      104.478  degrees  
    1.5708  radians, which is           90  degrees  
   1.31812  radians, which is      75.5225  degrees  
    1.0472  radians, which is           60  degrees  
  0.722734  radians, which is      41.4096  degrees  
         0  radians, which is            0  degrees  

asin

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the arcsine of the elements of the input valarray.

template <class Type>  
valarray<Type> asin(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the arcsine of the elements of the input valarray.

Remarks

The units of the returned elements are in radians.

The return value is a principal value between +pi/2 and –pi/2 that is consistent with the sine value input.

Example

// valarray_asin.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  0.25 * i - 1;  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << " " << va1 [ i ];  
   cout << endl;  
  
   va2 = asin ( va1 );  
   cout << "The arcsine of the initial valarray is:\n";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << setw(10) << va2 [ i ]  
           << "  radians, which is  "  
           << setw(11) << (180/pi) * va2 [ i ]  
           << "  degrees" << endl;  
}  
The initial valarray is: -1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1  
The arcsine of the initial valarray is:  
   -1.5708  radians, which is          -90  degrees  
 -0.848062  radians, which is     -48.5904  degrees  
 -0.523599  radians, which is          -30  degrees  
  -0.25268  radians, which is     -14.4775  degrees  
         0  radians, which is            0  degrees  
   0.25268  radians, which is      14.4775  degrees  
  0.523599  radians, which is           30  degrees  
  0.848062  radians, which is      48.5904  degrees  
    1.5708  radians, which is           90  degrees  

atan

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the principal value of the arctangent of the elements of the input valarray.

template <class Type>  
valarray<Type> atan(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the arctangent of the elements of the input valarray.

Remarks

The units of the returned elements are in radians.

The return value is a principal value between +pi/2 and –pi/2 that is consistent with the tangent value input.

Example

// valarray_atan.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   va1 [ 0 ] = -100;  
   for ( i = 1 ; i < 8 ; i++ )  
      va1 [ i ] =  5 * ( 0.25 * i - 1 );  
   va1 [ 8 ] = 100;  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is: ";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va1 [ i ] << " ";  
   cout << "." << endl;  
  
   va2 = atan ( va1 );  
   cout << "The arcsine of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << setw(10) << va2 [ i ]  
           << "  radians, which is  "  
           << setw(11) << (180/pi) * va2 [ i ]  
           << "  degrees" << endl;  
   cout << endl;  
}  
The initial valarray is: -100 -3.75 -2.5 -1.25 0 1.25 2.5 3.75 100 .  
The arcsine of the initial valarray is:  
   -1.5608  radians, which is     -89.4271  degrees  
  -1.31019  radians, which is     -75.0686  degrees  
  -1.19029  radians, which is     -68.1986  degrees  
 -0.896055  radians, which is     -51.3402  degrees  
         0  radians, which is            0  degrees  
  0.896055  radians, which is      51.3402  degrees  
   1.19029  radians, which is      68.1986  degrees  
   1.31019  radians, which is      75.0686  degrees  
    1.5608  radians, which is      89.4271  degrees  

atan2

Returns a valarray whose elements are equal to the arctangent of the Cartesian components specified by a combination of constants and elements of valarrays.

template <class Type>  
valarray<Type> atan2(const valarray<Type>& left, const valarray<Type>& right);

template <class Type>  
valarray<Type> atan2(const valarray<Type> left, const Type& right);

template <class Type>  
valarray<Type> atan2(const Type& left, const valarray<Type>& right);

Parameters

left
The constant numerical data type or input valarray whose elements provide the values for the y-coordinate of the arctangent argument.

right
The constant numerical data type or input valarray whose elements provide the values for the x-coordinate of the arctangent argument.

Return Value

A valarray whose elements I are equal to the arctangent of:

  • left [ I ] / _Right [ I ] for the first template function.

  • left [ I ] / right for the second template function.

  • left / right [ I ] for the third template function.

Remarks

The units of the returned elements are in radians.

This function preserves information about the signs of the components in the argument that is lost by the standard tangent function, and this knowledge of the quadrant enables the return value to be assigned a unique angle between +pi and –pi.

If left and right have a different number of elements, the result is undefined.

Example

// valarray_atan2.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1y ( 1 , 4 ), va1x ( 1 , 4 );  
   va1x [ 1 ] = -1;  
   va1x [ 2 ] = -1;  
   va1y [ 2 ] = -1;  
   va1y [ 3 ] = -1;  
   valarray<double> va2 ( 4 );  
  
   cout << "The initial valarray for the x coordinate is: ( ";  
   for ( i = 0 ; i < 4 ; i++ )  
      cout << va1x [ i ] << " ";  
   cout << ")." << endl;  
  
   cout << "The initial valarray for the y coordinate is: ( ";  
   for ( i = 0 ; i < 4 ; i++ )  
      cout << va1y [ i ] << " ";  
   cout << ")." << endl;  
  
   va2 = atan2 ( va1y , va1x );  
   cout << "The atan2 ( y / x ) of the initial valarrays is:\n";  
   for ( i = 0 ; i < 4 ; i++ )  
      cout << setw( 10 ) << va2 [ i ]  
           << "  radians, which is  "  
           << setw( 11 ) << ( 180/pi ) * va2 [ i ]  
           << "degrees" << endl;  
   cout << endl;  
}  
The initial valarray for the x coordinate is: ( 1 -1 -1 1 ).  
The initial valarray for the y coordinate is: ( 1 1 -1 -1 ).  
The atan2 ( y / x ) of the initial valarrays is:  
  0.785398  radians, which is           45degrees  
   2.35619  radians, which is          135degrees  
  -2.35619  radians, which is         -135degrees  
 -0.785398  radians, which is          -45degrees  

cos

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the cosine of the elements of the input valarray.

template <class Type>  
valarray<Type> cos(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the absolute value of the elements of the input valarray.

Example

// valarray_cos.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )  
      va1 [ i ] =  ( pi ) * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << setw( 10 ) << va1 [ i ]  
      << "  radians, which is  "  
      << setw( 5 ) << ( 180/pi ) * va1 [ i ]  
      << "  degrees" << endl;  
   cout << endl;  
  
   va2 = cos ( va1 );  
   cout << "The cosine of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
  -3.14159  radians, which is   -180  degrees  
  -2.35619  radians, which is   -135  degrees  
   -1.5708  radians, which is    -90  degrees  
 -0.785398  radians, which is    -45  degrees  
         0  radians, which is      0  degrees  
  0.785398  radians, which is     45  degrees  
    1.5708  radians, which is     90  degrees  
   2.35619  radians, which is    135  degrees  
   3.14159  radians, which is    180  degrees  
  
The cosine of the initial valarray is:  
-1  
-0.707107  
-1.03412e-013  
0.707107  
1  
0.707107  
-1.03412e-013  
-0.707107  
-1  

cosh

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the hyperbolic cosine of the elements of the input valarray.

template <class Type>  
valarray<Type> cosh(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the hyperbolic cosine of the elements of the input valarray.

Remarks

Identities defining the hyperbolic cosine in terms of exponential function:

cosh ( z ) = ( exp ( z ) + exp ( - z ) ) / 2

Example

// valarray_cosh.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  pi * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << setw( 10 ) << va1 [ i ]  
      << "  radians, which is  "  
      << setw( 5 ) << ( 180/pi ) * va1 [ i ]  
      << "  degrees" << endl;  
   cout << endl;  
  
   va2 = cosh ( va1 );  
   cout << "The hyperbolic cosine of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
  -3.14159  radians, which is   -180  degrees  
  -2.35619  radians, which is   -135  degrees  
   -1.5708  radians, which is    -90  degrees  
 -0.785398  radians, which is    -45  degrees  
         0  radians, which is      0  degrees  
  0.785398  radians, which is     45  degrees  
    1.5708  radians, which is     90  degrees  
   2.35619  radians, which is    135  degrees  
   3.14159  radians, which is    180  degrees  
  
The hyperbolic cosine of the initial valarray is:  
11.592  
5.32275  
2.50918  
1.32461  
1  
1.32461  
2.50918  
5.32275  
11.592  

exp

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the natural exponential of the elements of the input valarray.

template <class Type>  
valarray<Type> exp(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the natural exponential of the elements of the input valarray.

Example

// valarray_exp.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  10 * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "Initial valarray:";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << " " << va1 [ i ];  
   cout << endl;  
  
   va2 = exp ( va1 );  
   cout << "The natural exponential of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
Initial valarray: -10 -7.5 -5 -2.5 0 2.5 5 7.5 10  
The natural exponential of the initial valarray is:  
4.53999e-005  
0.000553084  
0.00673795  
0.082085  
1  
12.1825  
148.413  
1808.04  
22026.5  

log

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the natural logarithm of the elements of the input valarray.

template <class Type>  
valarray<Type> log(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the absolute value of the elements of the input valarray.

Example

// valarray_log.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for (i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  10 * i;  
   valarray<double> va2 ( 9 );  
  
   cout << "Initial valarray:";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << " " << va1 [ i ];  
   cout << endl;  
  
   va2 = log ( va1 );  
   cout << "The natural logarithm of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
Initial valarray: 0 10 20 30 40 50 60 70 80  
The natural logarithm of the initial valarray is:  
-1.#INF  
2.30259  
2.99573  
3.4012  
3.68888  
3.91202  
4.09434  
4.2485  
4.38203  

log10

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the base 10 or common logarithm of the elements of the input valarray.

template <class Type>  
valarray<Type> log10(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the common logarithm of the elements of the input valarray.

Example

// valarray_log10.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   int i;  
  
   valarray<double> va1 ( 11 );  
   for ( i = 0 ; i < 11 ; i++ )   
      va1 [ i ] =  10 * i;  
   valarray<double> va2 ( 9 );  
  
   cout << "Initial valarray:";  
   for (i = 0 ; i < 11 ; i++ )  
      cout << " " << va1 [ i ];  
   cout << endl;  
  
   va2 = log10 ( va1 );  
   cout << "The common logarithm of the initial valarray is:\n";  
   for (i = 0 ; i < 11 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
Initial valarray: 0 10 20 30 40 50 60 70 80 90 100  
The common logarithm of the initial valarray is:  
-1.#INF  
1  
1.30103  
1.47712  
1.60206  
1.69897  
1.77815  
1.8451  
1.90309  
1.95424  
2  

pow

Operates on the elements of input valarrays and constants, returning a valarray whose elements are equal to a base specified either by the elements of an input valarray or a constant raised to an exponent specified either by the elements of an input valarray or a constant.

template <class Type>  
valarray<Type>  
pow(
    const valarray<Type>& left,  
    const valarray<Type>& right);

template <class Type>  
valarray<Type>  
pow(
    const valarray<Type>& left,   
    const Type& right);

template <class Type>  
valarray<Type>  
pow(
    const Type& left,   
    const valarray<Type>& right);

Parameters

left
The input valarray whose elements supply the base for each element to be exponentiated.

right
The input valarray whose elements supply the power for each element to be exponentiated.

Return Value

A valarray whose elements I are equal to:

  • left [ I ] raised to the power right [ I ] for the first template function.

  • left [ I ] raised to the power right for the second template function.

  • left raised to the power right [ I ] for the third template function.

Remarks

If left and right have a different number of elements, the result is undefined.

Example

#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> vabase ( 6 );  
   for ( i = 0 ; i < 6 ; i++ )  
      vabase [ i ] =  i/2;  
   valarray<double> vaexp ( 6 );  
   for ( i = 0 ; i < 6 ; i++ )  
      vaexp [ i ] =  2 * i;  
  
   valarray<double> va2 ( 6 );  
  
   cout << "The initial valarray for the base is: ( ";  
      for ( i = 0 ; i < 6 ; i++ )  
         cout << vabase [ i ] << " ";  
   cout << ")." << endl;  
  
   cout << "The initial valarray for the exponent is: ( ";  
      for ( i = 0 ; i < 6 ; i++ )  
         cout << vaexp[ i ] << " ";  
   cout << ")." << endl;  
  
   va2 = pow ( vabase , vaexp );  
   cout << "The power of (n/2) * exp (2n) for n = 0 to n = 5 is: \n";  
      for ( i = 0 ; i < 6 ; i++ )  
         cout <<  "n = " << i << "\tgives " << va2 [ i ] << endl;  
}  
The initial valarray for the base is: ( 0 0 1 1 2 2 ).  
The initial valarray for the exponent is: ( 0 2 4 6 8 10 ).  
The power of (n/2) * exp (2n) for n = 0 to n = 5 is:  
n = 0   gives 1  
n = 1   gives 0  
n = 2   gives 1  
n = 3   gives 1  
n = 4   gives 256  
n = 5   gives 1024  

sin

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the sine of the elements of the input valarray.

template <class Type>  
valarray<Type> sin(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the sine of the elements of the input valarray.

Example

// valarray_sin.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  pi * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << setw(10) << va1 [ i ]  
      << "   radians, which is   "   
      << setw(5) << ( 180/pi ) * va1 [ i ]  
      << "   degrees" << endl;  
   cout << endl;  
  
   va2 = sin ( va1 );  
   cout << "The sine of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
  -3.14159   radians, which is    -180   degrees  
  -2.35619   radians, which is    -135   degrees  
   -1.5708   radians, which is     -90   degrees  
 -0.785398   radians, which is     -45   degrees  
         0   radians, which is       0   degrees  
  0.785398   radians, which is      45   degrees  
    1.5708   radians, which is      90   degrees  
   2.35619   radians, which is     135   degrees  
   3.14159   radians, which is     180   degrees  
  
The sine of the initial valarray is:  
2.06823e-013  
-0.707107  
-1  
-0.707107  
0  
0.707107  
1  
0.707107  
-2.06823e-013  

sinh

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the hyperbolic sine of the elements of the input valarray.

template <class Type>  
valarray<Type> sinh(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the hyperbolic sine of the elements of the input valarray.

Remarks

Identities defining the hyperbolic sine in terms of exponential function:

sinh ( z ) = ( exp ( z ) – exp ( - z ) ) / 2

Example

// valarray_sinh.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for (i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  pi * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for (i = 0 ; i < 9 ; i++ )  
      cout << setw( 10 ) << va1 [ i ]  
      << "   radians, which is   "  
      << setw( 5 ) << ( 180/pi ) * va1 [ i ]  
      << "  degrees" << endl;  
   cout << endl;  
  
   va2 = sinh ( va1 );  
   cout << "The hyperbolic sine of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
  -3.14159   radians, which is    -180  degrees  
  -2.35619   radians, which is    -135  degrees  
   -1.5708   radians, which is     -90  degrees  
 -0.785398   radians, which is     -45  degrees  
         0   radians, which is       0  degrees  
  0.785398   radians, which is      45  degrees  
    1.5708   radians, which is      90  degrees  
   2.35619   radians, which is     135  degrees  
   3.14159   radians, which is     180  degrees  
  
The hyperbolic sine of the initial valarray is:  
-11.5487  
-5.22797  
-2.3013  
-0.868671  
0  
0.868671  
2.3013  
5.22797  
11.5487  

sqrt

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the square root of the elements of the input valarray.

template <class Type>  
valarray<Type> sqrt(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the square root of the elements of the input valarray.

Example

// valarray_sqrt.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <cmath>  
  
int main( )  
{  
   using namespace std;  
   int i;  
  
   valarray<double> va1 ( 6 );  
   for ( i = 0 ; i < 5 ; i++ )  
      va1 [ i ] = i * i;  
  
   cout << "The initial valarray is: ( ";  
      for ( i = 0 ; i < 5 ; i++ )  
         cout << va1 [ i ] << " ";  
   cout << ")." << endl;  
  
   valarray<double> va2 = sqrt ( va1 );  
   cout << "The square root of the initial valarray is: ( ";  
      for ( i = 0 ; i < 5 ; i++ )  
         cout << va2 [ i ] << " ";  
   cout << ")." << endl;  
}  
The initial valarray is: ( 0 1 4 9 16 ).  
The square root of the initial valarray is: ( 0 1 2 3 4 ).  

swap

Exchanges the elements of two valarrays.

template <class Type>  
void swap(
    valarray<Type>& left,  
    valarray<Type>& right);

Parameters

Parameter Description
left An object of type valarray.
right An object of type valarray.

Remarks

The template function executes left.swap( right).

tan

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the tangent of the elements of the input valarray.

template <class Type>  
valarray<Type> tan(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the tangent of the elements of the input valarray.

Example

// valarray_tan.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  ( pi/2 ) * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << setw( 10 ) << va1 [ i ]  
      << "     radians, which is   "  
      << setw( 5 ) << ( 180/pi ) * va1 [ i ]  
      << "   degrees" << endl;  
   cout << endl;  
  
   va2 = tan ( va1 );  
   cout << "The tangent of the initial valarray is:\n ";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
   -1.5708     radians, which is     -90   degrees  
   -1.1781     radians, which is   -67.5   degrees  
 -0.785398     radians, which is     -45   degrees  
 -0.392699     radians, which is   -22.5   degrees  
         0     radians, which is       0   degrees  
  0.392699     radians, which is    22.5   degrees  
  0.785398     radians, which is      45   degrees  
    1.1781     radians, which is    67.5   degrees  
    1.5708     radians, which is      90   degrees  
  
The tangent of the initial valarray is:  
 9.6701e+012  
-2.41421  
-1  
-0.414214  
0  
0.414214  
1  
2.41421  
-9.6701e+012  

tanh

Operates on the elements of an input valarray, returning a valarray whose elements are equal to the hyperbolic tangent of the elements of the input valarray.

template <class Type>  
valarray<Type> tanh(const valarray<Type>& left);

Parameters

left
The input valarray whose elements are to be operated on by the member function.

Return Value

A valarray whose elements are equal to the hyperbolic cosine of the elements of the input valarray.

Remarks

Identities defining the hyperbolic tangent in terms of the exponential function:

tanh ( z ) = sinh ( z ) / cosh ( z ) = ( exp ( z ) – exp ( - z ) ) / ( exp ( z ) + exp ( - z ) )

Example

// valarray_tanh.cpp  
// compile with: /EHsc  
#include <valarray>  
#include <iostream>  
#include <iomanip>  
  
int main( )  
{  
   using namespace std;  
   double pi = 3.14159265359;  
   int i;  
  
   valarray<double> va1 ( 9 );  
   for ( i = 0 ; i < 9 ; i++ )   
      va1 [ i ] =  pi * ( 0.25 * i - 1 );  
   valarray<double> va2 ( 9 );  
  
   cout << "The initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
      cout << setw( 10 ) << va1 [ i ]  
      << "   radians, which is     "  
                    << setw( 5 ) << ( 180/pi ) * va1 [ i ]  
                    << "  degrees" << endl;  
   cout << endl;  
  
   va2 = tanh ( va1 );  
   cout << "The hyperbolic tangent of the initial valarray is:\n";  
   for ( i = 0 ; i < 9 ; i++ )  
     cout << va2 [ i ] << endl;  
}  
The initial valarray is:  
  -3.14159   radians, which is      -180  degrees  
  -2.35619   radians, which is      -135  degrees  
   -1.5708   radians, which is       -90  degrees  
 -0.785398   radians, which is       -45  degrees  
         0   radians, which is         0  degrees  
  0.785398   radians, which is        45  degrees  
    1.5708   radians, which is        90  degrees  
   2.35619   radians, which is       135  degrees  
   3.14159   radians, which is       180  degrees  
  
The hyperbolic tangent of the initial valarray is:  
-0.996272  
-0.982193  
-0.917152  
-0.655794  
0  
0.655794  
0.917152  
0.982193  
0.996272  

See Also

<valarray>