Public Types | Public Member Functions | Protected Attributes | List of all members
o2scl::tensor< vec_t, vec_size_t > Class Template Reference

Tensor class with arbitrary dimensions. More...

#include <tensor.h>

Inheritance diagram for o2scl::tensor< vec_t, vec_size_t >:
o2scl::tensor1< vec_t, vec_size_t > o2scl::tensor2< vec_t, vec_size_t > o2scl::tensor3< vec_t, vec_size_t > o2scl::tensor4< vec_t, vec_size_t > o2scl::tensor_grid< vec_t, vec_size_t > o2scl::tensor_grid1< vec_t, vec_size_t > o2scl::tensor_grid2< vec_t, vec_size_t > o2scl::tensor_grid3< vec_t, vec_size_t > o2scl::tensor_grid4< vec_t, vec_size_t >

Public Types

typedef boost::numeric::ublas::vector_slice< boost::numeric::ublas::vector< double > > ubvector_slice
 
typedef boost::numeric::ublas::slice slice
 

Public Member Functions

 tensor ()
 Create an empty tensor with zero rank.
 
template<class size_vec_t >
 tensor (size_t rank, const size_vec_t &dim)
 Create a tensor of rank rank with sizes given in dim. More...
 
Clear method
void clear ()
 Clear the tensor of all data and free allocated memory.
 
Set functions
template<class size_vec_t >
void set (const size_vec_t &index, double val)
 Set the element indexed by index to value val.
 
void set_all (double x)
 Set all elements in a tensor to some fixed value.
 
Get functions
template<class size_vec_t >
double & get (const size_vec_t &index)
 Get the element indexed by index.
 
template<class size_vec_t >
double const & get (const size_vec_t &index) const
 Get a const reference to the element indexed by index.
 
Slice function
template<class size_vec_t >
ubvector_slice vector_slice (size_t ix, const size_vec_t &index)
 Fix all but one index to create a vector. More...
 
Resize method
template<class size_vec_t >
void resize (size_t rank, const size_vec_t &dim)
 Resize the tensor to rank rank with sizes given in dim. More...
 
Size functions
size_t get_rank () const
 Return the rank of the tensor.
 
size_t get_size (size_t i) const
 Returns the size of the ith index.
 
const vec_size_t & get_size_arr () const
 Return the full vector of sizes.
 
const vec_t & get_data () const
 Return the full data vector.
 
size_t total_size () const
 Returns the size of the tensor (the product of the sizes over every index)
 
Index manipulation
template<class size_vec_t >
size_t pack_indices (const size_vec_t &index)
 Pack the indices into a single vector index.
 
template<class size_vec_t >
void unpack_indices (size_t ix, size_vec_t &index)
 Unpack the single vector index into indices.
 
Minimum and maximum
double min_value ()
 Compute the minimum value in the tensor.
 
void min_index (vec_size_t &index)
 Compute the index of the minimum value in the tensor.
 
void min (vec_size_t &index, double &val)
 Compute the index of the minimum value in the tensor and return the minimum.
 
double max_value ()
 Compute the maximum value in the tensor.
 
void max_index (vec_size_t &index)
 Compute the index of the maximum value in the tensor.
 
void max (vec_size_t &index, double &val)
 Compute the index and value of the maximum value in the tensor and return the maximum.
 
void minmax_value (double &min, double &max)
 Compute the minimum and maximum values in the tensor.
 
void minmax_index (vec_size_t &index_min, vec_size_t &index_max)
 Compute the indices of the minimum and maximum values in the tensor.
 
void minmax (vec_size_t &index, size_t &index_min, double &min, size_t &index_max, double &max)
 Compute the indices and values of the maximum and minimum in the tensor.
 

Protected Attributes

vec_t data
 The data.
 
vec_size_t size
 A rank-sized vector of the sizes of each dimension.
 
size_t rk
 Rank.
 

Detailed Description

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
class o2scl::tensor< vec_t, vec_size_t >

The elements of a tensor are typically specified as a list of size_t numbers with length equal to the tensor rank. For a rank-4 tensor named t, the element t[1][2][0][3] can be obtained with something similar to

size_t ix[4]={1,2,0,3};
double x=t.get(ix);

Empty tensors have zero rank.

The type vec_t can be any vector type with operator[], size() and resize() methods. The type vec_size_t can be any integer-like vector type with operator[], size() and resize() methods.

For I/O with tensors, see o2scl_hdf::hdf_file::setd_ten() and o2scl_hdf::hdf_file::getd_ten() . See also the discussion in the sections Tensors and I/O and contiguous storage of the user's guide.

The storage pattern is a generalization of row-major order. In the case of a 4-rank tensor, the location of a generic element is

\[ \left( \left( i_0 s_1 + i_1 \right) s_2 + i_2 \right) s_3 + i_3 \, . \]

In this case the distance between two elements $(i_0,i_1, i_2,i_3)$ and $ (i_0+1,i_1,i_2,i_3) $ is $ s_1 s_2 s_3 $,the distance between two elements $(i_0,i_1,i_2, i_3)$ and $ (i_0,i_1+1,i_2,i_3) $ is $ s_2 s_3 $, and the distance between two elements $(i_0,i_1,i_2,i_3)$ and $ (i_0,i_1,i_2,i_3+1) $ is just unity.

Note
Slices of tensors are subsets obtained from fixing the index of several dimensions, while letting other dimensions vary. For a slice with one dimension not fixed, see vector_slice(). The o2scl::tensor::vector_slice() function should clearly work for uBlas vectors, and seems to work with std::vector objects also, but latter use has not been fully tested.
Idea for Future:
Create an operator[] for tensor and not just tensor1?
Idea for Future:
Could implement arithmetic operators + and - and some different products.
Idea for Future:
Implement copies to and from vector and matrices
Idea for Future:
Implement tensor contractions, i.e. tensor = tensor * tensor
Idea for Future:
Consider making a template type to replace double, e.g. value_t.
Idea for Future:
Could be interesting to write an iterator for this class.
Idea for Future:
Create an is_valid() function which checks sizes

Definition at line 120 of file tensor.h.

Constructor & Destructor Documentation

◆ tensor()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec_t >
o2scl::tensor< vec_t, vec_size_t >::tensor ( size_t  rank,
const size_vec_t &  dim 
)
inline

The parameter dim must be a pointer to a vector of sizes with length rank. If the user requests any of the sizes to be zero, this constructor will call the error handler, create an empty tensor, and will allocate no memory.

Definition at line 154 of file tensor.h.

Member Function Documentation

◆ resize()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec_t >
void o2scl::tensor< vec_t, vec_size_t >::resize ( size_t  rank,
const size_vec_t &  dim 
)
inline

The parameter dim must be a vector of sizes with a length equal to rank. This resize method is always destructive.

If the user requests any of the sizes to be zero, this function will call the error handler.

Definition at line 374 of file tensor.h.

◆ vector_slice()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec_t >
ubvector_slice o2scl::tensor< vec_t, vec_size_t >::vector_slice ( size_t  ix,
const size_vec_t &  index 
)
inline

This fixes all of the indices to the values given in index except for the index number ix, and returns the corresponding vector, whose length is equal to the size of the tensor in that index. The value index[ix] is ignored.

For example, for a rank 3 tensor allocated with

size_t dim[3]={3,4,5};
t.resize(3,dim);

the following code

size_t index[3]={1,0,3};
ubvector_view v=t.vector_slice(1,index);

Gives a vector v of length 4 which refers to the values t(1,0,3), t(1,1,3), t(1,2,3), and t(1,3,3).

Definition at line 329 of file tensor.h.


The documentation for this class was generated from the following file:

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).