Jet  v1.3.3
matrix_csr.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_MATRIX_CSR_H_
8 #define INCLUDE_JET_MATRIX_CSR_H_
9 
10 #include <jet/matrix_expression.h>
11 #include <jet/size2.h>
12 #include <jet/vector_n.h>
13 
14 #include <tuple>
15 
16 namespace jet {
17 
18 template <typename T>
19 class MatrixCsr;
20 
30 template <typename T, typename VE>
32  : public VectorExpression<T, MatrixCsrVectorMul<T, VE>> {
33  public:
34  MatrixCsrVectorMul(const MatrixCsr<T>& m, const VE& v);
36 
38  size_t size() const;
39 
41  T operator[](size_t i) const;
42 
43  private:
44  const MatrixCsr<T>& _m;
45  const VE& _v;
46  const VE* _v2;
47 };
48 
59 template <typename T, typename ME>
61  : public MatrixExpression<T, MatrixCsrMatrixMul<T, ME>> {
62  public:
63  MatrixCsrMatrixMul(const MatrixCsr<T>& m1, const ME& m2);
64 
66  Size2 size() const;
67 
69  size_t rows() const;
70 
72  size_t cols() const;
73 
75  T operator()(size_t i, size_t j) const;
76 
77  private:
78  const MatrixCsr<T>& _m1;
79  const ME& _m2;
80  const T* const _nnz;
81  const size_t* const _rp;
82  const size_t* const _ci;
83 };
84 
95 template <typename T>
96 class MatrixCsr final : public MatrixExpression<T, MatrixCsr<T>> {
97  public:
98  static_assert(
99  std::is_floating_point<T>::value,
100  "MatrixCsr only can be instantiated with floating point types");
101 
102  struct Element {
103  size_t i;
104  size_t j;
105  T value;
106 
108 
109  Element(size_t i, size_t j, const T& value);
110  };
111 
112  typedef std::vector<T> NonZeroContainerType;
113  typedef typename NonZeroContainerType::iterator NonZeroIterator;
114  typedef typename NonZeroContainerType::const_iterator ConstNonZeroIterator;
115 
116  typedef std::vector<size_t> IndexContainerType;
117  typedef typename IndexContainerType::iterator IndexIterator;
118  typedef typename IndexContainerType::const_iterator ConstIndexIterator;
119 
120  // MARK: Constructors
121 
124 
145  MatrixCsr(const std::initializer_list<std::initializer_list<T>>& lst,
146  T epsilon = std::numeric_limits<T>::epsilon());
147 
155  template <typename E>
157  T epsilon = std::numeric_limits<T>::epsilon());
158 
160  MatrixCsr(const MatrixCsr& other);
161 
164 
165  // MARK: Basic setters
166 
168  void clear();
169 
171  void set(const T& s);
172 
174  void set(const MatrixCsr& other);
175 
177  void reserve(size_t rows, size_t cols, size_t numNonZeros);
178 
200  void compress(const std::initializer_list<std::initializer_list<T>>& lst,
201  T epsilon = std::numeric_limits<T>::epsilon());
202 
210  template <typename E>
211  void compress(const MatrixExpression<T, E>& other,
212  T epsilon = std::numeric_limits<T>::epsilon());
213 
215  void addElement(size_t i, size_t j, const T& value);
216 
218  void addElement(const Element& element);
219 
226  void addRow(const NonZeroContainerType& nonZeros,
227  const IndexContainerType& columnIndices);
228 
230  void setElement(size_t i, size_t j, const T& value);
231 
233  void setElement(const Element& element);
234 
235  // MARK: Basic getters
236  bool isEqual(const MatrixCsr& other) const;
237 
240  bool isSimilar(const MatrixCsr& other,
241  double tol = std::numeric_limits<double>::epsilon()) const;
242 
244  bool isSquare() const;
245 
247  Size2 size() const;
248 
250  size_t rows() const;
251 
253  size_t cols() const;
254 
256  size_t numberOfNonZeros() const;
257 
259  const T& nonZero(size_t i) const;
260 
262  T& nonZero(size_t i);
263 
265  const size_t& rowPointer(size_t i) const;
266 
268  const size_t& columnIndex(size_t i) const;
269 
272 
274  const T* const nonZeroData() const;
275 
277  const size_t* const rowPointersData() const;
278 
280  const size_t* const columnIndicesData() const;
281 
284 
287 
290 
293 
296 
299 
302 
305 
308 
311 
314 
317 
318  // MARK: Binary operator methods - new instance = this instance (+) input
319 
321  MatrixCsr add(const T& s) const;
322 
324  MatrixCsr add(const MatrixCsr& m) const;
325 
327  MatrixCsr sub(const T& s) const;
328 
330  MatrixCsr sub(const MatrixCsr& m) const;
331 
333  MatrixCsr mul(const T& s) const;
334 
336  template <typename VE>
338 
340  template <typename ME>
342 
344  MatrixCsr div(const T& s) const;
345 
346  // MARK: Binary operator methods - new instance = input (+) this instance
347 
349  MatrixCsr radd(const T& s) const;
350 
352  MatrixCsr radd(const MatrixCsr& m) const;
353 
355  MatrixCsr rsub(const T& s) const;
356 
358  MatrixCsr rsub(const MatrixCsr& m) const;
359 
361  MatrixCsr rmul(const T& s) const;
362 
364  MatrixCsr rdiv(const T& s) const;
365 
366  // MARK: Augmented operator methods - this instance (+)= input
367 
369  void iadd(const T& s);
370 
372  void iadd(const MatrixCsr& m);
373 
375  void isub(const T& s);
376 
378  void isub(const MatrixCsr& m);
379 
381  void imul(const T& s);
382 
384  template <typename ME>
386 
388  void idiv(const T& s);
389 
390  // MARK: Complex getters
391 
393  T sum() const;
394 
396  T avg() const;
397 
399  T min() const;
400 
402  T max() const;
403 
405  T absmin() const;
406 
408  T absmax() const;
409 
412  T trace() const;
413 
415  template <typename U>
417 
418  // MARK: Setter operators
419 
427  template <typename E>
428  MatrixCsr& operator=(const E& m);
429 
431  MatrixCsr& operator=(const MatrixCsr& other);
432 
435 
437  MatrixCsr& operator+=(const T& s);
438 
441 
443  MatrixCsr& operator-=(const T& s);
444 
447 
449  MatrixCsr& operator*=(const T& s);
450 
452  template <typename ME>
454 
456  MatrixCsr& operator/=(const T& s);
457 
458  // MARK: Getter operators
459 
461  T operator()(size_t i, size_t j) const;
462 
464  bool operator==(const MatrixCsr& m) const;
465 
467  bool operator!=(const MatrixCsr& m) const;
468 
469  // MARK: Builders
472  static MatrixCsr<T> makeIdentity(size_t m);
473 
474  private:
475  Size2 _size;
476  NonZeroContainerType _nonZeros;
477  IndexContainerType _rowPointers;
478  IndexContainerType _columnIndices;
479 
480  size_t hasElement(size_t i, size_t j) const;
481 
482  template <typename Op>
483  MatrixCsr binaryOp(const MatrixCsr& m, Op op) const;
484 };
485 
488 
491 
492 } // namespace jet
493 
494 #include "detail/matrix_csr-inl.h"
495 
496 #endif // INCLUDE_JET_MATRIX_CSR_H_
jet::MatrixCsr::IndexIterator
IndexContainerType::iterator IndexIterator
Definition: matrix_csr.h:117
jet::MatrixCsr::MatrixCsr
MatrixCsr(const std::initializer_list< std::initializer_list< T >> &lst, T epsilon=std::numeric_limits< T >::epsilon())
Compresses given initializer list lst into a sparse matrix.
jet::MatrixCsr::addElement
void addElement(const Element &element)
Adds non-zero element.
jet::MatrixCsr::isSquare
bool isSquare() const
Returns true if this matrix is a square matrix.
jet::MatrixCsr::MatrixCsr
MatrixCsr()
Constructs an empty matrix.
jet::MatrixCsr::sub
MatrixCsr sub(const T &s) const
Returns this matrix - input scalar.
jet::MatrixCsr::Element::Element
Element()
jet::MatrixCsrMatrixMul
Matrix expression for CSR matrix-matrix multiplication.
Definition: matrix_csr.h:61
jet::VectorExpression
Base class for vector expression.
Definition: vector_expression.h:25
jet::MatrixCsr::makeIdentity
static MatrixCsr< T > makeIdentity(size_t m)
jet::MatrixCsr::setElement
void setElement(const Element &element)
Sets non-zero element.
jet::MatrixCsr::operator+=
MatrixCsr & operator+=(const T &s)
Addition assignment with input scalar.
jet::MatrixCsr::MatrixCsr
MatrixCsr(MatrixCsr &&other)
Move constructor.
jet::MatrixCsr::nonZeroBegin
NonZeroIterator nonZeroBegin()
Returns the begin iterator of the non-zero elements.
jet::MatrixCsr::Element
Definition: matrix_csr.h:102
jet::MatrixCsr::columnIndicesData
const size_t *const columnIndicesData() const
Returns constant pointer of the column indices data.
jet::MatrixCsr::rdiv
MatrixCsr rdiv(const T &s) const
Returns input matrix / this scalar.
jet::MatrixCsr::Element::i
size_t i
Definition: matrix_csr.h:103
jet::MatrixCsr::trace
T trace() const
jet::MatrixCsrMatrixMul::MatrixCsrMatrixMul
MatrixCsrMatrixMul(const MatrixCsr< T > &m1, const ME &m2)
jet::MatrixCsr::mul
MatrixCsr mul(const T &s) const
Returns this matrix * input scalar.
jet::MatrixCsr::operator+=
MatrixCsr & operator+=(const MatrixCsr &m)
Addition assignment with input matrix (element-wise).
jet::MatrixCsr::operator*=
MatrixCsr & operator*=(const T &s)
Multiplication assignment with input scalar.
jet::MatrixCsr::imul
void imul(const T &s)
Multiplies input scalar to this matrix.
jet::MatrixCsr::ConstNonZeroIterator
NonZeroContainerType::const_iterator ConstNonZeroIterator
Definition: matrix_csr.h:114
jet::MatrixCsr::rmul
MatrixCsr rmul(const T &s) const
Returns input scalar * this matrix.
size2.h
jet::MatrixCsrMatrixMul::operator()
T operator()(size_t i, size_t j) const
Returns matrix element at (i, j).
jet::MatrixCsr::operator=
MatrixCsr & operator=(const E &m)
Compresses input (dense) matrix expression into a sparse matrix.
jet::MatrixCsr::clear
void clear()
Clears the matrix and make it zero-dimensional.
jet::MatrixCsr::nonZeroData
const T *const nonZeroData() const
Returns constant pointer of the non-zero elements data.
jet::MatrixCsr::add
MatrixCsr add(const MatrixCsr &m) const
Returns this matrix + input matrix (element-wise).
jet::MatrixCsr::nonZero
T & nonZero(size_t i)
Returns i-th non-zero element.
jet::MatrixCsr::addRow
void addRow(const NonZeroContainerType &nonZeros, const IndexContainerType &columnIndices)
jet::MatrixCsrVectorMul
Vector expression for CSR matrix-vector multiplication.
Definition: matrix_csr.h:32
jet::MatrixCsr::radd
MatrixCsr radd(const MatrixCsr &m) const
Returns input matrix + this matrix (element-wise).
jet::MatrixCsr::Element::j
size_t j
Definition: matrix_csr.h:104
jet::MatrixCsr::rowPointersData
const size_t *const rowPointersData() const
Returns constant pointer of the row pointers data.
jet::MatrixCsr::addElement
void addElement(size_t i, size_t j, const T &value)
Adds non-zero element to (i, j).
jet::MatrixCsr
Compressed Sparse Row (CSR) matrix class.
Definition: matrix_csr.h:19
jet::MatrixCsr::Element::Element
Element(size_t i, size_t j, const T &value)
jet::MatrixCsr::columnIndicesEnd
IndexIterator columnIndicesEnd()
Returns the end iterator of the column indices.
jet::MatrixCsr::compress
void compress(const std::initializer_list< std::initializer_list< T >> &lst, T epsilon=std::numeric_limits< T >::epsilon())
Compresses given initializer list lst into a sparse matrix.
jet::MatrixCsr::nonZeroData
T * nonZeroData()
Returns pointer of the non-zero elements data.
jet::MatrixCsr::operator!=
bool operator!=(const MatrixCsr &m) const
Returns true if is not equal to m.
jet::MatrixCsr::rsub
MatrixCsr rsub(const MatrixCsr &m) const
Returns input matrix - this matrix (element-wise).
jet::MatrixCsr::rowPointersEnd
ConstIndexIterator rowPointersEnd() const
Returns the end const iterator of the row pointers.
jet::MatrixCsr::Element::value
T value
Definition: matrix_csr.h:105
jet
Definition: advection_solver2.h:18
jet::MatrixCsr::size
Size2 size() const
Returns the size of this matrix.
jet::MatrixCsr::numberOfNonZeros
size_t numberOfNonZeros() const
Returns the number of non-zero elements.
jet::MatrixCsr::operator-=
MatrixCsr & operator-=(const MatrixCsr &m)
Subtraction assignment with input matrix (element-wise).
jet::MatrixCsr::mul
MatrixCsrMatrixMul< T, ME > mul(const MatrixExpression< T, ME > &m) const
Returns this matrix * input matrix.
jet::MatrixCsr::operator*=
MatrixCsr & operator*=(const MatrixExpression< T, ME > &m)
Multiplication assignment with input matrix.
jet::MatrixCsr::isub
void isub(const MatrixCsr &m)
Subtracts input matrix from this matrix (element-wise).
jet::MatrixCsr::MatrixCsr
MatrixCsr(const MatrixExpression< T, E > &other, T epsilon=std::numeric_limits< T >::epsilon())
Compresses input (dense) matrix expression into a sparse matrix.
jet::MatrixCsr::columnIndicesBegin
IndexIterator columnIndicesBegin()
Returns the begin iterator of the column indices.
jet::MatrixCsr::min
T min() const
Returns minimum among all elements.
jet::MatrixExpression
Base class for matrix expression.
Definition: matrix_expression.h:26
jet::MatrixCsr::MatrixCsr
MatrixCsr(const MatrixCsr &other)
Copy constructor.
jet::MatrixCsr::nonZeroEnd
ConstNonZeroIterator nonZeroEnd() const
Returns the end const iterator of the non-zero elements.
jet::MatrixCsr::isub
void isub(const T &s)
Subtracts input scalar from this matrix.
jet::MatrixCsr::ConstIndexIterator
IndexContainerType::const_iterator ConstIndexIterator
Definition: matrix_csr.h:118
jet::MatrixCsr::operator=
MatrixCsr & operator=(const MatrixCsr &other)
Copies to this matrix.
jet::MatrixCsr::compress
void compress(const MatrixExpression< T, E > &other, T epsilon=std::numeric_limits< T >::epsilon())
Compresses input (dense) matrix expression into a sparse matrix.
jet::MatrixCsr::NonZeroContainerType
std::vector< T > NonZeroContainerType
Definition: matrix_csr.h:112
jet::MatrixCsr::isEqual
bool isEqual(const MatrixCsr &other) const
matrix_expression.h
jet::MatrixCsr::IndexContainerType
std::vector< size_t > IndexContainerType
Definition: matrix_csr.h:116
jet::MatrixCsr::iadd
void iadd(const MatrixCsr &m)
Adds input matrix to this matrix (element-wise).
jet::MatrixCsr::setElement
void setElement(size_t i, size_t j, const T &value)
Sets non-zero element to (i, j).
jet::MatrixCsr::absmax
T absmax() const
Returns absolute maximum among all elements.
jet::MatrixCsrMatrixMul::rows
size_t rows() const
Number of rows.
jet::MatrixCsr::radd
MatrixCsr radd(const T &s) const
Returns input scalar + this matrix.
jet::MatrixCsr::columnIndicesEnd
ConstIndexIterator columnIndicesEnd() const
Returns the end const iterator of the column indices.
jet::MatrixCsr::NonZeroIterator
NonZeroContainerType::iterator NonZeroIterator
Definition: matrix_csr.h:113
jet::MatrixCsrVectorMul::MatrixCsrVectorMul
MatrixCsrVectorMul(const MatrixCsrVectorMul &)
jet::MatrixCsr::max
T max() const
Returns maximum among all elements.
jet::MatrixCsr::operator-=
MatrixCsr & operator-=(const T &s)
Subtraction assignment with input scalar.
jet::MatrixCsr::nonZeroBegin
ConstNonZeroIterator nonZeroBegin() const
Returns the begin const iterator of the non-zero elements.
jet::MatrixCsrF
MatrixCsr< float > MatrixCsrF
Float-type CSR matrix.
Definition: matrix_csr.h:487
jet::MatrixCsr::rows
size_t rows() const
Returns number of rows of this matrix.
jet::MatrixCsrMatrixMul::size
Size2 size() const
Size of the matrix.
jet::Size2
2-D size class.
Definition: size2.h:19
jet::MatrixCsrMatrixMul::cols
size_t cols() const
Number of columns.
jet::MatrixCsr::rsub
MatrixCsr rsub(const T &s) const
Returns input scalar - this matrix.
jet::MatrixCsrVectorMul::MatrixCsrVectorMul
MatrixCsrVectorMul(const MatrixCsr< T > &m, const VE &v)
jet::MatrixCsr::set
void set(const MatrixCsr &other)
Copy from given sparse matrix.
jet::MatrixCsr::imul
void imul(const MatrixExpression< T, ME > &m)
Multiplies input matrix to this matrix.
jet::MatrixCsr::div
MatrixCsr div(const T &s) const
Returns this matrix / input scalar.
jet::MatrixCsrD
MatrixCsr< double > MatrixCsrD
Double-type CSR matrix.
Definition: matrix_csr.h:490
jet::MatrixCsr::rowPointersEnd
IndexIterator rowPointersEnd()
Returns the end iterator of the row pointers.
jet::MatrixCsr::avg
T avg() const
Returns average of all elements.
jet::MatrixCsr::add
MatrixCsr add(const T &s) const
Returns this matrix + input scalar.
jet::MatrixCsr::isSimilar
bool isSimilar(const MatrixCsr &other, double tol=std::numeric_limits< double >::epsilon()) const
vector_n.h
jet::MatrixCsr::rowPointersBegin
ConstIndexIterator rowPointersBegin() const
Returns the begin const iterator of the row pointers.
jet::MatrixCsr::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::MatrixCsr::columnIndex
const size_t & columnIndex(size_t i) const
Returns i-th column index.
jet::MatrixCsr::rowPointer
const size_t & rowPointer(size_t i) const
Returns i-th row pointer.
jet::MatrixCsr::nonZero
const T & nonZero(size_t i) const
Returns i-th non-zero element.
jet::MatrixCsr::operator==
bool operator==(const MatrixCsr &m) const
Returns true if is equal to m.
jet::MatrixCsr::reserve
void reserve(size_t rows, size_t cols, size_t numNonZeros)
Reserves memory space of this matrix.
jet::MatrixCsrVectorMul::operator[]
T operator[](size_t i) const
Returns vector element at i.
jet::MatrixCsr::sub
MatrixCsr sub(const MatrixCsr &m) const
Returns this matrix - input matrix (element-wise).
jet::MatrixCsr::mul
MatrixCsrVectorMul< T, VE > mul(const VectorExpression< T, VE > &v) const
Returns this matrix * input vector.
jet::MatrixCsr::rowPointersBegin
IndexIterator rowPointersBegin()
Returns the begin iterator of the row pointers.
jet::MatrixCsr::columnIndicesBegin
ConstIndexIterator columnIndicesBegin() const
Returns the begin const iterator of the column indices.
jet::MatrixCsr::nonZeroEnd
NonZeroIterator nonZeroEnd()
Returns the end iterator of the non-zero elements.
jet::MatrixCsr::operator=
MatrixCsr & operator=(MatrixCsr &&other)
Moves to this matrix.
jet::MatrixCsr::iadd
void iadd(const T &s)
Adds input scalar to this matrix.
jet::MatrixCsr::set
void set(const T &s)
Sets whole matrix with input scalar.
jet::MatrixCsr::idiv
void idiv(const T &s)
Divides this matrix with input scalar.
jet::MatrixCsr::cols
size_t cols() const
Returns number of columns of this matrix.
jet::MatrixCsr::operator/=
MatrixCsr & operator/=(const T &s)
Division assignment with input scalar.
jet::MatrixCsrVectorMul::size
size_t size() const
Size of the vector.
jet::MatrixCsr::sum
T sum() const
Returns sum of all elements.
jet::MatrixCsr::castTo
MatrixCsr< U > castTo() const
Type-casts to different value-typed matrix.
jet::MatrixCsr::operator()
T operator()(size_t i, size_t j) const
Returns (i,j) element.