This is an older version of Boost and was released in 2020.
The current version is 1.89.0.
Tensor
Tensor
Tensor
Description
The templated class tensor<value_t,format_t,storage_t> is the base container adaptor for dense tensors.
Every element $t_{i_1,i_2,\dots,i_p}$ of a $p$-order $(n_1 \times n_2 \times \cdots \times n_p)$-dimensional tensor $T$ is mapped to $j$-th element of a one-dimensional container where $j = \sum_{r=1}^p i_r \cdot w_r$ with $1 \leq i_r \leq n_r $ for $1 \leq r \leq p$.
For the first-order orientation $w_1 = 1$ and $w_k = n_{k-1} \cdot w_{k-1}$ for $k > 1$. For last-order orientation $w_p = 1$ and $ w_k = n_{k+1} \cdot w_{k+1}$ for $k < p$.
Example
#include <boost/numeric/ublas/tensor.hpp>
int main () {
using namespace boost::numeric::ublas;
tensor<double> t{4,2,3};
for (auto k = 0ul; k < t.size (2); ++ k)
for (auto j = 0ul; j < t.size (1); ++ j)
for (auto i = 0ul; i < t.size (0); ++ i)
t.at(i,j,k) = 3*i + 2*j + 5*k;
std::cout << t << std::endl;
}
Evaluates the tensor expressionexpr and copyies all elements of the result.
tensor& operator=(tensor other)
Copies or moves elements of other.
tensor& operator=(const_reference v)
Initialiates all elements of a tensor with v.
Capacity
Member function
Description
bool empty() const
Returns true if a tensor has zero elements.
size_type size() const
Returns the number of elements of the tensor.
size_type rank() const
Returns the number of dimensions of the tensor.
size_type order() const
Returns the number of dimensions of the tensor.
strides_type const& strides() const
Returns a constant reference to the strides of the tensor.
extents_type const& extents() const
Returns a constant reference to the extents of the tensor.
Element access
Member function
Description
pointer data()
Returns a pointer the first element of the tensor.
const_pointer data() const
Returns a const_pointer the first element of the tensor.
reference operator[](size_type j)
Returns a reference to the j-th element of the storage array of the tensor. Corresponds to the function call tensor::data()+j
const_reference operator[](size_type j) const
Returns a const_reference to the j-th element of the storage array of the tensor. Corresponds to the function call tensor::data()+j.
template<class ... size_types> reference at(size_type i, size_types ... is)
Returns a reference to the (i,is...)-th element of the tensor where (i,is...) denotes a multi-index with tensor::order() elements. If sizeof...(is)==0, tensor::operator[i] is called.
template<class ... size_types> const_reference at(size_type i, size_types ... is)
Returns a const_reference to the (i,is...)-th element of the tensor where (i,is...) denotes a multi-index with tensor::order() elements. If sizeof...(is)==0, tensor::operator[i] is called.
Proxy Generation
Member function
Description
template<std::size_t I, class ... index_types> tensor_index operator()(indices::Index<I> p, index_types ... ps)
Returns a tensor index instance with index objects (p,ps...) for a tensor contraction where sizeof...(ps)+1 must be equal to tensor::order().
Iterators
Member function
Description
const_iterator begin() const
Returns a const_iterator pointing to the first element of the tensor.
const_iterator cbegin() const
Returns a const_iterator pointing to the first element of the tensor.
iterator begin()
Returns an iterator pointing to the first element of the tensor.
const_iterator end() const
Returns a const_iterator pointing to the position after the last element of the tensor.
const_iterator cend() const
Returns a const_iterator pointing to the position after the last element of the tensor.
iterator begin()
Returns an iterator pointing to the position after the last element of the tensor.
Modifiers
Member function
Description
void reshape(extents_type const& e, value_type v = value_type{})
Reshapes the tensor according to the extents e. If e.product() is greater than tensor::size(), the tensor is resized with v.
Notes
[1] Supported parameters
for the storage organization are first_order and
last_order.
[2] Common parameters
for the storage array are std::array<N,T> and
std::vector<T>.