Is it possible to speed up large sparse matrix calculations by e.g. placing parantheses optimally?
What I'm asking is: Can I speed up the following code by forcing Matlab to do the operations in a specified order (for instance "from right to left" or something similar)?
I have a sparse square symmetric matrix H, that previously has been factorized, and a sparse vector M with length equal to the dimension of H. What I want to do is the following:
EDIT: Some additional information: H is typically 4000x4000. The calculations of z and c are done around 4000 times, whereas the computation of dVa and dVaComp is done 10 times for every 4000 loops, thus 40000 in total. (dVa and dVaComp are solved iteratively, where P_mis is updated).
Here M*c*M'
, will become a sparse matrix with 4 non-zero element. In Matlab:
[L U P] = lu(H); % H is sparse (thus also L, U and P)
% for i = 1:4000 % Just to illustrate
M = sparse([bf bt],1,[1 -1],n,1); % Sparse vector with two non-zero elements in bt and bf
z = -M'*(U \ (L \ (P * M))); % M^t*H^-1*M = a scalar
c = (1/dyp + z)^-1; % dyp is a scalar
% while (iterations < 10 && ~=converged)
dVa = - (U \ (L \ (P * P_mis)));
dVaComp = (U \ (L \ (P * M * c * M' * dVa)));
% Update P_mis etc.
% end while
% end for
And for the record: Even though I use the inverse of H many times, it is not faster to pre-compute it.
Thanks =)
P
is a pre-defined full vector", but you generate it as a sparse matrix the same size asH
. Also, between the "mathematical"z
and implementedz
, there is a minus sign different. This makes it a bit unclear what you want to accomplish exactly; could you please correct/clarify these issues? – Rody Oldenhuis