1 #ifndef _theplu_yat_utility_merge_
2 #define _theplu_yat_utility_merge_
25 #include "concept_check.h"
27 #include "MatrixWeighted.h"
30 #include "yat/statistics/Average.h"
32 #include <boost/concept_check.hpp>
33 #include <boost/iterator/permutation_iterator.hpp>
65 template<
class Container2D>
66 void merge(
const Container2D& x, std::vector<std::string>& labels, Matrix& y);
102 template<
class Container2D,
class Functor>
103 void merge(
const Container2D& x, std::vector<std::string>& labels, Matrix& y,
152 template<
class Container2D,
class Functor1,
class Functor2>
153 void merge(
const Container2D& x, std::vector<std::string>& labels,
154 MatrixWeighted& y, Functor1 data_func, Functor2 weight_func);
164 template<
typename Iterator,
class Functor1,
class Functor2>
165 void assign(
double& x, Iterator first, Iterator last, Functor1 func1,
173 template<
typename Iterator,
class Functor1,
class Functor2>
174 void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
177 void merge_labels(std::vector<std::string>&,
178 std::map<std::string, std::vector<size_t> >&);
181 template<
class Container2D,
class MutableContainer2D,
182 class Functor1,
class Functor2>
183 void merge(
const Container2D& x,
184 std::map<std::string, std::vector<size_t> >& label2index,
185 MutableContainer2D& result, Functor1 func1, Functor2 func2);
192 template<
class Container2D>
199 template<
class Container2D,
class Functor>
201 Matrix& result, Functor func)
204 std::map<std::string, std::vector<size_t> > label2index;
205 detail::merge_labels(labels, label2index);
206 detail::merge(x, label2index, result, func, func);
210 template<
class Container2D,
class Functor1,
class Functor2>
215 std::map<std::string, std::vector<size_t> > label2index;
216 detail::merge_labels(labels, label2index);
217 detail::merge(x, label2index, result, func1, func2);
223 template<
typename Iterator,
class Functor1,
class Functor2>
224 void assign(
double& x, Iterator first, Iterator last, Functor1 func1,
227 x = func1(first, last);
231 template<
typename Iterator,
class Functor1,
class Functor2>
232 void assign(DataWeight& x, Iterator first, Iterator last, Functor1 func1,
235 x.data() = func1(first, last);
236 x.weight() = func2(first, last);
240 void merge_labels(std::vector<std::string>& labels,
241 std::map<std::string, std::vector<size_t> >& label2index)
243 inverse(labels.begin(), labels.end(), label2index);
244 labels.resize(label2index.size());
251 template<
class Container2D,
class MutableContainer2D,
252 class Functor1,
class Functor2>
253 void merge(
const Container2D& x,
254 std::map<std::string, std::vector<size_t> >& label2index,
255 MutableContainer2D& result, Functor1 func1, Functor2 func2)
257 BOOST_CONCEPT_ASSERT((utility::Mutable_Container2D<MutableContainer2D>));
258 BOOST_CONCEPT_ASSERT((utility::Container2D<Container2D>));
259 result.resize(label2index.size(), x.columns());
260 typedef std::map<std::string, std::vector<size_t> > Map;
261 Map::const_iterator iter=label2index.begin();
263 for (
size_t row=0; row<result.rows(); ++row) {
264 const std::vector<size_t>& index = iter->second;
265 for (
size_t col=0; col<result.columns(); ++col) {
266 using boost::make_permutation_iterator;
267 assign(result(row,col),
268 make_permutation_iterator(x.begin_column(col), index.begin()),
269 make_permutation_iterator(x.end_column(col), index.end()),
void inverse(InputIterator first, InputIterator last, std::map< Key, std::vector< size_t >, Comp > &m)
Definition: stl_utility.h:552
void merge(const Container2D &x, std::vector< std::string > &labels, Matrix &y)
merge rows in a Container2D
Definition: merge.h:193
Functor to take average of a range.
Definition: Average.h:38
Concept check for Container2D.
Definition: concept_check.h:59
boost::transform_iterator< PairFirst< typename boost::remove_reference< typename std::iterator_traits< Iter >::reference >::type >, Iter > pair_first_iterator(Iter i)
Definition: stl_utility.h:844
Interface to GSL matrix.
Definition: Matrix.h:63
Weighted Matrix.
Definition: MatrixWeighted.h:44