-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLTSolver.m
More file actions
73 lines (56 loc) · 1.54 KB
/
LLTSolver.m
File metadata and controls
73 lines (56 loc) · 1.54 KB
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
63
64
65
66
67
68
69
70
71
72
73
function [y] = LLTSolver(L,r,verbose)
%LLTSOLVER Solves y = (L*L')^(-1)*r.
%% Purpose:
% LLTSolver solves (L*L')*y=r for y using forward and backward substitution
%% Input Definition:
% L: real valued lower triangle matrix nxn with nonzero diagonal elements
% r: column vector in R^n
% verbose: bool, if set to true, verbose information is displayed
%% Output Definition:
% y: column vector in R^n (solution in domain space)
%% Required files:
% <none>
%% Test cases:
% [y]=LLTSolver([2,0,0;0.5,sqrt(15/4),0;0,0,2],[5;5;4],true);
% should return
% y=[1;1;1];
% [y]=LLTSolver([22,0,0,0,0;17,13,0,0,0;13,-2,17,0,0;8,-4,-7,18,0;4,-5,-4,-5,19],[1320;773;1192;132;1405],true);
% should return
% y=[1;0;2;0;3];
%% Input verification:
n=length(r);
if ~isequal(size(L), [n,n])
error('A has wrong dimension.');
end
if nargin < 3
verbose = false;
end
%% Implementation:
if verbose
disp('Start LLTSolver...');
end
%static
%none
%dynamic
for i=1:n
s (i,1)= r(i,1);
for j=1:i-1
s(i,1)=s(i,1)-L(i,j)*s(j);
end
if (L(i,i)==0)
disp('Zero diagonal element detected...');
end
s(i,1)=s(i,1)/L(i,i);
end
y=s;
for i=n:-1:1
for j=n:-1:i+1
y(i,1)=y(i,1)-L(j,i)*y(j);
end
y(i,1)=y(i,1)/L(i,i);
end
if verbose
residual = norm((L*L')*y-r)
disp(sprintf('LLTSolver terminated with norm of residual =%d\n', norm(residual)));
end
end