IVSparse  v1.0
A sparse matrix compression library.
IVCSC_SparseMatrix.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
28 template <typename T, typename indexT = uint64_t, uint8_t compressionLevel = 3, bool columnMajor = true>
30  private:
31  //* The Matrix Data *//
32 
33  void **data = nullptr; // The data of the matrix
34  void **endPointers = nullptr; // The pointers to the end of each column
35 
36  //* Private Methods *//
37 
38  // Compression Algorithm for going from CSC to VCSC or IVCSC
39  template <typename T2, typename indexT2>
40  void compressCSC(T2 *vals, indexT2 *innerIndices, indexT2 *outerPointers);
41 
42  // Takes info about the value type and encodes it into a single uint32_t
43  void encodeValueType();
44 
45  // Checks the value type matches the class template T
46  void checkValueType();
47 
48  // Does checks on the class to ensure it is valid
49  void userChecks();
50 
51  // Method to calcuate and set the byte size of the matrix in memory
52  void calculateCompSize();
53 
54  // Private Helper Constructor for tranposing a IVSparse matrix
55  SparseMatrix(std::unordered_map<T, std::vector<indexT>> maps[],
56  uint32_t num_rows, uint32_t num_cols);
57 
58  // Scalar Multiplication
60  scalarMultiply(T scalar);
61 
62  // In Place Scalar Multiplication
63  inline void inPlaceScalarMultiply(T scalar);
64 
65  // Matrix Vector Multiplication
66  inline Eigen::Matrix<T, -1, 1> vectorMultiply(Eigen::Matrix<T, -1, 1> vec);
67 
68  // Matrix Vector Multiplication 2 (with IVSparse Vector)
69  inline Eigen::Matrix<T, -1, 1> vectorMultiply(
71  &vec);
72 
73  // Matrix Matrix Multiplication
74  inline Eigen::Matrix<T, -1, -1> matrixMultiply(Eigen::Matrix<T, -1, -1> mat);
75 
76  public:
77  //* Nested Subclasses *//
78 
79  // Vector Class for IVCSC Sparse Matrix
80  class Vector;
81 
82  // Iterator Class for IVCSC Sparse Matrix
83  class InnerIterator;
84 
85  //* Constructors *//
88 
100 
106  SparseMatrix(uint32_t num_rows, uint32_t num_cols);
107 
115  SparseMatrix(Eigen::SparseMatrix<T> &mat);
116 
123  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
124 
136  template <uint8_t compressionLevel2>
139 
146  SparseMatrix(const IVSparse::SparseMatrix<T, indexT, compressionLevel,
147  columnMajor> &other);
148 
155  template <typename T2, typename indexT2>
156  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr,
157  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
158 
170  template <typename T2, typename indexT2>
171  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries,
172  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
173 
181  SparseMatrix(typename IVSparse::SparseMatrix<T, indexT, compressionLevel,
182  columnMajor>::Vector &vec);
183 
191  SparseMatrix(std::vector<typename IVSparse::SparseMatrix<
192  T, indexT, compressionLevel, columnMajor>::Vector> &vecs);
193 
201  SparseMatrix(const char *filename);
202 
206  ~SparseMatrix();
207 
209 
210  //* Getters *//
214 
227  T coeff(uint32_t row, uint32_t col);
228 
235  bool isColumnMajor() const;
236 
245  void *vectorPointer(uint32_t vec);
246 
256  typename IVSparse::SparseMatrix<T, indexT, compressionLevel,
257  columnMajor>::Vector getVector(uint32_t vec);
258 
267  size_t getVectorSize(uint32_t vec) const;
268 
270 
271  //* Calculations *//
275 
280  inline std::vector<T> outerSum();
281 
285  inline std::vector<T> innerSum();
286 
290  inline std::vector<T> maxColCoeff();
291 
295  inline std::vector<T> maxRowCoeff();
296 
300  inline std::vector<T> minColCoeff();
301 
305  inline std::vector<T> minRowCoeff();
306 
312  inline T trace();
313 
317  inline T sum();
318 
322  inline double norm();
323 
327  inline double vectorLength(uint32_t vec);
328 
330 
331  //* Utility Methods *//
335 
346  void write(const char *filename);
347 
354  void print();
355 
360 
365 
369  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor>
370  toEigen();
371 
373 
374  //* Matrix Manipulation Methods *//
378 
386 
392  void inPlaceTranspose();
393 
400  void append(typename SparseMatrix<T, indexT, compressionLevel,
401  columnMajor>::Vector &vec);
402 
407  std::vector<typename IVSparse::SparseMatrix<T, indexT, compressionLevel,
408  columnMajor>::Vector>
409  slice(uint32_t start, uint32_t end);
410 
412 
413  //* Operator Overloads *//
414 
415  // Assignment Operator
418  &other);
419 
420  // Equality Operator
421  bool operator==(
423 
424  // Inequality Operator
425  bool operator!=(
427 
428  // Coefficient Access Operator
429  T operator()(uint32_t row, uint32_t col);
430 
431  // Vector Access Operator
432  typename IVSparse::SparseMatrix<T, indexT, compressionLevel,
433  columnMajor>::Vector
434  operator[](uint32_t vec);
435 
436  // Scalar Multiplication
438  T scalar);
439 
440  // In Place Scalar Multiplication
441  void operator*=(T scalar);
442 
443  // Matrix Vector Multiplication
444  Eigen::Matrix<T, -1, 1> operator*(Eigen::Matrix<T, -1, 1> vec);
445 
446  // Matrix Vector Multiplication 2 (with IVSparse Vector)
447  Eigen::Matrix<T, -1, 1> operator*(
449  &vec);
450 
451  // Matrix Matrix Multiplication
452  Eigen::Matrix<T, -1, -1> operator*(Eigen::Matrix<T, -1, -1> mat);
453 
454 }; // End of SparseMatrix Class
455 
456 } // namespace IVSparse
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_Vector.hpp:25
Definition: CSC_SparseMatrix.hpp:24
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
IVSparse::SparseMatrix< T, indexT, 1, columnMajor > toCSC()
Definition: IVCSC_Methods.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
void * vectorPointer(uint32_t vec)
Definition: IVCSC_Methods.hpp:36
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
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:57
SparseMatrix(IVSparse::SparseMatrix< T, indexT, compressionLevel2, columnMajor > &other)
T trace()
Definition: IVCSC_BLAS.hpp:218