16 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
20 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
24 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
28 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
32 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
34 if (data[vec] == endPointers[vec]) {
return 0; }
35 return (
char *)endPointers[vec] - (
char *)data[vec];
41 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
44 FILE *fp = fopen(filename,
"wb");
47 fwrite(metadata, 1, NUM_META_DATA *
sizeof(uint32_t), fp);
50 for (uint32_t i = 0; i < outerDim; i++) {
51 uint64_t size = (uint8_t *)endPointers[i] - (uint8_t *)data[i];
52 fwrite(&size, 1,
sizeof(uint64_t), fp);
56 for (uint32_t i = 0; i < outerDim; i++) { fwrite(data[i], 1, (
char *)endPointers[i] - (
char *)data[i], fp); }
63 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
65 std::cout << std::endl;
66 std::cout <<
"IVSparse Matrix" << std::endl;
69 if (numRows < 100 && numCols < 100) {
71 for (uint32_t i = 0; i < numRows; i++) {
72 for (uint32_t j = 0; j < numCols; j++) {
73 std::cout << coeff(i, j) <<
" ";
75 std::cout << std::endl;
77 }
else if (numRows > 100 && numCols > 100) {
79 for (uint32_t i = 0; i < 100; i++) {
80 for (uint32_t j = 0; j < 100; j++) {
81 std::cout << coeff(i, j) <<
" ";
83 std::cout << std::endl;
87 std::cout << std::endl;
91 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
94 Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> eigenMatrix(numRows, numCols);
97 for (uint32_t i = 0; i < outerDim; ++i) {
100 eigenMatrix.insert(it.row(), it.col()) = it.value();
105 eigenMatrix.makeCompressed();
115 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
119 if (compressionLevel == 2) {
return *
this; }
124 T *values = (T *)malloc(nnz *
sizeof(T));
125 indexT *indices = (indexT *)malloc(nnz *
sizeof(indexT));
126 indexT *colPtrs = (indexT *)malloc((outerDim + 1) *
sizeof(indexT));
131 std::map<indexT, T> dict[outerDim];
134 for (uint32_t i = 0; i < outerDim; ++i) {
138 dict[i][it.getIndex()] = it.value();
142 colPtrs[i + 1] = colPtrs[i] + count;
148 for (uint32_t i = 0; i < outerDim; ++i) {
149 for (
auto &pair : dict[i]) {
150 values[count] = pair.second;
151 indices[count] = pair.first;
168 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
173 assert(outerDim > 0 &&
"Cannot convert an empty matrix to an Eigen matrix!");
177 Eigen::SparseMatrix<T, columnMajor ? Eigen::ColMajor : Eigen::RowMajor> eigenMatrix(numRows, numCols);
180 for (uint32_t i = 0; i < outerDim; ++i) {
183 if (data[i] ==
nullptr) {
continue; }
187 eigenMatrix.insert(it.row(), it.col()) = it.value();
192 eigenMatrix.makeCompressed();
201 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
207 if (vec.
begin() == vec.
end()) [[unlikely]] {
209 if (columnMajor) { numCols++; }
214 metadata[2] = outerDim;
218 data = (
void **)realloc(data, outerDim *
sizeof(
void *));
219 endPointers = (
void **)realloc(endPointers, outerDim *
sizeof(
void *));
220 }
catch (std::bad_alloc &e) {
221 throw std::bad_alloc();
225 data[outerDim - 1] =
nullptr;
226 endPointers[outerDim - 1] =
nullptr;
235 throw std::invalid_argument(
"The vector must be the same size as the outer dimension of the matrix!");
239 if (columnMajor) { numCols++; }
243 metadata[2] = outerDim;
248 data = (
void **)realloc(data, outerDim *
sizeof(
void *));
249 endPointers = (
void **)realloc(endPointers, outerDim *
sizeof(
void *));
250 }
catch (std::bad_alloc &e) {
throw std::bad_alloc(); }
254 data[outerDim - 1] = malloc(vec.
byteSize());
255 endPointers[outerDim - 1] = (
char *)data[outerDim - 1] + vec.
byteSize();
256 }
catch (std::bad_alloc &e) {
throw std::bad_alloc(); }
268 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
271 std::unordered_map<T, std::vector<indexT>> mapsT[innerDim];
274 for (uint32_t i = 0; i < outerDim; ++i) {
278 if constexpr (columnMajor) { mapsT[it.row()][it.value()].push_back(it.col()); }
279 else { mapsT[it.col()][it.value()].push_back(it.row()); }
283 for (
auto &row : mapsT) {
284 for (
auto &col : row) {
287 size_t max = col.second[0];
290 for (uint32_t i = col.second.size() - 1; i > 0; --i) {
291 col.second[i] -= col.second[i - 1];
292 if ((
size_t)col.second[i] > max)
296 max = byteWidth(max);
298 col.second.push_back(max);
310 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
314 std::unordered_map<T, std::vector<indexT>> mapsT[innerDim];
317 for (uint32_t i = 0; i < outerDim; ++i) {
320 if constexpr (columnMajor) { mapsT[it.row()][it.value()].push_back(it.col()); }
321 else { mapsT[it.col()][it.value()].push_back(it.row()); }
326 for (
auto &row : mapsT) {
327 for (
auto &col : row) {
330 size_t max = col.second[0];
333 for (uint32_t i = col.second.size() - 1; i > 0; --i) {
334 col.second[i] -= col.second[i - 1];
335 if ((
size_t)col.second[i] > max)
339 max = byteWidth(max);
341 col.second.push_back(max);
349 template <
typename T,
typename indexT, u
int8_t compressionLevel,
bool columnMajor>
353 assert(start < outerDim && end <= outerDim && start < end &&
"Invalid start and end values!");
356 std::vector<typename IVSparse::SparseMatrix<T, indexT, compressionLevel, columnMajor>::Vector> vecs(end - start);
359 for (uint32_t i = start; i < end; ++i) {
364 vecs[i - start] = temp;
Definition: IVCSC_Iterator.hpp:25
Definition: IVCSC_Vector.hpp:27
uint32_t getLength()
Definition: IVCSC_Vector_Methods.hpp:183
void * end()
Definition: IVCSC_Vector_Methods.hpp:171
size_t byteSize()
Definition: IVCSC_Vector_Methods.hpp:175
uint32_t nonZeros()
Definition: IVCSC_Vector_Methods.hpp:163
void * begin()
Definition: IVCSC_Vector_Methods.hpp:167
Definition: CSC_SparseMatrix.hpp:24
Definition: VCSC_SparseMatrix.hpp:22
Definition: IVCSC_SparseMatrix.hpp:27
IVSparse::SparseMatrix< T, indexT, 2, columnMajor > toVCSC()
Definition: IVCSC_Methods.hpp:116
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector getVector(uint32_t vec)
Definition: IVCSC_Methods.hpp:29
IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor > transpose()
Definition: IVCSC_Methods.hpp:269
void append(typename SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector &vec)
Definition: IVCSC_Methods.hpp:202
IVSparse::SparseMatrix< T, indexT, 1, columnMajor > toCSC()
Definition: IVCSC_Methods.hpp:92
T coeff(uint32_t row, uint32_t col)
Definition: IVCSC_Methods.hpp:17
void write(const char *filename)
Definition: IVCSC_Methods.hpp:42
void print()
Definition: IVCSC_Methods.hpp:64
Eigen::SparseMatrix< T, columnMajor ? Eigen::ColMajor :Eigen::RowMajor > toEigen()
Definition: IVCSC_Methods.hpp:169
bool isColumnMajor() const
Definition: IVCSC_Methods.hpp:21
void * vectorPointer(uint32_t vec)
Definition: IVCSC_Methods.hpp:25
std::vector< typename IVSparse::SparseMatrix< T, indexT, compressionLevel, columnMajor >::Vector > slice(uint32_t start, uint32_t end)
Definition: IVCSC_Methods.hpp:350
void inPlaceTranspose()
Definition: IVCSC_Methods.hpp:311
size_t getVectorSize(uint32_t vec) const
Definition: IVCSC_Methods.hpp:33