Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
matrix.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2023 W. Liu *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
48 #ifndef MUI_SPARSE_MATRIX_H_
49 #define MUI_SPARSE_MATRIX_H_
50 
51 #include <map>
52 #include <vector>
53 #include <cassert>
54 #include "linalg_util.h"
55 
56 namespace mui {
57 namespace linalg {
58 
59 // Class of sparse matrix
60 template<typename ITYPE, typename VTYPE>
62 
63  public:
64 
65  // *****************************************
66  // ****** Constructors & Destructor ********
67  // *****************************************
68 
69  // Constructor - takes in size of row and column to generate an matrix, with default arguments of format vectors.
70  sparse_matrix<ITYPE,VTYPE>(ITYPE, ITYPE, const std::string & = "CSR", const std::vector<VTYPE> & = {}, const std::vector<ITYPE> & = {}, const std::vector<ITYPE> & = {});
71  // Constructor - null matrix
72  sparse_matrix<ITYPE,VTYPE>(const std::string & = "CSR");
73  // Constructor - takes in another sparse_matrix object as an argument
75  // Constructor - takes in a std::vector with row major dense matrix format as an argument
76  sparse_matrix<ITYPE,VTYPE>(const std::vector<std::vector<VTYPE>> &, const std::string & = "CSR");
77  // Constructor - generate various square matrices
78  sparse_matrix<ITYPE,VTYPE>(ITYPE, const std::string & = {}, const std::string & = "CSR");
79  // Destructor
81 
82  // *****************************************
83  // ********** Matrix I/O & info ************
84  // *****************************************
85 
86  // Member function to print matrix elements to the console
87  void print() const;
88  // Member function to print matrix vectors to the console
89  void print_vectors() const;
90  // Member function to write matrix vectors to the file
91  void write_vectors_to_file(const std::string &, const std::string & = {}, const std::string & = {}, const std::string & = {}) const;
92  // Member function to read matrix vectors from the file
93  void read_vectors_from_file(const std::string &, const std::string & = {}, const std::string & = {}, const std::string & = {});
94  // Member function to get the value at a given position
95  VTYPE get_value(ITYPE, ITYPE) const;
96  // Member function to get the number of rows
97  ITYPE get_rows() const;
98  // Member function to get the number of cols
99  ITYPE get_cols() const;
100  // Member function to get non-zero elements
101  std::vector<std::pair<ITYPE, ITYPE>> get_non_zero_elements() const;
102  // Member function to get number of non-zero elements
103  ITYPE non_zero_elements_count() const;
104  // Member function to check whether the matrix contains all zero elements
105  bool empty() const;
106  // Member function to get the format of the matrix
107  std::string get_format() const;
108  // Member function to check if the sparse matrix is sorted and deduplicated
109  bool is_sorted_unique(const std::string & = {}, const std::string & = {}) const;
110 
111  // *****************************************
112  // ********* Matrix manipulations **********
113  // *****************************************
114 
115  // Member function to resize an all-zero or null matrix
116  void resize(ITYPE, ITYPE);
117  // Member function to copy a sparse_matrix
118  void copy(const sparse_matrix<ITYPE,VTYPE> &);
119  // Member function to get a segment of a sparse_matrix
120  sparse_matrix<ITYPE,VTYPE> segment(ITYPE, ITYPE, ITYPE, ITYPE, bool = true);
121  // Member function to insert an element
122  void set_value(ITYPE, ITYPE, VTYPE, bool = true);
123  // Member function to insert the same value to all elements
124  void set_value(VTYPE);
125  // Member function to swap two elements in a sparse matrix
126  void swap_elements(ITYPE, ITYPE, ITYPE, ITYPE);
127  // Member function to set all elements to zero and empty the sparse matrix
128  void set_zero();
129  // Member function to add scalar to a specific elements
130  void add_scalar(ITYPE, ITYPE, VTYPE, bool = true);
131  // Member function to subtract a scalar from a specific elements
132  void subtract_scalar(ITYPE, ITYPE, VTYPE, bool = true);
133  // Member function to multiply a scalar from a specific elements
134  void multiply_scalar(ITYPE, ITYPE, VTYPE, bool = true);
135  // Overloaded assignment operator
137  // Member function to convert the format of the sparse matrix
138  void format_conversion(const std::string & = "COO", bool = true, bool = false, const std::string & = "overwrite");
139  // Member function to sort the entries for the sparse matrix
140  void sort_deduplication(bool = true, bool = true, const std::string & = "overwrite", bool = true);
141 
142  // *****************************************
143  // ********* Arithmetic operations *********
144  // *****************************************
145 
146  // Overload addition operator to perform sparse matrix addition
148  // Overload subtraction operator to perform sparse matrix subtraction
150  // Overload multiplication operator to perform sparse matrix multiplication
152  // Overload multiplication operator to perform scalar multiplication
153  template <typename STYPE>
154  sparse_matrix<ITYPE,VTYPE> operator*(const STYPE &) const;
155  // Member function of dot product
157  // Member function of Hadamard product
159  // Member function to get transpose of matrix
160  sparse_matrix<ITYPE,VTYPE> transpose(bool = true) const;
161  // Member function to perform LU decomposition
163  // Member function to perform QR decomposition
165  // Member function to get the inverse of matrix
167 
168  protected:
169 
170  // *****************************************
171  // ****** Constructors & Destructor ********
172  // *****************************************
173 
174  // Protected member function to set matrix format - helper function on matrix constructors
175  void set_matrix_format(const std::string & = "CSR");
176 
177  // *****************************************
178  // ********** Matrix I/O & info ************
179  // *****************************************
180 
181  // Protected member function to check if the COO matrix is sorted and deduplicated
182  bool is_coo_sorted_unique(const std::string & = {}, const std::string & = {}) const;
183  // Protected member function to check if the CSR matrix is sorted and deduplicated
184  bool is_csr_sorted_unique(const std::string & = {}, const std::string & = {}) const;
185  // Protected member function to check if the CSC matrix is sorted and deduplicated
186  bool is_csc_sorted_unique(const std::string & = {}, const std::string & = {}) const;
187 
188  // *****************************************
189  // ********* Matrix manipulations **********
190  // *****************************************
191 
192  // Protected member function to sort the entries by row and column for sparse matrix with COO format
193  void sort_coo(bool = true, bool = false, const std::string & = "overwrite");
194  // Protected member function to sort the entries for sparse matrix with CSR format
195  void sort_csr(bool = false, const std::string & = "overwrite");
196  // Protected member function to sort the entries for sparse matrix with CSC format
197  void sort_csc(bool = false, const std::string & = "overwrite");
198  // Protected member function for element operation of COO matrix
199  void coo_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string & = {}, const std::string & = {});
200  // Protected member function for element operation of CSR matrix
201  void csr_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string & = {}, const std::string & = {});
202  // Protected member function for element operation of CSC matrix
203  void csc_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string & = {}, const std::string & = {});
204  // Protected member function to convert COO matrix into CSR matrix
205  void coo_to_csr();
206  // Protected member function to convert COO matrix into CSC matrix
207  void coo_to_csc();
208  // Protected member function to convert CSR matrix into COO matrix
209  void csr_to_coo();
210  // Protected member function to convert CSR matrix into CSC matrix
211  void csr_to_csc();
212  // Protected member function to convert CSC matrix into COO matrix
213  void csc_to_coo();
214  // Protected member function to convert CSC matrix into CSR matrix
215  void csc_to_csr();
216  // Protected member function to clear all vectors of the sparse matrix
217  void clear_vectors();
218 
219  // *****************************************
220  // ********* Arithmetic operations *********
221  // *****************************************
222 
223  // Protected member function to reinterpret the row and column indexes for sparse matrix with COO format - helper function on matrix transpose
224  void index_reinterpretation();
225  // Protected member function to reinterpret the format of sparse matrix between CSR format and CSC format - helper function on matrix transpose
227 
228  // *****************************************
229  // **************** Asserts ****************
230  // *****************************************
231 
232  // Member function to assert the matrix vector sizes
233  void assert_valid_vector_size(const std::string & = {}, const std::string & = {}) const;
234  // Member function to assert if the COO matrix is sorted and deduplicated
235  void assert_coo_sorted_unique(const std::string & = {}, const std::string & = {}) const;
236  // Member function to assert if the CSR matrix is sorted and deduplicated
237  void assert_csr_sorted_unique(const std::string & = {}, const std::string & = {}) const;
238  // Member function to assert if the CSC matrix is sorted and deduplicated
239  void assert_csc_sorted_unique(const std::string & = {}, const std::string & = {}) const;
240 
241  private:
242 
243  // *****************************************
244  // ***** Data structure infrastructure *****
245  // *****************************************
246 
247  // Format of sparse matrix
248  enum class format {
249  COO,
250  CSR,
251  CSC
252  };
253 
254  // COO format data struct
255  struct m_coo {
256  // Values of non-zero elements of sparse matrix
257  std::vector<VTYPE> values_;
258  // Row index of each element in the values_ vector
259  std::vector<ITYPE> row_indices_;
260  // Column index of each element in the values_ vector
261  std::vector<ITYPE> col_indices_;
262  };
263 
264  // CSR format data struct
265  struct m_csr {
266  // Values of non-zero elements of sparse matrix
267  std::vector<VTYPE> values_;
268  // Row pointers of each element in the values_ vector
269  std::vector<ITYPE> row_ptrs_;
270  // Column index of each element in the values_ vector
271  std::vector<ITYPE> col_indices_;
272  };
273 
274  // CSC format data struct
275  struct m_csc {
276  // Values of non-zero elements of sparse matrix
277  std::vector<VTYPE> values_;
278  // Row index of each element in the values_ vector
279  std::vector<ITYPE> row_indices_;
280  // Column pointers of each element in the values_ vector
281  std::vector<ITYPE> col_ptrs_;
282  };
283 
284  // *****************************************
285  // ******* Sparse matrix attributes ********
286  // *****************************************
287 
288  // Number of rows of sparse matrix
289  ITYPE rows_ = 0;
290  // Number of columns of sparse matrix
291  ITYPE cols_ = 0;
292  // Number of non-zero elements of sparse matrix
293  ITYPE nnz_ = 0;
294  // Format indicator with default value of format::CSR
295  format matrix_format_ = format::CSR;
296  // COO format data
297  m_coo matrix_coo;
298  // CSR format data
299  m_csr matrix_csr;
300  // CSC format data
301  m_csc matrix_csc;
302 
303  // Dummy member variable for invalid or unassigned elements in sparse matrix
304  VTYPE dummy_ = 0;
305  // Sparse matrix debug switch
306  bool DEBUG = false;
307 
308 };
309 
310 } // linalg
311 } // mui
312 
313 // Include implementations
314 #include "../linear_algebra/matrix_ctor_dtor.h"
315 #include "../linear_algebra/matrix_io_info.h"
316 #include "../linear_algebra/matrix_manipulation.h"
317 #include "../linear_algebra/matrix_arithmetic.h"
318 #include "../linear_algebra/matrix_asserts.h"
319 
320 #endif /* MUI_SPARSE_MATRIX_H_ */
Definition: matrix.h:61
void clear_vectors()
Definition: matrix_manipulation.h:2002
void index_reinterpretation()
Definition: matrix_arithmetic.h:1202
ITYPE get_rows() const
Definition: matrix_io_info.h:579
void set_matrix_format(const std::string &="CSR")
Definition: matrix_ctor_dtor.h:343
void sort_deduplication(bool=true, bool=true, const std::string &="overwrite", bool=true)
Definition: matrix_manipulation.h:764
bool is_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_io_info.h:701
void csr_to_csc()
Definition: matrix_manipulation.h:1852
sparse_matrix< ITYPE, VTYPE > operator*(sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_arithmetic.h:521
void csc_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string &={}, const std::string &={})
Definition: matrix_manipulation.h:1585
VTYPE dot_product(sparse_matrix< ITYPE, VTYPE > &) const
Definition: matrix_arithmetic.h:759
void print() const
Definition: matrix_io_info.h:66
bool is_csc_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_io_info.h:837
void coo_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string &={}, const std::string &={})
Definition: matrix_manipulation.h:1299
void sort_csr(bool=false, const std::string &="overwrite")
Definition: matrix_manipulation.h:1037
void set_zero()
Definition: matrix_manipulation.h:418
sparse_matrix< ITYPE, VTYPE > transpose(bool=true) const
Definition: matrix_arithmetic.h:943
ITYPE non_zero_elements_count() const
Definition: matrix_io_info.h:635
void print_vectors() const
Definition: matrix_io_info.h:78
bool is_csr_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_io_info.h:787
void qr_decomposition(sparse_matrix< ITYPE, VTYPE > &, sparse_matrix< ITYPE, VTYPE > &) const
Definition: matrix_arithmetic.h:1043
void set_value(ITYPE, ITYPE, VTYPE, bool=true)
Definition: matrix_manipulation.h:292
bool is_coo_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_io_info.h:736
sparse_matrix< ITYPE, VTYPE > operator+(sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_arithmetic.h:62
VTYPE get_value(ITYPE, ITYPE) const
Definition: matrix_io_info.h:523
sparse_matrix< ITYPE, VTYPE > & operator=(const sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_manipulation.h:587
sparse_matrix< ITYPE, VTYPE > segment(ITYPE, ITYPE, ITYPE, ITYPE, bool=true)
Definition: matrix_manipulation.h:153
void write_vectors_to_file(const std::string &, const std::string &={}, const std::string &={}, const std::string &={}) const
Definition: matrix_io_info.h:154
void assert_csc_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:318
void csc_to_coo()
Definition: matrix_manipulation.h:1912
std::string get_format() const
Definition: matrix_io_info.h:681
void coo_to_csc()
Definition: matrix_manipulation.h:1772
void assert_valid_vector_size(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:61
sparse_matrix< ITYPE, VTYPE > hadamard_product(sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_arithmetic.h:772
void read_vectors_from_file(const std::string &, const std::string &={}, const std::string &={}, const std::string &={})
Definition: matrix_io_info.h:252
void resize(ITYPE, ITYPE)
Definition: matrix_manipulation.h:62
void assert_csr_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:278
void add_scalar(ITYPE, ITYPE, VTYPE, bool=true)
Definition: matrix_manipulation.h:447
ITYPE get_cols() const
Definition: matrix_io_info.h:585
sparse_matrix< ITYPE, VTYPE > inverse() const
Definition: matrix_arithmetic.h:1134
void swap_elements(ITYPE, ITYPE, ITYPE, ITYPE)
Definition: matrix_manipulation.h:407
void multiply_scalar(ITYPE, ITYPE, VTYPE, bool=true)
Definition: matrix_manipulation.h:540
void csr_element_operation(ITYPE, ITYPE, VTYPE, const std::string &, const std::string &={}, const std::string &={})
Definition: matrix_manipulation.h:1451
void copy(const sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_manipulation.h:79
void coo_to_csr()
Definition: matrix_manipulation.h:1720
void sort_coo(bool=true, bool=false, const std::string &="overwrite")
Definition: matrix_manipulation.h:808
void format_conversion(const std::string &="COO", bool=true, bool=false, const std::string &="overwrite")
Definition: matrix_manipulation.h:639
void csc_to_csr()
Definition: matrix_manipulation.h:1946
void format_reinterpretation()
Definition: matrix_arithmetic.h:1215
sparse_matrix< ITYPE, VTYPE > operator-(sparse_matrix< ITYPE, VTYPE > &)
Definition: matrix_arithmetic.h:289
bool empty() const
Definition: matrix_io_info.h:665
void assert_coo_sorted_unique(const std::string &={}, const std::string &={}) const
Definition: matrix_asserts.h:237
void csr_to_coo()
Definition: matrix_manipulation.h:1818
std::vector< std::pair< ITYPE, ITYPE > > get_non_zero_elements() const
Definition: matrix_io_info.h:591
void lu_decomposition(sparse_matrix< ITYPE, VTYPE > &, sparse_matrix< ITYPE, VTYPE > &) const
Definition: matrix_arithmetic.h:984
void subtract_scalar(ITYPE, ITYPE, VTYPE, bool=true)
Definition: matrix_manipulation.h:494
void sort_csc(bool=false, const std::string &="overwrite")
Definition: matrix_manipulation.h:1168
Utility functions for mui::linalg.
Definition: comm.h:54