sum_rows.hpp
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Sums the rows of a row-major or column major matrix.
5  *
6  * \author O. Krause
7  * \date 2016
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 
31 #ifndef REMORA_KERNELS_DEFAULT_SUM_ROWS_HPP
32 #define REMORA_KERNELS_DEFAULT_SUM_ROWS_HPP
33 
34 #include "../../expression_types.hpp"//for vector/matrix_expression
35 #include "../../detail/traits.hpp"//for orientations
36 
37 
38 namespace remora{namespace bindings{
39 
40 template<class M,class V, class Tag1, class Tag2>
41 void sum_rows(
42  matrix_expression<M, cpu_tag> const& A,
43  vector_expression<V, cpu_tag>& v,
44  typename V::value_type alpha,
45  column_major, Tag1, Tag2
46 ){
47  for(std::size_t i = 0; i != A().size2(); ++i){
48  typename V::value_type s = 0;
49  auto end = A().column_end(i);
50  for(auto pos = A().column_begin(i); pos != end; ++pos){
51  s += *pos;
52  }
53  v()(i) += alpha * s;
54  }
55 }
56 
57 template<class M,class V, class Tag1, class Tag2>
58 void sum_rows(
59  matrix_expression<M, cpu_tag> const& A,
60  vector_expression<V, cpu_tag>& v,
61  typename V::value_type alpha,
62  row_major, Tag1, Tag2
63 ){
64  for(std::size_t i = 0; i != A().size1(); ++i){
65  auto end = A().row_end(i);
66  for(auto pos = A().row_begin(i); pos != end; ++pos)
67  v()(pos.index()) += alpha * (*pos);
68  }
69 }
70 
71 
72 
73 //dispatcher for triangular matrix
74 template<class M,class V,class Orientation,class Triangular, class Tag1, class Tag2>
75 void sum_rows(
76  matrix_expression<M, cpu_tag> const& A,
77  vector_expression<V, cpu_tag>& v,
78  typename V::value_type alpha,
79  triangular<Orientation,Triangular>, Tag1, Tag2
80 ){
81  sum_rows(A,v,alpha,Orientation(), Tag1(), Tag2());
82 }
83 
84 }}
85 
86 #endif