IVSparse  v1.0
A sparse matrix compression library.
VCSC_SparseMatrix.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 namespace IVSparse {
12 
20 template <typename T, typename indexT, bool columnMajor>
21 class SparseMatrix<T, indexT, 2, columnMajor> : public SparseMatrixBase {
22  private:
23  //* The Matrix Data *//
24 
25  T **values = nullptr; // The values of the matrix
26  indexT **counts = nullptr; // The counts of the matrix
27  indexT **indices = nullptr; // The indices of the matrix
28 
29  indexT *valueSizes = nullptr; // The sizes of the value arrays
30  indexT *indexSizes = nullptr; // The sizes of the index arrays
31 
32  //* Private Methods *//
33 
34  // Compression Algorithm for going from CSC to VCSC or IVCSCC
35  template <typename T2, typename indexT2>
36  void compressCSC(T2 *vals, indexT2 *innerIndices, indexT2 *outerPointers);
37 
38  // Encodes the value type of the matrix
39  void encodeValueType();
40 
41  // Checks if the value type is correct for the matrix
42  void checkValueType();
43 
44  // performs some simple user checks on the matrices metadata
45  void userChecks();
46 
47  // Calculates the current byte size of the matrix in memory
48  void calculateCompSize();
49 
50  // Private Helper Constructor for tranposing a IVSparse matrix
51  SparseMatrix(std::unordered_map<T, std::vector<indexT>> maps[],
52  uint32_t num_rows, uint32_t num_cols);
53 
54  // Scalar Multiplication
56  T scalar);
57 
58  // In Place Scalar Multiplication
59  inline void inPlaceScalarMultiply(T scalar);
60 
61  // Matrix Vector Multiplication
62  inline Eigen::Matrix<T, -1, 1> vectorMultiply(Eigen::Matrix<T, -1, 1> &vec);
63 
64  // Matrix Vector Multiplication 2 (with IVSparse Vector)
65  inline Eigen::Matrix<T, -1, 1> vectorMultiply(
67 
68  public:
69  //* Nested Subclasses *//
70 
71  // The Vector Class for VCSC Matrices
72  class Vector;
73 
74  // The Iterator Class for VCSC Matrices
75  class InnerIterator;
76 
77  //* Constructors and Destructor *//
80 
92 
100  SparseMatrix(Eigen::SparseMatrix<T> &mat);
101 
108  SparseMatrix(Eigen::SparseMatrix<T, Eigen::RowMajor> &mat);
109 
121  template <uint8_t compressionLevel2>
124 
132 
139  template <typename T2, typename indexT2>
140  SparseMatrix(T2 *vals, indexT2 *innerIndices, indexT2 *outerPtr,
141  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
142 
151  template <typename T2, typename indexT2>
152  SparseMatrix(std::vector<std::tuple<indexT2, indexT2, T2>> &entries,
153  uint32_t num_rows, uint32_t num_cols, uint32_t nnz);
154 
162  SparseMatrix(
164 
172  SparseMatrix(std::vector<typename IVSparse::SparseMatrix<
173  T, indexT, 2, columnMajor>::Vector> &vecs);
174 
182  SparseMatrix(const char *filename);
183 
187  ~SparseMatrix();
188 
190 
191  //* Getters *//
195 
208  T coeff(uint32_t row, uint32_t col);
209 
216  bool isColumnMajor() const;
217 
222  T *getValues(uint32_t vec) const;
223 
228  indexT *getCounts(uint32_t vec) const;
229 
234  indexT *getIndices(uint32_t vec) const;
235 
240  indexT getNumUniqueVals(uint32_t vec) const;
241 
247  indexT getNumIndices(uint32_t vec) const;
248 
259  uint32_t vec);
260 
262 
263  //* Calculations *//
267 
272  inline std::vector<T> outerSum();
273 
277  inline std::vector<T> innerSum();
278 
282  inline std::vector<T> maxColCoeff();
283 
287  inline std::vector<T> maxRowCoeff();
288 
292  inline std::vector<T> minColCoeff();
293 
297  inline std::vector<T> minRowCoeff();
298 
304  inline T trace();
305 
309  inline T sum();
310 
314  inline double norm();
315 
319  inline double vectorLength(uint32_t vec);
320 
322 
323  //* Utility Methods *//
327 
338  void write(const char *filename);
339 
346  void print();
347 
352 
357 
361  Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor>
362  toEigen();
363 
365 
366  //* Matrix Manipulation Methods *//
370 
378 
384  void inPlaceTranspose();
385 
393 
398  std::vector<
400  slice(uint32_t start, uint32_t end);
401 
403 
404  //* Operator Overloads *//
405 
406  // Assignment Operator
409 
410  // Equality Operator
411  bool operator==(const SparseMatrix<T, indexT, 2, columnMajor> &other);
412 
413  // Inequality Operator
414  bool operator!=(const SparseMatrix<T, indexT, 2, columnMajor> &other);
415 
416  // Coefficient Access Operator
417  T operator()(uint32_t row, uint32_t col);
418 
419  // Vector Access Operator
421  uint32_t vec);
422 
423  // Scalar Multiplication
425 
426  // In Place Scalar Multiplication
427  void operator*=(T scalar);
428 
429  // Matrix Vector Multiplication
430  Eigen::Matrix<T, -1, 1> operator*(Eigen::Matrix<T, -1, 1> &vec);
431 
432  // Matrix Vector Multiplication 2 (with IVSparse Vector)
433  Eigen::Matrix<T, -1, 1> operator*(
435 
436  // Matrix Matrix Multiplication
437  Eigen::Matrix<T, -1, -1> operator*(Eigen::Matrix<T, -1, -1> mat);
438 
439 }; // End of VCSC Sparse Matrix Class
440 
441 } // namespace IVSparse
Definition: IVCSC_Vector.hpp:25
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:21
SparseMatrix()
Definition: VCSC_SparseMatrix.hpp:91
SparseMatrix(IVSparse::SparseMatrix< T, indexT, compressionLevel2, columnMajor > &other)
Definition: IVSparse_SparseMatrixBase.hpp:20
Definition: IVCSC_SparseMatrix.hpp:29
std::vector< T > maxRowCoeff()
Definition: IVCSC_BLAS.hpp:161
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
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