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  private:
26  //* The Matrix Data *//
27  T *vals = nullptr; // The values of the matrix
28  indexT *innerIdx = nullptr; // The inner indices of the matrix
29  indexT *outerPtr = nullptr; // The outer pointers of the matrix
30 
31  //* Private Methods *//
32 
33  // Encodes the value type of the matrix in a uint32_t
34  void encodeValueType();
35 
36  // Checks if the value type is correct for the matrix
37  void checkValueType();
38 
39  // performs some simple user checks on the matrices metadata
40  void userChecks();
41 
42  // Calculates the current byte size of the matrix in memory
43  void calculateCompSize();
44 
45  // Scalar Multiplication
47  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(
58 
59  // Matrix Matrix Multiplication
60  inline Eigen::Matrix<T, -1, -1> matrixMultiply(Eigen::Matrix<T, -1, -1> &mat);
61 
62  public:
63  //* Nested Subclasses *//
64 
65  // Vector Class for CSC Sparse Matrices
66  class Vector;
67 
68  // Iterator Class for CSC Sparse Matrices
69  class InnerIterator;
70 
71  //* Constructors and Destructor *//
74 
86 
94  SparseMatrix(Eigen::SparseMatrix<T> &mat);
95 
102  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
103 
115  template <uint8_t compressionLevel2>
116  SparseMatrix(
118 
126 
133  template <typename T2, typename indexT2>
134  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr,
135  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
136 
145  template <typename T2, typename indexT2>
146  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries,
147  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
148 
156  SparseMatrix(
158 
166  SparseMatrix(std::vector<typename IVSparse::SparseMatrix<
167  T, indexT, 1, columnMajor>::Vector> &vecs);
168 
176  SparseMatrix(const char *filename);
177 
181  ~SparseMatrix();
182 
184 
185  //* Getters *//
189 
199  T coeff(uint32_t row, uint32_t col);
200 
207  bool isColumnMajor() const;
208 
212  T *getValues(uint32_t vec) const;
213 
217  indexT *getInnerIndices(uint32_t vec) const;
218 
222  indexT *getOuterPointers() const;
223 
234  uint32_t vec);
235 
237 
238  //* Calculations *//
242 
247  inline std::vector<T> outerSum();
248 
252  inline std::vector<T> innerSum();
253 
257  inline std::vector<T> maxColCoeff();
258 
262  inline std::vector<T> maxRowCoeff();
263 
267  inline std::vector<T> minColCoeff();
268 
272  inline std::vector<T> minRowCoeff();
273 
279  inline T trace();
280 
284  inline T sum();
285 
289  inline double norm();
290 
294  inline double vectorLength(uint32_t vec);
295 
297 
298  //* Utility Methods *//
302 
313  void write(const char *filename);
314 
321  void print();
322 
327 
332 
336  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor>
337  toEigen();
338 
340 
341  //* Matrix Manipulation Methods *//
345 
351 
355  void inPlaceTranspose();
356 
364 
369  std::vector<
371  slice(uint32_t start, uint32_t end);
372 
374 
375  //* Operator Overloads *//
376 
377  // Assignment Operator
380 
381  // Equality Operator
382  bool operator==(const SparseMatrix<T, indexT, 1, columnMajor> &other);
383 
384  // Inequality Operator
385  bool operator!=(const SparseMatrix<T, indexT, 1, columnMajor> &other);
386 
387  // Coefficient Access Operator
388  T operator()(uint32_t row, uint32_t col);
389 
390  // Vector Access Operator
392  uint32_t vec);
393 
394  // Scalar Multiplication
396 
397  // In Place Scalar Multiplication
398  void operator*=(T scalar);
399 
400  // Matrix Vector Multiplication
401  Eigen::VectorXd operator*(Eigen::VectorXd &vec);
402 
403  // Matrix Vector Multiplication 2 (with IVSparse Vector)
404  Eigen::VectorXd operator*(
406 
407  // Matrix Matrix Multiplication
408  Eigen::Matrix<T, -1, -1> operator*(Eigen::Matrix<T, -1, -1> mat);
409 };
410 
411 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:25
Definition: CSC_SparseMatrix.hpp:24
SparseMatrix()
Definition: CSC_SparseMatrix.hpp:85
Definition: VCSC_SparseMatrix.hpp:21
Definition: IVSparse_SparseMatrixBase.hpp:20
Definition: IVCSC_SparseMatrix.hpp:29
std::vector< T > maxRowCoeff()
Definition: IVCSC_BLAS.hpp:161
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
std::vector< T > minRowCoeff()
Definition: IVCSC_BLAS.hpp:199
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
std::vector< T > innerSum()
Definition: IVCSC_BLAS.hpp:126
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
T sum()
Definition: IVCSC_BLAS.hpp:239
double norm()
Definition: IVCSC_BLAS.hpp:256
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:210
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:30
SparseMatrix()
Definition: IVCSC_SparseMatrix.hpp:99
std::vector< T > minColCoeff()
Definition: IVCSC_BLAS.hpp:180
std::vector< T > outerSum()
Definition: IVCSC_BLAS.hpp:111
double vectorLength(uint32_t vec)
Definition: IVCSC_BLAS.hpp:272
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:416
std::vector< T > maxColCoeff()
Definition: IVCSC_BLAS.hpp:142
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:374
~SparseMatrix()
Destroy the Sparse Matrix object.
Definition: IVCSC_Constructors.hpp:15
T trace()
Definition: IVCSC_BLAS.hpp:218