STK++ 0.9.13
STK_InvertUpperTriangular.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_INVERTUPPERTRIANGULAR_H
36#define STK_INVERTUPPERTRIANGULAR_H
37
38namespace STK
39{
40
41namespace hidden
42{
43
47template<class Matrix, int Size_>
49{
50 typedef typename Matrix::Type Type;
57 {
58 const int iShift = m.beginRows(), jShift = m.beginCols();
59 inv.resize(TRange<Size_>(0, 1), TRange<Size_>(0, 1));
60 // cofactor (0,0) [0]
61 inv(0, 0) = Type(1);
62 // compute determinant
63 Type det = m(iShift+0, jShift+0);
64 if (det == Type(0)) return Type(0);
65 // compute inverse matrix
66 inv /= det;
67 return det;
68 }
75 {
76 const int iShift = m.beginRows(), jShift = m.beginCols();
77 inv.resize(TRange<Size_>(0, 2), TRange<Size_>(0, 2));
78 // cofactor (0,0) [0]
79 inv(0, 0) = m(iShift+1, jShift+1);
80 // cofactor (1,1) [2]
81 inv(0, 1) = - m(iShift+0, jShift+1);
82 // cofactor (1,1) [3]
83 inv(1, 1) = m(iShift+0, jShift+0);
84 // determinant
85 Type det = m(iShift+0, jShift+0) * inv(0,0);
86 if (det == Type(0)) return Type(0);
87 // compute inverse matrix
88 inv /= det;
89 return det;
90 }
97 {
98 const int iShift = m.beginRows(), jShift = m.beginCols();
99 inv.resize(TRange<Size_>(0, 3), TRange<Size_>(0, 3));
100 // cofactor
101 inv(0, 0) = m(iShift+1, jShift+1) * m(iShift+2, jShift+2);
102 inv(0, 1) = -m(iShift+0, jShift+1) * m(iShift+2, jShift+2);
103 inv(0, 2) = m(iShift+0, jShift+1) * m(iShift+1, jShift+2)
104 -m(iShift+0, jShift+2) * m(iShift+1, jShift+1);
105 inv(1, 1) = m(iShift+0, jShift+0) * m(iShift+2, jShift+2);
106 inv(1, 2) = -m(iShift+0, jShift+0) * m(iShift+1, jShift+2);
107 inv(2, 2) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1);
108 // computes the inverse of a matrix m
109 Type det = m(iShift+0, jShift+0) * inv(0,0);
110 if (det == Type(0)) return Type(0);
111 // compute inverse matrix
112 inv /= det;
113 return det;
114 }
121 {
122 const int iShift = m.beginRows(), jShift = m.beginCols();
123 inv.resize(TRange<Size_>(0, 4), TRange<Size_>(0, 4));
124 // cofactor (0,0) [0]
125 inv(0, 0) = m(iShift+1, jShift+1) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
126 // cofactor (0,1) [4]
127 inv(0, 1) = -m(iShift+0, jShift+1) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
128 // cofactor (0,2) [8]
129 inv(0, 2) = m(iShift+0, jShift+1) * m(iShift+1, jShift+2) * m(iShift+3, jShift+3)
130 - m(iShift+0, jShift+2) * m(iShift+1, jShift+1) * m(iShift+3, jShift+3);
131 // cofactor (0,3) [12]
132 inv(0, 3) = -m(iShift+0, jShift+1) * m(iShift+1, jShift+2) * m(iShift+2, jShift+3)
133 + m(iShift+0, jShift+1) * m(iShift+2, jShift+2) * m(iShift+1, jShift+3)
134 + m(iShift+0, jShift+2) * m(iShift+1, jShift+1) * m(iShift+2, jShift+3)
135 - m(iShift+0, jShift+3) * m(iShift+1, jShift+1) * m(iShift+2, jShift+2);
136 // cofactor (1,1) [5]
137 inv(1, 1) = m(iShift+0, jShift+0) * m(iShift+2, jShift+2) * m(iShift+3, jShift+3);
138 // cofactor (1,2) [9]
139 inv(1, 2) = -m(iShift+0, jShift+0) * m(iShift+1, jShift+2) * m(iShift+3, jShift+3);
140 // cofactor (1,3) [13]
141 inv(1, 3) = m(iShift+0, jShift+0) * m(iShift+1, jShift+2) * m(iShift+2, jShift+3)
142 - m(iShift+0, jShift+0) * m(iShift+2, jShift+2) * m(iShift+1, jShift+3);
143 // cofactor (2,2)
144 inv(2, 2) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+3, jShift+3);
145 // cofactor (2,3)
146 inv(2, 3) = -m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+2, jShift+3);
147 // cofactor (3,3) [15]
148 inv(3, 3) = m(iShift+0, jShift+0) * m(iShift+1, jShift+1) * m(iShift+2, jShift+2);
149 // compute determinant
150 Type det = m(iShift+0, jShift+0) * inv(0, 0);
151 if (det == Type(0)) return Type(0);
152 // compute inverse matrix
153 inv /= det;
154 return det;
155 }
163 {
164 inv.resize(m.rows(), m.cols());
165 Real det = Type(1);
166 for(int j = m.beginRows(); j < m.endRows(); ++j)
167 {
168 det *= m(j,j);
169 inv(j,j) = m(j,j) ? Type(1)/m(j,j) : Type(0);
170 for(int i = j+1; i < m.endCols(); ++i)
171 {
172 Type sum = Type(0);
173 if (m(i,i))
174 {
175 for (int k = j; k < i; ++k)
176 { sum -= m(k, i) * inv(j, k);}
177 sum /= m(i,i);
178 }
179 inv(j,i) = sum;
180 }
181 }
182 return det;
183 }
184};
185
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_INVERTUPPERTRIANGULAR_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 invertUpperTriangular22(Matrix const &m, Array2DUpperTriangular< Type > &inv)
compute the inverse of the 2x2 matrix m and store the result in inv.
static Type invertUpperTriangularXX(Matrix const &m, Array2DUpperTriangular< Type > &inv)
compute the inverse of the upper triangular matrix m and store the result in inv.
static Type invertUpperTriangular33(Matrix const &m, Array2DUpperTriangular< Type > &inv)
compute the inverse of the 3x3 matrix m and store the result in inv.
static Type invertUpperTriangular11(Matrix const &m, Array2DUpperTriangular< Type > &inv)
compute the inverse of the 1x1 matrix and store the result in inv.
static Type invertUpperTriangular44(Matrix const &m, Array2DUpperTriangular< 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, Array2DUpperTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DUpperTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DUpperTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DUpperTriangular< Type > &inv)
static Type run(Matrix const &m, Array2DUpperTriangular< Type > &inv)
Implementation of the inversion matrix method for upper triangular matrices.