MUQ  0.4.3
BlockOperations.cpp
Go to the documentation of this file.
2 
3 
4 int main()
5 {
6  // Open the HDF5 file for reading and writing
7  auto f = muq::Utilities::OpenFile("Data.h5");
8 
9  // Store a vector in the group
10  Eigen::VectorXd baseVector = Eigen::VectorXd::LinSpaced(11,0,10);
11  f["/Vector"] = baseVector;
12 
13  // Now, extract the first two components of the dataset
14  Eigen::VectorXd tempVector = f["/Vector"].head(2);
15 
16  std::cout << tempVector.transpose() << "\nvs\n"
17  << baseVector.head(2).transpose() << std::endl << std::endl;
18 
19  // Extract a length 3 segment from the middle of the dataset, staring at index 2.
20  tempVector = f["/Vector"].segment(2,3);
21 
22  std::cout << tempVector.transpose() << "\nvs\n"
23  << baseVector.segment(2,3).transpose() << std::endl << std::endl;
24 
25  // Insert a vector into the middle of the dataset
26  std::cout << "Old vector = "
27  << f["/Vector"].eval().transpose() << std::endl;
28 
29  f["/Vector"].segment(3,2) = Eigen::VectorXd::Zero(2).eval();
30 
31  std::cout << "New vector = "
32  << f["/Vector"].eval().transpose() << std::endl << std::endl;
33 
34 
35  // Create a matrix dataset from the outer product of the [0,1,2...] vector
36  f["/Matrix"] = (baseVector*baseVector.transpose()).eval();
37 
38  // Grab a block in the middle of matrix
39  std::cout << "4x5 matrix block = \n" << f["/Matrix"].block(2,3,4,5).eval() << std::endl;
40 
41  // It is also possible to use all of the other Eigen block operations (e.g., .col(), .row(), .bottomLeftCorner(), .topRows(), .leftCols(), etc....)
42 
43  return 0;
44 }
int main()
H5Object OpenFile(std::string const &filename)
Open an HDF5 file and return the root object.
Definition: H5Object.cpp:321