The method mldivide
, generally represented as \
accepts solving many systems with the same A
at once.
x = A\[b1 b2 b3 b4] # where bi are vectors with n rows
Solves the system for each b
, and will return an nx4 matrix, where each column is the solution of each b
. Calling mldivide like this should improve efficiency becaus the descomposition is only done once.
As in many decompositions like LU od LDL' (and in the one you are interested in particular) the matrix multiplying x
is upper diagonal, the first value to be solved is x(n)
. However, having to do the LDL' decomposition, a simple backwards substitution algorithm won't be the bottleneck of the code. Therefore, the decomposition can be saved in order to avoid repeating the calculation for every bi
. Thus, the code would look similar to this:
[LA,DA] = ldl(A);
DA = sparse(DA);
% LA = sparse(LA); %LA can also be converted to sparse matrix
% loop over bi
xi = LA'\(DA\(LA\bi));
% end loop
As you can see in the documentation of mldivide (Algorithms section), it performs some checks on the input matrixes, and having defined LA
as full and DA
as sparse, it should directly go for a triangular solver and a tridiagonal solver. If LA
was converted to sparse, it would use a triangular solver too, and I don't know if the conversion to sparse would represent any improvement.