| Common mathematical functions | |||||||||||||||||||||||||||||||
| Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
| Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
| Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
| Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
| Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray) | |||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||
| Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
| Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
valarray<T> apply( T func(T))const; | ||
valarray<T> apply( T func(const T&))const; | ||
Returns a new valarray of the same size with values which are acquired by applying functionfunc to the previous values of the elements.
Contents |
| func | - | function to apply to the values |
The resulting valarray with values acquired by applying functionfunc.
The function can be implemented with the return type different fromstd::valarray. In this case, the replacement type has the following properties:
Following straightforward implementations can be replaced by expression templates for a higher efficiency.
template<class T>valarray<T> valarray<T>::apply(T func(T))const{ valarray<T> other=*this;for(T& i: other) i= func(i);return other;} template<class T>valarray<T> valarray<T>::apply(T func(const T&))const{ valarray<T> other=*this;for(T& i: other) i= func(i);return other;} |
Calculates and prints the first 10 factorials.
#include <cmath>#include <iostream>#include <valarray> int main(){std::valarray<int> v={1,2,3,4,5,6,7,8,9,10}; v= v.apply([](int n)->int{returnstd::round(std::tgamma(n+1));});for(auto n: v)std::cout<< n<<' ';std::cout<<'\n';}
Output:
1 2 6 24 120 720 5040 40320 362880 3628800
| applies a unaryfunction object to elements from arange (function template)[edit] | |
(C++20) | applies a unaryfunction object to elements from arange (algorithm function object)[edit] |