16 template <
typename T,
typename indexT,
bool columnMajor>
20 assert((row < numRows && col < numCols) &&
"Row or Column out of bounds");
21 assert((row >= 0 && col >= 0) &&
"Row or Column out of bounds");
24 return (*
this)(row, col);
28 template <
typename T,
typename indexT,
bool columnMajor>
34 template <
typename T,
typename indexT,
bool columnMajor>
38 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
45 template <
typename T,
typename indexT,
bool columnMajor>
49 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
56 template <
typename T,
typename indexT,
bool columnMajor>
62 template <
typename T,
typename indexT,
bool columnMajor>
67 assert((vec < outerDim && vec >= 0) &&
"Vector index out of bounds");
78 template <
typename T,
typename indexT,
bool columnMajor>
81 FILE *fp = fopen(filename,
"wb");
85 assert(fp != NULL &&
"File could not be opened!");
89 fwrite(metadata,
sizeof(uint32_t), NUM_META_DATA, fp);
92 fwrite(vals,
sizeof(T), nnz, fp);
95 fwrite(innerIdx,
sizeof(indexT), nnz, fp);
98 fwrite(outerPtr,
sizeof(indexT), outerDim + 1, fp);
105 template <
typename T,
typename indexT,
bool columnMajor>
107 std::cout << std::endl;
108 std::cout <<
"IVSparse Matrix" << std::endl;
111 if (numRows < 100 && numCols < 100) {
113 for (uint32_t i = 0; i < numRows; i++) {
114 for (uint32_t j = 0; j < numCols; j++) {
115 std::cout <<
coeff(i, j) <<
" ";
117 std::cout << std::endl;
119 }
else if (numRows >= 100 && numCols >= 100) {
121 for (uint32_t i = 0; i < 100; i++) {
122 for (uint32_t j = 0; j < 100; j++) {
123 std::cout <<
coeff(i, j) <<
" ";
125 std::cout << std::endl;
131 template <
typename T,
typename indexT,
bool columnMajor>
136 vals, innerIdx, outerPtr, numRows, numCols, nnz);
143 template <
typename T,
typename indexT,
bool columnMajor>
148 vals, innerIdx, outerPtr, numRows, numCols, nnz);
155 template <
typename T,
typename indexT,
bool columnMajor>
156 Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor>
160 Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> eigenMat(numRows, numCols);
168 for (uint32_t i = 0; i < outerDim; i++) {
169 for (indexT j = outerPtr[i]; j < outerPtr[i + 1]; j++) {
170 eigenMat.insert(innerIdx[j], i) = vals[j];
181 template <
typename T,
typename indexT,
bool columnMajor>
184 Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
187 eigenMat = eigenMat.transpose();
197 template <
typename T,
typename indexT,
bool columnMajor>
200 Eigen::SparseMatrix<T> eigenMat = (*this).toEigen();
203 eigenMat = eigenMat.transpose();
210 template <
typename T,
typename indexT,
bool columnMajor>
214 #ifdef IVSPARSE_DEBUG
215 assert((columnMajor && vec.size() == numRows) ||
216 (!columnMajor && vec.size() == numCols) &&
217 "Vector is not the correct size!");
228 metadata[2] = outerDim;
233 vals = (T *)realloc(vals,
sizeof(T) * nnz);
234 innerIdx = (indexT *)realloc(innerIdx,
sizeof(indexT) * nnz);
235 outerPtr = (indexT *)realloc(outerPtr,
sizeof(indexT) * (outerDim + 1));
236 }
catch (std::bad_alloc &ba) {
237 std::cerr <<
"Error: " << ba.what() << std::endl;
242 memcpy(vals + (nnz - vec.
nonZeros()), vec.getValues(),
244 memcpy(innerIdx + (nnz - vec.
nonZeros()), vec.getInnerIndices(),
248 outerPtr[outerDim] = nnz;
252 outerPtr[outerDim - 1] = nnz;
261 template <
typename T,
typename indexT,
bool columnMajor>
262 std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector>
266 #ifdef IVSPARSE_DEBUG
267 assert(start < outerDim && end <= outerDim && start < end &&
268 "Invalid start and end values!");
272 std::vector<typename IVSparse::SparseMatrix<T, indexT, 1, columnMajor>::Vector>vecs(end - start);
275 for (uint32_t i = start; i < end; ++i) {
280 vecs[i - start] = temp;
Definition: IVCSC_Vector.hpp:25
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:165
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:21
Definition: IVCSC_SparseMatrix.hpp:29
IVSparse::SparseMatrix< T, indexT, 2, columnMajor > toVCSC()
Definition: IVCSC_Methods.hpp:153
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector getVector(uint32_t vec)
Definition: IVCSC_Methods.hpp:47
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor > transpose()
Definition: IVCSC_Methods.hpp:329
void append(typename SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector &vec)
Definition: IVCSC_Methods.hpp:245
T coeff(uint32_t row, uint32_t col)
Definition: IVCSC_Methods.hpp:17
void write(const char *filename)
Definition: IVCSC_Methods.hpp:72
void print()
Definition: IVCSC_Methods.hpp:97
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:210
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:30
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:416
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:374