MatrixCSR.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: MatrixCSR.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Vector expression for CSR matrix-vector multiplication.
6 > Created Time: 2017/09/27
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX_CSR_H
10 #define CUBBYFLOW_MATRIX_CSR_H
11 
14 
15 namespace CubbyFlow
16 {
17  // Forward declaration
18  template <typename T>
19  class MatrixCSR;
20 
30  template <typename T, typename VE>
31  class MatrixCSRVectorMul : public VectorExpression<T, MatrixCSRVectorMul<T, VE>>
32  {
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_m;
45  const VE& m_v;
46  const VE* m_v2;
47  };
48 
59  template <typename T, typename ME>
60  class MatrixCSRMatrixMul : public MatrixExpression<T, MatrixCSRMatrixMul<T, ME>>
61  {
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>& m_m1;
79  const ME& m_m2;
80  const T* const m_nnz;
81  const size_t* const m_rp;
82  const size_t* const m_ci;
83  };
84 
95  template <typename T>
96  class MatrixCSR final : public MatrixExpression<T, MatrixCSR<T>>
97  {
98  public:
99  static_assert(std::is_floating_point<T>::value, "MatrixCSR only can be instantiated with floating point types");
100 
101  struct Element
102  {
103  size_t i;
104  size_t j;
105  T value;
106 
107  Element();
108 
109  Element(size_t i, size_t j, const T& value);
110  };
111 
112  using NonZeroContainerType = std::vector<T>;
113  using NonZeroIterator = typename NonZeroContainerType::iterator;
114  using ConstNonZeroIterator = typename NonZeroContainerType::const_iterator;
115 
116  using IndexContainerType = std::vector<size_t>;
117  using IndexIterator = IndexContainerType::iterator;
118  using ConstIndexIterator = IndexContainerType::const_iterator;
119 
120  // MARK: Constructors
122  MatrixCSR();
123 
145  MatrixCSR(const std::initializer_list<std::initializer_list<T>>& list, T epsilon = std::numeric_limits<T>::epsilon());
146 
154  template <typename E>
155  MatrixCSR(const MatrixExpression<T, E>& other, T epsilon = std::numeric_limits<T>::epsilon());
156 
158  MatrixCSR(const MatrixCSR& other);
159 
161  MatrixCSR(MatrixCSR&& other) noexcept;
162 
163  // MARK: Basic setters
165  void Clear();
166 
168  void Set(const T& s);
169 
171  void Set(const MatrixCSR& other);
172 
174  void Reserve(size_t rows, size_t cols, size_t numNonZeros);
175 
198  void Compress(const std::initializer_list<std::initializer_list<T>>& list, T epsilon = std::numeric_limits<T>::epsilon());
199 
207  template <typename E>
208  void Compress(const MatrixExpression<T, E>& other, T epsilon = std::numeric_limits<T>::epsilon());
209 
211  void AddElement(size_t i, size_t j, const T& value);
212 
214  void AddElement(const Element& element);
215 
222  void AddRow(const NonZeroContainerType& nonZeros, const IndexContainerType& columnIndices);
223 
225  void SetElement(size_t i, size_t j, const T& value);
226 
228  void SetElement(const Element& element);
229 
230  // MARK: Basic getters
231  bool IsEqual(const MatrixCSR& other) const;
232 
234  bool IsSimilar(const MatrixCSR& other, double tol = std::numeric_limits<double>::epsilon()) const;
235 
237  bool IsSquare() const;
238 
240  Size2 size() const;
241 
243  size_t Rows() const;
244 
246  size_t Cols() const;
247 
249  size_t NumberOfNonZeros() const;
250 
252  const T& NonZero(size_t i) const;
253 
255  T& NonZero(size_t i);
256 
258  const size_t& RowPointer(size_t i) const;
259 
261  const size_t& ColumnIndex(size_t i) const;
262 
264  T* NonZeroData();
265 
267  const T* NonZeroData() const;
268 
270  const size_t* RowPointersData() const;
271 
273  const size_t* ColumnIndicesData() const;
274 
277 
280 
283 
286 
289 
292 
295 
298 
301 
304 
307 
310 
311  // MARK: Binary operator methods - new instance = this instance (+) input
313  MatrixCSR Add(const T& s) const;
314 
316  MatrixCSR Add(const MatrixCSR& m) const;
317 
319  MatrixCSR Sub(const T& s) const;
320 
322  MatrixCSR Sub(const MatrixCSR& m) const;
323 
325  MatrixCSR Mul(const T& s) const;
326 
328  template <typename VE>
330 
332  template <typename ME>
334 
336  MatrixCSR Div(const T& s) const;
337 
338  // MARK: Binary operator methods - new instance = input (+) this instance
340  MatrixCSR RAdd(const T& s) const;
341 
343  MatrixCSR RAdd(const MatrixCSR& m) const;
344 
346  MatrixCSR RSub(const T& s) const;
347 
349  MatrixCSR RSub(const MatrixCSR& m) const;
350 
352  MatrixCSR RMul(const T& s) const;
353 
355  MatrixCSR RDiv(const T& s) const;
356 
357  // MARK: Augmented operator methods - this instance (+)= input
359  void IAdd(const T& s);
360 
362  void IAdd(const MatrixCSR& m);
363 
365  void ISub(const T& s);
366 
368  void ISub(const MatrixCSR& m);
369 
371  void IMul(const T& s);
372 
374  template <typename ME>
375  void IMul(const MatrixExpression<T, ME>& m);
376 
378  void IDiv(const T& s);
379 
380  // MARK: Complex getters
382  T Sum() const;
383 
385  T Avg() const;
386 
388  T Min() const;
389 
391  T Max() const;
392 
394  T AbsMin() const;
395 
397  T AbsMax() const;
398 
401  T Trace() const;
402 
404  template <typename U>
405  MatrixCSR<U> CastTo() const;
406 
407  // MARK: Setter operators
414  template <typename E>
415  MatrixCSR& operator=(const E& m);
416 
418  MatrixCSR& operator=(const MatrixCSR& other);
419 
421  MatrixCSR& operator=(MatrixCSR&& other) noexcept;
422 
424  MatrixCSR& operator+=(const T& s);
425 
427  MatrixCSR& operator+=(const MatrixCSR& m);
428 
430  MatrixCSR& operator-=(const T& s);
431 
433  MatrixCSR& operator-=(const MatrixCSR& m);
434 
436  MatrixCSR& operator*=(const T& s);
437 
439  template <typename ME>
441 
443  MatrixCSR& operator/=(const T& s);
444 
445  // MARK: Getter operators
447  T operator()(size_t i, size_t j) const;
448 
450  bool operator==(const MatrixCSR& m) const;
451 
453  bool operator!=(const MatrixCSR& m) const;
454 
455  // MARK: Builders
457  static MatrixCSR<T> MakeIdentity(size_t m);
458 
459  private:
460  Size2 m_size;
461  NonZeroContainerType m_nonZeros;
462  IndexContainerType m_rowPointers;
463  IndexContainerType m_columnIndices;
464 
465  size_t HasElement(size_t i, size_t j) const;
466 
467  template <typename Op>
468  MatrixCSR BinaryOp(const MatrixCSR& m, Op op) const;
469  };
470 
473 
476 }
477 
479 
480 #endif
Vector expression for CSR matrix-vector multiplication.
Definition: MatrixCSR.h:31
void ISub(const T &s)
Subtracts input scalar from this matrix.
Definition: MatrixCSR-Impl.h:662
bool operator!=(const MatrixCSR &m) const
Returns true if is not equal to m.
Definition: MatrixCSR-Impl.h:924
T Trace() const
Definition: MatrixCSR-Impl.h:788
MatrixCSR< U > CastTo() const
Type-casts to different value-typed matrix.
Definition: MatrixCSR-Impl.h:808
void Clear()
Clears the matrix and make it zero-dimensional.
Definition: MatrixCSR-Impl.h:142
const T & NonZero(size_t i) const
Returns i-th non-zero element.
Definition: MatrixCSR-Impl.h:432
Matrix expression for CSR matrix-matrix multiplication.
Definition: MatrixCSR.h:60
const size_t * RowPointersData() const
Returns constant pointer of the row pointers data.
Definition: MatrixCSR-Impl.h:468
T AbsMax() const
Returns absolute maximum among all elements.
Definition: MatrixCSR-Impl.h:771
MatrixCSR & operator+=(const T &s)
Addition assignment with input scalar.
Definition: MatrixCSR-Impl.h:855
bool IsEqual(const MatrixCSR &other) const
Definition: MatrixCSR-Impl.h:328
MatrixCSRMatrixMul(const MatrixCSR< T > &m1, const ME &m2)
Definition: MatrixCSR-Impl.h:58
size_t Cols() const
Number of columns.
Definition: MatrixCSR-Impl.h:77
Base class for vector expression.
Definition: VectorExpression.h:28
std::vector< size_t > IndexContainerType
Definition: MatrixCSR.h:116
void IMul(const T &s)
Multiplies input scalar to this matrix.
Definition: MatrixCSR-Impl.h:674
size_t i
Definition: MatrixCSR.h:103
T Sum() const
Returns sum of all elements.
Definition: MatrixCSR-Impl.h:694
2-D point class.
Definition: Point2.h:25
const size_t * ColumnIndicesData() const
Returns constant pointer of the column indices data.
Definition: MatrixCSR-Impl.h:474
IndexIterator ColumnIndicesEnd()
Returns the end iterator of the column indices.
Definition: MatrixCSR-Impl.h:540
IndexContainerType::const_iterator ConstIndexIterator
Definition: MatrixCSR.h:118
T Min() const
Returns minimum among all elements.
Definition: MatrixCSR-Impl.h:717
T * NonZeroData()
Returns pointer of the non-zero elements data.
Definition: MatrixCSR-Impl.h:456
void IDiv(const T &s)
Divides this matrix with input scalar.
Definition: MatrixCSR-Impl.h:688
const size_t & RowPointer(size_t i) const
Returns i-th row pointer.
Definition: MatrixCSR-Impl.h:444
MatrixCSR Div(const T &s) const
Returns this matrix / input scalar.
Definition: MatrixCSR-Impl.h:602
bool IsSimilar(const MatrixCSR &other, double tol=std::numeric_limits< double >::epsilon()) const
Returns true if this matrix is similar to the input matrix within the given tolerance.
Definition: MatrixCSR-Impl.h:365
T operator[](size_t i) const
Returns vector element at i.
Definition: MatrixCSR-Impl.h:38
Size2 size() const
Returns the size of this matrix.
Definition: MatrixCSR-Impl.h:408
void Set(const T &s)
Sets whole matrix with input scalar.
Definition: MatrixCSR-Impl.h:152
MatrixCSR Add(const T &s) const
Returns this matrix + input scalar.
Definition: MatrixCSR-Impl.h:552
MatrixCSR Mul(const T &s) const
Returns this matrix * input scalar.
Definition: MatrixCSR-Impl.h:580
void AddElement(size_t i, size_t j, const T &value)
Adds non-zero element to (i, j).
Definition: MatrixCSR-Impl.h:243
void SetElement(size_t i, size_t j, const T &value)
Sets non-zero element to (i, j).
Definition: MatrixCSR-Impl.h:307
IndexContainerType::iterator IndexIterator
Definition: MatrixCSR.h:117
size_t Rows() const
Returns number of rows of this matrix.
Definition: MatrixCSR-Impl.h:414
Definition: pybind11Utils.h:24
bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition: MatrixCSR-Impl.h:402
Size2 size() const
Size of the matrix.
Definition: MatrixCSR-Impl.h:65
IndexIterator ColumnIndicesBegin()
Returns the begin iterator of the column indices.
Definition: MatrixCSR-Impl.h:528
size_t NumberOfNonZeros() const
Returns the number of non-zero elements.
Definition: MatrixCSR-Impl.h:426
MatrixCSR Sub(const T &s) const
Returns this matrix - input scalar.
Definition: MatrixCSR-Impl.h:566
MatrixCSRVectorMul(const MatrixCSR< T > &m, const VE &v)
Definition: MatrixCSR-Impl.h:20
NonZeroIterator NonZeroEnd()
Returns the end iterator of the non-zero elements.
Definition: MatrixCSR-Impl.h:492
void Compress(const std::initializer_list< std::initializer_list< T >> &list, T epsilon=std::numeric_limits< T >::epsilon())
Compresses given initializer list list into a sparse matrix.
Definition: MatrixCSR-Impl.h:176
IndexIterator RowPointersEnd()
Returns the end iterator of the row pointers.
Definition: MatrixCSR-Impl.h:516
MatrixCSR RMul(const T &s) const
Returns input scalar * this matrix.
Definition: MatrixCSR-Impl.h:636
typename NonZeroContainerType::iterator NonZeroIterator
Definition: MatrixCSR.h:113
MatrixCSR & operator*=(const T &s)
Multiplication assignment with input scalar.
Definition: MatrixCSR-Impl.h:883
static MatrixCSR< T > MakeIdentity(size_t m)
Makes a m x m matrix with all diagonal elements to 1, and other elements to 0.
Definition: MatrixCSR-Impl.h:930
Element()
Definition: MatrixCSR-Impl.h:99
Compressed Sparse Row (CSR) matrix class.
Definition: MatrixCSR.h:19
typename NonZeroContainerType::const_iterator ConstNonZeroIterator
Definition: MatrixCSR.h:114
MatrixCSR RAdd(const T &s) const
Returns input scalar + this matrix.
Definition: MatrixCSR-Impl.h:610
const MatrixCSRMatrixMul< T, ME > & operator()() const
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.h:34
Definition: MatrixCSR.h:101
void Reserve(size_t rows, size_t cols, size_t numNonZeros)
Reserves memory space of this matrix.
Definition: MatrixCSR-Impl.h:167
void IAdd(const T &s)
Adds input scalar to this matrix.
Definition: MatrixCSR-Impl.h:650
T Max() const
Returns maximum among all elements.
Definition: MatrixCSR-Impl.h:736
Base class for matrix expression.
Definition: MatrixExpression.h:27
size_t Rows() const
Number of rows.
Definition: MatrixCSR-Impl.h:71
size_t Cols() const
Returns number of columns of this matrix.
Definition: MatrixCSR-Impl.h:420
MatrixCSR & operator=(const E &m)
Compresses input (dense) matrix expression into a sparse matrix.
MatrixCSR & operator/=(const T &s)
Division assignment with input scalar.
Definition: MatrixCSR-Impl.h:898
MatrixCSR RDiv(const T &s) const
Returns input matrix / this scalar.
Definition: MatrixCSR-Impl.h:642
const size_t & ColumnIndex(size_t i) const
Returns i-th column index.
Definition: MatrixCSR-Impl.h:450
MatrixCSR & operator-=(const T &s)
Subtraction assignment with input scalar.
Definition: MatrixCSR-Impl.h:869
size_t size() const
Size of the vector.
Definition: MatrixCSR-Impl.h:32
MatrixCSR()
Constructs an empty matrix.
Definition: MatrixCSR-Impl.h:111
MatrixCSR RSub(const T &s) const
Returns input scalar - this matrix.
Definition: MatrixCSR-Impl.h:622
T value
Definition: MatrixCSR.h:105
T Avg() const
Returns average of all elements.
Definition: MatrixCSR-Impl.h:711
bool operator==(const MatrixCSR &m) const
Returns true if is equal to m.
Definition: MatrixCSR-Impl.h:918
NonZeroIterator NonZeroBegin()
Returns the begin iterator of the non-zero elements.
Definition: MatrixCSR-Impl.h:480
T AbsMin() const
Returns absolute minimum among all elements.
Definition: MatrixCSR-Impl.h:755
void AddRow(const NonZeroContainerType &nonZeros, const IndexContainerType &columnIndices)
Definition: MatrixCSR-Impl.h:278
IndexIterator RowPointersBegin()
Returns the begin iterator of the row pointers.
Definition: MatrixCSR-Impl.h:504
std::vector< double > NonZeroContainerType
Definition: MatrixCSR.h:112
size_t j
Definition: MatrixCSR.h:104