NUMCXX  0.13.20181108
Numerical library for small projects and teaching purposes
tmatrix.hxx
Go to the documentation of this file.
1 #ifndef NUMCXX_TMATRIX_H
6 #define NUMCXX_TMATRIX_H
7 
8 #include <cmath>
9 #include <cstdlib>
10 
11 #include "tarray.hxx"
12 
13 namespace numcxx
14 {
36 
37  template<typename T>
38  class TMatrix: public TArray<T>, public TLinOperator<T>, public MatrixExpressionBase
39  {
40  public:
41  using TArray<T>::size;
42  using TArray<T>::shape;
43  using TArray<T>::operator[];
44 
45 
50  TMatrix(index n1, index n2): TArray<T>(n1,n2){_assert_square();};
51 
52 
59  TMatrix(index n1, index n2, T*data,std::function<void(T*p)> deleter): TArray<T>(n1,n2,data,deleter){_assert_square();};
60 
68  TMatrix(index n1, index n2, T*data, std::shared_ptr<void> datamanager): TArray<T>(n1,n2,data,datamanager){_assert_square();};
69 
70 
72  TMatrix(const std::initializer_list<std::initializer_list<T>> &il ): TArray<T>(il){_assert_square();};
73 
75  TMatrix(const TMatrix<T>& A):TArray<T>(A.shape(0),A.shape(1)){assign(*this,A);}
76 
77 
81  static std::shared_ptr<TMatrix <T> > create(index n1, index n2) { return std::make_shared<TMatrix<T>> (n1,n2); }
82 
84  static std::shared_ptr<TMatrix <T> > create(const std::initializer_list<std::initializer_list<T>> &il) { return std::make_shared<TMatrix<T>> (il); }
85 
89  std::shared_ptr<TMatrix <T> > copy() const { return std::make_shared<TMatrix<T>>(*this); }
90 
94  std::shared_ptr<TMatrix <T> > clone() const { return create(shape(0),shape(1)); }
95 
101  T item(index i0,index i1) { return _data[_idx(i0,i1)];};
102 
103 
109  void itemset(index i0, index i1, T x) { _data[_idx(i0,i1)]=x;};
110 
111 
113  const T& xentry(const index i, const index j) const {return _data[_idx(i,j)];}
114 
121  std::shared_ptr<TArray1 <T> > const __getitem__(index i0){ return std::shared_ptr<TArray1<T>>(new TArray1<T>(shape(1), &_data[_idx(i0,0)], [](T*p){;}));}
122 
123 
124 
126  TMatrix<T>& operator=(const TMatrix<T> &expr) {return static_cast<TMatrix<T>&>(assign(*this,expr));}
127 
129  TMatrix<T>& operator=(const T &expr) {return static_cast<TMatrix<T>&>(assign(*this,expr));}
130 
140  void apply(const TArray<T> &u, TArray<T> &v) const;
141 
143  std::shared_ptr<TMatrix<T>> calculate_inverse();
144 
147 
148 
149  bool is_matrix(){return true;}
150 
151  private:
152  using TArray<T>::_data;
153  using TArray<T>::_idx;
155 
156  };
157 
158 }
159 
160 #include "tmatrix.ixx"
161 #endif
162 
static std::shared_ptr< TMatrix< T > > create(const std::initializer_list< std::initializer_list< T >> &il)
Construct matrix from std::initializer list.
Definition: tmatrix.hxx:84
void _assert_square() const
Check if all shapes are the same.
Definition: tarray.ixx:145
TMatrix(const std::initializer_list< std::initializer_list< T >> &il)
Construct 2D Array from std::initializer list.
Definition: tmatrix.hxx:72
TMatrix< T > & operator=(const TMatrix< T > &expr)
Assignment operator.
Definition: tmatrix.hxx:126
index _idx(index i0) const
1D Array index calculation with optional bounds check.
Definition: tarray.ixx:156
Inline method definitions for class numcxx::TMatrix.
const T & xentry(const index i, const index j) const
Matrix entry access for use in expression templates.
Definition: tmatrix.hxx:113
static std::shared_ptr< TMatrix< T > > create(index n1, index n2)
Construct empty square matrix.
Definition: tmatrix.hxx:81
TArray is the common template base class for arrays and dense matrices of the numcxx project...
Definition: tarray.hxx:17
TMatrix< T > & operator=(const T &expr)
Assignment operator.
Definition: tmatrix.hxx:129
std::shared_ptr< TMatrix< T > > copy() const
Create a copy of the matrix.
Definition: tmatrix.hxx:89
bool is_matrix()
Definition: tmatrix.hxx:149
TMatrix(index n1, index n2)
Construct an empty matrix.
Definition: tmatrix.hxx:50
T item(index i0, index i1)
Element read access.
Definition: tmatrix.hxx:101
TMatrix(const TMatrix< T > &A)
Copy constructor.
Definition: tmatrix.hxx:75
std::shared_ptr< TMatrix< T > > clone() const
Create a clone of the matrix.
Definition: tmatrix.hxx:94
unsigned int index
Definition: numcxx.hxx:21
One dimensional array class.
Definition: tarray1.hxx:31
Dense matrix class.
Definition: tmatrix.hxx:38
std::shared_ptr< TMatrix< T > > calculate_inverse()
Calculate inverse of matrix.
Definition: tmatrix.ixx:70
T * _data
Data pointer.
Definition: tarray.hxx:186
Numcxx template library.
Definition: expression.ixx:41
TMatrix(index n1, index n2, T *data, std::function< void(T *p)> deleter)
Construct matrix from data pointer.
Definition: tmatrix.hxx:59
void apply(const TArray< T > &u, TArray< T > &v) const
Apply matrix to vector.
Definition: tmatrix.ixx:60
TArray< T > & assign(TArray< T > &A, const EXPR &expr, const EXPR *x=0)
Definition: tarray.ixx:35
index shape(const index dim) const
Obtain shape of array for given dimension.
Definition: tarray.hxx:68
std::shared_ptr< TArray1< T > > const __getitem__(index i0)
Getter routine for access from python.
Definition: tmatrix.hxx:121
Base class for linear operators (matrices, sparse matrices)
Definition: tarray.hxx:284
TMatrix()
Construct zero size matrix.
Definition: tmatrix.hxx:146
void itemset(index i0, index i1, T x)
Element write access.
Definition: tmatrix.hxx:109
T * data() const
Obtain C-pointer of data array.
Definition: tarray.hxx:128
Header for numcxx::TArray.
TMatrix(index n1, index n2, T *data, std::shared_ptr< void > datamanager)
Construct matrix from data pointer.
Definition: tmatrix.hxx:68