table of contents
std::sqrt(std::valarray)(3) | C++ Standard Libary | std::sqrt(std::valarray)(3) |
NAME¶
std::sqrt(std::valarray) - std::sqrt(std::valarray)
Synopsis¶
Defined in header <valarray>
template< class T >
valarray<T> sqrt( const valarray<T>& va );
For each element in va computes the square root of the value of the
element.
Parameters¶
va - value array to apply the operation to
Return value¶
Value array containing square roots of the values in va.
Notes¶
Unqualified function (sqrt) is used to perform the computation.
If such function is
not available, std::sqrt is used due to argument-dependent lookup.
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++11) 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.
Possible implementation¶
template< class T >
valarray<T> sqrt( const valarray<T>& va )
{
valarray<T> other = va;
for (T &i : other) {
i = sqrt(i);
}
return other; // proxy object may be returned
}
Example¶
Finds real roots of multiple quadratic equations.
// Run this code
#include <cstddef>
#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 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
See also¶
applies the function std::pow to two valarrays or a valarray and
pow(std::valarray) a value
(function template)
sqrt computes square root (\(\small{\sqrt{x} }\)
sqrtf √
sqrtl x)
(C++11) (function)
(C++11)
sqrt(std::complex) complex square root in the range of the right half-plane
(function template)
2022.07.31 | http://cppreference.com |