Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/valarray"

From cppreference.com
< cpp‎ | numeric
(Non-member functions: {{small}} -> {{dcl small}})
m (fmt)
 
(42 intermediate revisions by 11 users not shown)
Line 1: Line 1:
 
{{cpp/title|valarray}}
 
{{cpp/title|valarray}}
{{cpp/numeric/valarray/sidebar}}
+
{{cpp/numeric/valarray/navbar}}
{{ddcl | header=valarray |
+
{{ddcl|header=valarray|
template< class >
+
template< class T >
 
class valarray;
 
class valarray;
 
}}
 
}}
  
{{cpp|std::valarray}} is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access. {{cpp|std::valarray}} and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized.
+
{{tt|std::valarray}} is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.
 +
 
 +
===Notes===
 +
{{tt|std::valarray}} and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword {{ltt|c/language/restrict}} in the C programming language. In addition, functions and operators that take {{tt|valarray}} arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression such as {{c|1=v1 = a * v2 + v3;}} as a single loop that executes {{c|1=v1[i] = a * v2[i] + v3[i];}} avoiding any temporaries or multiple passes. However, {{enwiki|expression templates}} make the same optimization technique available for any C++ container, and the majority of numeric libraries prefer expression templates to valarrays for flexibility. Some C++ standard library implementations use expression templates to implement efficient operations on {{tt|std::valarray}} (e.g. GNU libstdc++ and LLVM libc++). Only rarely are valarrays optimized any further, as in e.g. [https://software.intel.com/en-us/node/684140 Intel Integrated Performance Primitives].
 +
 
 +
===Template parameters===
 +
{{par begin}}
 +
{{par|T|the type of the elements. The type must meet the {{named req|NumericType}} requirements}}
 +
{{par end}}
  
 
===Member types===
 
===Member types===
{{tdcl list begin}}
+
{{dsc begin}}
{{tdcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{tdcl list item | {{tt|value_type}} | {{tt|T}}}}
+
{{dsc|{{tt|value_type}}|{{tt|T}}}}
{{tdcl list end}}
+
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/numeric/valarray/valarray | constructs new valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc valarray}}
{{dcl list mem dtor | cpp/numeric/valarray/~valarray | destructs the valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc ~valarray}}
{{dcl list mem fun | cpp/numeric/valarray/operator{{=}} | replaces the contents of valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator{{=}}}}
{{dcl list mem fun | cpp/numeric/valarray/operator_at | title=operator[] | get/set valarray element, slice, or mask}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_at}}
{{dcl list mem fun | cpp/numeric/valarray/operator_unary | title=operator+<br>operator-<br>operator~<br>operator! | applies a unary arithmetic operator to each element of the valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_arith}}
{{dcl list mem fun | cpp/numeric/valarray/operator_compound | title=operator*=<br>operator/=<br>operator%=<br>operator+=<br>operator-=<br>operator^=<br>operator&=<br>operator{{!}}=<br>operator<<=<br>operator>>= | applies compound assignment operator to each element of the valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_arith2}}
{{dcl list mem fun | cpp/numeric/valarray/swap | swaps with another valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc swap}}
{{dcl list mem fun | cpp/numeric/valarray/size | returns the size of valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc size}}
{{dcl list mem fun | cpp/numeric/valarray/resize | changes the size of valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc resize}}
{{dcl list mem fun | cpp/numeric/valarray/sum | calculates the sum of all elements}}
+
{{dsc inc|cpp/numeric/valarray/dsc sum}}
{{dcl list mem fun | cpp/numeric/valarray/min | returns the smallest element}}
+
{{dsc inc|cpp/numeric/valarray/dsc min}}
{{dcl list mem fun | cpp/numeric/valarray/max | returns the largest element}}
+
{{dsc inc|cpp/numeric/valarray/dsc max}}
{{dcl list mem fun | cpp/numeric/valarray/shift | zero-filling shift the elements of the valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc shift}}
{{dcl list mem fun | cpp/numeric/valarray/cshift | circular shift of the elements of the valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc cshift}}
{{dcl list mem fun | cpp/numeric/valarray/apply | applies a function to every element of a valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc apply}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Non-member functions===
 
===Non-member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list tfun | cpp/numeric/valarray/swap2 | title=std::swap{{small|(std::valarray)}} | specializes the {{cpp/ltf|cpp/algorithm/swap}} algorithm| notes={{mark c++11}}}}
+
{{dsc inc|cpp/numeric/valarray/dsc swap2}}
{{dcl list tfun | cpp/numeric/valarray/operator_binary | title=operator*<br>operator/<br>operator%<br>operator+<br>operator-<br>operator^<br>operator&<br>operator{{!}}<br>operator<<<br>operator>><br>operator&&<br>operator{{!!}} | applies binary operators to each element of two valarrays, or a valarray and a value }}
+
{{dsc inc|cpp/numeric/valarray/dsc begin2}}
{{dcl list tfun | cpp/numeric/valarray/operator_comp | title=operator==<br>operator!=<br>operator<<br>operator><br>operator<=<br>operator>=<br> | compares two valarrays or a valarray with a value }}
+
{{dsc inc|cpp/numeric/valarray/dsc end2}}
{{dcl list tfun | cpp/numeric/valarray/sin | title=sin{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::sin}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_arith3}}
{{dcl list tfun | cpp/numeric/valarray/cos | title=cos{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::cos}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc operator_cmp}}
{{dcl list tfun | cpp/numeric/valarray/tan | title=tan{{dcl small|(std::valarray)}}     | applies the function {{cpp|std::tan}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc abs}}
{{dcl list tfun | cpp/numeric/valarray/asin | title=asin{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::asin}} to each element of valarray }}
+
 
{{dcl list tfun | cpp/numeric/valarray/acos | title=acos{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::acos}} to each element of valarray }}
+
{{dsc h2|Exponential functions}}
{{dcl list tfun | cpp/numeric/valarray/atan | title=atan{{dcl small|(std::valarray)}}     | applies the function {{cpp|std::atan}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc exp}}
{{dcl list tfun | cpp/numeric/valarray/atan2 | title=atan2{{dcl small|(std::valarray)}}  | applies the function {{cpp|std::atan2}} to a valarray and a value }}
+
{{dsc inc|cpp/numeric/valarray/dsc log}}
{{dcl list tfun | cpp/numeric/valarray/sinh | title=sinh{{dcl small|(std::valarray)}}     | applies the function {{cpp|std::sinh}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc log10}}
{{dcl list tfun | cpp/numeric/valarray/cosh | title=cosh{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::cosh}} to each element of valarray }}
+
 
{{dcl list tfun | cpp/numeric/valarray/tanh | title=tanh{{dcl small|(std::valarray)}}    | applies the function {{cpp|std::tanh}} to each element of valarray }}
+
{{dsc h2|Power functions}}
{{dcl list tfun | cpp/numeric/valarray/abs  |  title=abs{{dcl small|(std::valarray)}} | applies the function {{cpp|std::abs}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc pow}}
{{dcl list tfun | cpp/numeric/valarray/exp  |  title=exp{{dcl small|(std::valarray)}} | applies the function {{cpp|std::exp}} to each element of valarray}}
+
{{dsc inc|cpp/numeric/valarray/dsc sqrt}}
{{dcl list tfun | cpp/numeric/valarray/log  |  title=log{{dcl small|(std::valarray)}} | applies the function {{cpp|std::log}} to each element of valarray}}
+
 
{{dcl list tfun | cpp/numeric/valarray/log10 | title=log10{{dcl small|(std::valarray)}} |  applies the function {{cpp|std::log10}} to each element of valarray }}
+
{{dsc h2|Trigonometric functions}}
{{dcl list tfun | cpp/numeric/valarray/pow  |  title=pow{{dcl small|(std::valarray)}} | applies the function {{cpp|std::pow}} to two valarrays or a valarray and a value}}
+
{{dsc inc|cpp/numeric/valarray/dsc sin}}
{{dcl list tfun | cpp/numeric/valarray/sqrt  | title=sqrt{{dcl small|(std::valarray)}} | applies the function {{cpp|std::sqrt}} to each element of valarray }}
+
{{dsc inc|cpp/numeric/valarray/dsc cos}}
{{dcl list template | cpp/numeric/valarray/dcl list begin2}}
+
{{dsc inc|cpp/numeric/valarray/dsc tan}}
{{dcl list template | cpp/numeric/valarray/dcl list end2}}
+
{{dsc inc|cpp/numeric/valarray/dsc asin}}
{{dcl list end}}
+
{{dsc inc|cpp/numeric/valarray/dsc acos}}
 +
{{dsc inc|cpp/numeric/valarray/dsc atan}}
 +
{{dsc inc|cpp/numeric/valarray/dsc atan2}}
 +
 
 +
{{dsc h2|Hyperbolic functions}}
 +
{{dsc inc|cpp/numeric/valarray/dsc sinh}}
 +
{{dsc inc|cpp/numeric/valarray/dsc cosh}}
 +
{{dsc inc|cpp/numeric/valarray/dsc tanh}}
 +
{{dsc end}}
  
 
===Helper classes===
 
===Helper classes===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list class | cpp/numeric/valarray/slice | BLAS-like slice of a valarray: starting index, length, stride }}
+
{{dsc inc|cpp/numeric/valarray/dsc slice}}
{{dcl list tclass | cpp/numeric/valarray/slice_array | proxy to a subset of a valarray after applying a slice }}
+
{{dsc inc|cpp/numeric/valarray/dsc slice_array}}
{{dcl list class | cpp/numeric/valarray/gslice | generalized slice of a valarray: starting index, set of lengths, set of strides}}
+
{{dsc inc|cpp/numeric/valarray/dsc gslice}}
{{dcl list tclass | cpp/numeric/valarray/gslice_array | proxy to a subset of a valarray after applying a gslice }}
+
{{dsc inc|cpp/numeric/valarray/dsc gslice_array}}
{{dcl list tclass | cpp/numeric/valarray/mask_array | proxy to a subset of a valarray after applying a boolean mask operator[] }}
+
{{dsc inc|cpp/numeric/valarray/dsc mask_array}}
{{dcl list tclass | cpp/numeric/valarray/indirect_array | proxy to a subset of a valarray after applying indirect operator[]}}
+
{{dsc inc|cpp/numeric/valarray/dsc indirect_array}}
{{dcl list end}}
+
{{dsc end}}
 +
 
 +
===[[cpp/numeric/valarray/deduction_guides|Deduction guides]]{{mark since c++17}}===
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/experimental/simd/dsc simd}}
 +
{{dsc inc|cpp/experimental/simd/dsc simd_mask}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 02:07, 4 October 2023

 
 
 
 
Defined in header <valarray>
template< class T >
class valarray;

std::valarray is the class for representing and manipulating arrays of values. It supports element-wise mathematical operations and various forms of generalized subscript operators, slicing and indirect access.

Contents

[edit] Notes

std::valarray and helper classes are defined to be free of certain forms of aliasing, thus allowing operations on these classes to be optimized similar to the effect of the keyword restrict in the C programming language. In addition, functions and operators that take valarray arguments are allowed to return proxy objects to make it possible for the compiler to optimize an expression such as v1 = a * v2 + v3; as a single loop that executes v1[i] = a * v2[i] + v3[i]; avoiding any temporaries or multiple passes. However, expression templates make the same optimization technique available for any C++ container, and the majority of numeric libraries prefer expression templates to valarrays for flexibility. Some C++ standard library implementations use expression templates to implement efficient operations on std::valarray (e.g. GNU libstdc++ and LLVM libc++). Only rarely are valarrays optimized any further, as in e.g. Intel Integrated Performance Primitives.

[edit] Template parameters

T - the type of the elements. The type must meet the NumericType requirements

[edit] Member types

Member type Definition
value_type T

[edit] Member functions

constructs new numeric array
(public member function) [edit]
destructs the numeric array
(public member function) [edit]
assigns the contents
(public member function) [edit]
get/set valarray element, slice, or mask
(public member function) [edit]
applies a unary arithmetic operator to each element of the valarray
(public member function) [edit]
applies compound assignment operator to each element of the valarray
(public member function) [edit]
swaps with another valarray
(public member function) [edit]
returns the size of valarray
(public member function) [edit]
changes the size of valarray
(public member function) [edit]
calculates the sum of all elements
(public member function) [edit]
returns the smallest element
(public member function) [edit]
returns the largest element
(public member function) [edit]
zero-filling shift the elements of the valarray
(public member function) [edit]
circular shift of the elements of the valarray
(public member function) [edit]
applies a function to every element of a valarray
(public member function) [edit]

[edit] Non-member functions

specializes the std::swap algorithm
(function template) [edit]
overloads std::begin
(function template) [edit]
specializes std::end
(function template) [edit]
applies binary operators to each element of two valarrays, or a valarray and a value
(function template) [edit]
compares two valarrays or a valarray with a value
(function template) [edit]
applies the function abs to each element of valarray
(function template) [edit]
Exponential functions
applies the function std::exp to each element of valarray
(function template) [edit]
applies the function std::log to each element of valarray
(function template) [edit]
applies the function std::log10 to each element of valarray
(function template) [edit]
Power functions
applies the function std::pow to two valarrays or a valarray and a value
(function template) [edit]
applies the function std::sqrt to each element of valarray
(function template) [edit]
Trigonometric functions
applies the function std::sin to each element of valarray
(function template) [edit]
applies the function std::cos to each element of valarray
(function template) [edit]
applies the function std::tan to each element of valarray
(function template) [edit]
applies the function std::asin to each element of valarray
(function template) [edit]
applies the function std::acos to each element of valarray
(function template) [edit]
applies the function std::atan to each element of valarray
(function template) [edit]
applies the function std::atan2 to a valarray and a value
(function template) [edit]
Hyperbolic functions
applies the function std::sinh to each element of valarray
(function template) [edit]
applies the function std::cosh to each element of valarray
(function template) [edit]
applies the function std::tanh to each element of valarray
(function template) [edit]

[edit] Helper classes

BLAS-like slice of a valarray: starting index, length, stride
(class) [edit]
proxy to a subset of a valarray after applying a slice
(class template) [edit]
generalized slice of a valarray: starting index, set of lengths, set of strides
(class) [edit]
proxy to a subset of a valarray after applying a gslice
(class template) [edit]
proxy to a subset of a valarray after applying a boolean mask operator[]
(class template) [edit]
proxy to a subset of a valarray after applying indirect operator[]
(class template) [edit]

[edit] Deduction guides(since C++17)

[edit] See also

(parallelism TS v2)
data-parallel vector type
(class template) [edit]
(parallelism TS v2)
data-parallel type with the element type bool
(class template) [edit]