matrix_fold.hpp
Go to the documentation of this file.
1 /*!
2  * \brief Kernels for folding matrix expressions
3  *
4  * \author O. Krause
5  * \date 2016
6  *
7  *
8  * \par Copyright 1995-2015 Shark Development Team
9  *
10  * <BR><HR>
11  * This file is part of Shark.
12  * <http://image.diku.dk/shark/>
13  *
14  * Shark is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU Lesser General Public License as published
16  * by the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * Shark is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
26  *
27  */
28 #ifndef REMORA_KERNELS_DEFAULT_MATRIX_FOLD_HPP
29 #define REMORA_KERNELS_DEFAULT_MATRIX_FOLD_HPP
30 
31 #include "../../detail/traits.hpp" //orientations, major_begin/end
32 #include "../../expression_types.hpp"
33 #include <type_traits>
34 namespace remora{namespace bindings{
35 
36 template<class F, class M, class Orientation, class Tag>
37 void matrix_fold(matrix_expression<M, cpu_tag> const& m, typename F::result_type& value, Orientation, Tag) {
38  typedef typename std::conditional<
39  std::is_same<Orientation,unknown_orientation>::value,
40  row_major,
41  Orientation
42  >::type chosen_orientation;
43  F f;
44  std::size_t size = chosen_orientation::index_M(m().size1(),m().size2());
45  for(std::size_t i = 0; i != size; ++i){
46  auto end = major_end(m,i);
47  for(auto pos = major_begin(m,i);pos != end; ++pos){
48  value = f(value,*pos);
49  }
50  }
51 }
52 
53 }}
54 #endif