Loading...
Searching...
No Matches
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
15
16namespace CubbyFlow
17{
18template <typename T>
19class MatrixCSR;
20
31template <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
66template <typename T>
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;
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
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>
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
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
264
267
270
273
276
279
282
285
288
291
294
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>
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>
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>
396
398 MatrixCSR& operator+=(const T& s);
399
402
404 MatrixCSR& operator-=(const T& s);
405
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
Compressed Sparse Row (CSR) matrix class.
Definition MatrixCSR.hpp:70
MatrixCSR< U > CastTo() const
Type-casts to different value-typed matrix.
Definition MatrixCSR-Impl.hpp:828
bool IsEqual(const MatrixCSR &other) const
Definition MatrixCSR-Impl.hpp:323
typename NonZeroContainerType::const_iterator ConstNonZeroIterator
Definition MatrixCSR.hpp:89
MatrixCSR RSub(const T &s) const
Returns input scalar - this matrix.
Definition MatrixCSR-Impl.hpp:624
T AbsMax() const
Returns absolute maximum among all elements.
Definition MatrixCSR-Impl.hpp:789
IndexContainerType::iterator IndexIterator
Definition MatrixCSR.hpp:92
IndexContainerType::const_iterator ConstIndexIterator
Definition MatrixCSR.hpp:93
MatrixCSR Add(const T &s) const
Returns this matrix + input scalar.
Definition MatrixCSR-Impl.hpp:548
void AddElement(size_t i, size_t j, const T &value)
Adds non-zero element to (i, j).
Definition MatrixCSR-Impl.hpp:235
void IAdd(const T &s)
Adds input scalar to this matrix.
Definition MatrixCSR-Impl.hpp:658
void Set(const T &s)
Sets whole matrix with input scalar.
Definition MatrixCSR-Impl.hpp:143
IndexIterator RowPointersBegin()
Returns the begin iterator of the row pointers.
Definition MatrixCSR-Impl.hpp:499
size_t NumberOfNonZeros() const
Returns the number of non-zero elements.
Definition MatrixCSR-Impl.hpp:421
size_t GetRows() const
Returns number of rows of this matrix.
Definition MatrixCSR-Impl.hpp:409
const size_t & RowPointer(size_t i) const
Returns i-th row pointer.
Definition MatrixCSR-Impl.hpp:439
IndexIterator ColumnIndicesEnd()
Returns the end iterator of the column indices.
Definition MatrixCSR-Impl.hpp:536
const T & NonZero(size_t i) const
Returns i-th non-zero element.
Definition MatrixCSR-Impl.hpp:427
T Sum() const
Returns sum of all elements.
Definition MatrixCSR-Impl.hpp:707
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.
Definition MatrixCSR-Impl.hpp:167
std::vector< T > NonZeroContainerType
Definition MatrixCSR.hpp:87
MatrixCSR & operator=(const MatrixExpression< T, R, C, ME > &m)
Compresses input (dense) matrix expression into a sparse matrix.
void IDiv(const T &s)
Divides this matrix with input scalar.
Definition MatrixCSR-Impl.hpp:700
T operator()(size_t i, size_t j) const
Returns (i,j) element.
Definition MatrixCSR-Impl.hpp:915
MatrixCSR & operator/=(const T &s)
Division assignment with input scalar.
Definition MatrixCSR-Impl.hpp:907
bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition MatrixCSR-Impl.hpp:397
MatrixCSR & operator*=(const T &s)
Multiplication assignment with input scalar.
Definition MatrixCSR-Impl.hpp:890
const size_t * RowPointersData() const
Returns constant pointer of the row pointers data.
Definition MatrixCSR-Impl.hpp:463
T * NonZeroData()
Returns pointer of the non-zero elements data.
Definition MatrixCSR-Impl.hpp:451
bool operator==(const MatrixCSR &m) const
Returns true if is equal to m.
Definition MatrixCSR-Impl.hpp:928
NonZeroIterator NonZeroBegin()
Returns the begin iterator of the non-zero elements.
Definition MatrixCSR-Impl.hpp:475
const size_t & ColumnIndex(size_t i) const
Returns i-th column index.
Definition MatrixCSR-Impl.hpp:445
MatrixCSR(const MatrixExpression< T, R, C, E > &other, T epsilon=std::numeric_limits< T >::epsilon())
Compresses input (dense) matrix expression into a sparse matrix.
static MatrixCSR< T > MakeIdentity(size_t m)
Definition MatrixCSR-Impl.hpp:940
void AddRow(const NonZeroContainerType &nonZeros, const IndexContainerType &columnIndices)
Definition MatrixCSR-Impl.hpp:273
void Clear()
Clears the matrix and make it zero-dimensional.
Definition MatrixCSR-Impl.hpp:133
void IMul(const T &s)
Multiplies input scalar to this matrix.
Definition MatrixCSR-Impl.hpp:684
MatrixCSR()
Constructs an empty matrix.
Definition MatrixCSR-Impl.hpp:77
T Min() const
Returns minimum among all elements.
Definition MatrixCSR-Impl.hpp:731
bool operator!=(const MatrixCSR &m) const
Returns true if is not equal to m.
Definition MatrixCSR-Impl.hpp:934
bool IsSimilar(const MatrixCSR &other, double tol=std::numeric_limits< double >::epsilon()) const
Definition MatrixCSR-Impl.hpp:360
IndexIterator ColumnIndicesBegin()
Returns the begin iterator of the column indices.
Definition MatrixCSR-Impl.hpp:523
T AbsMin() const
Returns absolute minimum among all elements.
Definition MatrixCSR-Impl.hpp:771
Vector2UZ Size() const
Returns the size of this matrix.
Definition MatrixCSR-Impl.hpp:403
MatrixCSR Sub(const T &s) const
Returns this matrix - input scalar.
Definition MatrixCSR-Impl.hpp:565
T Avg() const
Returns average of all elements.
Definition MatrixCSR-Impl.hpp:725
void SetElement(size_t i, size_t j, const T &value)
Sets non-zero element to (i, j).
Definition MatrixCSR-Impl.hpp:303
MatrixCSR Div(const T &s) const
Returns this matrix / input scalar.
Definition MatrixCSR-Impl.hpp:601
size_t GetCols() const
Returns number of columns of this matrix.
Definition MatrixCSR-Impl.hpp:415
~MatrixCSR()=default
Default destructor.
std::vector< size_t > IndexContainerType
Definition MatrixCSR.hpp:91
MatrixCSR & operator*=(const MatrixExpression< T, R, C, ME > &m)
Multiplication assignment with input matrix.
T Max() const
Returns maximum among all elements.
Definition MatrixCSR-Impl.hpp:751
MatrixCSR RMul(const T &s) const
Returns input scalar * this matrix.
Definition MatrixCSR-Impl.hpp:641
T Trace() const
Definition MatrixCSR-Impl.hpp:807
MatrixCSR RAdd(const T &s) const
Returns input scalar + this matrix.
Definition MatrixCSR-Impl.hpp:612
const size_t * ColumnIndicesData() const
Returns constant pointer of the column indices data.
Definition MatrixCSR-Impl.hpp:469
void ISub(const T &s)
Subtracts input scalar from this matrix.
Definition MatrixCSR-Impl.hpp:671
IndexIterator RowPointersEnd()
Returns the end iterator of the row pointers.
Definition MatrixCSR-Impl.hpp:511
MatrixCSR RDiv(const T &s) const
Returns input matrix / this scalar.
Definition MatrixCSR-Impl.hpp:647
NonZeroIterator NonZeroEnd()
Returns the end iterator of the non-zero elements.
Definition MatrixCSR-Impl.hpp:487
MatrixCSR & operator=(const MatrixCSR &other)
Copies to this matrix.
Definition MatrixCSR-Impl.hpp:113
MatrixCSR & operator-=(const T &s)
Subtraction assignment with input scalar.
Definition MatrixCSR-Impl.hpp:874
void Reserve(size_t rows, size_t cols, size_t numNonZeros)
Reserves memory space of this matrix.
Definition MatrixCSR-Impl.hpp:158
typename NonZeroContainerType::iterator NonZeroIterator
Definition MatrixCSR.hpp:88
MatrixCSR & operator+=(const T &s)
Addition assignment with input scalar.
Definition MatrixCSR-Impl.hpp:858
MatrixCSR Mul(const T &s) const
Returns this matrix * input scalar.
Definition MatrixCSR-Impl.hpp:582
Matrix expression for CSR matrix-matrix multiplication.
Definition MatrixCSR.hpp:35
size_t GetCols() const
Number of columns.
Definition MatrixCSR-Impl.hpp:41
T operator()(size_t i, size_t j) const
Returns matrix element at (i, j).
Definition MatrixCSR-Impl.hpp:47
size_t GetRows() const
Number of rows.
Definition MatrixCSR-Impl.hpp:35
Base class for matrix expression.
Definition MatrixExpression.hpp:94
Definition Matrix.hpp:30
Definition pybind11Utils.hpp:21
MatrixCSR< double > MatrixCSRD
Double-type CSR matrix.
Definition MatrixCSR.hpp:448
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
MatrixCSR< float > MatrixCSRF
Float-type CSR matrix.
Definition MatrixCSR.hpp:445
Definition MatrixCSR.hpp:77
Element()
Definition MatrixCSR-Impl.hpp:64
T value
Definition MatrixCSR.hpp:80
size_t i
Definition MatrixCSR.hpp:78
size_t j
Definition MatrixCSR.hpp:79