Overview
In C/C++, fixed size arrays can be allocated on the stack and variable size arrays are allocated on the heap. However this is inflexible and also managing the array such as adding elements, removing elements or retrieving will require extra code.
The standard library provides valarray class is similar to a vector but mathematical functions can be applied directly to all the elements. It also allows to get different cross section of the elements in the valarray including multidimensional using helper classes.
Details
valarray are basically template based, class is designed to hold an array of values of arithmetic types, and easily perform mathematical operations on them. It also allows special mechanisms to refer to subsets of elements in the arrays.
Most mathematical operations can be applied directly to valarray objects, including arithmetic and comparison operators, affecting all its elements.
Syntax
The syntax is as below. Template parameter T represents the datatype to store.
template < class T >class valarray;
Members
It defines following types
member type | definition |
---|---|
value_type | The first template parameter (T) |
Helper classes
The following helper classes are used to refer to a cross section of elements within a valarray.
Name | Description |
---|---|
slice➹ | A definition to a single dimension collection of elements to a valarray. |
slice_array➹ | A collection of elements in the valarray referred by the slice. |
gslice➹ | A definition to a multi dimension collection of elements to a valarray. |
gslice_array➹ | A collection of elements in the valarray referred by the slice. |
mask_array➹ | A valarray<bool> object that refers to individual elements in a valarray. |
indirect_array➹ | A valarray<size_t> object that contain indices to individual elements in a valarray. |
Functionality
Constructors
Name | Description |
---|---|
valarray() | Default Constructor Example: //v:{} valarray<int> v; |
valarray(size_type n) | Constructs a container with n elements initialized with T(). Example: //v:{0,0,0,0,0} valarray<int> v(5); |
valarray(size_type n, const value_type& val) | Constructs a container with n elements initialized with with val. Example: //v:{10,10,10,10,10}
valarray<int> v(5,10); |
valarray(const valarray& x) | copy constructor |
valarray(valarray&& x) | move constructor |
valarray(slice_array<value_type> sa) | slice_array constructor Example: valarray<int> m {1,2,3,4,5,6,7,8,9}; //diag:{1,5,9} auto diag = valarray(m[slice(0,3,4)]); |
valarray(gslice_array<value_type> gsa) | gslice_array constructor Example: valarray<int> m(4*4*3); int n = 0; for (auto i=0; i<3; ++i) for (auto k=0; k<4; ++k) for (auto j=0; j<4; ++j) m[n++]=(k+1)*100+(j+1)*10+(i+1); |
valarray(mask_array<bool> ma) | mask_array constructor Example: valarray<int> v{1,2,3,4,5,6,7,8,9,10}; valarray<bool> m{1,0,1,0,1,0,1,0,1,0}; //odd:1 3 5 7 9 auto odd = valarray(v[m]); |
valarray(indiriect_array<size_t> sa) | indirect_array constructor Example: valarray<int> v{1,2,3,4,5,6,7,8,9,10}; valarray<size_t> i{1,3,5,6,9}; //even:2 4 6 7 10 auto even = valarray(v[i]); |
valarray(initializer_list<value_type> il) | initializer_list constructor Example: //v:{1,2,3,4,5} valarray<int> v{1,2,3,4,5}; |
Iterator
iterators are externally supported.
Name | Description |
---|---|
/*unspecified*/ begin() /*unspecified*/ end() | Returns an unspecified iterator to begin and end. Example: valarray<int> v{1,2,3,4,5}; //prints 1 2 3 4 5 for (auto itr=begin(v); itr!=end(v); ++itr) cout << *itr << ' '; |
Overloaded Operators
Name | Description |
---|---|
valarray operator+() valarray operator-() valarray operator~() valarray <bool> operator!() | Unary operators. Return valarray after applying the operator. |
valarray& operator*= (const valarray& v) valarray& operator*= (const value_type & val) | Self multiplication operators. Return valarray after applying the operator. |
valarray& operator/= (const valarray& v) valarray& operator/= (const value_type & val) | Self division operators. Return valarray after applying the operator. |
valarray& operator%= (const valarray& v) valarray& operator%= (const value_type & val) | Self modulo operators. Return valarray after applying the operator. |
valarray& operator+= (const valarray& v) valarray& operator+= (const value_type & val) | Self add operators. Return valarray after applying the operator. |
valarray& operator-= (const valarray& v) valarray& operator-= (const value_type & val) | Self subtraction operators. Return valarray after applying the operator. |
valarray& operator^= (const valarray& v) valarray& operator^= (const value_type & val) | Self xor operators. Return valarray after applying the operator. |
valarray& operator&= (const valarray& v) valarray& operator&= (const value_type & val) | Self and operators. Return valarray after applying the operator. |
valarray& operator|= (const valarray& v) valarray& operator|= (const value_type & val) | Self or operators. Return valarray after applying the operator. |
valarray& operator<<= (const valarray& v) valarray& operator<<= (const value_type & val) | Self left shift operators. Return valarray after applying the operator. |
valarray& operator>>= (const valarray& v) valarray& operator>>= (const value_type & val) | Self right shift operators. Return valarray after applying the operator. |
valarray<T> operator+ ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator+ ( const value_type & val, const valarray<T>& r) valarray<T> operator+ ( const valarray<T>& l, const value_type & val) | Binary addition operators. Return valarray after applying the operator. |
valarray<T> operator- ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator- ( const value_type & val, const valarray<T>& r) valarray<T> operator- ( const valarray<T>& l, const value_type & val) | Binary subtraction operators. Return valarray after applying the operator. |
valarray<T> operator* ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator* ( const value_type & val, const valarray<T>& r) valarray<T> operator* ( const valarray<T>& l, const value_type & val) | Binary multiplication operators. Return valarray after applying the operator. |
valarray<T> operator/ ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator/ ( const value_type & val, const valarray<T>& r) valarray<T> operator/ ( const valarray<T>& l, const value_type & val) | Binary divide operators. Return valarray after applying the operator. |
valarray<T> operator% ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator% ( const value_type & val, const valarray<T>& r) valarray<T> operator% ( const valarray<T>& l, const value_type & val) | Binary modulo operators. Return valarray after applying the operator. |
valarray<T> operator& ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator& ( const value_type & val, const valarray<T>& r) valarray<T> operator& ( const valarray<T>& l, const value_type & val) | Binary AND operators. Return valarray after applying the operator. |
valarray<T> operator| ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator| ( const value_type & val, const valarray<T>& r) valarray<T> operator| ( const valarray<T>& l, const value_type & val) | Binary OR operators. Return valarray after applying the operator. |
valarray<T> operator^ ( const valarray<T>& l, const valarray<T>& r) valarray<T> operator^ ( const value_type & val, const valarray<T>& r) valarray<T> operator^ ( const valarray<T>& l, const value_type & val) | Binary XOR operators. Return valarray after applying the operator. |
valarray<T> operator<<( const valarray<T>& l, const valarray<T>& r) valarray<T> operator<< ( const value_type & val, const valarray<T>& r) valarray<T> operator<< ( const valarray<T>& l, const value_type & val) | Binary left shift operators. Return valarray after applying the operator. |
valarray<T> operator>>( const valarray<T>& l, const valarray<T>& r) valarray<T> operator>> ( const value_type & val, const valarray<T>& r) valarray<T> operator>> ( const valarray<T>& l, const value_type & val) | Binary right shift operators. Return valarray after applying the operator. |
valarray<bool> operator&&( const valarray<T>& l, const valarray<T>& r) valarray<T> operator&& ( const value_type & val, const valarray<T>& r) valarray<T> operator&& ( const valarray<T>& l, const value_type & val) | Logical AND operators. Return valarray after applying the operator. |
valarray<bool> operator||( const valarray<T>& l, const valarray<T>& r) valarray<T> operator|| ( const value_type & val, const valarray<T>& r) valarray<T> operator|| ( const valarray<T>& l, const value_type & val) | Logical OR operators. Return valarray after applying the operator. |
valarray<T>& operator=( const valarray<T>& other ) valarray<T>& operator=( valarray<T>&& other ) valarray<T>& operator=( const T& val ) valarray<T>& operator=( const slice_array<T>& other ) valarray<T>& operator=( const gslice_array<T>& other ) valarray<T>& operator=( const mask_array<T>& other ) valarray<T>& operator=( const indirect_array<T>& other ) valarray<T>& operator=( initializer_list<T> il ) | Assignment operators. Return valarray after applying the operator. |
Element Access
Name | Description |
---|---|
T& operator[]( size_t pos ) valarray<T> operator[]( slice slicearr ) slice_array<T> operator[]( slice slicearr ) valarray<T> operator[]( const gslice& gslicearr ) gslice_array<T> operator[]( const gslice& gslice ) valarray<T> operator[]( const valarray<bool>& boolarr ) mask_array<T> operator[]( const valarray<bool>& boolarr ) valarray<T> operator[]( const valarray<size_t>& indarr ) indirect_array<T> operator[]( const valarray<size_t>& indarr ) | Index operator |
Modifiers
Name | Description |
---|---|
void resize (size_type n, const value_type& val ) | Size is also changed to n. All elements are initialized to val. Example valarray<int> v{1,2,3}; //v:{9,9,9,9,9,9,9,9} v.resize(8,9); |
void swap( valarray& other ) | Swaps contents with another valarray. Example: valarray<int> v{1,2,3,4}; valarray<int> v2{6,7,8,9}; v.swap(v2); |
Non Modifiers
Name | Description |
---|---|
size_type size() | Returns the number of elements. Example: valarray<int> v{1,2,3,4,5}; //prints 5 cout << v.size(); |
T min() T max() | Returns the minimum and maximum element in the valarray. Example: valarray<int> v{1,2,3,4,5}; //prints 1 5 cout << v.min() << " " << v.max(); |
T sum() | Returns sum of all the element in the valarray. Example: valarray<int> v{1,2,3,4,5}; //prints 15 cout << v.sum(); |
valarray shift (int n) | Returns the valarray of the same size after shifting n elements to the left if n>0 or right if n< 0. The remaining elements are set to default value. Example: valarray<int> v{1,2,3,4,5}; |
valarray shift (int n) | Returns the valarray of the same size after circularly shifting n elements to the left if n>0 or right if n< 0. Example: valarray<int> v{1,2,3,4,5}; |
valarray<T> apply( T func(T) ) | Returns a new valarray of the same size with values which are acquired by applying function func to the previous values of the elements. Example: valarray<int> v{1,2,3,4,5}; //v2:{83,86,77,15,93} valarray<int> v2 = v.apply([](int n){return rand()%100;}); |
Math Functions
The following math functions are overloaded to accept valarray and return after applying the function.
Name | Description |
---|---|
Absolute function. Returns an valarray after applying the function abs to each element of valarray. Example: valarray<int> v{-1,2,-3,4,-5}; //v2:{1,2,3,4,5} valarray<int> v2 = abs(v); | |
valarray<T> exp( const valarray<T>& va ) valarray<T> log( const valarray<T>& va )valarray<T> log10( const valarray<T>& va ) | Logarithmic functions. Returns an valarray after applying the function exp/log/log10 to each element of valarray. |
valarray<T> pow( const valarray<T>& va ) valarray<T> sqrt( const valarray<T>& va ) | Power functions. Returns an valarray after applying the function pow/sqrt to each element of valarray. |
valarray<T>sin( const valarray<T>& va ) valarray<T> cos( const valarray<T>& va )valarray<T> tan( const valarray<T>& va ) | Trigonometric functions. Returns an valarray after applying the function sin/cos/tan to each element of valarray. |
valarray<T> asin( const valarray<T>& va ) valarray<T> acos( const valarray<T>& va )valarray<T> atan( const valarray<T>& va ) valarray<T> atan2( const valarray<T>& va ) | Trigonometric arc functions Returns an valarray after applying the function asin/acos/atan/atan2 to each element of valarray. |
valarray<T> sinh( const valarray<T>& va ) valarray<T> cosh( const valarray<T>& va )valarray<T> tanh( const valarray<T>& va ) | Hyperbolic functions. Returns an valarray after applying the function sinh/cosh/tanh to each element of valarray. |
No comments:
Post a Comment