IVSparse  v1.0
A sparse matrix compression library.
CSC_SparseMatrix.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
23  template <typename T, typename indexT, bool columnMajor>
24  class SparseMatrix<T, indexT, 1, columnMajor> : public SparseMatrixBase {
25 
26  private:
27  //* The Matrix Data *//
28  T *vals = nullptr; // The values of the matrix
29  indexT *innerIdx = nullptr; // The inner indices of the matrix
30  indexT *outerPtr = nullptr; // The outer pointers of the matrix
31 
32  //* Private Methods *//
33 
34  // Encodes the value type of the matrix in a uint32_t
35  void encodeValueType();
36 
37  // Checks if the value type is correct for the matrix
38  void checkValueType();
39 
40  // performs some simple user checks on the matrices metadata
41  void userChecks();
42 
43  // Calculates the current byte size of the matrix in memory
44  void calculateCompSize();
45 
46  // Scalar Multiplication
47  inline IVSparse::SparseMatrix<T, indexT, 1, columnMajor> scalarMultiply(T scalar);
48 
49  // In Place Scalar Multiplication
50  inline void inPlaceScalarMultiply(T scalar);
51 
52  // Matrix Vector Multiplication
53  inline Eigen::VectorXd vectorMultiply(Eigen::VectorXd &vec);
54 
55  // Matrix Vector Multiplication 2 (with IVSparse Vector)
56  inline Eigen::VectorXd vectorMultiply(typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec);
57 
58  // Matrix Matrix Multiplication
59  inline Eigen::Matrix<T, -1, -1> matrixMultiply(Eigen::Matrix<T, -1, -1> &mat);
60 
61  public:
62  //* Nested Subclasses *//
63 
64  // Vector Class for CSC Sparse Matrices
65  class Vector;
66 
67  // Iterator Class for CSC Sparse Matrices
68  class InnerIterator;
69 
70  //* Constructors and Destructor *//
73 
84 
91  SparseMatrix(Eigen::SparseMatrix<T> &mat);
92 
99  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
100 
110  template <uint8_t compressionLevel2>
112 
120 
126  template <typename T2, typename indexT2>
127  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
128 
136  template <typename T2, typename indexT2>
137  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries, uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
138 
146 
154 
161  SparseMatrix(const char *filename);
162 
166  ~SparseMatrix();
167 
169 
170  //* Getters *//
174 
183  T coeff(uint32_t row, uint32_t col);
184 
191  bool isColumnMajor() const;
192 
196  T *getValues(uint32_t vec) const;
197 
201  indexT *getInnerIndices(uint32_t vec) const;
202 
206  indexT *getOuterPointers() const;
207 
217 
219 
220  //* Calculations *//
224 
229  inline std::vector<T> outerSum();
230 
234  inline std::vector<T> innerSum();
235 
239  inline std::vector<T> maxColCoeff();
240 
244  inline std::vector<T> maxRowCoeff();
245 
249  inline std::vector<T> minColCoeff();
250 
254  inline std::vector<T> minRowCoeff();
255 
261  inline T trace();
262 
266  inline T sum();
267 
271  inline double norm();
272 
276  inline double vectorLength(uint32_t vec);
277 
279 
280  //* Utility Methods *//
284 
295  void write(const char *filename);
296 
302  void print();
303 
308 
313 
317  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> toEigen();
318 
320 
321  //* Matrix Manipulation Methods *//
325 
331 
335  void inPlaceTranspose();
336 
343 
347  std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector> slice(uint32_t start, uint32_t end);
348 
350 
351  //* Operator Overloads *//
352 
353  // Assignment Operator
355 
356  // Equality Operator
357  bool operator==(const SparseMatrix<T, indexT, 1, columnMajor> &other);
358 
359  // Inequality Operator
360  bool operator!=(const SparseMatrix<T, indexT, 1, columnMajor> &other);
361 
362  // Coefficient Access Operator
363  T operator()(uint32_t row, uint32_t col);
364 
365  // Vector Access Operator
366  typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector operator[](uint32_t vec);
367 
368  // Scalar Multiplication
370 
371  // In Place Scalar Multiplication
372  void operator*=(T scalar);
373 
374  // Matrix Vector Multiplication
375  Eigen::VectorXd operator*(Eigen::VectorXd &vec);
376 
377  // Matrix Vector Multiplication 2 (with IVSparse Vector)
378  Eigen::VectorXd operator*(typename SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec);
379 
380  // Matrix Matrix Multiplication
381  Eigen::Matrix<T, -1, -1> operator*(Eigen::Matrix<T, -1, -1> mat);
382 
383  };
384 
385 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:27
Definition: CSC_SparseMatrix.hpp:24
SparseMatrix()
Definition: CSC_SparseMatrix.hpp:83
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
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