Tensor< T > Class Template Reference

This template class defines a tensor for general purpose use. More...

#include <tensor.h>

Inheritance diagram for Tensor< T >:

Public Member Functions

 Tensor ()
 
 Tensor (const Vector< size_t > &)
 
 Tensor (const Vector< size_t > &, const T &)
 
 Tensor (const size_t &)
 
 Tensor (const size_t &, const size_t &)
 
 Tensor (const size_t &, const size_t &, const size_t &)
 
 Tensor (const size_t &, const size_t &, const size_t &, const size_t &)
 
 Tensor (const Matrix< T > &)
 
virtual ~Tensor ()
 Destructor.
 
Tensor< T > & operator= (const Tensor< T > &)
 
bool operator== (const T &)
 
bool operator== (const Tensor< T > &)
 
bool operator>= (const T &)
 
bool operator<= (const T &)
 
T & operator() (const size_t &)
 
T & operator() (const size_t &, const size_t &)
 
T & operator() (const size_t &, const size_t &, const size_t &)
 
T & operator() (const size_t &, const size_t &, const size_t &, const size_t &)
 
const T & operator() (const size_t &) const
 
const T & operator() (const size_t &, const size_t &) const
 
const T & operator() (const size_t &, const size_t &, const size_t &) const
 
const T & operator() (const size_t &, const size_t &, const size_t &, const size_t &) const
 
Tensor< T > operator+ (const T &) const
 
Tensor< T > operator- (const T &) const
 
Tensor< T > operator* (const T &) const
 
Tensor< T > operator/ (const T &) const
 
Tensor< T > operator+ (const Tensor< T > &) const
 
Tensor< T > operator- (const Tensor< T > &) const
 
Tensor< T > operator* (const Tensor< T > &) const
 
Tensor< T > operator/ (const Tensor< T > &) const
 
void operator*= (const Tensor< T > &)
 
size_t get_dimensions_number () const
 Returns the number of dimensions of the tensor.
 
size_t get_dimension (const size_t &) const
 
size_t get_element (const size_t &, const size_t &) const
 
void add_matrix (const Matrix< T > &)
 
Vector< T > get_row (const size_t &) const
 
Vector< T > get_column (const size_t &) const
 
Vector< string > get_header () const
 Returns a string vector with the header.
 
Tensor< T > get_tensor (const size_t &) const
 Returns a tensor with the elements corresponding to the given index.
 
Matrix< T > get_matrix (const size_t &) const
 
Matrix< T > get_matrix (const size_t &, const size_t &) const
 
void embed (const size_t &, const size_t &, const Matrix< T > &)
 
void embed (const size_t &, const size_t &, const Tensor< T > &)
 
Vector< T > to_vector () const
 This method transforms the tensor into a vector.
 
Matrix< T > to_matrix () const
 This method transforms the tensor into a matrix.
 
Tensor< T > to_2d_tensor () const
 Returns the tensor reshaped as a 2-dimensional tensor.
 
Vector< size_t > get_dimensions () const
 Returns the total number of dimensions of the tensor.
 
void set ()
 Sets the size of this tensor to zero.
 
void set (const size_t &)
 
void set (const size_t &, const size_t &)
 
void set (const Vector< size_t > &)
 
void set (const Vector< size_t > &, const T &)
 
void set (const Tensor< T > &)
 
void set_row (const size_t &, const Vector< T > &)
 
void set_matrix (const size_t &, const size_t &, const Matrix< T > &)
 
void set_matrix (const size_t &, const Matrix< T > &)
 
void set_tensor (const size_t &, const Tensor< T > &)
 
void embed (const size_t &, const Vector< T > &)
 
void initialize (const T &)
 
void initialize_sequential ()
 Initializes all the elements of the tensor in a sequential order (0, 1, 2...).
 
void randomize_uniform (const T &, const T &)
 
void randomize_normal (const double &=0.0, const double &=1.0)
 
calculate_sum () const
 Returns the sum of all the elements in the tensor.
 
Tensor< T > divide (const Vector< T > &, const size_t &) const
 

Private Attributes

Vector< size_t > dimensions
 

Detailed Description

template<typename T>
class OpenNN::Tensor< T >

This template class defines a tensor for general purpose use.

This tensor also implements some mathematical methods which can be useful.

Definition at line 37 of file tensor.h.

Constructor & Destructor Documentation

◆ Tensor() [1/8]

Tensor ( )
explicit

Default constructor. It creates an empty tensor.

Definition at line 162 of file tensor.h.

◆ Tensor() [2/8]

Tensor ( const Vector< size_t > &  new_dimensions)
explicit

Dimensions constructor. It creates a tensor with n dimensions. Note that this method does not initialize the tensor.

Parameters
new_dimensionsDimension of the tensor.

Definition at line 174 of file tensor.h.

◆ Tensor() [3/8]

Tensor ( const Vector< size_t > &  new_dimensions,
const T &  value 
)
explicit

Constructor and initializer. It creates a tensor with given dimensions and initializes it with a given value.

Parameters
new_dimensionsDimension of the tensor.
valueValue to initializes.

Definition at line 186 of file tensor.h.

◆ Tensor() [4/8]

Tensor ( const size_t &  dimension_1)
explicit

Constructor of a first order tensor. Note that this method does not initialize the tensor.

Parameters
dimension_1Number of items.

Definition at line 197 of file tensor.h.

◆ Tensor() [5/8]

Tensor ( const size_t &  dimension_1,
const size_t &  dimension_2 
)
explicit

Constructor of a second order tensor. Note that this method not initialize the tensor.

Parameters
dimension_1Number of items in the first dimension.
dimension_2Number of items in the second dimension.

Definition at line 209 of file tensor.h.

◆ Tensor() [6/8]

Tensor ( const size_t &  dimension_1,
const size_t &  dimension_2,
const size_t &  dimension_3 
)
explicit

Constructor of a third order tensor. Note that this method not initialize the tensor.

Parameters
dimension_1Number of items in the first dimension.
dimension_2Number of items in the second dimension.
dimension_3Number of items in the third dimension.

Definition at line 222 of file tensor.h.

◆ Tensor() [7/8]

Tensor ( const size_t &  dimension_1,
const size_t &  dimension_2,
const size_t &  dimension_3,
const size_t &  dimension_4 
)
explicit

Constructor of a fourth order tensor. Note that this method not initialize the tensor.

Parameters
dimension_1Number of items in the first dimension.
dimension_2Number of items in the second dimension.
dimension_3Number of items in the third dimension.
dimension_4Number of items in the fourth dimension.

Definition at line 236 of file tensor.h.

◆ Tensor() [8/8]

Tensor ( const Matrix< T > &  matrix)
explicit

Copy constructor. It creates a copy of an existing Matrix.

Parameters
matrixMatrix to be copied.

Definition at line 247 of file tensor.h.

Member Function Documentation

◆ embed() [1/3]

void embed ( const size_t &  row_position,
const size_t &  column_position,
const Matrix< T > &  other_matrix 
)

Tuck in another matrix starting from a given position. Note that this method is only valid for tensors of order two.

Parameters
row_positionInsertion row position.
column_positionInsertion row position.
other_matrixMatrix to be inserted.

Definition at line 1079 of file tensor.h.

◆ embed() [2/3]

void embed ( const size_t &  row_position,
const size_t &  column_position,
const Tensor< T > &  other_tensor 
)

Tuck in another matrix starting from a given position. Note that this method is only valid for tensors of order two.

Parameters
row_positionInsertion row position.
column_positionInsertion row position.
other_tensorTensor to be inserted.

Definition at line 1129 of file tensor.h.

◆ embed() [3/3]

void embed ( const size_t &  index,
const Vector< T > &  vector 
)

This method sets a vector of given values into the tensor (by rows) starting from a given index.

Parameters
indexStarting index
vectorVector of values

Definition at line 1346 of file tensor.h.

◆ get_column()

Vector< T > get_column ( const size_t &  j) const

Returns a vector with the column of the given index. Note that this method is only valid for tensors of order two.

Parameters
jColumn index.

Definition at line 834 of file tensor.h.

◆ get_dimension()

size_t get_dimension ( const size_t &  index_dimension) const

Returns the number of itemns in the dimesion with given index. This may be : dimesion_0 , dimension_1, dimension_2 ...

Parameters
index_dimesionSelected dimension..

Definition at line 784 of file tensor.h.

◆ get_element()

size_t get_element ( const size_t &  row,
const size_t &  column 
) const

Returns the element of this tensor with the given indices i,j. Note that this method is only valid for tensors of order two.

Parameters
rowRow index.
columnColumn index.

Definition at line 805 of file tensor.h.

◆ get_matrix() [1/2]

Matrix< T > get_matrix ( const size_t &  matrix_index) const

Returns a matrix with the column and rows of the given indices.

Parameters
matrix_indexMatrix index.

Definition at line 883 of file tensor.h.

◆ get_matrix() [2/2]

Matrix< T > get_matrix ( const size_t &  index_0,
const size_t &  index_1 
) const

Returns a matrix with the elements of this vector between some given indices.

Parameters
index_0Matrix index.
index_1

Definition at line 934 of file tensor.h.

◆ get_row()

Vector< T > get_row ( const size_t &  i) const

Returns a vector with the row of the given index. Note that this method is only valid for tensors of order two.

Parameters
iRow index.

Definition at line 816 of file tensor.h.

◆ initialize()

void initialize ( const T &  value)

Initializes all the elements of the tensor with a given value.

Parameters
valueType value.

Definition at line 1363 of file tensor.h.

◆ operator*() [1/2]

Tensor< T > operator* ( const T &  value) const

Product tensor*scalar arithmetic operator.

Parameters
valueScalar value to be multiplied to this tensor.

Definition at line 693 of file tensor.h.

◆ operator*() [2/2]

Tensor< T > operator* ( const Tensor< T > &  other) const

Element by element product tensor*tensor arithmetic operator.

Parameters
otherTensor to be multiplied to this tensor.

Definition at line 710 of file tensor.h.

◆ operator*=()

void operator*= ( const Tensor< T > &  other)

Tensor product and assignment operator.

Parameters
otherTensor to be multiplied to this tensor.

Definition at line 761 of file tensor.h.

◆ operator+() [1/2]

Tensor< T > operator+ ( const T &  value) const

Sum tensor+scalar arithmetic operator.

Parameters
valueScalar value to be added to this tensor.

Definition at line 625 of file tensor.h.

◆ operator+() [2/2]

Tensor< T > operator+ ( const Tensor< T > &  other) const

Sum tensor+tensor arithmetic operator.

Parameters
vectorTensor to be added to this tensor.

Definition at line 642 of file tensor.h.

◆ operator-() [1/2]

Tensor< T > operator- ( const T &  value) const

Difference tensor-scalar arithmetic operator.

Parameters
valueScalar value to be subtracted to this tensor.

Definition at line 659 of file tensor.h.

◆ operator-() [2/2]

Tensor< T > operator- ( const Tensor< T > &  other) const

Difference tensor-tensor arithmetic operator.

Parameters
otherTensor to be subtracted to this tensor.

Definition at line 676 of file tensor.h.

◆ operator/() [1/2]

Tensor< T > operator/ ( const T &  value) const

Cocient tensor/scalar arithmetic operator.

Parameters
valueScalar value to be divided to this tensor.

Definition at line 727 of file tensor.h.

◆ operator/() [2/2]

Tensor< T > operator/ ( const Tensor< T > &  other) const

Cocient tensor/tensor arithmetic operator.

Parameters
otherTensor to be divided to this tensor.

Definition at line 744 of file tensor.h.

◆ operator<=()

bool operator<= ( const T &  value)
inline

Return true if all the elements of the tensor are equal or less than an other value given to the matrix.

Parameters
valueValue to compare.

Definition at line 422 of file tensor.h.

◆ operator=()

Tensor< T > & operator= ( const Tensor< T > &  other_tensor)
inline

Sets all the entries to a given list.

Parameters
otherTensor to be sets.

Definition at line 360 of file tensor.h.

◆ operator==() [1/2]

bool operator== ( const T &  value)
inline

Return true if all the elements of the tensor are equal to a value given to the matrix.

Parameters
valueValue to compare.

Definition at line 392 of file tensor.h.

◆ operator==() [2/2]

bool operator== ( const Tensor< T > &  other)
inline

Return true if the tensor is equal to an other tensor given to the function

Parameters
otherThe other tensor to compare.

Definition at line 377 of file tensor.h.

◆ operator>=()

bool operator>= ( const T &  value)
inline

Return true if all the elements of the tensor are equal or greater than an other value given to the matrix.

Parameters
valueValue to compare.

Definition at line 407 of file tensor.h.

◆ randomize_normal()

void randomize_normal ( const double &  mean = 0.0,
const double &  standard_deviation = 1.0 
)

Assigns random values to each element in the tensor. These are taken from a normal distribution with single mean and standard deviation values for all the elements.

Parameters
meanMean value of uniform distribution.
standard_deviationStandard deviation value of uniform distribution.

Definition at line 1417 of file tensor.h.

◆ randomize_uniform()

void randomize_uniform ( const T &  minimum,
const T &  maximum 
)

Assigns a random value comprised between a minimum value and a maximum value to each element in the tensor.

Parameters
minimumMinimum initialization value.
maximumMaximum initialization value.

Definition at line 1385 of file tensor.h.

◆ set() [1/3]

void set ( const size_t &  size)

Sets a new dimensions to the tensor. It does not initialize the data.

Parameters
sizeSize for the tensor.

Definition at line 1002 of file tensor.h.

◆ set() [2/3]

void set ( const Vector< size_t > &  new_dimensions)

Sets a new dimension to the tensor. It does not initialize the data.

Parameters
new_dimensionsDimensions for the tensor.

Definition at line 1021 of file tensor.h.

◆ set() [3/3]

void set ( const Vector< size_t > &  new_dimensions,
const T &  value 
)

Sets a new dimension to the vector and initializes all its elements with a given value.

Parameters
new_dimensionsDimensions for the tensor.
new_valueValue to initialize.

Definition at line 1034 of file tensor.h.

◆ set_matrix() [1/2]

void set_matrix ( const size_t &  index_0,
const Matrix< T > &  matrix 
)

This method sets new matrix into this tensor at the given positions.

Parameters
index_0Index of first position.
matrixMatrix to be inserted in this tensor.

Definition at line 1310 of file tensor.h.

◆ set_matrix() [2/2]

void set_matrix ( const size_t &  index_0,
const size_t &  index_1,
const Matrix< T > &  matrix 
)

This method sets new matrix into this tensor at the given positions.

Parameters
index_0Index of first position.
index_1Index of Second position.
matrixMatrix to be inserted in this tensor.

Definition at line 1273 of file tensor.h.

◆ set_row()

void set_row ( const size_t &  row_index,
const Vector< T > &  new_row 
)

Sets new values of a single row in the matrix.

Parameters
row_indexIndex of row.
new_rowNew values of single row.

Definition at line 1061 of file tensor.h.

◆ set_tensor()

void set_tensor ( const size_t &  index_0,
const Tensor< T > &  tensor 
)

This method sets a new tensor into this tensor at the given position.

Parameters
index_0Position the next tensor is to be inserted into.
tensorTensor to be inserted into this tensor.

Definition at line 1331 of file tensor.h.


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