trsv.hpp
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief -
5  *
6  * \author O. Krause
7  * \date 2011
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 #ifndef REMORA_KERNELS_DEFAULT_TRSV_HPP
31 #define REMORA_KERNELS_DEFAULT_TRSV_HPP
32 
33 #include "../../expression_types.hpp" //vector/matrix_expression
34 #include "../../assignment.hpp" //plus_assign
35 #include "../dot.hpp" //dot kernel
36 #include "../../detail/vector_proxy_classes.hpp" //vector_range
37 #include "../../detail/matrix_proxy_classes.hpp" //matrix_row, matrix_transpose
38 #include "../../detail/structure.hpp" //structure tags
39 #include <stdexcept> //exception when matrix is singular
40 #include <type_traits> //std::false_type marker for unoptimized
41 
42 namespace remora {namespace bindings {
43 
44 //Lower triangular(row-major) - vector
45 template<bool Unit, class MatA, class V>
46 void trsv_impl(
47  matrix_expression<MatA, cpu_tag> const& A,
48  vector_expression<V, cpu_tag> &b,
49  lower, column_major, left
50 ) {
51  REMORA_SIZE_CHECK(A().size1() == A().size2());
52  REMORA_SIZE_CHECK(A().size2() == b().size());
53 
54  typedef typename MatA::value_type value_type;
55  typedef matrix_transpose<typename const_expression<MatA>::type> TransA;
56  TransA transA(A());
57  std::size_t size = b().size();
58  for (std::size_t n = 0; n != size; ++ n) {
59  if(!Unit){
60  if(A()(n, n) == value_type()){//matrix is singular
61  throw std::invalid_argument("[TRSV] Matrix is singular!");
62  }
63  b()(n) /= A()(n, n);
64  }
65  if (b()(n) != value_type/*zero*/()){
66  matrix_row<TransA> colA = row(transA,n);
67  vector_range<V> blower(b(),n+1,size);
68  vector_range<matrix_row<TransA> > colAlower(colA,n+1,size);
69  plus_assign(blower,colAlower,-b()(n));
70  }
71  }
72 }
73 //Lower triangular(column-major) - vector
74 template<bool Unit, class MatA, class V>
75 void trsv_impl(
76  matrix_expression<MatA, cpu_tag> const& A,
77  vector_expression<V, cpu_tag> &b,
78  lower, row_major, left
79 ) {
80  REMORA_SIZE_CHECK(A().size1() == A().size2());
81  REMORA_SIZE_CHECK(A().size2() == b().size());
82 
83  typedef typename MatA::value_type value_type;
84 
85  std::size_t size = b().size();
86  for (std::size_t n = 0; n < size; ++ n) {
87  typedef matrix_row<typename const_expression<MatA>::type> RowA;
88  RowA matRow(A(),n);
89  vector_range<V> blower(b(),0,n);
90  vector_range<RowA> matRowLower(matRow,0,n);
91  value_type value;
92  kernels::dot(blower,matRowLower,value);
93  b()(n) -= value;
94  if(!Unit){
95  if(A()(n, n) == value_type()){//matrix is singular
96  throw std::invalid_argument("[TRSV] Matrix is singular!");
97  }
98  b()(n) /= A()(n, n);
99  }
100  }
101 }
102 
103 //upper triangular(column-major)-vector
104 template<bool Unit, class MatA, class V>
105 void trsv_impl(
106  matrix_expression<MatA, cpu_tag> const& A,
107  vector_expression<V, cpu_tag> &b,
108  upper, column_major, left
109 ) {
110  REMORA_SIZE_CHECK(A().size1() == A().size2());
111  REMORA_SIZE_CHECK(A().size2() == b().size());
112 
113  typedef typename MatA::value_type value_type;
114  typedef matrix_transpose<typename const_expression<MatA>::type> TransA;
115  TransA transA(A());
116  std::size_t size = b().size();
117  for (std::size_t i = 0; i < size; ++ i) {
118  std::size_t n = size-i-1;
119  if(!Unit){
120  if(A()(n, n) == value_type()){//matrix is singular
121  throw std::invalid_argument("[TRSV] Matrix is singular!");
122  }
123  b()(n) /= A()(n, n);
124  }
125  if (b()(n) != value_type/*zero*/()) {
126  matrix_row<TransA> colA = row(transA,n);
127  vector_range<V> blower(b(),0,n);
128  vector_range<matrix_row<TransA> > colAlower(colA,0,n);
129  plus_assign(blower,colAlower, -b()(n));
130  }
131  }
132 }
133 //upper triangular(row-major)-vector
134 template<bool Unit, class MatA, class V>
135 void trsv_impl(
136  matrix_expression<MatA, cpu_tag> const& A,
137  vector_expression<V, cpu_tag> &b,
138  upper, row_major, left
139 ) {
140  REMORA_SIZE_CHECK(A().size1() == A().size2());
141  REMORA_SIZE_CHECK(A().size2() == b().size());
142 
143  typedef typename MatA::value_type value_type;
144 
145  std::size_t size = A().size1();
146  for (std::size_t i = 0; i < size; ++ i) {
147  std::size_t n = size-i-1;
148  typedef matrix_row<typename const_expression<MatA>::type> RowA;
149  RowA matRow(A(),n);
150  vector_range<V> blower(b(),n+1,size);
151  vector_range<RowA> matRowLower(matRow,n+1,size);
152  value_type value;
153  kernels::dot(blower,matRowLower,value);
154  b()(n) -= value;
155  if(!Unit){
156  if(A()(n, n) == value_type()){//matrix is singular
157  throw std::invalid_argument("[TRSV] Matrix is singular!");
158  }
159  b()(n) /= A()(n, n);
160  }
161  }
162 }
163 
164 
165 //right is mapped onto left via transposing A
166 template<bool Unit, class Triangular, class Orientation, class MatA, class V>
167 void trsv_impl(
168  matrix_expression<MatA, cpu_tag> const& A,
169  vector_expression<V, cpu_tag> &b,
170  Triangular, Orientation, right
171 ) {
172  matrix_transpose<typename const_expression<MatA>::type> transA(A());
173  trsv_impl<Unit>(
174  transA, b,
175  typename Triangular::transposed_orientation(),
176  typename Orientation::transposed_orientation(),
177  left()
178  );
179 }
180 
181 //dispatcher
182 template <class Triangular, class Side,typename MatA, typename V>
183 void trsv(
184  matrix_expression<MatA, cpu_tag> const& A,
185  vector_expression<V, cpu_tag> & b,
186  std::false_type//unoptimized
187 ){
188  trsv_impl<Triangular::is_unit>(
189  A, b,
190  triangular_tag<Triangular::is_upper,false>(),
191  typename MatA::orientation(), Side());
192 }
193 
194 }}
195 #endif