-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |