IVSparse  v1.0
A sparse matrix compression library.
IVCSC_SparseMatrix.hpp
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
26  template <typename T, typename indexT = uint64_t, uint8_t compressionLevel = 3, bool columnMajor = true>
27  class SparseMatrix : public SparseMatrixBase {
28 
29  private:
30  //* The Matrix Data *//
31 
32  void **data = nullptr; // The data of the matrix
33  void **endPointers = nullptr; // The pointers to the end of each column
34 
35  //* Private Methods *//
36 
37  // Compression Algorithm for going from CSC to VCSC or IVCSC
38  template <typename T2, typename indexT2>
39  void compressCSC(T2 *vals, indexT2 *innerIndices, indexT2 *outerPointers);
40 
41  // Takes info about the value type and encodes it into a single uint32_t
42  void encodeValueType();
43 
44  // Checks the value type matches the class template T
45  void checkValueType();
46 
47  // Does checks on the class to ensure it is valid
48  void userChecks();
49 
50  // Method to calcuate and set the byte size of the matrix in memory
51  void calculateCompSize();
52 
53  // Private Helper Constructor for tranposing a IVSparse matrix
54  SparseMatrix(std::unordered_map<T, std::vector<indexT>> maps[], uint32_t num_rows, uint32_t num_cols);
55 
56  // Scalar Multiplication
58 
59  // In Place Scalar Multiplication
60  inline void inPlaceScalarMultiply(T scalar);
61 
62  // Matrix Vector Multiplication
63  inline Eigen::VectorXd vectorMultiply(Eigen::VectorXd &vec);
64 
65  // Matrix Vector Multiplication 2 (with IVSparse Vector)
66  inline Eigen::VectorXd vectorMultiply(typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector &vec);
67 
68  // Matrix Matrix Multiplication
69  inline Eigen::Matrix<T, -1, -1> matrixMultiply(Eigen::Matrix<T, -1, -1> &mat);
70 
71  public:
72  //* Nested Subclasses *//
73 
74  // Vector Class for IVCSC Sparse Matrix
75  class Vector;
76 
77  // Iterator Class for IVCSC Sparse Matrix
78  class InnerIterator;
79 
80  //* Constructors *//
83 
94 
100  SparseMatrix(uint32_t num_rows, uint32_t num_cols);
101 
108  SparseMatrix(Eigen::SparseMatrix<T> &mat);
109 
116  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
117 
127  template <uint8_t compressionLevel2>
129 
137 
143  template <typename T2, typename indexT2>
144  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
145 
155  template <typename T2, typename indexT2>
156  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
157 
165 
173 
180  SparseMatrix(const char *filename);
181 
185  ~SparseMatrix();
186 
188 
189  //* Getters *//
193 
204  T coeff(uint32_t row, uint32_t col);
205 
212  bool isColumnMajor() const;
213 
222  void *vectorPointer(uint32_t vec);
223 
233 
242  size_t getVectorSize(uint32_t vec) const;
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, compressionLevel, 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, compressionLevel, columnMajor> &other);
388 
389  // Inequality Operator
390  bool operator!=(const SparseMatrix<T, indexT, compressionLevel, columnMajor> &other);
391 
392  // Coefficient Access Operator
393  T operator()(uint32_t row, uint32_t col);
394 
395  // Vector Access Operator
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, compressionLevel, 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 SparseMatrix Class
414 
415 } // End of IVSparse Namespace
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_Vector.hpp:27
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:22
Definition: IVSparse_SparseMatrixBase.hpp:20
Definition: IVCSC_SparseMatrix.hpp:27
std::vector< T > maxRowCoeff()
Definition: IVCSC_BLAS.hpp:159
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
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
void * vectorPointer(uint32_t vec)
Definition: IVCSC_Methods.hpp:25
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
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:33
SparseMatrix(IVSparse::SparseMatrix< T, indexT, compressionLevel2, columnMajor > &other)
T trace()
Definition: IVCSC_BLAS.hpp:205