-
Notifications
You must be signed in to change notification settings - Fork 40
/
schol.m
63 lines (58 loc) · 1.24 KB
/
schol.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
%SCHOL Cholesky factorization for positive semidefinite matrices
%
% Syntax:
% [L,def] = schol(A)
%
% In:
% A - Symmetric pos.semi.def matrix to be factorized
%
% Out:
% L - Lower triangular matrix such that A=L*L' if def>=0.
% def - Value 1,0,-1 denoting that A was positive definite,
% positive semidefinite or negative definite, respectively.
%
% Description:
% Compute lower triangular Cholesky factor L of symmetric positive
% semidefinite matrix A such that
%
% A = L*L'
%
% See also
% CHOL
% Copyright (C) 2006 Simo Särkkä
%
% $Id$
%
% This software is distributed under the GNU General Public
% Licence (version 2 or later); please refer to the file
% Licence.txt, included with the software, for details.
function [L,def] = schol(A)
L = zeros(size(A));
def = 1;
for i=1:size(A,1)
for j=1:i
s = A(i,j);
for k=1:j-1
s = s - L(i,k)*L(j,k);
end
if j < i
if L(j,j) > eps
L(i,j) = s / L(j,j);
else
L(i,j) = 0;
end
else
if (s < -eps)
s = 0;
def = -1;
elseif (s < eps)
s = 0;
def = min(0,def);
end
L(j,j) = sqrt(s);
end
end
end
if (nargout < 2) & (def < 0)
warning('Matrix is negative definite !!!!');
end