SMG2S
Sparse Matrix Generator with Given Spectrum
MatrixCSR.hpp
1 /*
2 MIT License
3 Copyright (c) 2019 Xinzhe WU @ Maison de la Simulation, France
4 Copyright (c) 2019-2022, Xinzhe Wu @ Simulation and Data Laboratory Quantum
5  Materials, Forschungszentrum Juelich GmbH.
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22 */
23 
24 #ifndef __MATRIXCSR_H__
25 #define __MATRIXCSR_H__
26 
27 #include <vector>
28 
33 
45 template<typename T, typename S>
46 struct MatrixCSR
47 {
49 
52  S nrows;
54 
57  S nnz;
59 
64  S ncols;
65 
67 
72  std::vector<S> rows;
74 
79  std::vector<S> cols;
81 
86  std::vector<T> vals;
87 
88  MatrixCSR(){};
89 
91 
95  MatrixCSR(S nnz_in, S nrows_in)
96  {
97 
98  nnz = nnz_in;
99  ncols = nrows_in;
100  nrows = nrows_in;
101 
102  rows.reserve(nrows_in+1);
103  cols.reserve(nnz);
104  vals.reserve(nnz);
105 
106  };
108 
113  MatrixCSR(std::vector<S> rowoffs, std::vector<S> colidxs, std::vector<T> values)
114  {
115 
116  nnz = colidxs.size();
117  ncols = rowoffs.size() - 1;
118  nrows = rowoffs.size() - 1;
119 
120  rows = rowoffs;
121  cols = colidxs;
122  vals = values;
123  };
124 
127  {
128  if(nnz != 0){
129  rows.empty();
130  cols.empty();
131  vals.empty();
132  }
133  };
134 
136 
140  T GetValue(S row, S col)
141  {
142  T val;
143  S currCol;
144 
145  for(S pos = rows[row - 1] - 1; pos < rows[row] - 1; ++pos){
146  currCol = cols[pos];
147  if (currCol == col){
148  val = vals[pos];
149  } else{
150  val = 0.0;
151  }
152  }
153  return val;
154  };
155 
156 
158 
163  void SetValue(S row, S col, T val)
164  {
165  S currCol;
166 
167  for(S pos = rows[row - 1] - 1; pos < rows[row] - 1; ++pos){
168  currCol = cols[pos];
169  if (currCol == col){
170  vals[pos] = val;
171  }
172  }
173  };
174 
176 
182  void InsertValue(S row, S col, T val)
183  {
184  S currCol, prevCol;
185  typename std::vector<S>::iterator it=cols.begin();
186  typename std::vector<T>::iterator itv=vals.begin();
187 
188  for(S pos = rows[row - 1] - 1; pos < rows[row] - 1; ++pos){
189  currCol = cols[pos];
190  prevCol = cols[pos-1];
191  if (currCol == col){
192  SetValue(row, col, val);
193  } else if((currCol > col) && (prevCol < col)){
194  std::advance(it,currCol);
195  std::advance(itv,currCol);
196  cols.insert(it,col);
197  vals.insert(itv, val);
198  nnz++;
199  return;
200  }
201  }
202  };
203 
205 
208  void show(){
209  std::cout << "MatrixCSR display:" << std::endl;
210  for(auto i = 0; i < rows.size() - 1; i++){
211  for(auto j = rows[i]; j < rows[i+1]; j++){
212  std::cout << "row " << i << ": (" << cols[j] << "," << vals[j]<< ")";
213  }
214  std::cout << std::endl;
215  }
216  }
217 
218 }; // end of group6
220 #endif
MatrixCSR::vals
std::vector< T > vals
An array stores the values of non-zero elements.
Definition: MatrixCSR.hpp:86
MatrixCSR::ncols
S ncols
dimension of the array MatrixCSR::cols
Definition: MatrixCSR.hpp:64
MatrixCSR::MatrixCSR
MatrixCSR(std::vector< S > rowoffs, std::vector< S > colidxs, std::vector< T > values)
A constructor of `MatrixCSR`. The three arrays MatrixCSR::rows, MatrixCSR::cols and MatrixCSR::vals a...
Definition: MatrixCSR.hpp:113
MatrixCSR::rows
std::vector< S > rows
An array points to row starts in indices and data.
Definition: MatrixCSR.hpp:72
MatrixCSR::~MatrixCSR
~MatrixCSR()
This a destructor of MatrixCSR.
Definition: MatrixCSR.hpp:126
MatrixCSR::InsertValue
void InsertValue(S row, S col, T val)
Insert a value `val` in the position `(row, col)` in sparse matrix.
Definition: MatrixCSR.hpp:182
MatrixCSR
A struct which stores a sparse matrix in CSR format.
Definition: MatrixCSR.hpp:46
MatrixCSR::nnz
S nnz
number of non-zero entries of a sparse matrix
Definition: MatrixCSR.hpp:57
MatrixCSR::MatrixCSR
MatrixCSR(S nnz_in, S nrows_in)
A constructor of `MatrixCSR`. The three arrays MatrixCSR::rows, MatrixCSR::cols and MatrixCSR::vals a...
Definition: MatrixCSR.hpp:95
MatrixCSR::show
void show()
Print the sparse matrix in COO format.
Definition: MatrixCSR.hpp:208
MatrixCSR::GetValue
T GetValue(S row, S col)
Get the value of position `(row, col)` from sparse matrix.
Definition: MatrixCSR.hpp:140
MatrixCSR::cols
std::vector< S > cols
An array stores the column indices of non-zero elements.
Definition: MatrixCSR.hpp:79
MatrixCSR::nrows
S nrows
number of rows of a sparse matrix
Definition: MatrixCSR.hpp:52
MatrixCSR::SetValue
void SetValue(S row, S col, T val)
Overwrite the value of entry of position `(row, col)` in sparse matrix by `val`.
Definition: MatrixCSR.hpp:163