14 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
15 SparseMatrix<T, indexT, compressionLevel, columnMajor> &
16 SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator=(
21 if (data !=
nullptr) {
22 for (uint32_t i = 0; i < outerDim; i++) {
23 if (data[i] !=
nullptr) {
29 if (endPointers !=
nullptr) {
32 if (metadata !=
nullptr) {
37 numRows = other.numRows;
38 numCols = other.numCols;
39 outerDim = other.outerDim;
40 innerDim = other.innerDim;
42 compSize = other.compSize;
46 data = (
void **)malloc(outerDim *
sizeof(
void *));
47 endPointers = (
void **)malloc(outerDim *
sizeof(
void *));
48 metadata =
new uint32_t[NUM_META_DATA];
49 }
catch (std::bad_alloc &e) {
50 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix"
56 memcpy(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA);
60 index_t = other.index_t;
63 for (uint32_t i = 0; i < outerDim; i++) {
65 if (other.data[i] ==
nullptr) {
67 endPointers[i] =
nullptr;
73 }
catch (std::bad_alloc &e) {
74 std::cerr <<
"Error: Could not allocate memory for IVSparse matrix"
87 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
88 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator==(
89 const SparseMatrix<T, indexT, compressionLevel, columnMajor> &other) {
94 if (memcmp(metadata, other.metadata,
sizeof(uint32_t) * NUM_META_DATA) != 0)
98 for (uint32_t i = 0; i < outerDim; i++) {
99 if (memcmp(data[i], other.data[i], getVectorSize(i)) != 0)
return false;
106 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
107 bool SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator!=(
108 const SparseMatrix<T, indexT, compressionLevel, columnMajor> &other) {
110 return !(*
this == other);
114 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
115 T SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator()(uint32_t row, uint32_t col) {
116 #ifdef IVSPARSE_DEBUG
118 if (row >= numRows || col >= numCols) {
119 std::cerr <<
"Error: Index out of bounds" << std::endl;
124 uint32_t vector = columnMajor ? col : row;
125 uint32_t index = columnMajor ? row : col;
128 if (data[vector] ==
nullptr)
return 0;
131 for (
typename SparseMatrix<T, indexT, compressionLevel,
132 columnMajor>::InnerIterator it(*
this, vector);
134 if (it.getIndex() == (indexT)index) {
144 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
145 typename SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector
146 SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator[](uint32_t vec) {
148 #ifdef IVSPARSE_DEBUG
150 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
160 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
163 #ifndef IVSPARSE_DEBUG
164 if (mat.
cols() > 110) {
165 std::cout <<
"IVSparse matrix is too large to print" << std::endl;
172 T **matrix =
new T *[mat.
rows()];
173 for (
size_t i = 0; i < mat.
rows(); i++) {
174 matrix[i] = (T *)calloc(mat.
cols(),
sizeof(T));
178 for (
size_t i = 0; i < mat.
cols(); i++) {
180 columnMajor>::InnerIterator it(mat, i);
184 matrix[it.row()][it.col()] = it.value();
192 for (
size_t i = 0; i < mat.
rows(); i++) {
193 for (
size_t j = 0; j < mat.
cols(); j++) {
194 os << matrix[i][j] <<
" ";
199 for (
int i = 0; i < mat.
rows(); i++) {
210 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
212 return scalarMultiply(scalar);
216 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
217 void SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*=(T scalar) {
218 return inPlaceScalarMultiply(scalar);
222 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
223 Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(
224 SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector &vec) {
226 return vectorMultiply(vec);
230 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
231 Eigen::Matrix<T, -1, 1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(
232 Eigen::Matrix<T, -1, 1> vec) {
234 return vectorMultiply(vec);
238 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
239 Eigen::Matrix<T, -1, -1> SparseMatrix<T, indexT, compressionLevel, columnMajor>::operator*(Eigen::Matrix<T, -1, -1> mat) {
241 #ifdef IVSPARSE_DEBUG
243 if (mat.rows() != outerDim)
244 throw std::invalid_argument(
245 "The left matrix must have the same # of rows as columns in the right "
249 Eigen::Matrix<T, -1, -1> newMatrix = Eigen::Matrix<T, -1, -1>::Zero(mat.cols(), innerDim);
250 Eigen::Matrix<T, -1, -1> matTranspose = mat.transpose();
252 #ifdef IVSPARSE_HAS_OPENMP
253 #pragma omp parallel for
255 for (
int col = 0; col < outerDim; col++) {
256 for (
typename SparseMatrix<T, indexT, 3, columnMajor>::InnerIterator
258 matIter; ++matIter) {
259 newMatrix.col(matIter.row()) += matTranspose.col(col) * matIter.value();
262 return newMatrix.transpose();
Definition: IVCSC_Vector.hpp:25
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
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:57