STK++ 0.9.13
STK_InvertLowerTriangular.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::Algebra
27 * created on: 9 août 2016
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_INVERTLOWERTRIANGULAR_H
36#define STK_INVERTLOWERTRIANGULAR_H
37
38namespace STK
39{
40
41namespace hidden
42{
46template<class Matrix, int Size_>
48{
49 typedef typename Matrix::Type Type;
56 {
57 const int iShift = m.beginRows(), jShift = m.beginCols();
58 inv.resize(TRange<Size_>(0, 1), TRange<Size_>(0, 1));
59 // cofactor (0,0) [0]
60 inv(0, 0) = Type(1);
61 // compute determinant
62 Type det = m(iShift+0, jShift+0);
63 if (det == Type(0)) return Type(0);
64 // compute inverse matrix
65 inv /= det;
66 return det;
67 }
74 {
75 const int iShift = m.beginRows(), jShift = m.beginCols();
76 inv.resize(TRange<Size_>(0, 2), TRange<Size_>(0, 2));
77 // cofactor (0,0) [0]
78 inv(0, 0) = m(iShift+1, jShift+1);
79 // cofactor (1,0) [1]
80 inv(1, 0) = - m(iShift+1, jShift+0);
81 // cofactor (1,1) [3]
82 inv(1, 1) = m(iShift+0, jShift+0);
83 // compute determinant
84 Type det = m(iShift+0, jShift+0) * m(iShift+1, jShift+1);
85 if (det == Type(0)) return Type(0);
86 // compute inverse matrix
87 inv /= det;
88 return det;
89 }
96 {
97 const int iShift = m.beginRows(), jShift = m.beginCols();
98 inv.resize(TRange<Size_>(0, 3), TRange<Size_>(0, 3));
99 // cofactor
100 inv(0,0) = m(iShift+1, jShift+1) * m(iShift+2, jShift+2);
101 inv(1,0) = -m(iShift+1, jShift+0) * m(iShift+2, jShift+2);
102 inv(1,1) = m(iShift+0, jShift+0) * m(iShift+2, jShift+2);
103 inv(2,0) = m(iShift+1, jShift+0) * m(iShift+2, jShift+1)
104 -m(iShift+2, jShift+0) * m(iShift+1, jShift+1);
105 inv(2,1) = -m(iShift+0, jShift+0) * m(iShift+2, jShift+1);
106 inv(2,2) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1);
107 // computes determinant
108 Type det = m(iShift+0, jShift+0) * inv(0,0) ;
109 if (det == Type(0)) return Type(0);
110 // compute inverse matrix
111 inv /= det;
112 return det;
113 }
120 {
121 const int iShift = m.beginRows(), jShift = m.beginCols();
122 inv.resize(TRange<Size_>(0, 4), TRange<Size_>(0, 4));
123 // cofactor (0,0) [0]
124 inv(0,0) = m(iShift+1, jShift+1) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
125 // cofactor (1,0) [1]
126 inv(1,0) = -m(iShift+1, jShift+0) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
127 // cofactor (1,1) [5]
128 inv(1,1) = m(iShift+0, jShift+0) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
129 // cofactor (2,0) [2]
130 inv(2,0) = m(iShift+1, jShift+0) * m(iShift+2, jShift+1) * m(iShift+3, jShift+3)
131 - m(iShift+1, jShift+1) * m(iShift+2, jShift+0) * m(iShift+3, jShift+3);
132 // cofactor (2,1)
133 inv(2,1) = -m(iShift+0, jShift+0) * m(iShift+2, jShift+1) * m(iShift+3, jShift+3);
134 // cofactor (2,2)
135 inv(2,2) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+3, jShift+3);
136 // cofactor (3,0)
137 inv(3,0) = -m(iShift+1, jShift+0) * m(iShift+2, jShift+1) * m(iShift+3, jShift+2)
138 + m(iShift+1, jShift+0) * m(iShift+3, jShift+1) * m(iShift+2, jShift+2)
139 + m(iShift+1, jShift+1) * m(iShift+2, jShift+0) * m(iShift+3, jShift+2)
140 - m(iShift+1, jShift+1) * m(iShift+3, jShift+0) * m(iShift+2, jShift+2);
141 // cofactor (3,1) [7]
142 inv(3,1) = m(iShift+0, jShift+0) * m(iShift+2, jShift+1) * m(iShift+3, jShift+2)
143 - m(iShift+0, jShift+0) * m(iShift+3, jShift+1) * m(iShift+2, jShift+2);
144 // cofactor (3,2) [11]
145 inv(3,2) = -m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+3, jShift+2);
146 // cofactor (3,3) [15]
147 inv(3,3) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+2, jShift+2);
148 // compute determinant
149 Type det = m(iShift+0, jShift+0) * inv(0,0);
150 if (det == Type(0)) return Type(0);
151 // compute inverse matrix
152 inv /= det;
153 return det;
154 }
162 {
163 inv.resize(m.rows(), m.cols());
164 Real det = Type(1);
165 for(int j = m.beginCols(); j < m.endCols(); ++j)
166 {
167 det *= m(j,j);
168 inv(j, j) = m(j, j) ? Type(1)/m(j, j) : Type(0);
169 for(int i = j+1; i < m.endRows(); ++i)
170 {
171 Type sum = Type(0);
172 if (m(i, i))
173 {
174 for (int k = j; k < i; ++k)
175 { sum -= m(i, k) * inv(k, j);}
176 sum /= m(i,i);
177 }
178 inv(i, j) = sum;
179 }
180 }
181 return det;
182 }
183};
184
188template<class Matrix, int Size_>
204
205
206template<class Matrix>
208{
209 typedef typename Matrix::Type Type;
212};
213
214template<class Matrix>
216{
217 typedef typename Matrix::Type Type;
220};
221
222template<class Matrix>
224{
225 typedef typename Matrix::Type Type;
228};
229
230template<class Matrix>
232{
233 typedef typename Matrix::Type Type;
236};
237
238} // namespace hidden
239
240} // namespace STK
241
242#endif /* STK_INVERTLOWERTRIANGULAR_H */
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Arrays::SumOp< Lhs, Rhs >::result_type sum(Lhs const &lhs, Rhs const &rhs)
convenience function for summing two arrays
double Real
STK fundamental type of Real values.
static Type invertLowerTriangularXX(Matrix const &m, Array2DLowerTriangular< Type > &inv)
compute the inverse of the lower triangular matrix m and store the result in inv.
static Type invertLowerTriangular11(Matrix const &m, Array2DLowerTriangular< Type > &inv)
compute the inverse of the 1x1 matrix and store the result in inv.
static Type invertLowerTriangular33(Matrix const &m, Array2DLowerTriangular< Type > &inv)
compute the inverse of the 3x3 matrix m and store the result in inv.
static Type invertLowerTriangular22(Matrix const &m, Array2DLowerTriangular< Type > &inv)
compute the inverse of the 2x2 matrix m and store the result in inv.
static Type invertLowerTriangular44(Matrix const &m, Array2DLowerTriangular< Type > &inv)
compute the inverse of the 4x4 matrix m and store the result in inv.
The namespace STK is the main domain space of the Statistical ToolKit project.
static Type run(Matrix const &m, Array2DLowerTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DLowerTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DLowerTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DLowerTriangular< Type > &inv)
utility class allowing to call the correct static function computing the inverse of a matrix.
static Type run(Matrix const &m, Array2DLowerTriangular< Type > &inv)
Implementation of the computation of the inverse of a lower triangular matrix.