planc
Parallel Lowrank Approximation with Non-negativity Constraints
Public Member Functions | Public Attributes | List of all members
planc::Tensor Class Reference

Data is stored such that the unfolding $Y_0$ is column major. More...

#include <tensor.hpp>

Public Member Functions

 Tensor ()
 
 Tensor (const UVEC &i_dimensions)
 Constructor that takes only dimensions of every mode as a vector. More...
 
 Tensor (const UVEC &i_dimensions, const UVEC &i_start_idx)
 
 Tensor (const UVEC &i_dimensions, double *i_data)
 Need when copying from matrix to Tensor. More...
 
 ~Tensor ()
 
 Tensor (const Tensor &src)
 copy constructor More...
 
Tensoroperator= (const Tensor &other)
 
void swap (Tensor &in)
 
void clear ()
 Clears the data and also destroys the storage. More...
 
int modes () const
 Return the number of modes. It is a scalar value. More...
 
UVEC dimensions () const
 Returns a vector of dimensions on every mode. More...
 
UVEC global_idx () const
 
int dimension (int i) const
 Returns the dimension of the input mode. More...
 
UWORD numel () const
 Returns total number of elements. More...
 
void set_idx (const UVEC &i_start_idx)
 
UWORD dimensions_leave_out_one (int i) const
 Return the product of dimensions except mode i. More...
 
void zeros ()
 Zeros out the entire tensor. More...
 
void rand ()
 set the tensor with uniform random. More...
 
void randi ()
 set the tensor with uniform int values More...
 
void randu (const int i_seed=-1)
 set the tensor with uniform random values starting with a seed. More...
 
void mttkrp (const int i_n, const MAT &i_krp, MAT *o_mttkrp) const
 size of krp must be product of all dimensions leaving out nxk. More...
 
void print () const
 prints the value of the tensor. More...
 
void print (const UVEC &global_dims, const UVEC &global_start_sub)
 
double norm () const
 returns the frobenius norm of the tensor More...
 
double err (const Tensor &b) const
 Computes the squared error with the input tensor. More...
 
template<typename NumericType >
void scale (NumericType scale)
 Scales the tensor with the constant value. More...
 
template<typename NumericType >
void shift (NumericType i_shift)
 Shifts (add or subtract) the tensor with the constant value. More...
 
template<typename NumericType >
void bound (NumericType min, NumericType max)
 Truncate all the value between min and max. More...
 
template<typename NumericType >
void lower_bound (NumericType min)
 Sets only the lower bound. More...
 
void write (std::string filename, std::ios_base::openmode mode=std::ios_base::out)
 Write the tensor to the given filename. More...
 
void read (std::string filename, std::ios_base::openmode mode=std::ios_base::in)
 Reads a tensor from the file. More...
 
UWORD sub2ind (UVEC sub)
 Given a vector of subscripts, it return the linear index in the tensor. More...
 
double at (UVEC sub)
 

Public Attributes

std::vector< double > m_data
 

Detailed Description

Data is stored such that the unfolding $Y_0$ is column major.

This means the flattening $Y_{N-1}$ is row-major, and any other flattening $Y_n$ can be represented as a set of $\prod\limits_{k=n+1}^{N-1}I_k$ row major matrices, each of which is $I_n \times \prod\limits_{k=0}^{n-1}I_k$.

Definition at line 32 of file tensor.hpp.

Constructor & Destructor Documentation

◆ Tensor() [1/5]

planc::Tensor::Tensor ( )
inline

Definition at line 75 of file tensor.hpp.

◆ Tensor() [2/5]

planc::Tensor::Tensor ( const UVEC i_dimensions)
inlineexplicit

Constructor that takes only dimensions of every mode as a vector.

Definition at line 82 of file tensor.hpp.

◆ Tensor() [3/5]

planc::Tensor::Tensor ( const UVEC i_dimensions,
const UVEC i_start_idx 
)
inline

Definition at line 90 of file tensor.hpp.

◆ Tensor() [4/5]

planc::Tensor::Tensor ( const UVEC i_dimensions,
double *  i_data 
)
inline

Need when copying from matrix to Tensor.

otherwise copy constructor will be called. The data will be passed in row major order

Definition at line 104 of file tensor.hpp.

◆ ~Tensor()

planc::Tensor::~Tensor ( )
inline

Definition at line 112 of file tensor.hpp.

◆ Tensor() [5/5]

planc::Tensor::Tensor ( const Tensor src)
inline

copy constructor

Definition at line 117 of file tensor.hpp.

Member Function Documentation

◆ at()

double planc::Tensor::at ( UVEC  sub)
inline

Definition at line 516 of file tensor.hpp.

◆ bound()

template<typename NumericType >
void planc::Tensor::bound ( NumericType  min,
NumericType  max 
)
inline

Truncate all the value between min and max.

Any value beyond the min and max will be truncated to min and max.

Parameters
[in]min- any value less than min will be set to min
[in]max- any value greater than max will be set to max

Definition at line 399 of file tensor.hpp.

◆ clear()

void planc::Tensor::clear ( )
inline

Clears the data and also destroys the storage.

Definition at line 153 of file tensor.hpp.

◆ dimension()

int planc::Tensor::dimension ( int  i) const
inline

Returns the dimension of the input mode.

Parameters
[in]amode of the tensor.
Returns
dimension of ith mode

Definition at line 170 of file tensor.hpp.

◆ dimensions()

UVEC planc::Tensor::dimensions ( ) const
inline

Returns a vector of dimensions on every mode.

Definition at line 161 of file tensor.hpp.

◆ dimensions_leave_out_one()

UWORD planc::Tensor::dimensions_leave_out_one ( int  i) const
inline

Return the product of dimensions except mode i.

Parameters
[in]modei
Returns
product of dimensions except mode i

Definition at line 180 of file tensor.hpp.

◆ err()

double planc::Tensor::err ( const Tensor b) const
inline

Computes the squared error with the input tensor.

Parameters
[in]ban input tensor

Definition at line 357 of file tensor.hpp.

◆ global_idx()

UVEC planc::Tensor::global_idx ( ) const
inline

Definition at line 162 of file tensor.hpp.

◆ lower_bound()

template<typename NumericType >
void planc::Tensor::lower_bound ( NumericType  min)
inline

Sets only the lower bound.

Parameters
[in]min- any value less than min will be set to min

Definition at line 413 of file tensor.hpp.

◆ modes()

int planc::Tensor::modes ( ) const
inline

Return the number of modes. It is a scalar value.

Definition at line 159 of file tensor.hpp.

◆ mttkrp()

void planc::Tensor::mttkrp ( const int  i_n,
const MAT i_krp,
MAT o_mttkrp 
) const
inline

size of krp must be product of all dimensions leaving out nxk.

o_mttkrp will be of size dimension[n]xk. Memory must be allocated and freed by the caller

Parameters
[in]i_nmode number
[in]i_krpKhatri-rao product matrix leaving out mode i_n
[out]o_mttkrppointer to the mttkrp matrix.

Definition at line 242 of file tensor.hpp.

◆ norm()

double planc::Tensor::norm ( ) const
inline

returns the frobenius norm of the tensor

Definition at line 346 of file tensor.hpp.

◆ numel()

UWORD planc::Tensor::numel ( ) const
inline

Returns total number of elements.

Definition at line 172 of file tensor.hpp.

◆ operator=()

Tensor& planc::Tensor::operator= ( const Tensor other)
inline

Definition at line 127 of file tensor.hpp.

◆ print() [1/2]

void planc::Tensor::print ( ) const
inline

prints the value of the tensor.

Definition at line 325 of file tensor.hpp.

◆ print() [2/2]

void planc::Tensor::print ( const UVEC global_dims,
const UVEC global_start_sub 
)
inline

Definition at line 332 of file tensor.hpp.

◆ rand()

void planc::Tensor::rand ( )
inline

set the tensor with uniform random.

Definition at line 191 of file tensor.hpp.

◆ randi()

void planc::Tensor::randi ( )
inline

set the tensor with uniform int values

Definition at line 200 of file tensor.hpp.

◆ randu()

void planc::Tensor::randu ( const int  i_seed = -1)
inline

set the tensor with uniform random values starting with a seed.

can be used to generate the same tensor again and again.

Parameters
[in]aninteger seed value preferrably a relative prime number

Definition at line 215 of file tensor.hpp.

◆ read()

void planc::Tensor::read ( std::string  filename,
std::ios_base::openmode  mode = std::ios_base::in 
)
inline

Reads a tensor from the file.

Compile with -D_FILE_OFFSET_BITS=64 to support large files greater than 2GB

Parameters
[in]filenameas std::string

Definition at line 460 of file tensor.hpp.

◆ scale()

template<typename NumericType >
void planc::Tensor::scale ( NumericType  scale)
inline

Scales the tensor with the constant value.

Parameters
[in]scalecan be an int, float or double value

Definition at line 372 of file tensor.hpp.

◆ set_idx()

void planc::Tensor::set_idx ( const UVEC i_start_idx)
inline

Definition at line 174 of file tensor.hpp.

◆ shift()

template<typename NumericType >
void planc::Tensor::shift ( NumericType  i_shift)
inline

Shifts (add or subtract) the tensor with the constant value.

If the i_shift is negative it subtracts, otherwise it adds.

Parameters
[in]i_shiftcan be an int, float or double value

Definition at line 385 of file tensor.hpp.

◆ sub2ind()

UWORD planc::Tensor::sub2ind ( UVEC  sub)
inline

Given a vector of subscripts, it return the linear index in the tensor.

Parameters
[in]vectorof subscript
Returns
linear index within the tensor

Definition at line 508 of file tensor.hpp.

◆ swap()

void planc::Tensor::swap ( Tensor in)
inline

Definition at line 140 of file tensor.hpp.

◆ write()

void planc::Tensor::write ( std::string  filename,
std::ios_base::openmode  mode = std::ios_base::out 
)
inline

Write the tensor to the given filename.

Compile with -D_FILE_OFFSET_BITS=64 to support large files greater than 2GB

Parameters
[in]filenameas std::string

Definition at line 426 of file tensor.hpp.

◆ zeros()

void planc::Tensor::zeros ( )
inline

Zeros out the entire tensor.

Definition at line 185 of file tensor.hpp.

Member Data Documentation

◆ m_data

std::vector<double> planc::Tensor::m_data

Definition at line 73 of file tensor.hpp.


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