Skip to content

Commit

Permalink
Support YAML parsing of complex matrix and vector
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Jul 16, 2024
1 parent e83e4e0 commit acb6f1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/DataStructures/DynamicMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

#include "DataStructures/DynamicMatrix.hpp"

#include <complex>

#include "Options/ParseOptions.hpp"
#include "Options/StdComplex.hpp"
#include "Utilities/GenerateInstantiations.hpp"

namespace DynamicMatrix_detail {
Expand All @@ -20,7 +23,7 @@ std::vector<std::vector<Type>> parse_to_vectors(
template std::vector<std::vector<TYPE(data)>> parse_to_vectors( \
const Options::Option& options);

GENERATE_INSTANTIATIONS(INSTANTIATE, (double))
GENERATE_INSTANTIATIONS(INSTANTIATE, (double, std::complex<double>))

#undef INSTANTIATE
#undef TYPE
Expand Down
5 changes: 4 additions & 1 deletion src/DataStructures/DynamicVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

#include "DataStructures/DynamicVector.hpp"

#include <complex>

#include "Options/Options.hpp"
#include "Options/ParseOptions.hpp"
#include "Options/StdComplex.hpp"
#include "Utilities/GenerateInstantiations.hpp"

namespace DynamicVector_detail {
Expand All @@ -20,7 +23,7 @@ std::vector<T> parse_to_vector(const Options::Option& options) {
template std::vector<TYPE(data)> parse_to_vector( \
const Options::Option& options);

GENERATE_INSTANTIATIONS(INSTANTIATE, (double))
GENERATE_INSTANTIATIONS(INSTANTIATE, (double, std::complex<double>))

#undef INSTANTIATE
#undef TYPE
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/DataStructures/Test_BlazeInteroperability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ SPECTRE_TEST_CASE("Unit.DataStructures.BlazeInteroperability",
set_number_of_grid_points(make_not_null(&v), 3_st);
CHECK(v.size() == 3);
}
{
INFO("Complex DynamicVector");
using namespace std::complex_literals;
test_serialization(blaze::DynamicVector<std::complex<double>>{
0. + 1.i, 1. + 2.i, 0. - 1.i, 2. + 3.i});
CHECK(make_with_value<blaze::DynamicVector<std::complex<double>>>(
blaze::DynamicVector<std::complex<double>>(3), 1.) ==
blaze::DynamicVector<std::complex<double>>(3, 1.));
CHECK(
TestHelpers::test_creation<blaze::DynamicVector<std::complex<double>>>(
"[[0., 1.], [1., 2.], [0., -1.], [2., 3.]]") ==
blaze::DynamicVector<std::complex<double>>{0. + 1.i, 1. + 2.i, 0. - 1.i,
2. + 3.i});

blaze::DynamicVector<std::complex<double>> v{0. + 1.i, 1. + 2.i, 0. - 1.i,
2. + 3.i};
set_number_of_grid_points(make_not_null(&v), 4_st);
CHECK(v == blaze::DynamicVector<std::complex<double>>{0. + 1.i, 1. + 2.i,
0. - 1.i, 2. + 3.i});
set_number_of_grid_points(make_not_null(&v), 3_st);
CHECK(v.size() == 3);
}
{
INFO("CompressedVector");
test_serialization(blaze::CompressedVector<double>{0., 1., 0., 2.});
Expand Down Expand Up @@ -107,6 +129,34 @@ SPECTRE_TEST_CASE("Unit.DataStructures.BlazeInteroperability",
write_csv(ss, matrix);
CHECK(ss.str() == "0,1,2\n3,0,4\n");
}
{
INFO("Complex DynamicMatrix");
using namespace std::complex_literals;
test_serialization(
blaze::DynamicMatrix<std::complex<double>, blaze::columnMajor>{
{0. + 1.i, 1. + 3.i, 2. + 2.i}, {3. + 4.i, 0. - 1.i, 4. + 5.i}});
test_serialization(
blaze::DynamicMatrix<std::complex<double>, blaze::rowMajor>{
{0. + 1.i, 1. + 3.i, 2. + 2.i}, {3. + 4.i, 0. - 1.i, 4. + 5.i}});
CHECK(TestHelpers::test_creation<
blaze::DynamicMatrix<std::complex<double>, blaze::columnMajor>>(
"[[[0., 1.], [1., 3.], [2., 2.]], [[3., 4.], [0., -1.], [4., "
"5.]]]") ==
blaze::DynamicMatrix<std::complex<double>, blaze::columnMajor>{
{0. + 1.i, 1. + 3.i, 2. + 2.i}, {3. + 4.i, 0. - 1.i, 4. + 5.i}});
CHECK(TestHelpers::test_creation<
blaze::DynamicMatrix<std::complex<double>, blaze::rowMajor>>(
"[[[0., 1.], [1., 3.], [2., 2]], [[3., 4.], [0., -1.], [4., "
"5.]]]") ==
blaze::DynamicMatrix<std::complex<double>, blaze::rowMajor>{
{0. + 1.i, 1. + 3.i, 2. + 2.i}, {3. + 4.i, 0. - 1.i, 4. + 5.i}});
// Test `write_csv`
std::stringstream ss{};
blaze::DynamicMatrix<std::complex<double>, blaze::columnMajor> matrix{
{{0. + 1.i, 1. + 3.i, 2. + 2.i}, {3. + 4.i, 0. - 1.i, 4. + 5.i}}};
write_csv(ss, matrix);
CHECK(ss.str() == "(0,1),(1,3),(2,2)\n(3,4),(0,-1),(4,5)\n");
}
{
INFO("CompressedMatrix");
test_serialization(blaze::CompressedMatrix<double, blaze::columnMajor>{
Expand Down

0 comments on commit acb6f1f

Please sign in to comment.