Loading...
Searching...
No Matches
PLMD::TensorGeneric< n, m > Class Template Reference

Class implementing fixed size matrices of doubles. More...

#include <Tensor.h>

Inheritance diagram for PLMD::TensorGeneric< n, m >:

Public Member Functions

 TensorGeneric ()
 initialize the tensor to zero
 TensorGeneric (const VectorGeneric< n > &v1, const VectorGeneric< m > &v2)
 initialize a tensor as an external product of two Vector
template<typename... Args>
 TensorGeneric (double first, Args... arg)
 Constructor accepting n*m double parameters.
double determinant () const
double determinant () const
 returns the determinant
VectorGeneric< n > getCol (unsigned j) const
 get j-th column
VectorGeneric< m > getRow (unsigned i) const
 get i-th row
TensorGeneric< 3, 3 > inverse () const
TensorGeneric inverse () const
 return the matrix inverse
double & operator() (unsigned i, unsigned j)
 access element
const double & operator() (unsigned i, unsigned j) const
 access element
TensorGenericoperator*= (double)
 multiply
TensorGeneric operator+ () const
 return +t
TensorGenericoperator+= (const TensorGeneric< n, m > &b)
 increment
TensorGeneric operator- () const
 return -t
TensorGenericoperator-= (const TensorGeneric< n, m > &b)
 decrement
TensorGenericoperator/= (double)
 divide
Row operator[] (unsigned i)
 access element (with [][] syntax)
TensorGenericsetCol (unsigned j, const VectorGeneric< n > &c)
 set j-th column
TensorGenericsetRow (unsigned i, const VectorGeneric< m > &r)
 set i-th row
TensorGeneric< m, n > transpose () const
 return the transpose matrix
void zero ()
 set it to zero

Static Public Member Functions

static TensorGeneric< n, n > identity ()
 return an identity tensor

Private Member Functions

void auxiliaryConstructor ()
 Auxiliary private function for constructor.
template<typename... Args>
void auxiliaryConstructor (double first, Args... arg)
 Auxiliary private function for constructor.

Private Attributes

std::array< double, n *m > d

Friends

TensorGeneric< 3, 3 > dcrossDv1 (const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
TensorGeneric< 3, 3 > dcrossDv2 (const VectorGeneric< 3 > &v1, const VectorGeneric< 3 > &v2)
TensorGeneric< 3, 3 > deriNorm (const VectorGeneric< 3 > &v1, const TensorGeneric< 3, 3 > &v2)
 Derivative of a normalized vector.
double determinant (const TensorGeneric< 3, 3 > &t)
 returns the determinant of a tensor
template<unsigned n_, unsigned m_>
void diagMatSym (const TensorGeneric< n_, n_ > &, VectorGeneric< m_ > &evals, TensorGeneric< m_, n_ > &evec)
 Diagonalize tensor.
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > extProduct (const VectorGeneric< n > &v1, const VectorGeneric< m > &v2)
 returns the transpose of a tensor (same as TensorGeneric(const VectorGeneric&,const VectorGeneric&))
TensorGeneric< 3, 3 > inverse (const TensorGeneric< 3, 3 > &t)
 returns the inverse of a tensor (same as inverse())
template<unsigned n_, unsigned m_, unsigned l_>
TensorGeneric< n_, l_ > matmul (const TensorGeneric< n_, m_ > &, const TensorGeneric< m_, l_ > &)
 matrix-matrix multiplication
template<unsigned n_, unsigned m_, unsigned l_, unsigned i_>
TensorGeneric< n_, i_ > matmul (const TensorGeneric< n_, m_ > &, const TensorGeneric< m_, l_ > &, const TensorGeneric< l_, i_ > &)
 matrix-matrix-matrix multiplication
template<unsigned n_, unsigned m_, unsigned l_>
VectorGeneric< n_ > matmul (const TensorGeneric< n_, m_ > &, const TensorGeneric< m_, l_ > &, const VectorGeneric< l_ > &)
 matrix-matrix-vector multiplication
template<unsigned n_, unsigned m_>
VectorGeneric< n_ > matmul (const TensorGeneric< n_, m_ > &, const VectorGeneric< m_ > &)
 matrix-vector multiplication
template<unsigned n_, unsigned m_>
VectorGeneric< n_ > matmul (const VectorGeneric< m_ > &, const TensorGeneric< m_, n_ > &)
 vector-matrix multiplication
template<unsigned n_, unsigned m_, unsigned l_>
VectorGeneric< l_ > matmul (const VectorGeneric< n_ > &, const TensorGeneric< n_, m_ > &, const TensorGeneric< m_, l_ > &)
 vector-matrix-matrix multiplication
template<unsigned n_, unsigned m_>
double matmul (const VectorGeneric< n_ > &, const TensorGeneric< n_, m_ > &, const VectorGeneric< m_ > &)
 vector-matrix-vector multiplication
template<unsigned n_>
double matmul (const VectorGeneric< n_ > &a, const VectorGeneric< n_ > &b)
 vector-vector multiplication (maps to dotProduct)
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator* (const TensorGeneric< n_, m_ > &, double s)
 scale the tensor by a factor s
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator* (double, const TensorGeneric< n_, m_ > &)
 scale the tensor by a factor s
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator+ (const TensorGeneric< n_, m_ > &, const TensorGeneric< n_, m_ > &)
 return t1+t2
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator- (const TensorGeneric< n_, m_ > &, const TensorGeneric< n_, m_ > &)
 return t1+t2
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator/ (const TensorGeneric< n_, m_ > &, double s)
 scale the tensor by a factor 1/s
template<unsigned n_, unsigned m_>
std::ostream & operator<< (std::ostream &os, const TensorGeneric< n_, m_ > &)
 << operator.
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > transpose (const TensorGeneric< m_, n_ > &)
 returns the transpose of a tensor (same as transpose())
TensorGeneric< 3, 3 > VcrossTensor (const TensorGeneric< 3, 3 > &v2, const VectorGeneric< 3 > &v1)
TensorGeneric< 3, 3 > VcrossTensor (const VectorGeneric< 3 > &v1, const TensorGeneric< 3, 3 > &v2)

Detailed Description

template<unsigned n, unsigned m>
class PLMD::TensorGeneric< n, m >

Class implementing fixed size matrices of doubles.

Template Parameters
nThe number rows
mThe number columns

This class implements a matrix of doubles with size fixed at compile time. It is useful for small fixed size objects (e.g. 3x3 tensors) as it does not waste space to store the vector size. Moreover, as the compiler knows the size, it can be completely opimized inline. Most of the loops are explicitly unrolled using PLMD::LoopUnroller class Matrix elements are initialized to zero by default. Notice that this means that constructor is a bit slow. This point might change in future if we find performance issues. It takes advantage of MatrixSquareBracketsAccess to provide both () and [] syntax for access. Several functions are declared as friends even if not necessary so as to properly appear in Doxygen documentation.

Aliases are defined to simplify common declarations (Tensor, Tensor2d, Tensor3d, Tensor4d). Also notice that some operations are only available for 3x3 tensors.

Example of usage

#include "Tensor.h"

using namespace PLMD;

int main(){
  Tensor a;
  TensorGeneric<3,2> b;
  TensorGeneric<3,2> c=matmul(a,b);
  return 0;
}

Constructor & Destructor Documentation

◆ TensorGeneric() [1/3]

template<unsigned n, unsigned m>
template<typename... Args>
PLMD::TensorGeneric< n, m >::TensorGeneric ( double first,
Args... arg )

Constructor accepting n*m double parameters.

Can be used as Tensor<2,2>(1.0,2.0,3.0,4.0) In case a wrong number of parameters is given, a static assertion will fail.

◆ TensorGeneric() [2/3]

template<unsigned n, unsigned m>
PLMD::TensorGeneric< n, m >::TensorGeneric ( )

initialize the tensor to zero

◆ TensorGeneric() [3/3]

template<unsigned n, unsigned m>
PLMD::TensorGeneric< n, m >::TensorGeneric ( const VectorGeneric< n > & v1,
const VectorGeneric< m > & v2 )

initialize a tensor as an external product of two Vector

Member Function Documentation

◆ auxiliaryConstructor() [1/2]

template<unsigned n, unsigned m>
void PLMD::TensorGeneric< n, m >::auxiliaryConstructor ( )
private

Auxiliary private function for constructor.

◆ auxiliaryConstructor() [2/2]

template<unsigned n, unsigned m>
template<typename... Args>
void PLMD::TensorGeneric< n, m >::auxiliaryConstructor ( double first,
Args... arg )
private

Auxiliary private function for constructor.

◆ determinant() [1/2]

double PLMD::TensorGeneric< 3, 3 >::determinant ( ) const
inline

◆ determinant() [2/2]

template<unsigned n, unsigned m>
double PLMD::TensorGeneric< n, m >::determinant ( ) const

returns the determinant

◆ getCol()

template<unsigned n, unsigned m>
VectorGeneric< n > PLMD::TensorGeneric< n, m >::getCol ( unsigned j) const

get j-th column

◆ getRow()

template<unsigned n, unsigned m>
VectorGeneric< m > PLMD::TensorGeneric< n, m >::getRow ( unsigned i) const

get i-th row

◆ identity()

template<unsigned n, unsigned m>
TensorGeneric< n, n > PLMD::TensorGeneric< n, m >::identity ( )
inlinestatic

return an identity tensor

◆ inverse() [1/2]

TensorGeneric< 3, 3 > PLMD::TensorGeneric< 3, 3 >::inverse ( ) const
inline

◆ inverse() [2/2]

template<unsigned n, unsigned m>
TensorGeneric PLMD::TensorGeneric< n, m >::inverse ( ) const

return the matrix inverse

◆ operator()() [1/2]

template<unsigned n, unsigned m>
double & PLMD::TensorGeneric< n, m >::operator() ( unsigned i,
unsigned j )

access element

◆ operator()() [2/2]

template<unsigned n, unsigned m>
const double & PLMD::TensorGeneric< n, m >::operator() ( unsigned i,
unsigned j ) const

access element

◆ operator*=()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::operator*= ( double s)

multiply

◆ operator+()

template<unsigned n, unsigned m>
TensorGeneric< n, m > PLMD::TensorGeneric< n, m >::operator+ ( ) const

return +t

◆ operator+=()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::operator+= ( const TensorGeneric< n, m > & b)

increment

◆ operator-()

template<unsigned n, unsigned m>
TensorGeneric< n, m > PLMD::TensorGeneric< n, m >::operator- ( ) const

return -t

◆ operator-=()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::operator-= ( const TensorGeneric< n, m > & b)

decrement

◆ operator/=()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::operator/= ( double s)

divide

◆ operator[]()

MatrixSquareBracketsAccess< TensorGeneric< n, m >, double, unsigned, unsigned >::Row PLMD::MatrixSquareBracketsAccess< TensorGeneric< n, m >, double, unsigned, unsigned >::operator[] ( unsigned i)
inherited

access element (with [][] syntax)

◆ setCol()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::setCol ( unsigned j,
const VectorGeneric< n > & c )

set j-th column

◆ setRow()

template<unsigned n, unsigned m>
TensorGeneric< n, m > & PLMD::TensorGeneric< n, m >::setRow ( unsigned i,
const VectorGeneric< m > & r )

set i-th row

◆ transpose()

template<unsigned n, unsigned m>
TensorGeneric< m, n > PLMD::TensorGeneric< n, m >::transpose ( ) const

return the transpose matrix

◆ zero()

template<unsigned n, unsigned m>
void PLMD::TensorGeneric< n, m >::zero ( )

set it to zero

◆ dcrossDv1

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > dcrossDv1 ( const VectorGeneric< 3 > & v1,
const VectorGeneric< 3 > & v2 )
friend

◆ dcrossDv2

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > dcrossDv2 ( const VectorGeneric< 3 > & v1,
const VectorGeneric< 3 > & v2 )
friend

◆ deriNorm

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > deriNorm ( const VectorGeneric< 3 > & v1,
const TensorGeneric< 3, 3 > & v2 )
friend

Derivative of a normalized vector.

◆ determinant

template<unsigned n, unsigned m>
double determinant ( const TensorGeneric< 3, 3 > & t)
friend

returns the determinant of a tensor

◆ diagMatSym

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
void diagMatSym ( const TensorGeneric< n_, n_ > & ,
VectorGeneric< m_ > & evals,
TensorGeneric< m_, n_ > & evec )
friend

Diagonalize tensor.

Syntax is the same as Matrix::diagMat. In addition, it is possible to call if with m_ smaller than n_. In this case, only the first (smaller) m_ eigenvalues and eigenvectors are retrieved. If case lapack fails (info!=0) it throws an exception. Notice that tensor is assumed to be symmetric!!!

◆ extProduct

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > extProduct ( const VectorGeneric< n > & v1,
const VectorGeneric< m > & v2 )
friend

returns the transpose of a tensor (same as TensorGeneric(const VectorGeneric&,const VectorGeneric&))

◆ inverse

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > inverse ( const TensorGeneric< 3, 3 > & t)
friend

returns the inverse of a tensor (same as inverse())

◆ matmul [1/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_, unsigned l_>
TensorGeneric< n_, l_ > matmul ( const TensorGeneric< n_, m_ > & ,
const TensorGeneric< m_, l_ > &  )
friend

matrix-matrix multiplication

◆ matmul [2/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_, unsigned l_, unsigned i_>
TensorGeneric< n_, i_ > matmul ( const TensorGeneric< n_, m_ > & ,
const TensorGeneric< m_, l_ > & ,
const TensorGeneric< l_, i_ > &  )
friend

matrix-matrix-matrix multiplication

◆ matmul [3/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_, unsigned l_>
VectorGeneric< n_ > matmul ( const TensorGeneric< n_, m_ > & ,
const TensorGeneric< m_, l_ > & ,
const VectorGeneric< l_ > &  )
friend

matrix-matrix-vector multiplication

◆ matmul [4/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
VectorGeneric< n_ > matmul ( const TensorGeneric< n_, m_ > & ,
const VectorGeneric< m_ > &  )
friend

matrix-vector multiplication

◆ matmul [5/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
VectorGeneric< n_ > matmul ( const VectorGeneric< m_ > & ,
const TensorGeneric< m_, n_ > &  )
friend

vector-matrix multiplication

◆ matmul [6/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_, unsigned l_>
VectorGeneric< l_ > matmul ( const VectorGeneric< n_ > & ,
const TensorGeneric< n_, m_ > & ,
const TensorGeneric< m_, l_ > &  )
friend

vector-matrix-matrix multiplication

◆ matmul [7/8]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
double matmul ( const VectorGeneric< n_ > & ,
const TensorGeneric< n_, m_ > & ,
const VectorGeneric< m_ > &  )
friend

vector-matrix-vector multiplication

◆ matmul [8/8]

template<unsigned n, unsigned m>
template<unsigned n_>
double matmul ( const VectorGeneric< n_ > & a,
const VectorGeneric< n_ > & b )
friend

vector-vector multiplication (maps to dotProduct)

◆ operator* [1/2]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator* ( const TensorGeneric< n_, m_ > & ,
double s )
friend

scale the tensor by a factor s

◆ operator* [2/2]

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator* ( double ,
const TensorGeneric< n_, m_ > &  )
friend

scale the tensor by a factor s

◆ operator+

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator+ ( const TensorGeneric< n_, m_ > & ,
const TensorGeneric< n_, m_ > &  )
friend

return t1+t2

◆ operator-

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator- ( const TensorGeneric< n_, m_ > & ,
const TensorGeneric< n_, m_ > &  )
friend

return t1+t2

◆ operator/

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > operator/ ( const TensorGeneric< n_, m_ > & ,
double s )
friend

scale the tensor by a factor 1/s

◆ operator<<

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
std::ostream & operator<< ( std::ostream & os,
const TensorGeneric< n_, m_ > &  )
friend

<< operator.

Allows printing tensor t with std::cout<<t;

◆ transpose

template<unsigned n, unsigned m>
template<unsigned n_, unsigned m_>
TensorGeneric< n_, m_ > transpose ( const TensorGeneric< m_, n_ > & )
friend

returns the transpose of a tensor (same as transpose())

◆ VcrossTensor [1/2]

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > VcrossTensor ( const TensorGeneric< 3, 3 > & v2,
const VectorGeneric< 3 > & v1 )
friend

◆ VcrossTensor [2/2]

template<unsigned n, unsigned m>
TensorGeneric< 3, 3 > VcrossTensor ( const VectorGeneric< 3 > & v1,
const TensorGeneric< 3, 3 > & v2 )
friend

Member Data Documentation

◆ d

template<unsigned n, unsigned m>
std::array<double,n*m> PLMD::TensorGeneric< n, m >::d
private

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