14 template <
typename T,
typename indexT,
bool columnMajor>
15 SparseMatrix<T, indexT, 2, columnMajor> &SparseMatrix<T, indexT, 2, columnMajor>::operator=(
21 if (values !=
nullptr) {
22 for (uint32_t i = 0; i < outerDim; i++) {
23 if (values[i] !=
nullptr) free(values[i]);
27 if (counts !=
nullptr) {
28 for (uint32_t i = 0; i < outerDim; i++) {
29 if (counts[i] !=
nullptr) free(counts[i]);
33 if (indices !=
nullptr) {
34 for (uint32_t i = 0; i < outerDim; i++) {
35 if (indices[i] !=
nullptr) free(indices[i]);
40 if (valueSizes !=
nullptr) {
43 if (indexSizes !=
nullptr) {
46 if (metadata !=
nullptr) {
51 numRows = other.numRows;
52 numCols = other.numCols;
53 outerDim = other.outerDim;
54 innerDim = other.innerDim;
56 compSize = other.compSize;
60 values = (T **)malloc(outerDim *
sizeof(T *));
61 counts = (indexT **)malloc(outerDim *
sizeof(indexT *));
62 indices = (indexT **)malloc(outerDim *
sizeof(indexT *));
64 valueSizes = (indexT *)malloc(outerDim *
sizeof(indexT));
65 indexSizes = (indexT *)malloc(outerDim *
sizeof(indexT));
67 metadata =
new uint32_t[NUM_META_DATA];
68 }
catch (std::bad_alloc &e) {
69 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix"
75 memcpy(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA);
79 index_t = other.index_t;
82 for (uint32_t i = 0; i < outerDim; i++) {
84 values[i] = (T *)malloc(other.valueSizes[i] *
sizeof(T));
85 counts[i] = (indexT *)malloc(other.valueSizes[i] *
sizeof(indexT));
86 indices[i] = (indexT *)malloc(other.indexSizes[i] *
sizeof(indexT));
87 }
catch (std::bad_alloc &e) {
88 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix"
94 memcpy(values[i], other.values[i],
sizeof(T) * other.valueSizes[i]);
95 memcpy(counts[i], other.counts[i],
sizeof(indexT) * other.valueSizes[i]);
96 memcpy(indices[i], other.indices[i],
97 sizeof(indexT) * other.indexSizes[i]);
99 valueSizes[i] = other.valueSizes[i];
100 indexSizes[i] = other.indexSizes[i];
110 template <
typename T,
typename indexT,
bool columnMajor>
111 bool SparseMatrix<T, indexT, 2, columnMajor>::operator==(
112 const SparseMatrix<T, indexT, 2, columnMajor> &other) {
115 if (memcmp(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA) != 0) {
120 for (uint32_t i = 0; i < outerDim; i++) {
121 if (memcmp(values[i], other.values[i],
sizeof(T) * valueSizes[i]) != 0) {
127 for (uint32_t i = 0; i < outerDim; i++) {
128 if (memcmp(indices[i], other.indices[i],
sizeof(indexT) * indexSizes[i]) !=
135 for (uint32_t i = 0; i < outerDim; i++) {
136 if (memcmp(counts[i], other.counts[i],
sizeof(indexT) * valueSizes[i]) !=
147 template <
typename T,
typename indexT,
bool columnMajor>
148 bool SparseMatrix<T, indexT, 2, columnMajor>::operator!=(
const SparseMatrix<T, indexT, 2, columnMajor> &other) {
149 return !(*
this == other);
153 template <
typename T,
typename indexT,
bool columnMajor>
154 T SparseMatrix<T, indexT, 2, columnMajor>::operator()(uint32_t row, uint32_t col) {
156 #ifdef IVSPARSE_DEBUG
158 assert((row < numRows && row >= 0) &&
"Row index out of bounds");
159 assert((col < numCols && col >= 0) &&
"Column index out of bounds");
162 #ifdef IVSPARSE_DEBUG
164 if (row >= numRows || col >= numCols) {
165 std::cerr <<
"Error: Index out of bounds" << std::endl;
170 uint32_t vector = columnMajor ? col : row;
171 uint32_t index = columnMajor ? row : col;
174 for (
typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator it(
177 if (it.getIndex() == (indexT)index) {
187 template <
typename T,
typename indexT,
bool columnMajor>
188 typename SparseMatrix<T, indexT, 2, columnMajor>::Vector SparseMatrix<T, indexT, 2, columnMajor>::operator[](uint32_t vec) {
190 #ifdef IVSPARSE_DEBUG
192 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
202 template <
typename T,
typename indexT,
bool columnMajor>
205 #ifndef IVSPARSE_DEBUG
206 if (mat.
cols() > 110) {
207 std::cout <<
"IVSparse matrix is too large to print" << std::endl;
214 T **matrix =
new T *[mat.
rows()];
215 for (
size_t i = 0; i < mat.
rows(); i++) {
216 matrix[i] = (T *)calloc(mat.
cols(),
sizeof(T));
220 for (
size_t i = 0; i < mat.
cols(); i++) {
222 columnMajor>::InnerIterator it(mat, i);
226 matrix[it.row()][it.col()] = it.value();
231 for (
size_t i = 0; i < mat.
rows(); i++) {
232 for (
size_t j = 0; j < mat.
cols(); j++) {
233 os << matrix[i][j] <<
" ";
238 for (
int i = 0; i < mat.
rows(); i++) {
250 template <
typename T,
typename indexT,
bool columnMajor>
252 return scalarMultiply(scalar);
256 template <
typename T,
typename indexT,
bool columnMajor>
void SparseMatrix<T, indexT, 2, columnMajor>::operator*=(T scalar) {
257 return inPlaceScalarMultiply(scalar);
269 template <
typename T,
typename indexT,
bool columnMajor>
270 Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, 2, columnMajor>::operator*(Eigen::Matrix<T, -1, 1> &vec) {
271 return vectorMultiply(vec);
275 template <
typename T,
typename indexT,
bool columnMajor>
276 Eigen::Matrix<T, -1, -1> SparseMatrix<T, indexT, 2, columnMajor>::operator*(Eigen::Matrix<T, -1, -1> mat) {
278 #ifdef IVSPARSE_DEBUG
280 if (mat.rows() != outerDim)
281 throw std::invalid_argument(
282 "The left matrix must have the same # of rows as columns in the right "
286 Eigen::Matrix<T, -1, -1> newMatrix = Eigen::Matrix<T, -1, -1>::Zero(mat.cols(), innerDim);
287 Eigen::Matrix<T, -1, -1> matTranspose = mat.transpose();
289 #ifdef IVSPARSE_HAS_OPENMP
290 #pragma omp parallel for
292 for (
int col = 0; col < outerDim; col++) {
293 for (
typename SparseMatrix<T, indexT, 2, columnMajor>::InnerIterator matIter(*
this, col);
294 matIter; ++matIter) {
295 newMatrix.col(matIter.row()) += matTranspose.col(col) * matIter.value();
298 return newMatrix.transpose();
Definition: VCSC_SparseMatrix.hpp:21
uint32_t cols() const
Definition: IVSparse_Base_Methods.hpp:30
uint32_t rows() const
Definition: IVSparse_Base_Methods.hpp:27
Definition: IVCSC_SparseMatrix.hpp:29