I was comparing a few of my codes to stock MATLAB codes. I am surprised at the results.I ran a sample code (Sparse Matrix)n = 5000;a = diag(rand(n,1));b = rand(n,1);disp('For a\b');tic;a\b;toc;disp('For LU');tic;LULU;toc;disp('For Conj Grad');tic;conjgrad(a,b,1e-8);toc;disp('Inv(A)*B');tic;inv(a)*b;toc;Results :    For a\b    Elapsed time is 0.052838 seconds.    For LU    Elapsed time is 7.441331 seconds.    For Conj Grad    Elapsed time is 3.819182 seconds.    Inv(A)*B    Elapsed time is 38.511110 seconds.For Dense Matrix:n = 2000;a = rand(n,n);b = rand(n,1);disp('For a\b');tic;a\b;toc;disp('For LU');tic;LULU;toc;disp('For Conj Grad');tic;conjgrad(a,b,1e-8);toc;disp('For INV(A)*B');tic;inv(a)*b;toc;Results:For a\bElapsed time is 0.575926 seconds.For LUElapsed time is 0.654287 seconds.For Conj GradElapsed time is 9.875896 seconds.Inv(A)*BElapsed time is 1.648074 seconds.How the heck is a\b so awesome?
How does the MATLAB backslash operator solve $Ax=b$ for square matrices?
linear algebra;performance;matlab
In Matlab, the \ command invokes an algorithm which depends upon the structure of the matrix A and includes checks (small overhead) on properties of A.If A is sparse and banded, employ a banded solver. If A is an upper or lower triangular matrix, employ a backwardsubstitution algorithm. If A is symmetric and has real positive diagonal elements, attempt aCholesky factorization. If A is sparse, employ reordering first to minimizefill-in. If none of criteria above is fulfilled, do a general triangular factorizationusing Gaussian elimination with partial pivoting. If A is sparse, then employ the UMFPACK library. If A is not square, employ algorithms based on QR factorization forundetermined systems.To reduce overhead it is possible to use the linsolve command in Matlab and select a suitable solver among these options yourself.