Skip to content

Commit

Permalink
Add examples for Eigen.
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc committed Feb 19, 2024
1 parent 97d7120 commit 5ffe169
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ set(boost_examples
${CMAKE_CURRENT_SOURCE_DIR}/boost_ublas_double.cpp
)

set(eigen_examples
${CMAKE_CURRENT_SOURCE_DIR}/eigen_matrix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/eigen_vector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/eigen_map.cpp
)

set(hl_hdf5_examples
${CMAKE_CURRENT_SOURCE_DIR}/hl_hdf5_inmemory_files.cpp
)
Expand Down Expand Up @@ -70,6 +76,12 @@ if(HIGHFIVE_TEST_BOOST)
endforeach()
endif()

if(HIGHFIVE_TEST_EIGEN)
foreach(example_source ${eigen_examples})
compile_example(${example_source} HighFiveEigenDependency)
endforeach()
endif()

if(HDF5_IS_PARALLEL)
foreach(example_source ${parallel_hdf5_examples})
compile_example(${example_source})
Expand Down
48 changes: 48 additions & 0 deletions src/examples/eigen_map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <highfive/highfive.hpp>
#include <highfive/eigen.hpp>

// Example showing reading and writing of `Eigen::Map`. Using
// `Map<Matrix, ...>` as an example, but `Map<Array, ...>` works
// analogously.
//
// Both `Eigen::Matrix` and `Eigen::Vector` have their own examples.

int main() {
HighFive::File file("eigen_map.h5", HighFive::File::Truncate);

// Somehow allocate some memory:
double* p1 = (double*) malloc(4*3 * sizeof(double));

Eigen::Map<Eigen::MatrixXd> A(p1, 4, 3);

// clang-format off
A << 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12;
// clang-format on
std::cout << "A = \n" << A << "\n\n";

// Write it to the file:
file.createDataSet("mat", A);

// ... and read it back as fixed-size and row-major:
using Matrix43d = Eigen::Matrix<double, 4, 3, Eigen::RowMajor>;

// Again, memory was obtain somehow, and we create an `Eigen::Map`
// from it:
double* p2 = (double*) malloc(4*3 * sizeof(double));
Eigen::Map<Matrix43d> B(p2, 4, 3);

// Since, we've pre-allocated the memory, we use the overload of `read`
// accepts `B` and an argument. Note, this will throw if `B` needs to be
// resized, because a map shouldn't resize the underlying memory:
file.getDataSet("mat").read(B);

std::cout << "B = \n" << B << "\n";

free(p1);
free(p2);

return 0;
}
33 changes: 33 additions & 0 deletions src/examples/eigen_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <highfive/highfive.hpp>
#include <highfive/eigen.hpp>

// Example showing reading and writing of `Eigen::Matrix`. Using
// `Eigen::Matrix` as an example, but `Eigen::Array` works analogously.
//
// Both `Eigen::Vector` and `Eigen::Map` have their own examples.

int main() {
HighFive::File file("eigen_matrix.h5", HighFive::File::Truncate);

// Create a matrix.
Eigen::MatrixXd A(4, 3);
// clang-format off
A << 1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12;
// clang-format on
//
std::cout << "A = \n" << A << "\n\n";

// Write it to the file:
file.createDataSet("mat", A);

// ... and read it back as fixed-size and row-major:
using Matrix43d = Eigen::Matrix<double, 4, 3, Eigen::RowMajor>;
auto B = file.getDataSet("mat").read<Matrix43d>();

std::cout << "B = \n" << B << "\n";

return 0;
}
33 changes: 33 additions & 0 deletions src/examples/eigen_vector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <highfive/highfive.hpp>
#include <highfive/eigen.hpp>

// Example showing reading and writing of `Eigen::Matrix`. Using
// `Eigen::Matrix` as an example, but `Eigen::Array` works analogously.
//
// Both `Eigen::Vector` and `Eigen::Map` have their own examples.

int main() {
HighFive::File file("eigen_vector.h5", HighFive::File::Truncate);

// Create a matrix.
Eigen::VectorXd v(3);
v << 1, 2, 3;
std::cout << "v = \n" << v << "\n\n";

// Write it to the file:
file.createDataSet("col_vec", v);

// The twist is that Eigen typedefs:
// using VectorXd = Matrix<double, Dynamic, 1>;
//
// Therefore, for HighFive it's indistinguishable from a Nx1 matrix. Since,
// Eigen distinguishes row and column vectors, the HighFive chooses to
// respect the distinction and deduces the shape of vector as Nx1.

// ... and read it back as fixed-size:
auto w = file.getDataSet("col_vec").read<Eigen::Vector3d>();

std::cout << "w = \n" << w << "\n";

return 0;
}

0 comments on commit 5ffe169

Please sign in to comment.