14 template <
typename T,
typename indexT,
bool columnMajor>
21 if (vals !=
nullptr) { free(vals); }
22 if (innerIdx !=
nullptr) { free(innerIdx); }
23 if (outerPtr !=
nullptr) {free(outerPtr);}
24 if (metadata !=
nullptr) {
delete[] metadata;}
27 metadata =
new uint32_t[NUM_META_DATA];
28 memcpy(metadata, other.metadata, NUM_META_DATA *
sizeof(uint32_t));
31 numRows = other.numRows;
32 numCols = other.numCols;
33 outerDim = other.outerDim;
34 innerDim = other.innerDim;
36 compSize = other.compSize;
40 index_t =
sizeof(indexT);
51 vals = (T *)malloc(nnz *
sizeof(T));
52 innerIdx = (indexT *)malloc(nnz *
sizeof(indexT));
53 outerPtr = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
54 }
catch (std::bad_alloc &e) {
55 std::cerr <<
"Error: Failed to allocate memory for the matrix" << std::endl;
60 memcpy(vals, other.vals, nnz *
sizeof(T));
61 memcpy(innerIdx, other.innerIdx, nnz *
sizeof(indexT));
62 memcpy(outerPtr, other.outerPtr, (outerDim + 1) *
sizeof(indexT));
70 template <
typename T,
typename indexT,
bool columnMajor>
71 bool SparseMatrix<T, indexT, 1, columnMajor>::operator==(
const SparseMatrix<T, indexT, 1, columnMajor> &other) {
73 if (numRows != other.numRows || numCols != other.numCols) {
return false; }
76 if (nnz != other.nnz) {
return false; }
79 if (memcmp(vals, other.vals, nnz *
sizeof(T)) != 0) {
return false; }
80 if (memcmp(innerIdx, other.innerIdx, nnz *
sizeof(indexT)) != 0) {
return false; }
81 if (memcmp(outerPtr, other.outerPtr, (outerDim + 1) *
sizeof(indexT)) != 0) {
return false; }
88 template <
typename T,
typename indexT,
bool columnMajor>
89 bool SparseMatrix<T, indexT, 1, columnMajor>::operator!=(
const SparseMatrix<T, indexT, 1, columnMajor> &other) {
90 return !(*
this == other);
94 template <
typename T,
typename indexT,
bool columnMajor>
95 T SparseMatrix<T, indexT, 1, columnMajor>::operator()(uint32_t row, uint32_t col)
99 if (row >= numRows || col >= numCols) {
100 std::cerr <<
"Error: Index out of bounds" << std::endl;
105 uint32_t vector = columnMajor ? col : row;
106 uint32_t index = columnMajor ? row : col;
109 for (
typename SparseMatrix<T, indexT, 1, columnMajor>::InnerIterator it(*
this, vector); it; ++it) {
110 if (it.getIndex() == (indexT)index) {
121 template <
typename T,
typename indexT,
bool columnMajor>
122 typename SparseMatrix<T, indexT, 1, columnMajor>::Vector SparseMatrix<T, indexT, 1, columnMajor>::operator[](uint32_t vec) {
126 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
137 template <
typename T,
typename indexT,
bool columnMajor>
139 return scalarMultiply(scalar);
143 template <
typename T,
typename indexT,
bool columnMajor>
144 void SparseMatrix<T, indexT, 1, columnMajor>::operator*=(T scalar) {
145 return inPlaceScalarMultiply(scalar);
149 template <
typename T,
typename indexT,
bool columnMajor>
150 Eigen::VectorXd SparseMatrix<T, indexT, 1, columnMajor>::operator*(SparseMatrix<T, indexT, 1, columnMajor>::Vector &vec) {
151 return vectorMultiply(vec);
155 template <
typename T,
typename indexT,
bool columnMajor>
156 Eigen::VectorXd SparseMatrix<T, indexT, 1, columnMajor>::operator*(Eigen::VectorXd &vec) {
157 return vectorMultiply(vec);
161 template <
typename T,
typename indexT,
bool columnMajor>
162 Eigen::Matrix<T, -1, -1> SparseMatrix<T, indexT, 1, columnMajor>::operator*(Eigen::Matrix<T, -1, -1> mat) {
163 return matrixMultiply(mat);
Definition: CSC_SparseMatrix.hpp:24
Definition: IVCSC_SparseMatrix.hpp:27