NUMCXX
0.13.20181108
Numerical library for small projects and teaching purposes
|
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:
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.
Definition at line 38 of file tmatrix.hxx.
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... | |
T | 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... | |
|
inherited |
Definition at line 30 of file tarray.hxx.
|
inline |
Construct an empty matrix.
n0 | Number of rows |
n1 | Number of columns |
Definition at line 50 of file tmatrix.hxx.
|
inline |
Construct matrix from data pointer.
n0 | Number of rows |
n1 | Number of columns |
data | Pointer to data. |
deleter | Deleter method, |
Definition at line 59 of file tmatrix.hxx.
|
inline |
Construct matrix from data pointer.
n0 | Number of rows |
n1 | Number of columns |
data | Pointer to data. |
deleter | Deleter method. |
Definition at line 68 of file tmatrix.hxx.
|
inline |
Construct 2D Array from std::initializer list.
Definition at line 72 of file tmatrix.hxx.
|
inline |
Copy constructor.
Definition at line 75 of file tmatrix.hxx.
|
inline |
Construct zero size matrix.
Definition at line 146 of file tmatrix.hxx.
|
inlinestatic |
Construct empty square matrix.
Mainly for access from python
Definition at line 81 of file tmatrix.hxx.
|
inlinestatic |
Construct matrix from std::initializer list.
Definition at line 84 of file tmatrix.hxx.
|
inline |
Create a copy of the matrix.
Definition at line 89 of file tmatrix.hxx.
|
inline |
Create a clone of the matrix.
Definition at line 94 of file tmatrix.hxx.
|
inline |
Element read access.
i0 | Row index of element to be accessed. |
i1 | Column index of element to be accessed. |
Definition at line 101 of file tmatrix.hxx.
|
inline |
Element write access.
i0 | Row index of element to be accessed. |
i1 | Column index of element to be accessed. |
x | value to be copied to element at index. |
Definition at line 109 of file tmatrix.hxx.
|
inline |
Matrix entry access for use in expression templates.
Definition at line 113 of file tmatrix.hxx.
|
inline |
Getter routine for access from python.
This access is rather expensive, as it constructs a smart pointer to the row.
i0 | row index of element to be accessed. |
Definition at line 121 of file tmatrix.hxx.
|
inline |
Assignment operator.
Definition at line 126 of file tmatrix.hxx.
|
inline |
Assignment operator.
Definition at line 129 of file tmatrix.hxx.
|
inlinevirtual |
Apply matrix to vector.
This method performs a matrix-vector multiplication.
For float and double datatypes, sgemm resp. dgemm of LAPACK are called.
u | input |
v | output: v=A*u |
Reimplemented from numcxx::TLinOperator< T >.
Definition at line 60 of file tmatrix.ixx.
|
inline |
Calculate inverse of matrix.
Definition at line 70 of file tmatrix.ixx.
|
inline |
Definition at line 149 of file tmatrix.hxx.
|
inline |
Definition at line 18 of file tmatrix.ixx.
|
inline |
Definition at line 39 of file tmatrix.ixx.
|
inlineinherited |
Access operator for 1D arrays.
i0 | Index of element to be accessed. |
Definition at line 36 of file tarray.hxx.
|
inlineinherited |
Definition at line 37 of file tarray.hxx.
|
inlineinherited |
Access operator for 2D arrays.
i0 | Row index of element to be accessed. |
i0 | Column index of element to be accessed. |
Definition at line 44 of file tarray.hxx.
|
inlineinherited |
Definition at line 45 of file tarray.hxx.
|
inlineinherited |
Obtain tensor dimension of array.
Tensor dimension is 1 for vectors, 2 for matrices.
Definition at line 52 of file tarray.hxx.
|
inlineinherited |
Obtain size of array.
This ist the overall number of elements in the array
Definition at line 58 of file tarray.hxx.
|
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.
dim | Tensor dimension. |
Definition at line 68 of file tarray.hxx.
|
inlineinherited |
Add value to all elements.
a | Summand for each element. |
Definition at line 74 of file tarray.hxx.
|
inlineinherited |
Subtract value from all elements.
a | Value to be subracted from each element. |
Definition at line 80 of file tarray.hxx.
|
inlineinherited |
Multiply all elements by value.
a | Multiplicator for each element. |
Definition at line 87 of file tarray.hxx.
|
inlineinherited |
Divide each element by value.
a | Divisor for each element. |
Definition at line 94 of file tarray.hxx.
|
inlinestaticinherited |
Binary operation on arrays.
f | Function performing operation for each index. |
A | First array argument. |
B | Second array argument. |
Definition at line 314 of file tarray.ixx.
|
inlinestaticinherited |
Ternary operation on arrays.
f | Function performing operation for each index. |
A | First array argument. |
B | Second array argument. |
C | Third array argument. |
Definition at line 320 of file tarray.ixx.
|
inlineinherited |
|
inlineinherited |
Const reference to entry for use in expression templates.
Definition at line 118 of file tarray.hxx.
|
inlineinherited |
Obtain C-pointer of data array.
Definition at line 128 of file tarray.hxx.
|
inlineinherited |
Resize array.
Definition at line 282 of file tarray.ixx.
|
inlineinherited |
Definition at line 351 of file tarray.ixx.
|
inlineprotectedinherited |
Check if all shapes are the same.
Throw an exception on error
Definition at line 145 of file tarray.ixx.
|
inlineprotectedinherited |
1D Array index calculation with optional bounds check.
Definition at line 156 of file tarray.ixx.
|
inlineprotectedinherited |
2D Array index calculation with optional bounds check.
Definition at line 165 of file tarray.ixx.
|
inlineprotectedinherited |
3D Array index calculation with optional bounds check.
Definition at line 175 of file tarray.ixx.
|
inlineprotectedinherited |
Nullify contents of array (for move constructors)
Definition at line 254 of file tarray.ixx.
|
inlineprotectedinherited |
|
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.
|
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.
|
protectedinherited |
Data pointer.
Definition at line 186 of file tarray.hxx.