#include <Eigen/SparseCore>
#include <h5cpp/all>
#include <iostream>
#include <random>
#include <vector>
int main() {
using SpMat = Eigen::SparseMatrix<double>;
using SpVec = Eigen::SparseVector<double>;
const int rows = 10, cols = 15;
for (int j = 0; j < cols; ++j)
for (int i = 0; i < rows; ++i)
SpMat A(rows, cols);
A.setFromTriplets(trips.
begin(), trips.
end());
A.makeCompressed();
std::cout <<
"wrote SparseMatrix: " << A.rows() <<
"x" << A.cols()
<< " nnz=" << A.nonZeros() << "\n";
std::cout <<
"read SparseMatrix: " << A_back.rows() <<
"x" << A_back.cols()
<< " nnz=" << A_back.nonZeros() << "\n";
Eigen::MatrixXd diff = Eigen::MatrixXd(A) - Eigen::MatrixXd(A_back);
double err = diff.cwiseAbs().maxCoeff();
std::cout <<
"round-trip residual (max|A - A'|): " << err <<
"\n";
SpVec v(40);
v.insert(4) = 0.5; v.insert(11) = -3.14; v.insert(33) = 8.0;
std::cout <<
"vec round-trip: nnz=" << v_back.nonZeros()
<< ", v.coeff(11)=" << v_back.coeff(11) << " (expected -3.14)\n";
return (err == 0.0) ? 0 : 1;
}
T emplace_back(T... args)
h5::at_t create(const hid_t &parent, const std::string &path, args_t &&... args)
Create a new attribute of element type T on a parent HDF5 object.
Definition H5Acreate.hpp:100
T aread(const hid_t &ds, const std::string &name, const h5::acpl_t &acpl=h5::default_acpl)
Read an attribute by name and return its value as type T.
Definition H5Aread.hpp:76
h5::gr_t write(const LOC &parent, const std::string &path, const T &src)
Write a sparse matrix or vector as a CSC group.
Definition H5Dsparse.hpp:185