Namespaces
Variants
Views
Actions

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 T& val, const std::valarray<T>& rhs);

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

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

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

template <class T> std::valarray<bool> operator||(const std::valarray<T>& lhs, const T& 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.

Contents

[edit] Parameters

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

[edit] Return value

A valarray with the same size as the parameter.

[edit] 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:

  • 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++14) 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.

[edit] Example

Finds real roots of multiple quadratic equations.

#include <valarray>
#include <iostream>
 
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 (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 (size_t i = 0; i < a.size(); ++i) {
        std::cout << a[i] << "x\u00B2 + " << b[i] << "x + " << c[i] << " = 0   ";
        std::cout << x1[i] << ", " << x2[i] << "\n";
    }
}

Output:

quadratic equation    root 1,  root 2
1x² + 1x + -1 = 0   -1.61803, 0.618034
1x² + 2x + -2 = 0   -2.73205, 0.732051
1x² + 3x + -3 = 0   -3.79129, 0.791288
1x² + 4x + -4 = 0   -4.82843, 0.828427
1x² + 5x + -5 = 0   -5.8541, 0.854102
1x² + 6x + -6 = 0   -6.87298, 0.872983
1x² + 7x + -7 = 0   -7.88748, 0.887482
1x² + 8x + -8 = 0   -8.89898, 0.898979