source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/math/cholesky.cxx @ 1025

Revision 1025, 968 bytes checked in by gumbau, 18 years ago (diff)

namespace simplif

Line 
1#include <gfx/std.h>
2#include <gfx/math/Mat4.h>
3
4// Adapted directly from Numerical Recipes in C
5//
6// Takes a symmetric positive definite matrix a
7// (only the upper triangle is actually necessary)
8// and computes the Cholesky decomposition of a.
9// A return value of False indicates that the decomposition does not exist.
10// On return, the lower triangle of a contains the factor and the
11// vector p contains the diagonal elements of the factor.
12//
13
14using namespace simplif;
15
16bool cholesky(Mat4& a, Vec4& p)
17{
18    int n = 4;
19    int i,j,k;
20    real sum;
21
22    for(i=0; i<n; i++)
23    {
24        for(j=i; j<n; j++)
25        {
26            for(sum=a(i,j), k=i-1; k>=0; k--) sum -= a(i,k)*a(j,k);
27            if( i==j )
28            {
29                if( sum<=0.0 )
30                    return False;
31                p[i] = sqrt(sum);
32            }
33            else
34                a(j,i) = sum/p[i];
35        }
36    }
37
38    return True;
39}
Note: See TracBrowser for help on using the repository browser.