Why is the built-in Cholesky function so much faster than my own implementation?
Show older comments
Hi,
I am currently investigating the efficiency of matrix-inversion-methods and came across the Cholesky decomposition. I then implemented the cholesky in Matlab and compared it to the built-in chol()-function.
What you can see in the graph below is a Benchmark of my implemented Cholesky decompositions and the chol()-function: - "Cholesky" is the regular Cholesky Decomposition - "Incremental Cholesky" is a method where an old Cholesky decomp of a Matrix A is used to calculate the decomposition of an incremented Matrix B with one extra row and column
Both functions are also implemented in Mex-C which improved performance a little bit.

Here is the code of my Cholesky implementation:
function L = cholMatlab(M)
n = length( M );
L = zeros( n, n );
for i=1:n
L(i, i) = sqrt(M(i, i) - L(i, :)*L(i, :)');
for j=(i + 1):n
L(j, i) = (M(j, i) - L(i,:)*L(j ,:)')/L(i, i);
end
end
end
Can you tell me why the chol()-function performs so much better?
Best regards
Edit: Should have mentioned this: The calculations are based on random full symmetric positive definite matrices.
Accepted Answer
More Answers (2)
Marc
on 15 Nov 2015
1 vote
I am just guessing and based on the help information for chol() is that it uses sparseness as you increase the size of the matrix.
Several years ago, I ran into a fsolve() problem where an in house fortran based solver wasn't flinching as I increased the size of the matrix but fsolve() after n = 100,000 equations really got bogged down. One of TMW application engineers looked at my problem, tweaked things to take advantage of sparseness and it flew within milliseconds that I could not tell the difference between ML and the in house program.
Some of these guys are ninjas with this stuff....
3 Comments
Luke Skywalker
on 15 Nov 2015
John D'Errico
on 15 Nov 2015
sparsity information is ONLY used when you specify your matrix as sparse when you create the matrix. MATLAB does NOT decide on the fly that your matrix can benefit because it appears sparse. You need to specify that. As well, if you convert your matrix to sparse form and it is not truly sparse (i.e., a sparse matrix should have MANY zeros) then code will often take longer to run. So, just blithely using sparse form on all of your matrices will probably result in slower code overall.
Marc
on 15 Nov 2015
You guys may be right. I have no idea. I did not look into what you wrote but in the help file it says that chol() uses only the diagonal and upper triangle, assuming the lower is a complex conjugate transpose of the upper.
Does this mean it is doing "half" the computations that you are doing?
In general, I never disagree with John. His answer above wasn't showing when I typed mine in but I would always defer to him over me.
Greg Bishop
on 15 Feb 2017
0 votes
Your code doesn't seem to provide the same answer as chol(A) does.
1 Comment
John D'Errico
on 15 Feb 2017
This is not an answer. Please don't answer a question, just to add a comment. Use a comment for that.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!