NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
numcxx::TMatrix< T > Class Template Reference

Description

template<typename T>
class numcxx::TMatrix< T >

Dense matrix class.

Besides two dimensional indexing like for numcxx::TArray2 it provides the method numcxx::TMatrix::apply() for efficient matrix-vector multiplication via LAPACK dgemm.

Alternatively LeftMatrixMultiplicationExpression<A,B> operator*(const A& a, const B& b) can be used as an expression template which allows an instance of this class to be used in typical linear algebra expressions.

Instances of this class can be created in various ways. The preferred construction of empty array goes like this:

std::shared_ptr<numcxx::TMatrix<double>> pA=numcxx::TMatrix<double>::create(n,m)

As a derived class from numcxx::TArray<T> it is merely a facade to the content in the base class.

An alias numcxx::DMatrix for numcxx::TMatrix<double> is available from numcxx.h.

Examples:
13-numcxx-lapack.cxx.

Definition at line 38 of file tmatrix.hxx.

+ Inheritance diagram for numcxx::TMatrix< T >:
+ Collaboration diagram for numcxx::TMatrix< T >:

Public Types

typedef T value_type
 

Public Member Functions

 TMatrix (index n1, index n2)
 Construct an empty matrix. More...
 
 TMatrix (index n1, index n2, T *data, std::function< void(T *p)> deleter)
 Construct matrix from data pointer. More...
 
 TMatrix (index n1, index n2, T *data, std::shared_ptr< void > datamanager)
 Construct matrix from data pointer. More...
 
 TMatrix (const std::initializer_list< std::initializer_list< T >> &il)
 Construct 2D Array from std::initializer list. More...
 
 TMatrix (const TMatrix< T > &A)
 Copy constructor. More...
 
std::shared_ptr< TMatrix< T > > copy () const
 Create a copy of the matrix. More...
 
std::shared_ptr< TMatrix< T > > clone () const
 Create a clone of the matrix. More...
 
item (index i0, index i1)
 Element read access. More...
 
void itemset (index i0, index i1, T x)
 Element write access. More...
 
const T & xentry (const index i, const index j) const
 Matrix entry access for use in expression templates. More...
 
std::shared_ptr< TArray1< T > > const __getitem__ (index i0)
 Getter routine for access from python. More...
 
TMatrix< T > & operator= (const TMatrix< T > &expr)
 Assignment operator. More...
 
TMatrix< T > & operator= (const T &expr)
 Assignment operator. More...
 
void apply (const TArray< T > &u, TArray< T > &v) const
 Apply matrix to vector. More...
 
std::shared_ptr< TMatrix< T > > calculate_inverse ()
 Calculate inverse of matrix. More...
 
 TMatrix ()
 Construct zero size matrix. More...
 
bool is_matrix ()
 
template<>
void apply (const TArray< double > &u, TArray< double > &v) const
 
template<>
void apply (const TArray< float > &u, TArray< float > &v) const
 
T & operator() (const index i0)
 Access operator for 1D arrays. More...
 
const T & operator() (const index i0) const
 
T & operator() (const index i0, const index i1)
 Access operator for 2D arrays. More...
 
const T & operator() (const index i0, const index i1) const
 
index ndim () const
 Obtain tensor dimension of array. More...
 
size_t size () const
 Obtain size of array. More...
 
index shape (const index dim) const
 Obtain shape of array for given dimension. More...
 
template<typename VAL >
void operator+= (const VAL &a)
 Add value to all elements. More...
 
template<typename VAL >
void operator-= (const VAL &a)
 Subtract value from all elements. More...
 
template<typename VAL >
void operator*= (const VAL &a)
 Multiply all elements by value. More...
 
template<typename VAL >
void operator/= (const VAL &a)
 Divide each element by value. More...
 
T & operator[] (const index i)
 Alternative access operator for 1D arrays. More...
 
const T & operator[] (const index i) const
 Const reference to entry for use in expression templates. More...
 
T * data () const
 Obtain C-pointer of data array. More...
 
void resize (size_t n)
 Resize array. More...
 
void savetxt (std::ostream &s) const
 

Static Public Member Functions

static std::shared_ptr< TMatrix< T > > create (index n1, index n2)
 Construct empty square matrix. More...
 
static std::shared_ptr< TMatrix< T > > create (const std::initializer_list< std::initializer_list< T >> &il)
 Construct matrix from std::initializer list. More...
 
static void operate (std::function< void(T &a, T &b)> f, TArray< T > &A, TArray< T > &B)
 Binary operation on arrays. More...
 
static void operate (std::function< void(T &a, T &b, T &c)> f, TArray< T > &A, TArray< T > &B, TArray< T > &C)
 Ternary operation on arrays. More...
 

Protected Member Functions

void _assert_square () const
 Check if all shapes are the same. More...
 
index _idx (index i0) const
 1D Array index calculation with optional bounds check. More...
 
index _idx (index i0, index i1) const
 2D Array index calculation with optional bounds check. More...
 
index _idx (index i0, index i1, index i2) const
 3D Array index calculation with optional bounds check. More...
 
void _nullify ()
 Nullify contents of array (for move constructors) More...
 
void _setshape (index shape0)
 Set shape of 1D array (for move constructors) More...
 

Protected Attributes

std::function< void(T *p)> _deleter =nullptr
 Deleter method. More...
 
std::shared_ptr< void > _datamanager =nullptr
 Data manager. More...
 
T * _data =nullptr
 Data pointer. More...
 

Member Typedef Documentation

template<typename T>
typedef T numcxx::TArray< T >::value_type
inherited

Definition at line 30 of file tarray.hxx.

Constructor & Destructor Documentation

template<typename T>
numcxx::TMatrix< T >::TMatrix ( index  n1,
index  n2 
)
inline

Construct an empty matrix.

Parameters
n0Number of rows
n1Number of columns

Definition at line 50 of file tmatrix.hxx.

50 : TArray<T>(n1,n2){_assert_square();};
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145

+ Here is the call graph for this function:

template<typename T>
numcxx::TMatrix< T >::TMatrix ( index  n1,
index  n2,
T *  data,
std::function< void(T *p)>  deleter 
)
inline

Construct matrix from data pointer.

Parameters
n0Number of rows
n1Number of columns
dataPointer to data.
deleterDeleter method,
See also
TArray<T>::_deleter

Definition at line 59 of file tmatrix.hxx.

59 : TArray<T>(n1,n2,data,deleter){_assert_square();};
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128

+ Here is the call graph for this function:

template<typename T>
numcxx::TMatrix< T >::TMatrix ( index  n1,
index  n2,
T *  data,
std::shared_ptr< void >  datamanager 
)
inline

Construct matrix from data pointer.

Parameters
n0Number of rows
n1Number of columns
dataPointer to data.
deleterDeleter method.
See also
TArray<T>::_datamanager

Definition at line 68 of file tmatrix.hxx.

68 : TArray<T>(n1,n2,data,datamanager){_assert_square();};
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128

+ Here is the call graph for this function:

template<typename T>
numcxx::TMatrix< T >::TMatrix ( const std::initializer_list< std::initializer_list< T >> &  il)
inline

Construct 2D Array from std::initializer list.

Definition at line 72 of file tmatrix.hxx.

72 : TArray<T>(il){_assert_square();};
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145

+ Here is the call graph for this function:

template<typename T>
numcxx::TMatrix< T >::TMatrix ( const TMatrix< T > &  A)
inline

Copy constructor.

Definition at line 75 of file tmatrix.hxx.

75 :TArray<T>(A.shape(0),A.shape(1)){assign(*this,A);}
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35

+ Here is the call graph for this function:

template<typename T>
numcxx::TMatrix< T >::TMatrix ( )
inline

Construct zero size matrix.

Definition at line 146 of file tmatrix.hxx.

146 : TArray<T>(){_assert_square();};
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145

+ Here is the call graph for this function:

Member Function Documentation

template<typename T>
static std::shared_ptr<TMatrix <T> > numcxx::TMatrix< T >::create ( index  n1,
index  n2 
)
inlinestatic

Construct empty square matrix.

Mainly for access from python

Examples:
14-numcxx-lapack-sharedptr.cxx.

Definition at line 81 of file tmatrix.hxx.

81 { return std::make_shared<TMatrix<T>> (n1,n2); }
template<typename T>
static std::shared_ptr<TMatrix <T> > numcxx::TMatrix< T >::create ( const std::initializer_list< std::initializer_list< T >> &  il)
inlinestatic

Construct matrix from std::initializer list.

Definition at line 84 of file tmatrix.hxx.

84 { return std::make_shared<TMatrix<T>> (il); }
template<typename T>
std::shared_ptr<TMatrix <T> > numcxx::TMatrix< T >::copy ( ) const
inline

Create a copy of the matrix.

Returns
Matrix of the same size with contents initialized to this

Definition at line 89 of file tmatrix.hxx.

89 { return std::make_shared<TMatrix<T>>(*this); }
template<typename T>
std::shared_ptr<TMatrix <T> > numcxx::TMatrix< T >::clone ( ) const
inline

Create a clone of the matrix.

Returns
Matrix of the same size with empty contents.

Definition at line 94 of file tmatrix.hxx.

94 { return create(shape(0),shape(1)); }
static std::shared_ptr< TMatrix< T > > create(index n1, index n2)
Construct empty square matrix.
Definition: tmatrix.hxx:81
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68

+ Here is the call graph for this function:

template<typename T>
T numcxx::TMatrix< T >::item ( index  i0,
index  i1 
)
inline

Element read access.

Parameters
i0Row index of element to be accessed.
i1Column index of element to be accessed.
Returns
Value of element at (i0,i1)

Definition at line 101 of file tmatrix.hxx.

101 { return _data[_idx(i0,i1)];};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
void numcxx::TMatrix< T >::itemset ( index  i0,
index  i1,
x 
)
inline

Element write access.

Parameters
i0Row index of element to be accessed.
i1Column index of element to be accessed.
xvalue to be copied to element at index.

Definition at line 109 of file tmatrix.hxx.

109 { _data[_idx(i0,i1)]=x;};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
const T& numcxx::TMatrix< T >::xentry ( const index  i,
const index  j 
) const
inline

Matrix entry access for use in expression templates.

Definition at line 113 of file tmatrix.hxx.

113 {return _data[_idx(i,j)];}
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
std::shared_ptr<TArray1 <T> > const numcxx::TMatrix< T >::__getitem__ ( index  i0)
inline

Getter routine for access from python.

This access is rather expensive, as it constructs a smart pointer to the row.

Parameters
i0row index of element to be accessed.
Returns
Smart pointer to i0-th row.

Definition at line 121 of file tmatrix.hxx.

121 { return std::shared_ptr<TArray1<T>>(new TArray1<T>(shape(1), &_data[_idx(i0,0)], [](T*p){;}));}
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68

+ Here is the call graph for this function:

template<typename T>
TMatrix<T>& numcxx::TMatrix< T >::operator= ( const TMatrix< T > &  expr)
inline

Assignment operator.

Definition at line 126 of file tmatrix.hxx.

126 {return static_cast<TMatrix<T>&>(assign(*this,expr));}
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35

+ Here is the call graph for this function:

template<typename T>
TMatrix<T>& numcxx::TMatrix< T >::operator= ( const T &  expr)
inline

Assignment operator.

Definition at line 129 of file tmatrix.hxx.

129 {return static_cast<TMatrix<T>&>(assign(*this,expr));}
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35

+ Here is the call graph for this function:

template<typename T >
void numcxx::TMatrix< T >::apply ( const TArray< T > &  u,
TArray< T > &  v 
) const
inlinevirtual

Apply matrix to vector.

This method performs a matrix-vector multiplication.

For float and double datatypes, sgemm resp. dgemm of LAPACK are called.

Parameters
uinput
voutput: v=A*u

Reimplemented from numcxx::TLinOperator< T >.

Definition at line 60 of file tmatrix.ixx.

61  {
62  for (index i=0;i<shape(1);i++)
63  { v[i]=0.0;
64  for (index j=0;j<shape(0);j++)
65  v[i]+=_data[_idx(i,j)]*u[j];
66  }
67  }
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
unsigned int index
Definition: numcxx.hxx:21
T * _data
Data pointer.
Definition: tarray.hxx:186
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68
template<typename T >
std::shared_ptr< TMatrix< T > > numcxx::TMatrix< T >::calculate_inverse ( )
inline

Calculate inverse of matrix.

Definition at line 70 of file tmatrix.ixx.

71  {
72  auto pA=copy(); // essentially superfluous, but we seldomly use this
73  auto pLU=TSolverLapackLU<T>::create(pA);
74  return pLU->calculate_inverse();
75  }
static std::shared_ptr< TSolverLapackLU< T > > create(const std::shared_ptr< TMatrix< T >> pMatrix)
Static wrapper around constructor.
std::shared_ptr< TMatrix< T > > copy() const
Create a copy of the matrix.
Definition: tmatrix.hxx:89

+ Here is the call graph for this function:

template<typename T>
bool numcxx::TMatrix< T >::is_matrix ( )
inline

Definition at line 149 of file tmatrix.hxx.

149 {return true;}
template<>
void numcxx::TMatrix< double >::apply ( const TArray< double > &  u,
TArray< double > &  v 
) const
inline

Definition at line 18 of file tmatrix.ixx.

19  {
20  v.resize(u.size());
21  char transmat[2]={'T','\0'};
22  char transvec[2]={'N','\0'};
23  int n=shape(0);
24  int ione=1;
25  double done=1.0;
26  double dzero=0.0;
27  dgemm_(transmat,
28  transvec,
29  &n,&ione,&n,
30  &done,
31  _data,&n,
32  u.data(),&n,
33  &dzero,
34  v.data(),&n);
35  }
void dgemm_(char *TRANSA, char *TRANSB, int *M, int *N, int *K, double *ALPHA, const double *A, int *LDA, double *B, int *LDB, double *BETA, double *C, int *LDC)
T * _data
Data pointer.
Definition: tarray.hxx:186
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68

+ Here is the call graph for this function:

template<>
void numcxx::TMatrix< float >::apply ( const TArray< float > &  u,
TArray< float > &  v 
) const
inline

Definition at line 39 of file tmatrix.ixx.

40  {
41  v.resize(u.size());
42  char transmat[2]={'T','\0'};
43  char transvec[2]={'N','\0'};
44  int n=shape(0);
45  int ione=1;
46  float done=1.0;
47  float dzero=0.0;
48  sgemm_(transmat,
49  transvec,
50  &n,&ione,&n,
51  &done,
52  _data,&n,
53  u.data(),&n,
54  &dzero,
55  v.data(),&n);
56  }
void sgemm_(char *TRANSA, char *TRANSB, int *M, int *N, int *K, float *ALPHA, const float *A, int *LDA, float *B, int *LDB, float *BETA, float *C, int *LDC)
T * _data
Data pointer.
Definition: tarray.hxx:186
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68

+ Here is the call graph for this function:

template<typename T>
T& numcxx::TArray< T >::operator() ( const index  i0)
inlineinherited

Access operator for 1D arrays.

Parameters
i0Index of element to be accessed.
Returns
Reference to element to be accessed.

Definition at line 36 of file tarray.hxx.

36 { return _data[_idx(i0)];};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
const T& numcxx::TArray< T >::operator() ( const index  i0) const
inlineinherited

Definition at line 37 of file tarray.hxx.

37 { return _data[_idx(i0)];};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
T& numcxx::TArray< T >::operator() ( const index  i0,
const index  i1 
)
inlineinherited

Access operator for 2D arrays.

Parameters
i0Row index of element to be accessed.
i0Column index of element to be accessed.
Returns
Reference to element to be accessed.

Definition at line 44 of file tarray.hxx.

44 { return _data[_idx(i0,i1)];};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
const T& numcxx::TArray< T >::operator() ( const index  i0,
const index  i1 
) const
inlineinherited

Definition at line 45 of file tarray.hxx.

45 { return _data[_idx(i0,i1)];};
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T>
index numcxx::TArray< T >::ndim ( ) const
inlineinherited

Obtain tensor dimension of array.

Tensor dimension is 1 for vectors, 2 for matrices.

Returns
Dimension.

Definition at line 52 of file tarray.hxx.

52 {return _ndim;}
const index _ndim
Tensor dimension.
Definition: tarray.hxx:143
template<typename T>
size_t numcxx::TArray< T >::size ( ) const
inlineinherited

Obtain size of array.

This ist the overall number of elements in the array

Returns
Size.
Examples:
11-numcxx-ref.cxx, 12-numcxx-sharedptr.cxx, 44-transient-heat-fe.cxx, and 45-convdiff1d.cxx.

Definition at line 58 of file tarray.hxx.

58 { return _size;}
size_t _size
Size of array.
Definition: tarray.hxx:146
template<typename T>
index numcxx::TArray< T >::shape ( const index  dim) const
inlineinherited

Obtain shape of array for given dimension.

For 1D arrays, shape(0) is equivalent to the size For 2D arrays, shape(0) is the number of rows and shape(1) the number of columns. This corresponds to the "row major" storage format.

Parameters
dimTensor dimension.
Returns
Number of elements in given dimension.

Definition at line 68 of file tarray.hxx.

68 {return _shape[dim];}
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
template<typename T>
template<typename VAL >
void numcxx::TArray< T >::operator+= ( const VAL &  a)
inlineinherited

Add value to all elements.

Parameters
aSummand for each element.

Definition at line 74 of file tarray.hxx.

74 {xadd(*this,a);}
void xadd(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:54

+ Here is the call graph for this function:

template<typename T>
template<typename VAL >
void numcxx::TArray< T >::operator-= ( const VAL &  a)
inlineinherited

Subtract value from all elements.

Parameters
aValue to be subracted from each element.

Definition at line 80 of file tarray.hxx.

80 {xsub(*this,a);}
void xsub(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:73

+ Here is the call graph for this function:

template<typename T>
template<typename VAL >
void numcxx::TArray< T >::operator*= ( const VAL &  a)
inlineinherited

Multiply all elements by value.

Parameters
aMultiplicator for each element.

Definition at line 87 of file tarray.hxx.

87 {xmul(*this,a);}
void xmul(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:92

+ Here is the call graph for this function:

template<typename T>
template<typename VAL >
void numcxx::TArray< T >::operator/= ( const VAL &  a)
inlineinherited

Divide each element by value.

Parameters
aDivisor for each element.

Definition at line 94 of file tarray.hxx.

94 {xdiv(*this,a);}
void xdiv(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:111

+ Here is the call graph for this function:

template<typename T >
void numcxx::TArray< T >::operate ( std::function< void(T &a, T &b)>  f,
TArray< T > &  A,
TArray< T > &  B 
)
inlinestaticinherited

Binary operation on arrays.

Parameters
fFunction performing operation for each index.
AFirst array argument.
BSecond array argument.

Definition at line 314 of file tarray.ixx.

315  {
316  for(index i=0;i<A._size;i++) f(A._data[i],B._data[i]);
317  }
unsigned int index
Definition: numcxx.hxx:21
double B(double x)
template<typename T >
void numcxx::TArray< T >::operate ( std::function< void(T &a, T &b, T &c)>  f,
TArray< T > &  A,
TArray< T > &  B,
TArray< T > &  C 
)
inlinestaticinherited

Ternary operation on arrays.

Parameters
fFunction performing operation for each index.
AFirst array argument.
BSecond array argument.
CThird array argument.

Definition at line 320 of file tarray.ixx.

321  {
322  for(index i=0;i<A._size;i++) f(A._data[i],B._data[i],C._data[i]);
323  }
unsigned int index
Definition: numcxx.hxx:21
double B(double x)
template<typename T>
T& numcxx::TArray< T >::operator[] ( const index  i)
inlineinherited

Alternative access operator for 1D arrays.

Definition at line 115 of file tarray.hxx.

115 { return _data[i];}
T * _data
Data pointer.
Definition: tarray.hxx:186
template<typename T>
const T& numcxx::TArray< T >::operator[] ( const index  i) const
inlineinherited

Const reference to entry for use in expression templates.

Definition at line 118 of file tarray.hxx.

118 { return _data[i];};
T * _data
Data pointer.
Definition: tarray.hxx:186
template<typename T>
T* numcxx::TArray< T >::data ( ) const
inlineinherited

Obtain C-pointer of data array.

Returns
Address of C-Array managed by the class which holds the data

Definition at line 128 of file tarray.hxx.

128 { return _data;}
T * _data
Data pointer.
Definition: tarray.hxx:186

+ Here is the call graph for this function:

template<typename T >
void numcxx::TArray< T >::resize ( size_t  n)
inlineinherited

Resize array.

Definition at line 282 of file tarray.ixx.

283  {
284  if (_size==n) return;
285 
286  if (_ndim>1)
287  {
288  char errormsg[80];
289  snprintf(errormsg,80,"numcxx::TArray::resize: unable to resize 2D Array to 1D.\n");
290  throw std::runtime_error(errormsg);
291  }
292 
293  if (_datamanager==nullptr)
294  {
295  _deleter(_data);
296  _data=(T*)malloc(sizeof(T)*n);
297  if (_data==nullptr) throw std::runtime_error("numcxx: TArray::resize(): Memory allocation failed");
298  _deleter=[](T*p){free(p);};
299  _size=n;
300  _shape[0]=n;
301  _shape[1]=0;
302  }
303  else
304  {
305  char errormsg[80];
306  snprintf(errormsg,80,"numcxx::TArray::resize: unable to resize - data managed by different object.\n");
307  throw std::runtime_error(errormsg);
308  }
309  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
std::function< void(T *p)> _deleter
Deleter method.
Definition: tarray.hxx:172
size_t _size
Size of array.
Definition: tarray.hxx:146
T * _data
Data pointer.
Definition: tarray.hxx:186
std::shared_ptr< void > _datamanager
Data manager.
Definition: tarray.hxx:183
const index _ndim
Tensor dimension.
Definition: tarray.hxx:143
template<typename T >
void numcxx::TArray< T >::savetxt ( std::ostream &  s) const
inlineinherited

Definition at line 351 of file tarray.ixx.

352  {
353  if (ndim()==1)
354  for (index i=0;i<size();i++) s << _data[i] << std::endl << std::flush;
355  else
356  {
357  for (index i=0;i<shape(0);i++)
358  {
359  for (index j=0;j<shape(1);j++)
360  s << _data[_idx(i,j)] << " ";
361  s<< std::endl;
362  }
363  s << std::flush;
364  }
365  }
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
size_t size() const
Obtain size of array.
Definition: tarray.hxx:58
unsigned int index
Definition: numcxx.hxx:21
index ndim() const
Obtain tensor dimension of array.
Definition: tarray.hxx:52
T * _data
Data pointer.
Definition: tarray.hxx:186
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68

+ Here is the call graph for this function:

template<typename T >
void numcxx::TArray< T >::_assert_square ( ) const
inlineprotectedinherited

Check if all shapes are the same.

Throw an exception on error

Definition at line 145 of file tarray.ixx.

146  {
147  if (_ndim!=2 || _shape[0]!=_shape[1])
148  {
149  char errormsg[80];
150  snprintf(errormsg,80,"numcxx::TArray::_assert_square: unexpected non-equal array dimensions\n");
151  throw std::length_error(errormsg);
152  }
153  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
const index _ndim
Tensor dimension.
Definition: tarray.hxx:143
template<typename T >
index numcxx::TArray< T >::_idx ( index  i0) const
inlineprotectedinherited

1D Array index calculation with optional bounds check.

Definition at line 156 of file tarray.ixx.

157  {
158 #ifdef NUMCXX_CHECK_BOUNDS
159  _check_bounds(0,1,i0);
160 #endif
161  return i0;
162  }
void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const
Bounds checker.
Definition: tarray.ixx:128

+ Here is the call graph for this function:

template<typename T >
index numcxx::TArray< T >::_idx ( index  i0,
index  i1 
) const
inlineprotectedinherited

2D Array index calculation with optional bounds check.

Definition at line 165 of file tarray.ixx.

166  {
167 #ifdef NUMCXX_CHECK_BOUNDS
168  _check_bounds(0,2,i0);
169  _check_bounds(1,2,i1);
170 #endif
171  return i0*_shape[1]+i1;
172  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const
Bounds checker.
Definition: tarray.ixx:128

+ Here is the call graph for this function:

template<typename T >
index numcxx::TArray< T >::_idx ( index  i0,
index  i1,
index  i2 
) const
inlineprotectedinherited

3D Array index calculation with optional bounds check.

Definition at line 175 of file tarray.ixx.

176  {
177 #ifdef NUMCXX_CHECK_BOUNDS
178  _check_bounds(0,3,i0);
179  _check_bounds(1,3,i1);
180  _check_bounds(2,3,i2);
181 #endif
182  return (i0*_shape[0]+i1)*_shape[1]+i2;
183  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
void _check_bounds(index acc_dim, index acc_ndim, index acc_idx) const
Bounds checker.
Definition: tarray.ixx:128

+ Here is the call graph for this function:

template<typename T >
void numcxx::TArray< T >::_nullify ( )
inlineprotectedinherited

Nullify contents of array (for move constructors)

Definition at line 254 of file tarray.ixx.

255  {
256  _shape[0]=0;
257  _shape[1]=0;
258  _shape[2]=0;
259  _size=0;
260  _deleter=[](T*p){;};
261  _datamanager=nullptr;
262  _data=nullptr;
263  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
std::function< void(T *p)> _deleter
Deleter method.
Definition: tarray.hxx:172
size_t _size
Size of array.
Definition: tarray.hxx:146
T * _data
Data pointer.
Definition: tarray.hxx:186
std::shared_ptr< void > _datamanager
Data manager.
Definition: tarray.hxx:183
template<typename T >
void numcxx::TArray< T >::_setshape ( index  shape0)
inlineprotectedinherited

Set shape of 1D array (for move constructors)

Definition at line 266 of file tarray.ixx.

267  {
268  if (_ndim!=1)
269  {
270  char errormsg[80];
271  snprintf(errormsg,80,"numcxx::TArray::resize: unable to set 1D shape for 2D array.\n");
272  throw std::runtime_error(errormsg);
273  }
274  _shape[0]=shape0;
275  _shape[1]=0;
276  _shape[2]=0;
277  _size=shape0;
278  }
index _shape[3]
Shape vector.
Definition: tarray.hxx:149
size_t _size
Size of array.
Definition: tarray.hxx:146
const index _ndim
Tensor dimension.
Definition: tarray.hxx:143

Member Data Documentation

template<typename T>
std::function<void(T*p)> numcxx::TArray< T >::_deleter =nullptr
protectedinherited

Deleter method.

This is the proper method to be used to destroy the data pointer in the array if data manager is null. Depending on the way it was constructed, it may do nothing, free the memory, delete[] the memory, or something else.

Definition at line 172 of file tarray.hxx.

template<typename T>
std::shared_ptr<void> numcxx::TArray< T >::_datamanager =nullptr
protectedinherited

Data manager.

Smart pointer to some other object managing the data pointer. If it is not nullptr, the deleter is not called and the memory corresponding to the data pointer is freed when the object behind the data manager is destroyed.

An example in case is the use of shared_ptr<vector> v as datamanager and v->data() as data pointer

Definition at line 183 of file tarray.hxx.

template<typename T>
T* numcxx::TArray< T >::_data =nullptr
protectedinherited

Data pointer.

Definition at line 186 of file tarray.hxx.


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