operator+,-,*,/,%,&,|,^,<<,>>,&&,|| (std::valarray)

From cppreference.com
< cpp‎ | numeric‎ | valarray
 
 
 
 
Defined in header <valarray>
template< class T >

std::valarray<T> operator+ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator- ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator* ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator/ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator% ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator& ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator| ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator^ ( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator<<( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator>>( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator&&( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
template< class T >

std::valarray<bool> operator||( const std::valarray<T>& lhs, const std::valarray<T>& rhs );
(1)
template< class T >

std::valarray<T> operator+ ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator- ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator* ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator/ ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator% ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator& ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator| ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator^ ( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator<<( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<T> operator>>( const typename std::valarray<T>::value_type & val,
                             const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator&&( const typename std::valarray<T>::value_type & val,
                                const std::valarray<T>& rhs );
template< class T >
std::valarray<bool> operator||( const typename std::valarray<T>::value_type & val,

                                const std::valarray<T>& rhs );
(2)
template< class T >

std::valarray<T> operator+ ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator- ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator* ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator/ ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator% ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator& ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator| ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator^ ( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator<<( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<T> operator>>( const std::valarray<T>& lhs,
                             const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<bool> operator&&( const std::valarray<T>& lhs,
                                const typename std::valarray<T>::value_type & val );
template< class T >
std::valarray<bool> operator||( const std::valarray<T>& lhs,

                                const typename std::valarray<T>::value_type & val );
(3)

Apply binary operators to each element of two valarrays, or a valarray and a value.

1) The operators works on valarrays of the same size and returns a valarray with the same size as the parameters with the operation applied to every elements of the two arguments.
2,3) Applies the operator between each element of the valarray and the scalar.

Parameters

rhs - a numeric array
lhs - a numeric array
val - a value of type T

Return value

A valarray with the same size as the parameter.

Note

The behaviour is undefined when the two arguments are valarrays with different sizes.

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

Example

Finds real roots of multiple Quadratic equations.

#include <cstddef>
#include <iostream>
#include <valarray>
 
int main()
{
    std::valarray<double> a(1, 8);
    std::valarray<double> b{1, 2, 3, 4, 5, 6, 7, 8};
    std::valarray<double> c = -b;
    // literals must also be of type T until LWG3074 (double in this case)
    std::valarray<double> d = std::sqrt(b * b - 4.0 * a * c);
    std::valarray<double> x1 = (-b - d) / (2.0 * a);
    std::valarray<double> x2 = (-b + d) / (2.0 * a);
    std::cout << "quadratic equation:  root 1:    root 2:\n";
    for (std::size_t i = 0; i < a.size(); ++i)
        std::cout << a[i] << "\u00B7x\u00B2 + " << b[i] << "\u00B7x + "
                  << c[i] << " = 0  " << std::fixed << x1[i] << "  "
                  << x2[i] << std::defaultfloat << '\n';
}

Output:

quadratic equation:  root 1:    root 2:
1·x² + 1·x + -1 = 0  -1.618034  0.618034
1·x² + 2·x + -2 = 0  -2.732051  0.732051
1·x² + 3·x + -3 = 0  -3.791288  0.791288
1·x² + 4·x + -4 = 0  -4.828427  0.828427
1·x² + 5·x + -5 = 0  -5.854102  0.854102
1·x² + 6·x + -6 = 0  -6.872983  0.872983
1·x² + 7·x + -7 = 0  -7.887482  0.887482
1·x² + 8·x + -8 = 0  -8.898979  0.898979

Defect reports

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

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

See also

applies a unary arithmetic operator to each element of the valarray
(public member function)
applies compound assignment operator to each element of the valarray
(public member function)