11 #include <Eigen/Eigen>
19 template <
typename T,
int M,
int N>
21 return matrix.rows() * matrix.cols();
25 template <
typename T,
int M,
int N>
27 return std::accumulate(vec.begin(), vec.end(),
size_t{0u},
28 [](
size_t so_far,
const Eigen::Matrix<T,M,N>& v) {
29 return so_far + static_cast<size_t>(v.rows()) * static_cast<size_t>(v.cols());
35 template <
typename T,
size_t Dims>
37 return std::accumulate(vec.origin(), vec.origin() + vec.num_elements(),
size_t{0u},
38 [](
size_t so_far,
const T& v) {
39 return so_far + static_cast<size_t>(v.rows()) * static_cast<size_t>(v.cols());
45 template <
typename T,
int M,
int N>
47 return std::accumulate(vec.begin(), vec.end(),
size_t{0u},
48 [](
size_t so_far,
const Eigen::Matrix<T,M,N>& v) {
49 return so_far + static_cast<size_t>(v.rows());
55 template <
typename T,
int M,
int N>
61 : _dims(space.getDimensions()) {
62 assert(_dims.size() == 2);
66 if (_dims[0] !=
static_cast<size_t>(array.rows()) ||
67 _dims[1] !=
static_cast<size_t>(array.cols())) {
68 array.resize(
static_cast<typename MatrixTMN::Index
>(_dims[0]),
69 static_cast<typename MatrixTMN::Index
>(_dims[1]));
84 template <
typename T,
int M,
int N>
86 const std::vector<size_t>& dims,
87 const size_t current_dim,
88 std::vector<T>& buffer) {
91 for (
const auto& k : vec) {
92 std::copy(k.data(), k.data() + k.size(), std::back_inserter(buffer));
97 template <
typename T,
int M,
int N>
103 : _dims(space.getDimensions()), _space(space) {
104 assert(_dims.size() == 2);
109 return _vec_align.data();
114 vectors_to_single_buffer<T, M, N>(vec, _dims, 0, _vec_align);
115 return _vec_align.data();
119 T* start = _vec_align.data();
120 if (vec.size() > 0) {
122 v = Eigen::Map<MatrixTMN>(start, v.rows(), v.cols());
123 start += v.rows()*v.cols();
126 else if (M == -1 || N == -1) {
127 std::ostringstream ss;
128 ss <<
"Dynamic size(-1) used without pre-defined vector data layout.\n"
129 <<
"Initiliaze vector elements using Zero, i.e.:\n"
130 <<
"\t vector<MatrixXd> vec(5, MatrixXd::Zero(20,5))";
134 for (
size_t i = 0; i < _dims[0] /
static_cast<size_t>(M); ++i) {
135 vec.emplace_back(Eigen::Map<MatrixTMN>(start, M, N));
147 template <
typename T,
int M,
int N, std::
size_t Dims>
148 struct data_converter<boost::multi_array<Eigen::Matrix<T, M, N>, Dims>, void> {
149 typedef typename boost::multi_array<Eigen::Matrix<T, M, N>, Dims> MultiArrayEigen;
154 assert(_dims.size() == Dims);
159 return _vec_align.data();
164 for (
auto e = array.origin(); e < array.origin() + array.num_elements(); ++e) {
165 std::copy(e->data(), e->data() + e->size(), std::back_inserter(_vec_align));
167 return _vec_align.data();
171 T* start = _vec_align.data();
172 if (M != -1 && N != -1) {
173 for (
auto v = vec.origin(); v < vec.origin() + vec.num_elements(); ++v) {
174 *v = Eigen::Map<Eigen::Matrix<T, M, N>>(start, v->rows(), v->cols());
175 start += v->rows() * v->cols();
178 if (vec.origin()->rows() > 0 && vec.origin()->cols() > 0) {
179 const auto VEC_M = vec.origin()->rows(), VEC_N = vec.origin()->cols();
180 for (
auto v = vec.origin(); v < vec.origin() + vec.num_elements(); ++v) {
181 assert(v->rows() == VEC_M && v->cols() == VEC_N);
182 *v = Eigen::Map<Eigen::Matrix<T, M, N>>(start, VEC_M, VEC_N);
183 start += VEC_M * VEC_N;
186 throw DataSetException(
187 "Dynamic size(-1) used without pre-defined multi_array data layout.\n"
188 "Initialize vector elements using MatrixXd::Zero");
193 std::vector<size_t> _dims;
195 std::vector<typename type_of_array<T>::type> _vec_align;
197 #endif // H5_USE_BOOST