Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Cholesky using more vectorization #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions matlab/lu/Cholesky.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@
% calculates Cholesky factorization, A = L * L'
% A must be positive-definite
function [L U x] = Cholesky (A, b)
% get the size of the matrix A
[n n] = size(A);
% initialize the lower and upper matrix of size N
L = zeros(n);
U = zeros(n);

% check if A is positive definite
if IsPositiveDefinite(A) == 0 || A ~= A'

% Check positive definition and symetry of A
if !isequal(A, A') || IsPositiveDefinite(A) == 0
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

La mine nu a functionat ( A~=A' ).

L = NaN;
U = NaN;
x = NaN;
return;
endif

% A is positive definite and A is also symmetric, yay!
% calculate the factorization, A = L * L'
L(1, 1) = sqrt(A(1, 1));
% calculate only the elements situated under and on the main diagonal
for i = 1:n
for j = 1:i
sum_of_line = 0;
if i == j
sum_of_line = sum(L(i, 1:j - 1) .^ 2);
% calculate an element on the main diagonal
L(i, i) = sqrt(A(i, i) - sum_of_line);
else
sum_of_line = sum(L(i, 1: j - 1) .* L(j, 1: j - 1));
% calculate the sum of the previous elements on the same line
L(i, j) = (A(i, j) - sum_of_line) / L(j, j);
endif
endfor

% Inits
n = length(A);
L = zeros(n);
U = zeros(n);

for p = 1:n
% Calculate the elements on the main diagonal
tmp_sum = L(p, 1:(p - 1)) * L(p, 1:(p - 1))';
L(p, p) = sqrt(A(p, p) - tmp_sum);

% p == n --> last element that we calculated above; we need
% to exit
if p == n
return;
endif

% Calculate the elements under the main diagonal
% sum(A, 2) - sums matrix A with respect to the lines
tmp_sum2 = sum((L(p, 1:(p - 1)) .* L((p + 1):n, 1:(p - 1))), 2);
L((p + 1):n, p) = ( A((p + 1):n, p) - tmp_sum2 ) / L(p, p);
endfor

U = L';

% A * x = b; A = L * U; A = L * L'
% L * (L' * x) = b
% L' * x = y;
Expand Down