IVSparse  v1.0
A sparse matrix compression library.
VCSC_SparseMatrix.hpp
1 
9 #pragma once
10 
11 namespace IVSparse
12 {
13 
20  template <typename T, typename indexT, bool columnMajor>
21  class SparseMatrix<T, indexT, 2, columnMajor> : public SparseMatrixBase
22  {
23 
24  private:
25  //* The Matrix Data *//
26 
27  T **values = nullptr; // The values of the matrix
28  indexT **counts = nullptr; // The counts of the matrix
29  indexT **indices = nullptr; // The indices of the matrix
30 
31  indexT *valueSizes = nullptr; // The sizes of the value arrays
32  indexT *indexSizes = nullptr; // The sizes of the index arrays
33 
34  //* Private Methods *//
35 
36  // Compression Algorithm for going from CSC to VCSC or IVCSCC
37  template <typename T2, typename indexT2>
38  void compressCSC(T2 *vals, indexT2 *innerIndices, indexT2 *outerPointers);
39 
40  // Encodes the value type of the matrix
41  void encodeValueType();
42 
43  // Checks if the value type is correct for the matrix
44  void checkValueType();
45 
46  // performs some simple user checks on the matrices metadata
47  void userChecks();
48 
49  // Calculates the current byte size of the matrix in memory
50  void calculateCompSize();
51 
52  // Private Helper Constructor for tranposing a IVSparse matrix
53  SparseMatrix(std::unordered_map<T, std::vector<indexT>> maps[], uint32_t num_rows, uint32_t num_cols);
54 
55  // Scalar Multiplication
56  inline IVSparse::SparseMatrix<T, indexT, 2, columnMajor> scalarMultiply(T scalar);
57 
58  // In Place Scalar Multiplication
59  inline void inPlaceScalarMultiply(T scalar);
60 
61  // Matrix Vector Multiplication
62  inline Eigen::VectorXd vectorMultiply(Eigen::VectorXd &vec);
63 
64  // Matrix Vector Multiplication 2 (with IVSparse Vector)
65  inline Eigen::VectorXd vectorMultiply(typename SparseMatrix<T, indexT, 2, columnMajor>::Vector &vec);
66 
67  // Matrix Matrix Multiplication
68  inline Eigen::Matrix<T, -1, -1> matrixMultiply(Eigen::Matrix<T, -1, -1> &mat);
69 
70  public:
71  //* Nested Subclasses *//
72 
73  // The Vector Class for VCSC Matrices
74  class Vector;
75 
76  // The Iterator Class for VCSC Matrices
77  class InnerIterator;
78 
79  //* Constructors and Destructor *//
82 
93 
100  SparseMatrix(Eigen::SparseMatrix<T> &mat);
101 
108  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
109 
119  template <uint8_t compressionLevel2>
121 
129 
135  template <typename T2, typename indexT2>
136  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
137 
145  template <typename T2, typename indexT2>
146  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
147 
155 
163 
170  SparseMatrix(const char *filename);
171 
175  ~SparseMatrix();
176 
178 
179  //* Getters *//
183 
194  T coeff(uint32_t row, uint32_t col);
195 
202  bool isColumnMajor() const;
203 
208  T *getValues(uint32_t vec) const;
209 
214  indexT *getCounts(uint32_t vec) const;
215 
220  indexT *getIndices(uint32_t vec) const;
221 
226  indexT getNumUniqueVals(uint32_t vec) const;
227 
232  indexT getNumIndices(uint32_t vec) const;
233 
243 
245 
246  //* Calculations *//
250 
255  inline std::vector<T> outerSum();
256 
260  inline std::vector<T> innerSum();
261 
265  inline std::vector<T> maxColCoeff();
266 
270  inline std::vector<T> maxRowCoeff();
271 
275  inline std::vector<T> minColCoeff();
276 
280  inline std::vector<T> minRowCoeff();
281 
287  inline T trace();
288 
292  inline T sum();
293 
297  inline double norm();
298 
302  inline double vectorLength(uint32_t vec);
303 
305 
306  //* Utility Methods *//
310 
321  void write(const char *filename);
322 
328  void print();
329 
334 
339 
343  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> toEigen();
344 
346 
347  //* Matrix Manipulation Methods *//
351 
359 
365  void inPlaceTranspose();
366 
373 
377  std::vector<typename IVSparse::SparseMatrix<T, indexT, 2, columnMajor>::Vector> slice(uint32_t start, uint32_t end);
378 
380 
381  //* Operator Overloads *//
382 
383  // Assignment Operator
385 
386  // Equality Operator
387  bool operator==(const SparseMatrix<T, indexT, 2, columnMajor> &other);
388 
389  // Inequality Operator
390  bool operator!=(const SparseMatrix<T, indexT, 2, columnMajor> &other);
391 
392  // Coefficient Access Operator
393  T operator()(uint32_t row, uint32_t col);
394 
395  // Vector Access Operator
396  typename IVSparse::SparseMatrix<T, indexT, 2, columnMajor>::Vector operator[](uint32_t vec);
397 
398  // Scalar Multiplication
400 
401  // In Place Scalar Multiplication
402  void operator*=(T scalar);
403 
404  // Matrix Vector Multiplication
405  Eigen::VectorXd operator*(Eigen::VectorXd &vec);
406 
407  // Matrix Vector Multiplication 2 (with IVSparse Vector)
408  Eigen::VectorXd operator*(typename SparseMatrix<T, indexT, 2, columnMajor>::Vector &vec);
409 
410  // Matrix Matrix Multiplication
411  Eigen::Matrix<T, -1, -1> operator*(Eigen::Matrix<T, -1, -1> mat);
412 
413  }; // End of VCSC Sparse Matrix Class
414 
415 } // End of IVSparse Namespace
Definition: IVCSC_Vector.hpp:27
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:22
SparseMatrix()
Definition: VCSC_SparseMatrix.hpp:92
SparseMatrix(IVSparse::SparseMatrix< T, indexT, compressionLevel2, columnMajor > &other)
Definition: IVSparse_SparseMatrixBase.hpp:20
Definition: IVCSC_SparseMatrix.hpp:27
std::vector< T > maxRowCoeff()
Definition: IVCSC_BLAS.hpp:159
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector getVector(uint32_t vec)
Definition: IVCSC_Methods.hpp:29
std::vector< T > minRowCoeff()
Definition: IVCSC_BLAS.hpp:189
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
std::vector< T > innerSum()
Definition: IVCSC_BLAS.hpp:131
IVSparse::SparseMatrix< T, indexT, 1, columnMajor > toCSC()
Definition: IVCSC_Methods.hpp:92
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
T sum()
Definition: IVCSC_BLAS.hpp:219
double norm()
Definition: IVCSC_BLAS.hpp:232
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:169
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:21
SparseMatrix()
Definition: IVCSC_SparseMatrix.hpp:93
std::vector< T > minColCoeff()
Definition: IVCSC_BLAS.hpp:174
std::vector< T > outerSum()
Definition: IVCSC_BLAS.hpp:118
double vectorLength(uint32_t vec)
Definition: IVCSC_BLAS.hpp:245
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:350
std::vector< T > maxColCoeff()
Definition: IVCSC_BLAS.hpp:144
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:311
~SparseMatrix()
Destroy the Sparse Matrix object.
Definition: IVCSC_Constructors.hpp:15
T trace()
Definition: IVCSC_BLAS.hpp:205