63 void set(
const T& value);
136 ASSERT(n0 > 0,
"Dimension n0 must be greater than zero");
137 ASSERT(n1 > 0,
"Dimension n1 must be greater than zero");
138 ASSERT(n2 > 0,
"Dimension n2 must be greater than zero");
139 ASSERT(n3 > 0,
"Dimension n3 must be greater than zero");
140 ASSERT(n4 > 0,
"Dimension n4 must be greater than zero");
148 m_size = n0 * n1 * n2 * n3 * n4;
149 m_data.resize(m_size);
168 ASSERT(n0 > 0,
"Dimension n0 must be greater than zero");
169 ASSERT(n1 > 0,
"Dimension n1 must be greater than zero");
170 ASSERT(n2 > 0,
"Dimension n2 must be greater than zero");
171 ASSERT(n3 > 0,
"Dimension n3 must be greater than zero");
172 ASSERT(n4 > 0,
"Dimension n4 must be greater than zero");
180 m_size = n0 * n1 * n2 * n3 * n4;
181 m_data.resize(m_size, data);
238 m_size = n0 * n1 * n2 * n3 * n4;
239 m_data.resize(size());
272 std::fill_n(&m_data[0], size(), value);
309 std::swap(m_dim[0], m_dim[1]);
311 for(
MInt i = 0; i < tmp.
dim0(); i++) {
312 for(
MInt j = 0; j < tmp.
dim1(); j++) {
313 (*this)(j, i) = tmp(i, j);
333 for(
dim_type i = 0; i < m_maxNoDims; ++i)
350 ASSERT(d >= 0,
"Dimension d must be greater than or equal to zero!");
351 ASSERT(d < m_maxNoDims,
"Dimension d must be less than m_maxNoDims!");
460 ASSERT(index >= 0 && index < size(),
"Index out of bounds");
462 return m_data[index];
481 ASSERT(index >= 0 && index < size(),
"Index out of bounds");
483 return m_data[index];
499 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
517 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
536 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
537 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
539 return m_data[i0 * dim1() + i1];
556 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
557 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
559 return m_data[i0 * dim1() + i1];
577 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
578 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
579 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
581 return m_data[i0 * dim1() * dim2() + i1 * dim2() + i2];
599 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
600 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
601 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
603 return m_data[i0 * dim1() * dim2() + i1 * dim2() + i2];
622 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
623 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
624 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
625 ASSERT(i3 >= 0 && i3 < dim3(),
"Index i3 out of bounds");
627 return m_data[i0 * dim1() * dim2() * dim3() + i1 * dim2() * dim3() + i2 * dim3() + i3];
646 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
647 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
648 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
649 ASSERT(i3 >= 0 && i3 < dim3(),
"Index i3 out of bounds");
651 return m_data[i0 * dim1() * dim2() * dim3() + i1 * dim2() * dim3() + i2 * dim3() + i3];
671 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
672 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
673 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
674 ASSERT(i3 >= 0 && i3 < dim3(),
"Index i3 out of bounds");
675 ASSERT(i4 >= 0 && i4 < dim4(),
"Index i4 out of bounds");
677 return m_data[i0 * dim1() * dim2() * dim3() * dim4() + i1 * dim2() * dim3() * dim4() + i2 * dim3() * dim4()
698 ASSERT(i0 >= 0 && i0 < dim0(),
"Index i0 out of bounds");
699 ASSERT(i1 >= 0 && i1 < dim1(),
"Index i1 out of bounds");
700 ASSERT(i2 >= 0 && i2 < dim2(),
"Index i2 out of bounds");
701 ASSERT(i3 >= 0 && i3 < dim3(),
"Index i3 out of bounds");
702 ASSERT(i4 >= 0 && i4 < dim4(),
"Index i4 out of bounds");
704 return m_data[i0 * dim1() * dim2() * dim3() * dim4() + i1 * dim2() * dim3() * dim4() + i2 * dim3() * dim4()
723 for(
MInt i = 0; i < t.
dim0(); i++) {
724 for(
MInt j = 0; j < t.
dim1(); j++) {
725 for(
MInt k = 0; k < t.
dim2(); k++) {
726 for(
MInt l = 0; l < t.
dim3(); l++) {
727 os <<
"(" << i <<
"," << j <<
"," << k <<
"," << l <<
") = ";
728 os << t(i, j, k, l) << std::endl;
734 os <<
"Dimensions (i,j,k,l) = (" << t.
dim0() <<
"," << t.
dim1() <<
"," << t.
dim2() <<
"," << t.
dim3() <<
")"
884 ASSERT(n >= 0,
"New storage size must be greater or equal to zero.");
885 ASSERT(data != 0,
"Data pointer may not be a null pointer.");
925 ASSERT(n >= 0,
"New storage size must be greater or equal to zero.");
943 ASSERT(n >= 0,
"New storage size must be greater or equal to zero.");
944 ASSERT(data != 0,
"Data pointer may not be a null pointer.");
966 m_isAllocated =
false;
1033 ASSERT(n >= 0 && n < size(),
"Index out of bounds");
1051 ASSERT(n >= 0 && n < size(),
"Index out of bounds");
1065 return m_isAllocated;
1083 m_data = (n <= 0) ?
nullptr :
new T[std::min(n, PTRDIFF_MAX)];
1085 m_isAllocated =
true;
Provides a lightweight and fast class for accessing 1D arrays as multi-dimensional tensors (up to 4D)...
size_type m_dim[m_maxNoDims]
T & operator()(size_type i0, size_type i1)
Provides tensor access element using index notation (2D, non-const version).
Tensor(T *data, size_type n0, size_type n1=1, size_type n2=1, size_type n3=1, size_type n4=1)
Initializes the Tensor object by setting the data pointer and the internal dimensions.
const_pointer const_iterator
void transpose()
Transposes the first two dimensions (i.e. acting as if the tensor is a matrix).
const T & operator[](size_type index) const
Returns a reference to the element at position index (const version).
size_type dim0() const
Return the size of dimension 0.
void clear()
Deletes the data (if internal storage was used) and resets dimensions to zero.
size_type dim1() const
Return the size of dimension 1.
void set(const T &value)
Initializes tensor to constant value.
void assign(size_type n, const T &newValue)
Resizes the tensor to a 1D vector of length 'n' and with initial value 'value'.
const T & operator()(size_type i0) const
Provides tensor element access using index notation (1D, const version).
const value_type & const_reference
size_type size() const
Returns the size of the array as product of all five dimensions (i.e. not the actual array size but t...
size_type dim(size_type d) const
Return the size of dimension d.
Tensor()
Default constructor does nothing but setting all internal member variables to zero.
void swap(Tensor< T > &other)
Efficiently swap this tensor with another one (uses copying of pointers, no data is moved).
detail_::TensorStorage< T > DataStorage
T & operator()(size_type i0, size_type i1, size_type i2)
Provides tensor access element using index notation (3D, non-const version).
const T & operator()(size_type i0, size_type i1, size_type i2, size_type i3, size_type i4) const
Provides tensor element access using index notation (5D, const version).
T & operator()(size_type i0)
Provides tensor access element using index notation (1D, non-const version).
T & operator()(size_type i0, size_type i1, size_type i2, size_type i3)
Provides tensor access element using index notation (4D, non-const version).
Tensor(size_type n0, size_type n1=1, size_type n2=1, size_type n3=1, size_type n4=1)
Initializes the Tensor object by setting the data pointer and the internal dimensions.
T & operator[](size_type index)
Returns a reference to the element at position index (non-const version).
T & operator()(size_type i0, size_type i1, size_type i2, size_type i3, size_type i4)
Provides tensor access element using index notation (5D, non-const version).
size_type resize(size_type n0, size_type n1=1, size_type n2=1, size_type n3=1, size_type n4=1)
Deletes the old data structure and creates a new one with the requested dimensions.
static const dim_type m_maxNoDims
size_type dim2() const
Return the size of dimension 2.
const value_type * const_pointer
Tensor< T > & operator=(Tensor< T > copy)
Assignment operator.
friend void swap(Tensor< TT > &a, Tensor< TT > &b)
Non-member swap exchanges the contents of two Tensors.
const T & operator()(size_type i0, size_type i1, size_type i2) const
Provides tensor element access using index notation (3D, const version).
size_type dim4() const
Return the size of dimension 4.
const T & operator()(size_type i0, size_type i1, size_type i2, size_type i3) const
Provides tensor element access using index notation (4D, const version).
size_type dim3() const
Return the size of dimension 3.
~Tensor()
Resets all internal pointers and variables to zero.
const T & operator()(size_type i0, size_type i1) const
Provides tensor element access using index notation (2D, const version).
A vector-like data storage class that supports both internal and external storage mechanisms.
TensorStorage(size_type n)
Creates a new TensorStorage object using internal storage.
TensorStorage(size_type n, T *data)
Creates a new TensorStorage object using external storage, i.e. no memory is allocated.
MBool isAllocated()
Returns true if this class has allocated its own memory, and false if external memory is used.
~TensorStorage()
The destructor only calls clear().
void reallocate(size_type n)
Clears any existing internal data storage and then allocates new memory to store as many elements as ...
TensorStorage()
Default constructor does not allocate any memory but creates a zero-sized storage container.
T & operator[](size_type n)
Returns a reference to the element at position n (non-const version).
size_type size() const
Returns the current size of the data container.
void resize(size_type n)
Resizes the container by first dropping all existing elements and then reallocating new memory.
friend void swap(TensorStorage< TT > &a, TensorStorage< TT > &b)
Swap the contents between two TensorStorage objects.
void clear()
Clears the data storage by deallocating any previously allocated memory and setting the size to zero.
TensorStorage(const TensorStorage< T > &ts)
Copy constructor creates a copy of the provided object.
TensorStorage< T > & operator=(TensorStorage< T > copy)
Assignment operator copies another object to the current one.
const value_type * const_pointer
void swap(TensorStorage< T > &other)
Swap the contents of the existing TensorStorage object with another one.
void resize(size_type n, T *data)
Resizes the container by first dropping all existing elements and then using the external data pointe...
const value_type & const_reference
const_pointer const_iterator
const T & operator[](size_type n) const
Returns a reference to the element at position n (const version).
void swap(TensorStorage< TT > &a, TensorStorage< TT > &b)
Swap the contents between two TensorStorage objects.
std::ostream & operator<<(std::ostream &os, const Tensor< TT > &t)
Prints the values of the Tensor object together with the respective index.
void swap(Tensor< TT > &a, Tensor< TT > &b)
Non-member swap exchanges the contents of two Tensors.
Namespace for auxiliary functions/classes.
maia::tensor::Tensor< MFloat > MFloatMatrix
maia::tensor::Tensor< MInt > MIntVector
maia::tensor::Tensor< MInt > MIntMatrix
maia::tensor::Tensor< MFloat > MFloatTensor
maia::tensor::Tensor< MInt > MIntTensor
maia::tensor::Tensor< MFloat > MFloatVector