MatrixCSR.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_MATRIX_CSR_HPP
12 #define CUBBYFLOW_MATRIX_CSR_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 
16 namespace CubbyFlow
17 {
18 template <typename T>
19 class MatrixCSR;
20 
31 template <typename T, typename ME>
33  : public MatrixExpression<T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC,
34  MatrixCSRMatrixMul<T, ME>>
35 {
36  public:
37  MatrixCSRMatrixMul(const MatrixCSR<T>& m1, const ME& m2);
38 
40  [[nodiscard]] size_t GetRows() const;
41 
43  [[nodiscard]] size_t GetCols() const;
44 
46  T operator()(size_t i, size_t j) const;
47 
48  private:
49  const MatrixCSR<T>& m_m1;
50  const ME& m_m2;
51  const T* const m_nnz;
52  const size_t* const m_rp;
53  const size_t* const m_ci;
54 };
55 
66 template <typename T>
67 class MatrixCSR final
68  : public MatrixExpression<T, MATRIX_SIZE_DYNAMIC, MATRIX_SIZE_DYNAMIC,
69  MatrixCSR<T>>
70 {
71  public:
72  static_assert(
73  std::is_floating_point<T>::value,
74  "MatrixCSR only can be instantiated with floating point types");
75 
76  struct Element
77  {
78  size_t i;
79  size_t j;
80  T value;
81 
82  Element();
83 
84  Element(size_t i, size_t j, const T& value);
85  };
86 
87  using NonZeroContainerType = std::vector<T>;
88  using NonZeroIterator = typename NonZeroContainerType::iterator;
89  using ConstNonZeroIterator = typename NonZeroContainerType::const_iterator;
90 
91  using IndexContainerType = std::vector<size_t>;
92  using IndexIterator = IndexContainerType::iterator;
93  using ConstIndexIterator = IndexContainerType::const_iterator;
94 
96  MatrixCSR();
97 
118  MatrixCSR(const std::initializer_list<std::initializer_list<T>>& lst,
119  T epsilon = std::numeric_limits<T>::epsilon());
120 
128  template <size_t R, size_t C, typename E>
130  T epsilon = std::numeric_limits<T>::epsilon());
131 
133  ~MatrixCSR() = default;
134 
136  MatrixCSR(const MatrixCSR& other);
137 
139  MatrixCSR(MatrixCSR&& other) noexcept;
140 
142  MatrixCSR& operator=(const MatrixCSR& other);
143 
145  MatrixCSR& operator=(MatrixCSR&& other) noexcept;
146 
148  void Clear();
149 
151  void Set(const T& s);
152 
154  void Set(const MatrixCSR& other);
155 
157  void Reserve(size_t rows, size_t cols, size_t numNonZeros);
158 
180  void Compress(const std::initializer_list<std::initializer_list<T>>& lst,
181  T epsilon = std::numeric_limits<T>::epsilon());
182 
190  template <size_t R, size_t C, typename E>
191  void Compress(const MatrixExpression<T, R, C, E>& other,
192  T epsilon = std::numeric_limits<T>::epsilon());
193 
195  void AddElement(size_t i, size_t j, const T& value);
196 
198  void AddElement(const Element& element);
199 
206  void AddRow(const NonZeroContainerType& nonZeros,
207  const IndexContainerType& columnIndices);
208 
210  void SetElement(size_t i, size_t j, const T& value);
211 
213  void SetElement(const Element& element);
214 
215  [[nodiscard]] bool IsEqual(const MatrixCSR& other) const;
216 
219  [[nodiscard]] bool IsSimilar(
220  const MatrixCSR& other,
221  double tol = std::numeric_limits<double>::epsilon()) const;
222 
224  [[nodiscard]] bool IsSquare() const;
225 
227  [[nodiscard]] Vector2UZ Size() const;
228 
230  [[nodiscard]] size_t GetRows() const;
231 
233  [[nodiscard]] size_t GetCols() const;
234 
236  [[nodiscard]] size_t NumberOfNonZeros() const;
237 
239  [[nodiscard]] const T& NonZero(size_t i) const;
240 
242  [[nodiscard]] T& NonZero(size_t i);
243 
245  [[nodiscard]] const size_t& RowPointer(size_t i) const;
246 
248  [[nodiscard]] const size_t& ColumnIndex(size_t i) const;
249 
251  [[nodiscard]] T* NonZeroData();
252 
254  [[nodiscard]] const T* NonZeroData() const;
255 
257  [[nodiscard]] const size_t* RowPointersData() const;
258 
260  [[nodiscard]] const size_t* ColumnIndicesData() const;
261 
263  [[nodiscard]] NonZeroIterator NonZeroBegin();
264 
266  [[nodiscard]] ConstNonZeroIterator NonZeroBegin() const;
267 
269  [[nodiscard]] NonZeroIterator NonZeroEnd();
270 
272  [[nodiscard]] ConstNonZeroIterator NonZeroEnd() const;
273 
275  [[nodiscard]] IndexIterator RowPointersBegin();
276 
278  [[nodiscard]] ConstIndexIterator RowPointersBegin() const;
279 
281  [[nodiscard]] IndexIterator RowPointersEnd();
282 
284  [[nodiscard]] ConstIndexIterator RowPointersEnd() const;
285 
287  [[nodiscard]] IndexIterator ColumnIndicesBegin();
288 
290  [[nodiscard]] ConstIndexIterator ColumnIndicesBegin() const;
291 
293  [[nodiscard]] IndexIterator ColumnIndicesEnd();
294 
296  [[nodiscard]] ConstIndexIterator ColumnIndicesEnd() const;
297 
299  [[nodiscard]] MatrixCSR Add(const T& s) const;
300 
302  [[nodiscard]] MatrixCSR Add(const MatrixCSR& m) const;
303 
305  [[nodiscard]] MatrixCSR Sub(const T& s) const;
306 
308  [[nodiscard]] MatrixCSR Sub(const MatrixCSR& m) const;
309 
311  [[nodiscard]] MatrixCSR Mul(const T& s) const;
312 
314  template <size_t R, size_t C, typename ME>
315  [[nodiscard]] MatrixCSRMatrixMul<T, ME> Mul(
316  const MatrixExpression<T, R, C, ME>& m) const;
317 
319  [[nodiscard]] MatrixCSR Div(const T& s) const;
320 
322  [[nodiscard]] MatrixCSR RAdd(const T& s) const;
323 
325  [[nodiscard]] MatrixCSR RAdd(const MatrixCSR& m) const;
326 
328  [[nodiscard]] MatrixCSR RSub(const T& s) const;
329 
331  [[nodiscard]] MatrixCSR RSub(const MatrixCSR& m) const;
332 
334  [[nodiscard]] MatrixCSR RMul(const T& s) const;
335 
337  [[nodiscard]] MatrixCSR RDiv(const T& s) const;
338 
340  void IAdd(const T& s);
341 
343  void IAdd(const MatrixCSR& m);
344 
346  void ISub(const T& s);
347 
349  void ISub(const MatrixCSR& m);
350 
352  void IMul(const T& s);
353 
355  template <size_t R, size_t C, typename ME>
356  void IMul(const MatrixExpression<T, R, C, ME>& m);
357 
359  void IDiv(const T& s);
360 
362  [[nodiscard]] T Sum() const;
363 
365  [[nodiscard]] T Avg() const;
366 
368  [[nodiscard]] T Min() const;
369 
371  [[nodiscard]] T Max() const;
372 
374  [[nodiscard]] T AbsMin() const;
375 
377  [[nodiscard]] T AbsMax() const;
378 
381  [[nodiscard]] T Trace() const;
382 
384  template <typename U>
385  MatrixCSR<U> CastTo() const;
386 
394  template <size_t R, size_t C, typename ME>
395  MatrixCSR& operator=(const MatrixExpression<T, R, C, ME>& m);
396 
398  MatrixCSR& operator+=(const T& s);
399 
401  MatrixCSR& operator+=(const MatrixCSR& m);
402 
404  MatrixCSR& operator-=(const T& s);
405 
407  MatrixCSR& operator-=(const MatrixCSR& m);
408 
410  MatrixCSR& operator*=(const T& s);
411 
413  template <size_t R, size_t C, typename ME>
415 
417  MatrixCSR& operator/=(const T& s);
418 
420  T operator()(size_t i, size_t j) const;
421 
423  bool operator==(const MatrixCSR& m) const;
424 
426  bool operator!=(const MatrixCSR& m) const;
427 
430  static MatrixCSR<T> MakeIdentity(size_t m);
431 
432  private:
433  [[nodiscard]] size_t HasElement(size_t i, size_t j) const;
434 
435  template <typename Op>
436  MatrixCSR BinaryOp(const MatrixCSR& m, Op op) const;
437 
438  Vector2UZ m_size;
439  NonZeroContainerType m_nonZeros;
440  IndexContainerType m_rowPointers;
441  IndexContainerType m_columnIndices;
442 };
443 
446 
449 } // namespace CubbyFlow
450 
452 
453 #endif
constexpr bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition: MatrixExpression-Impl.hpp:71
T operator()(size_t i, size_t j) const
Returns matrix element at (i, j).
Definition: MatrixCSR-Impl.hpp:45
Matrix expression for CSR matrix-matrix multiplication.
Definition: MatrixCSR.hpp:32
MatrixCSRMatrixMul(const MatrixCSR< T > &m1, const ME &m2)
Definition: MatrixCSR-Impl.hpp:21
constexpr std::enable_if_t< IsMatrixSizeStatic< Rows, Cols >), bool > operator==(const MatrixExpression< T, Rows, Cols, M1 > &a, const MatrixExpression< T, Rows, Cols, M2 > &b)
Definition: Matrix-Impl.hpp:1408
std::vector< size_t > IndexContainerType
Definition: MatrixCSR.hpp:91
size_t i
Definition: MatrixCSR.hpp:78
MatrixCSR< float > MatrixCSRF
Float-type CSR matrix.
Definition: MatrixCSR.hpp:445
IndexContainerType::const_iterator ConstIndexIterator
Definition: MatrixCSR.hpp:93
Definition: Matrix.hpp:27
IndexContainerType::iterator IndexIterator
Definition: MatrixCSR.hpp:92
bool IsSimilar(const MatrixExpression< T, R, C, E > &m, double tol=std::numeric_limits< double >::epsilon()) const
Definition: MatrixExpression-Impl.hpp:46
void operator+=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1342
Definition: pybind11Utils.hpp:20
void operator/=(Matrix< T, Rows, Cols > &a, const T &b)
Definition: Matrix-Impl.hpp:1400
typename NonZeroContainerType::iterator NonZeroIterator
Definition: MatrixCSR.hpp:88
Compressed Sparse Row (CSR) matrix class.
Definition: MatrixCSR.hpp:19
size_t GetCols() const
Number of columns.
Definition: MatrixCSR-Impl.hpp:39
MatrixCSR< double > MatrixCSRD
Double-type CSR matrix.
Definition: MatrixCSR.hpp:448
typename NonZeroContainerType::const_iterator ConstNonZeroIterator
Definition: MatrixCSR.hpp:89
Definition: MatrixCSR.hpp:76
void operator*=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1366
Base class for matrix expression.
Definition: MatrixExpression.hpp:93
MatrixUnaryOp< U, Rows, Cols, const MatrixCSRMatrixMul< T, ME > &, TypeCast< T, U > > CastTo() const
size_t GetRows() const
Number of rows.
Definition: MatrixCSR-Impl.hpp:33
bool operator!=(const MatrixExpression< T, R1, C1, M1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1443
void operator-=(Matrix< T, R1, C1 > &a, const MatrixExpression< T, R2, C2, M2 > &b)
Definition: Matrix-Impl.hpp:1354
T value
Definition: MatrixCSR.hpp:80
std::vector< double > NonZeroContainerType
Definition: MatrixCSR.hpp:87
size_t j
Definition: MatrixCSR.hpp:79