cgv
Loading...
Searching...
No Matches
chol.h
1#pragma once
2
3#include <cgv/math/mat.h>
4
5#include <cgv/math/up_tri_mat.h>
6
7#include <limits>
8#include <algorithm>
9
10namespace cgv {
11namespace math {
12
13
14
15//compute a cholesky factorisation of a square positive definite matrix
16//returns false if matrix is not positive definite
17// further if a is symmetric then a == l*l^t
18template<typename T>
19bool chol(const mat<T> &a, low_tri_mat<T> &l)
20
21 {
22
23 assert(a.is_square());
24
25 unsigned N = a.nrows();
26
27 l.resize(N);
28
29
30
31 for(unsigned i = 0; i < N;i++)
32
33 {
34
35 for(unsigned j = i; j < N;j++)
36
37 {
38
39 T sum= a(i, j);
40
41 for(int k = i - 1; k >= 0;k--)
42
43 {
44
45 sum -= l(i, k) * l(j, k);
46
47 }
48
49
50
51 if(i == j)
52
53 {
54
55 if(sum <= 0)
56
57 return false;//not positive definite
58
59 else
60
61 l(j, i) = sqrt(sum);
62
63 }
64
65 else
66
67 l(j, i) = sum / l(i, i);
68
69 }
70
71 }
72
73
74
75
76
77
78
79 return true;
80
81 }
82
83
84//returns true if A is positive definite otherwise false
85template <typename T>
86bool is_pos_def(const cgv::math::mat<T>& A)
87{
88 low_tri_mat<T> G;
89 return chol(A,G);
90}
91
92
93
94
95}
96
97}
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
the cgv namespace
Definition print.h:11