15 template <
typename T,
typename indexT,
bool columnMajor>
24 if (values !=
nullptr)
26 for (uint32_t i = 0; i < outerDim; i++)
28 if (values[i] !=
nullptr)
33 if (counts !=
nullptr)
35 for (uint32_t i = 0; i < outerDim; i++)
37 if (counts[i] !=
nullptr)
42 if (indices !=
nullptr)
44 for (uint32_t i = 0; i < outerDim; i++)
46 if (indices[i] !=
nullptr)
52 if (valueSizes !=
nullptr)
56 if (indexSizes !=
nullptr)
60 if (metadata !=
nullptr)
66 numRows = other.numRows;
67 numCols = other.numCols;
68 outerDim = other.outerDim;
69 innerDim = other.innerDim;
71 compSize = other.compSize;
76 values = (T **)malloc(outerDim *
sizeof(T *));
77 counts = (indexT **)malloc(outerDim *
sizeof(indexT *));
78 indices = (indexT **)malloc(outerDim *
sizeof(indexT *));
80 valueSizes = (indexT *)malloc(outerDim *
sizeof(indexT));
81 indexSizes = (indexT *)malloc(outerDim *
sizeof(indexT));
83 metadata =
new uint32_t[NUM_META_DATA];
85 catch (std::bad_alloc &e)
87 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix" << std::endl;
92 memcpy(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA);
96 index_t = other.index_t;
99 for (uint32_t i = 0; i < outerDim; i++)
103 values[i] = (T *)malloc(other.valueSizes[i] *
sizeof(T));
104 counts[i] = (indexT *)malloc(other.valueSizes[i] *
sizeof(indexT));
105 indices[i] = (indexT *)malloc(other.indexSizes[i] *
sizeof(indexT));
107 catch (std::bad_alloc &e)
109 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix" << std::endl;
114 memcpy(values[i], other.values[i],
sizeof(T) * other.valueSizes[i]);
115 memcpy(counts[i], other.counts[i],
sizeof(indexT) * other.valueSizes[i]);
116 memcpy(indices[i], other.indices[i],
sizeof(indexT) * other.indexSizes[i]);
118 valueSizes[i] = other.valueSizes[i];
119 indexSizes[i] = other.indexSizes[i];
129 template <
typename T,
typename indexT,
bool columnMajor>
130 bool SparseMatrix<T, indexT, 2, columnMajor>::operator==(
const SparseMatrix<T, indexT, 2, columnMajor> &other)
134 if (memcmp(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA) != 0)
140 for (uint32_t i = 0; i < outerDim; i++)
142 if (memcmp(values[i], other.values[i],
sizeof(T) * valueSizes[i]) != 0)
149 for (uint32_t i = 0; i < outerDim; i++)
151 if (memcmp(indices[i], other.indices[i],
sizeof(indexT) * indexSizes[i]) != 0)
158 for (uint32_t i = 0; i < outerDim; i++)
160 if (memcmp(counts[i], other.counts[i],
sizeof(indexT) * valueSizes[i]) != 0)
171 template <
typename T,
typename indexT,
bool columnMajor>
172 bool SparseMatrix<T, indexT, 2, columnMajor>::operator!=(
const SparseMatrix<T, indexT, 2, columnMajor> &other) {
return !(*
this == other); }
175 template <
typename T,
typename indexT,
bool columnMajor>
176 T SparseMatrix<T, indexT, 2, columnMajor>::operator()(uint32_t row, uint32_t col)
179 if (row >= numRows || col >= numCols)
181 std::cerr <<
"Error: Index out of bounds" << std::endl;
185 uint32_t vector = columnMajor ? col : row;
186 uint32_t index = columnMajor ? row : col;
189 for (
typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator it(*
this, vector); it; ++it)
191 if (it.getIndex() == (indexT)index)
202 template <
typename T,
typename indexT,
bool columnMajor>
203 typename SparseMatrix<T, indexT, 2, columnMajor>::Vector SparseMatrix<T, indexT, 2, columnMajor>::operator[](uint32_t vec)
208 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
218 template <
typename T,
typename indexT,
bool columnMajor>
222 if (mat.
cols() > 110)
224 std::cout <<
"IVSparse matrix is too large to print" << std::endl;
230 T **matrix =
new T *[mat.
rows()];
231 for (
size_t i = 0; i < mat.
rows(); i++)
233 matrix[i] = (T *)calloc(mat.
cols(),
sizeof(T));
237 for (
size_t i = 0; i < mat.
cols(); i++)
242 matrix[it.row()][it.col()] = it.value();
247 for (
size_t i = 0; i < mat.
rows(); i++)
249 for (
size_t j = 0; j < mat.
cols(); j++)
251 os << matrix[i][j] <<
" ";
256 for (
int i = 0; i < mat.
rows(); i++)
269 template <
typename T,
typename indexT,
bool columnMajor>
272 return scalarMultiply(scalar);
276 template <
typename T,
typename indexT,
bool columnMajor>
277 void SparseMatrix<T, indexT, 2, columnMajor>::operator*=(T scalar)
279 return inPlaceScalarMultiply(scalar);
283 template <
typename T,
typename indexT,
bool columnMajor>
284 Eigen::VectorXd SparseMatrix<T, indexT, 2, columnMajor>::operator*(SparseMatrix<T, indexT, 2, columnMajor>::Vector &vec)
286 return vectorMultiply(vec);
290 template <
typename T,
typename indexT,
bool columnMajor>
291 Eigen::VectorXd SparseMatrix<T, indexT, 2, columnMajor>::operator*(Eigen::VectorXd &vec)
293 return vectorMultiply(vec);
297 template <
typename T,
typename indexT,
bool columnMajor>
298 Eigen::Matrix<T, -1, -1> SparseMatrix<T, indexT, 2, columnMajor>::operator*(Eigen::Matrix<T, -1, -1> mat)
300 return matrixMultiply(mat);
Definition: VCSC_SparseMatrix.hpp:22
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