Skip to content

Commit

Permalink
Write blaze::DynamicMatrix to CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Oct 11, 2023
1 parent 4436792 commit b1ca770
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/DataStructures/DynamicMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ struct Options::create_from_yaml<blaze::DynamicMatrix<Type, SO, Alloc, Tag>> {
return result;
}
};

/// Write a `blaze::DynamicMatrix` to a CSV file.
template <typename Type, bool SO, typename Alloc, typename Tag>
std::ostream& write_csv(
std::ostream& os, const blaze::DynamicMatrix<Type, SO, Alloc, Tag>& matrix,
const std::string& delimiter = ",") {
for (size_t i = 0; i < matrix.rows(); ++i) {
for (size_t j = 0; j < matrix.columns(); ++j) {
os << matrix(i, j);
if (j + 1 != matrix.columns()) {
os << delimiter;
}
}
os << '\n';
}
return os;
}
6 changes: 6 additions & 0 deletions tests/Unit/DataStructures/Test_BlazeInteroperability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ SPECTRE_TEST_CASE("Unit.DataStructures.BlazeInteroperability",
"[[0., 1., 2.], [3., 0., 4.]]") ==
blaze::DynamicMatrix<double, blaze::rowMajor>{{0., 1., 2.},
{3., 0., 4.}});
// Test `write_csv`
std::stringstream ss{};
blaze::DynamicMatrix<double, blaze::columnMajor> matrix{{0., 1., 2.},
{3., 0., 4.}};
write_csv(ss, matrix);
CHECK(ss.str() == "0,1,2\n3,0,4\n");
}
{
INFO("CompressedMatrix");
Expand Down

0 comments on commit b1ca770

Please sign in to comment.