IVSparse  v1.0
A sparse matrix compression library.
CSC_Methods.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
13 //* ------------------------------ Getters ------------------------------ *//
14 
15 // Gets the element stored at the given row and column
16 template <typename T, typename indexT, bool columnMajor>
17 T SparseMatrix<T, indexT, 1, columnMajor>::coeff(uint32_t row, uint32_t col) {
18  #ifdef IVSPARSE_DEBUG
19  // check if the row and column are out of bounds
20  assert((row < numRows && col < numCols) && "Row or Column out of bounds");
21  assert((row >= 0 && col >= 0) && "Row or Column out of bounds");
22  #endif
23 
24  return (*this)(row, col);
25 }
26 
27 // Check for Column Major
28 template <typename T, typename indexT, bool columnMajor>
30  return columnMajor;
31 }
32 
33 // Get the values of the matrix
34 template <typename T, typename indexT, bool columnMajor>
36  #ifdef IVSPARSE_DEBUG
37  // check if the vector is out of bounds
38  assert((vec < outerDim && vec >= 0) && "Vector index out of bounds");
39  #endif
40 
41  return vals;
42 }
43 
44 // Get the inner indices of the matrix
45 template <typename T, typename indexT, bool columnMajor>
47  #ifdef IVSPARSE_DEBUG
48  // check if the vector is out of bounds
49  assert((vec < outerDim && vec >= 0) && "Vector index out of bounds");
50  #endif
51 
52  return innerIdx;
53 }
54 
55 // Get the outer pointers of the matrix
56 template <typename T, typename indexT, bool columnMajor>
58  return outerPtr;
59 }
60 
61 // Get a IVSparse vector from the matrix
62 template <typename T, typename indexT, bool columnMajor>
65  #ifdef IVSPARSE_DEBUG
66  // check if the vector is out of bounds
67  assert((vec < outerDim && vec >= 0) && "Vector index out of bounds");
68  #endif
69 
70  // create a vector from the matrix and return it
72  return v;
73 }
74 
75 //* -------------------------- Utility Methods -------------------------- *//
76 
77 // write the matrix to file
78 template <typename T, typename indexT, bool columnMajor>
80  // open the file
81  FILE *fp = fopen(filename, "wb");
82 
83  #ifdef IVSPARSE_DEBUG
84  // check if the file was opened
85  assert(fp != NULL && "File could not be opened!");
86  #endif
87 
88  // write the metadata
89  fwrite(metadata, sizeof(uint32_t), NUM_META_DATA, fp);
90 
91  // write the values
92  fwrite(vals, sizeof(T), nnz, fp);
93 
94  // write the inner indices
95  fwrite(innerIdx, sizeof(indexT), nnz, fp);
96 
97  // write the outer pointers
98  fwrite(outerPtr, sizeof(indexT), outerDim + 1, fp);
99 
100  // close the file
101  fclose(fp);
102 }
103 
104 // print the matrix to stdout
105 template <typename T, typename indexT, bool columnMajor>
107  std::cout << std::endl;
108  std::cout << "IVSparse Matrix" << std::endl;
109 
110  // if the matrix is less than 100 rows and columns print the whole thing
111  if (numRows < 100 && numCols < 100) {
112  // print the matrix
113  for (uint32_t i = 0; i < numRows; i++) {
114  for (uint32_t j = 0; j < numCols; j++) {
115  std::cout << coeff(i, j) << " ";
116  }
117  std::cout << std::endl;
118  }
119  } else if (numRows >= 100 && numCols >= 100) {
120  // print the first 100 rows and columns
121  for (uint32_t i = 0; i < 100; i++) {
122  for (uint32_t j = 0; j < 100; j++) {
123  std::cout << coeff(i, j) << " ";
124  }
125  std::cout << std::endl;
126  }
127  }
128 }
129 
130 // Converts the CSC Matrix to a VCSC Matrix
131 template <typename T, typename indexT, bool columnMajor>
134  // create a new IVSparse matrix
136  vals, innerIdx, outerPtr, numRows, numCols, nnz);
137 
138  // return the matrix
139  return ivMat;
140 }
141 
142 // Converts the CSC Matrix to a IVCSC Matrix
143 template <typename T, typename indexT, bool columnMajor>
146  // create a new IVSparse matrix
148  vals, innerIdx, outerPtr, numRows, numCols, nnz);
149 
150  // return the matrix
151  return ivMat;
152 }
153 
154 // Converts the CSC Matrix to an Eigen Sparse Matrix
155 template <typename T, typename indexT, bool columnMajor>
156 Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor>
158 
159  // create a new sparse matrix with the correct dimensions
160  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> eigenMat(numRows, numCols);
161 
162  // check if the matrix is empty
163  if (nnz == 0) {
164  return eigenMat;
165  }
166 
167  // add the values to the matrix
168  for (uint32_t i = 0; i < outerDim; i++) {
169  for (indexT j = outerPtr[i]; j < outerPtr[i + 1]; j++) {
170  eigenMat.insert(innerIdx[j], i) = vals[j];
171  }
172  }
173 
174  // return the matrix
175  return eigenMat;
176 }
177 
178 //* -------------------------- Matrix Transformations -------------------------- *//
179 
180 // Transposes the CSC Matrix and Returns it
181 template <typename T, typename indexT, bool columnMajor>
183  // create an eigen matrix
184  Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
185 
186  // transpose the matrix
187  eigenMat = eigenMat.transpose();
188 
189  // create a new IVSparse matrix
191 
192  // return the matrix
193  return ivMat;
194 }
195 
196 // In Place Transpose
197 template <typename T, typename indexT, bool columnMajor>
199  // create an eigen matrix
200  Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
201 
202  // transpose the matrix
203  eigenMat = eigenMat.transpose();
204 
205  // set the matrix to the new matrix
207 }
208 
209 // Appends a CSC vector onto a CSC matrix
210 template <typename T, typename indexT, bool columnMajor>
212 
213  // ensure the vector is the correct dimensions
214  #ifdef IVSPARSE_DEBUG
215  assert((columnMajor && vec.size() == numRows) ||
216  (!columnMajor && vec.size() == numCols) &&
217  "Vector is not the correct size!");
218  #endif
219 
220  // update the dimensions and nnz
221  if (columnMajor) {
222  numCols++;
223  } else {
224  numRows++;
225  }
226  outerDim++;
227  nnz += vec.nonZeros();
228  metadata[2] = outerDim;
229  metadata[3] = nnz;
230 
231  // realloc the arrays
232  try {
233  vals = (T *)realloc(vals, sizeof(T) * nnz);
234  innerIdx = (indexT *)realloc(innerIdx, sizeof(indexT) * nnz);
235  outerPtr = (indexT *)realloc(outerPtr, sizeof(indexT) * (outerDim + 1));
236  } catch (std::bad_alloc &ba) {
237  std::cerr << "Error: " << ba.what() << std::endl;
238  exit(1);
239  }
240 
241  // copy the values
242  memcpy(vals + (nnz - vec.nonZeros()), vec.getValues(),
243  sizeof(T) * vec.nonZeros());
244  memcpy(innerIdx + (nnz - vec.nonZeros()), vec.getInnerIndices(),
245  sizeof(indexT) * vec.nonZeros());
246 
247  // update the outer pointers
248  outerPtr[outerDim] = nnz;
249 
250  // check if the vector is empty
251  if (vec.nonZeros() == 0) {
252  outerPtr[outerDim - 1] = nnz;
253  return;
254  }
255 
256  // calculate the compressed size
257  calculateCompSize();
258 }
259 
260 // slice method that returns a vector of IVSparse vectors
261 template <typename T, typename indexT, bool columnMajor>
262 std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector>
263 SparseMatrix<T, indexT, 1, columnMajor>::slice(uint32_t start, uint32_t end) {
264 
265  // check if the start and end values are valid
266  #ifdef IVSPARSE_DEBUG
267  assert(start < outerDim && end <= outerDim && start < end &&
268  "Invalid start and end values!");
269  #endif
270 
271  // make a vector of IVSparse vectors
272  std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector>vecs(end - start);
273 
274  // grab the vectors and add them to vecs
275  for (uint32_t i = start; i < end; ++i) {
276  // make a temp vector
278 
279  // add the vector to vecs
280  vecs[i - start] = temp;
281  }
282 
283  // return the vector
284  return vecs;
285 }
286 
287 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:25
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:165
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:21
Definition: IVCSC_SparseMatrix.hpp:29
IVSparse::SparseMatrix< T, indexT, 2, columnMajor > toVCSC()
Definition: IVCSC_Methods.hpp:153
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector getVector(uint32_t vec)
Definition: IVCSC_Methods.hpp:47
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor > transpose()
Definition: IVCSC_Methods.hpp:329
void append(typename SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector &vec)
Definition: IVCSC_Methods.hpp:245
T coeff(uint32_t row, uint32_t col)
Definition: IVCSC_Methods.hpp:17
void write(const char *filename)
Definition: IVCSC_Methods.hpp:72
void print()
Definition: IVCSC_Methods.hpp:97
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:210
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:30
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:416
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:374