STK++ 0.9.13
STK_Cholesky.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2017 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: 29 mars 2017
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
34
35
36#ifndef STK_CHOLESKY_H
37#define STK_CHOLESKY_H
38
39#include <Sdk.h>
40
44
45namespace STK
46{
47
62template < class Lhs>
63bool cholesky( ExprBase<Lhs> const& A
66{
67 typedef typename Lhs::Type Type;
68 if(A.rows() != A.cols())
69 { STKRUNTIME_ERROR_NO_ARG(cholesky,A must be square symmetric matrix);}
70 // start
71 D.resize(A.rows(), A.cols());
72 L.resize(A.rows(), A.cols());
73 bool nozero = true;
74 for (int j=A.beginCols(); j<A.endCols(); ++j )
75 {
76 Type sum1 = A(j,j);
77 for (int k=A.beginCols(); k<j; ++k) { sum1 -= L(j,k) * L(j,k) * D[k];}
78 D[j] = sum1;
79 nozero &= (sum1!=0);
80 L(j,j) = Type(1);
81 for (int i=j+1; i<A.endRows(); ++i)
82 {
83 Type sum2 = A(i,j);
84 for (int k=A.beginCols(); k<j; ++k) { sum2 -= L(i,k)*L(j,k)* D[k];}
85 L(i,j) = sum1 ? sum2/sum1 : sum2;
86 }
87 }
88 return nozero;
89}
90
91
92} // namespace STK
93
94#endif /* STK_CHOLESKY_H */
In this file, we define Array2DDiagonal class.
In this file we define the Array2DLowerTriangular class.
In this file we implement the final class CArraySquare.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
This file include all the other header files of the project Sdk.
Derivation of the Array2DDiagonal class for square arrays of Real.
Declaration of the lower triangular matrix class.
base class for template evaluation expressions and visitors.
Derived & resize(Range const &I, Range const &J)
resize the array.
bool cholesky(ExprBase< Lhs > const &A, Array2DDiagonal< typename Lhs::Type > &D, Array2DLowerTriangular< typename Lhs::Type > &L)
Compute the Cholesky decomposition of a symmetric matrix.
The namespace STK is the main domain space of the Statistical ToolKit project.