14 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
20 if (data !=
nullptr) {
21 for (uint32_t i = 0; i < outerDim; i++) {
if (data[i] !=
nullptr) {free(data[i]);} }
24 if (endPointers !=
nullptr) { free(endPointers); }
25 if (metadata !=
nullptr) {
delete[] metadata; }
28 numRows = other.numRows;
29 numCols = other.numCols;
30 outerDim = other.outerDim;
31 innerDim = other.innerDim;
33 compSize = other.compSize;
37 data = (
void **)malloc(outerDim *
sizeof(
void *));
38 endPointers = (
void **)malloc(outerDim *
sizeof(
void *));
39 metadata =
new uint32_t[NUM_META_DATA];
40 }
catch (std::bad_alloc &e) {
41 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix" << std::endl;
46 memcpy(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA);
50 index_t = other.index_t;
53 for (uint32_t i = 0; i < outerDim; i++) {
56 if (other.data[i] ==
nullptr) {
58 endPointers[i] =
nullptr;
64 }
catch (std::bad_alloc &e) {
65 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix" << std::endl;
77 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
78 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator==(
const SparseMatrix<T, indexT, compressionLevel, columnMajor> &other) {
82 if (memcmp(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA) != 0)
86 for (uint32_t i = 0; i < outerDim; i++) {
88 if (memcmp(data[i], other.data[i], getVectorSize(i)) != 0)
96 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
97 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator!=(
const SparseMatrix<T, indexT, compressionLevel, columnMajor> &other) {
98 return !(*
this == other);
102 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
103 T SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator()(uint32_t row, uint32_t col) {
105 if (row >= numRows || col >= numCols) {
106 std::cerr <<
"Error: Index out of bounds" << std::endl;
110 uint32_t vector = columnMajor ? col : row;
111 uint32_t index = columnMajor ? row : col;
114 if (data[vector] ==
nullptr)
118 for (
typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::InnerIterator it(*
this, vector); it; ++it) {
119 if (it.getIndex() == (indexT)index) {
return it.value(); }
127 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
128 typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator[](uint32_t vec) {
132 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
142 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
145 if (mat.
cols() > 110) {
146 std::cout <<
"IVSparse matrix is too large to print" << std::endl;
152 T **matrix =
new T *[mat.
rows()];
153 for (
size_t i = 0; i < mat.
rows(); i++) { matrix[i] = (T *)calloc(mat.
cols(),
sizeof(T)); }
156 for (
size_t i = 0; i < mat.
cols(); i++) {
159 matrix[it.row()][it.col()] = it.value();
167 for (
size_t i = 0; i < mat.
rows(); i++) {
168 for (
size_t j = 0; j < mat.
cols(); j++) {
169 os << matrix[i][j] <<
" ";
174 for (
int i = 0; i < mat.
rows(); i++) { free(matrix[i]); }
183 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
186 return scalarMultiply(scalar);
190 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
191 void SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*=(T scalar)
193 return inPlaceScalarMultiply(scalar);
197 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
198 Eigen::VectorXd SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector &vec)
200 return vectorMultiply(vec);
204 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
205 Eigen::VectorXd SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(Eigen::VectorXd &vec)
207 return vectorMultiply(vec);
211 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
212 Eigen::Matrix<T, -1, -1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(Eigen::Matrix<T, -1, -1> mat)
214 return matrixMultiply(mat);
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_Vector.hpp:27
uint32_t cols() const
Definition: IVSparse_Base_Methods.hpp:25
uint32_t rows() const
Definition: IVSparse_Base_Methods.hpp:22
Definition: IVCSC_SparseMatrix.hpp:27
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:33