Alexandria  2.27.0
SDC-CH common library for the Euclid project
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NdSamplerFromGrid.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef _FUNCTIONUTILS_NDSAMPLERFROMGRID_H
20 #define _FUNCTIONUTILS_NDSAMPLERFROMGRID_H
21 
23 #include "NdArray/NdArray.h"
24 #include "NdArray/Operations.h"
25 
26 namespace Euclid {
27 namespace MathUtils {
28 
29 template <typename Seq>
30 struct ExtractKnots {};
31 
32 template <std::size_t... Is>
33 struct ExtractKnots<Euclid::_index_sequence<Is...>> {
34  template <typename... Axes>
36  return std::tuple<std::vector<Axes>...>{{std::get<Is>(axes).begin(), std::get<Is>(axes).end()}...};
37  }
38 
39  template <typename... Axes>
41  return std::vector<std::size_t>{std::get<sizeof...(Is) - Is - 1>(axes).size()...};
42  }
43 };
44 
55 template <typename... Axes>
56 std::unique_ptr<NdSampler<Axes...>>
58  auto knots = ExtractKnots<Euclid::_make_index_sequence<sizeof...(Axes)>>::extract(grid.getAxesTuple());
59  auto pdf_shape = ExtractKnots<Euclid::_make_index_sequence<sizeof...(Axes)>>::extractShape(grid.getAxesTuple());
60  NdArray::NdArray<double> pdf(pdf_shape, grid.begin(), grid.end());
61  return Euclid::make_unique<NdSampler<Axes...>>(std::move(knots), std::move(pdf));
62 }
63 
82 template <typename CellAxis, typename... GridAxes>
83 std::unique_ptr<NdSampler<CellAxis, GridAxes...>>
85  const std::vector<CellAxis>& cell_axis) {
86  auto grid_knots = ExtractKnots<Euclid::_make_index_sequence<sizeof...(GridAxes)>>::extract(grid.getAxesTuple());
87  auto pdf_shape = ExtractKnots<Euclid::_make_index_sequence<sizeof...(GridAxes)>>::extractShape(grid.getAxesTuple());
88 
89  // Add one extra
90  pdf_shape.push_back(cell_axis.size());
91  auto knots = std::tuple_cat(std::make_tuple(cell_axis), grid_knots);
92 
93  // Build PDF considering the cell the last axis
94  NdArray::NdArray<double> pdf(pdf_shape);
95  std::size_t pdf_i = 0;
96  for (auto& cell : grid) {
97  assert(cell.size() == cell_axis.size());
98  for (auto& cv : cell) {
99  auto coords = NdArray::unravel_index(pdf_i++, pdf_shape);
100  pdf.at(coords) = cv;
101  }
102  }
103 
104  return Euclid::make_unique<NdSampler<CellAxis, GridAxes...>>(std::move(knots), std::move(pdf));
105 }
106 
115 template <typename... GridAxes>
116 std::unique_ptr<NdSampler<std::size_t, GridAxes...>>
118  auto& cell_ref = *grid.begin();
119  std::vector<std::size_t> cell_axis(cell_ref.size());
120  std::iota(cell_axis.begin(), cell_axis.end(), 0);
121  return createSamplerFromGrid(grid, cell_axis);
122 }
123 
124 } // namespace MathUtils
125 } // namespace Euclid
126 
127 #endif // _FUNCTIONUTILS_NDSAMPLERFROMGRID_H
T make_tuple(T...args)
Representation of a multi-dimensional grid which contains axis information.
Definition: GridContainer.h:97
T iota(T...args)
static std::vector< std::size_t > extractShape(const std::tuple< GridContainer::GridAxis< Axes >...> &axes)
Provides information related with an axis of a GridContainer.
Definition: GridAxis.h:49
std::vector< std::size_t > unravel_index(std::size_t index, const std::vector< std::size_t > &shape)
Definition: Operations.cpp:24
std::unique_ptr< NdSampler< Axes...> > createSamplerFromGrid(const GridContainer::GridContainer< std::vector< double >, Axes...> &grid)
T move(T...args)
T tuple_cat(T...args)
T size(T...args)
T & at(const std::vector< size_t > &coords)
STL class.
STL class.
typename _index_sequence_helper< N >::type _make_index_sequence
std::unique_ptr< T > make_unique(Args &&...args)
Constructs an object of type T and wraps it in a std::unique_ptr using args as the parameter list for...
Definition: memory_tools.h:42
static std::tuple< std::vector< Axes >...> extract(const std::tuple< GridContainer::GridAxis< Axes >...> &axes)