tensor Class Reference

Tensor class with arbitrary dimensions. More...

#include <tensor.h>

Inheritance diagram for tensor:

tensor1 tensor2 tensor3 tensor4 tensor_grid tensor_grid1 tensor_grid2 tensor_grid3 tensor_grid4

Detailed Description

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

Definition at line 56 of file tensor.h.


Public Member Functions

 tensor ()
 Create an empty tensor with zero rank.
 tensor (size_t rank, size_t *dim)
 Create a tensor of rank rank with sizes given in dim.
virtual int set (size_t *index, double val)
 Set the element indexed by index to value val.
virtual double & get (size_t *index)
 Get the element indexed by index.
virtual double const & get (size_t *index) const
 Get the element indexed by index.
ovector_array_stride vector_slice (size_t ix, size_t *index)
 Fix all but one index to create a vector.
omatrix_array matrix_slice (size_t ix, size_t *index)
 Fix all but two indices to create a matrix.
virtual int get_rank ()
 Return the rank of the tensor.
virtual int tensor_allocate (size_t rank, size_t *dim)
 Allocate space for a tensor of rank rank with sizes given in dim.
virtual int tensor_free ()
 Free allocated space (also sets rank to zero).
virtual size_t get_size (size_t i)
 Returns the size of the ith index.
virtual size_t total_size ()
 Returns the size of the tensor.
size_t pack_indices (size_t *index)
 Pack the indices into a single array index.
int unpack_indices (size_t ix, size_t *index)
 Unpack the single array index into indices.

Protected Attributes

double * data
 The data.
size_t * size
 A rank-sized array of the sizes of each dimension.
size_t rk
 Rank.

Constructor & Destructor Documentation

tensor ( size_t  rank,
size_t *  dim 
) [inline]

The parameter dim must be a pointer to an array 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 90 of file tensor.h.


Member Function Documentation

omatrix_array matrix_slice ( size_t  ix,
size_t *  index 
) [inline]

This fixes all of the indices to the values given in index except for the index number ix and the last index, and returns the corresponding matrix, whose size is equal to the size of the tensor in the two indices which are not fixed.

Since the last index is always fixed, the parameter ix is limited to be $ \in [0,\mathrm{rank}-2] $.

Definition at line 252 of file tensor.h.

virtual int tensor_allocate ( size_t  rank,
size_t *  dim 
) [inline, virtual]

The parameter dim must be a pointer to an array of sizes with length rank.

If memory was previously allocated, it will be freed before the new allocation.

If the user requests any of the sizes to be zero, this function will call the error handler and will allocate no memory. If memory was previously allocated, the tensor is left unmodified and no deallocation is performed.

Reimplemented in tensor_grid.

Definition at line 297 of file tensor.h.

ovector_array_stride vector_slice ( size_t  ix,
size_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

        tensor t;
        size_t dim[3]={3,4,5};
        t.tensor_allocate(3,dim);
the following code
        size_t index[3]={1,0,3};
        ovector_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 224 of file tensor.h.


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

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page