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) { return (*this)(row, col); }
18 
19  // Check for Column Major
20  template <typename T, typename indexT, bool columnMajor>
21  bool SparseMatrix<T, indexT, 1, columnMajor>::isColumnMajor() const { return columnMajor; }
22 
23  // Get the values of the matrix
24  template <typename T, typename indexT, bool columnMajor>
25  T *SparseMatrix<T, indexT, 1, columnMajor>::getValues(uint32_t vec) const { return vals; }
26 
27  // Get the inner indices of the matrix
28  template <typename T, typename indexT, bool columnMajor>
29  indexT *SparseMatrix<T, indexT, 1, columnMajor>::getInnerIndices(uint32_t vec) const { return innerIdx; }
30 
31  // Get the outer pointers of the matrix
32  template <typename T, typename indexT, bool columnMajor>
33  indexT *SparseMatrix<T, indexT, 1, columnMajor>::getOuterPointers() const { return outerPtr; }
34 
35  // Get a csf vector from the matrix
36  template <typename T, typename indexT, bool columnMajor>
38  #ifdef CSF_DEBUG
39  // check if the vector is out of bounds
40  assert((vec < outerDim && vec >= 0) && "Vector index out of bounds");
41  #endif
42 
43  // create a vector from the matrix and return it
45  return v;
46  }
47 
48  //* ------------------------------ Utility Methods ------------------------------ *//
49 
50  // write the matrix to file
51  template <typename T, typename indexT, bool columnMajor>
53 
54  // open the file
55  FILE *fp = fopen(filename, "wb");
56 
57  // write the metadata
58  fwrite(metadata, sizeof(uint32_t), NUM_META_DATA, fp);
59 
60  // write the values
61  fwrite(vals, sizeof(T), nnz, fp);
62 
63  // write the inner indices
64  fwrite(innerIdx, sizeof(indexT), nnz, fp);
65 
66  // write the outer pointers
67  fwrite(outerPtr, sizeof(indexT), outerDim + 1, fp);
68 
69  // close the file
70  fclose(fp);
71  }
72 
73  // print the matrix to stdout
74  template <typename T, typename indexT, bool columnMajor>
76  std::cout << std::endl;
77  std::cout << "IVSparse Matrix" << std::endl;
78 
79  // if the matrix is less than 100 rows and columns print the whole thing
80  if (numRows < 100 && numCols < 100) {
81  // print the matrix
82  for (uint32_t i = 0; i < numRows; i++) {
83  for (uint32_t j = 0; j < numCols; j++) { std::cout << coeff(i, j) << " "; }
84  std::cout << std::endl;
85  }
86  } else if (numRows >= 100 && numCols >= 100) {
87  // print the first 100 rows and columns
88  for (uint32_t i = 0; i < 100; i++) {
89  for (uint32_t j = 0; j < 100; j++) { std::cout << coeff(i, j) << " "; }
90  std::cout << std::endl;
91  }
92  }
93  }
94 
95  // Converts the CSC Matrix to a VCSC Matrix
96  template <typename T, typename indexT, bool columnMajor>
98  // create a new IVSparse matrix
99  IVSparse::SparseMatrix<T, indexT, 2, columnMajor> csfMat(vals, innerIdx, outerPtr, numRows, numCols, nnz);
100 
101  // return the matrix
102  return csfMat;
103  }
104 
105  // Converts the CSC Matrix to a IVCSC Matrix
106  template <typename T, typename indexT, bool columnMajor>
108  // create a new IVSparse matrix
109  IVSparse::SparseMatrix<T, indexT, 3, columnMajor> csfMat(vals, innerIdx, outerPtr, numRows, numCols, nnz);
110 
111  // return the matrix
112  return csfMat;
113  }
114 
115  // Converts the CSC Matrix to an Eigen Sparse Matrix
116  template <typename T, typename indexT, bool columnMajor>
117  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> SparseMatrix<T, indexT, 1, columnMajor>::toEigen() {
118  // create a new sparse matrix with the correct dimensions
119  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> eigenMat(numRows, numCols);
120 
121  // check if the matrix is empty
122  if (nnz == 0) { return eigenMat; }
123 
124  // add the values to the matrix
125  for (uint32_t i = 0; i < outerDim; i++) {
126  for (indexT j = outerPtr[i]; j < outerPtr[i + 1]; j++) {
127  eigenMat.insert(innerIdx[j], i) = vals[j];
128  }
129  }
130 
131  // return the matrix
132  return eigenMat;
133  }
134 
135  //* ------------------------------ Matrix Transformations ------------------------------ *//
136 
137  // Transposes the CSC Matrix and Returns it
138  template <typename T, typename indexT, bool columnMajor>
140  // create an eigen matrix
141  Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
142 
143  // transpose the matrix
144  eigenMat = eigenMat.transpose();
145 
146  // create a new IVSparse matrix
148 
149  // return the matrix
150  return csfMat;
151  }
152 
153  // In Place Transpose
154  template <typename T, typename indexT, bool columnMajor>
156  // create an eigen matrix
157  Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
158 
159  // transpose the matrix
160  eigenMat = eigenMat.transpose();
161 
162  // set the matrix to the new matrix
164  }
165 
166  // Appends a CSC vector onto a CSC matrix
167  template <typename T, typename indexT, bool columnMajor>
169 
170  // update the dimensions and nnz
171  if (columnMajor) { numCols++; }
172  else { numRows++; }
173  outerDim++;
174  nnz += vec.nonZeros();
175  metadata[2] = outerDim;
176  metadata[3] = nnz;
177 
178  // realloc the arrays
179  try {
180  vals = (T *)realloc(vals, sizeof(T) * nnz);
181  innerIdx = (indexT *)realloc(innerIdx, sizeof(indexT) * nnz);
182  outerPtr = (indexT *)realloc(outerPtr, sizeof(indexT) * (outerDim + 1));
183  } catch (std::bad_alloc &ba) {
184  std::cerr << "Error: " << ba.what() << std::endl;
185  exit(1);
186  }
187 
188  // copy the values
189  memcpy(vals + (nnz - vec.nonZeros()), vec.getValues(), sizeof(T) * vec.nonZeros());
190  memcpy(innerIdx + (nnz - vec.nonZeros()), vec.getInnerIndices(), sizeof(indexT) * vec.nonZeros());
191 
192  // update the outer pointers
193  outerPtr[outerDim] = nnz;
194 
195  // check if the vector is empty
196  if (vec.nonZeros() == 0) {
197  outerPtr[outerDim - 1] = nnz;
198  return;
199  }
200 
201  // calculate the compressed size
202  calculateCompSize();
203  }
204 
205  // slice method that returns a vector of IVSparse vectors
206  template <typename T, typename indexT, bool columnMajor>
207  std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector> SparseMatrix<T, indexT, 1, columnMajor>::slice(uint32_t start, uint32_t end) {
208  // check if the start and end values are valid
209  #ifdef CSF_DEBUG
210  assert(start < outerDim && end <= outerDim && start < end && "Invalid start and end values!");
211  #endif
212 
213  // make a vector of IVSparse vectors
214  std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector> vecs(end - start);
215 
216  // grab the vectors and add them to vecs
217  for (uint32_t i = start; i < end; ++i) {
218  // make a temp vector
220 
221  // add the vector to vecs
222  vecs[i - start] = temp;
223  }
224 
225  // return the vector
226  return vecs;
227  }
228 
229 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:27
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:163
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:22
Definition: IVCSC_SparseMatrix.hpp:27
IVSparse::SparseMatrix< T, indexT, 2, columnMajor > toVCSC()
Definition: IVCSC_Methods.hpp:116
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector getVector(uint32_t vec)
Definition: IVCSC_Methods.hpp:29
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor > transpose()
Definition: IVCSC_Methods.hpp:269
void append(typename SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector &vec)
Definition: IVCSC_Methods.hpp:202
T coeff(uint32_t row, uint32_t col)
Definition: IVCSC_Methods.hpp:17
void write(const char *filename)
Definition: IVCSC_Methods.hpp:42
void print()
Definition: IVCSC_Methods.hpp:64
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:169
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:21
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:350
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:311