(Very!) Slow elementwise division using large sparse matrices.
3 views (last 30 days)
Show older comments
Hi!
I have written a pretty large code. A small part of the code takes the bulk of the solving time.
The small part of the code that takes a long time to solve is included here.
phi and x are N x 1 full vectors and H is a NxN sparse matrix. Sparse, both in the sense that it has a very small number of non-zero entries and it is the type of the matrix itself. N is approx. 50.000 and p is just a postive, real number.
sinxe = x.^p.*sin(phi);
cosxe = x.^p.*cos(phi);
Hssin = H*sinxe;
Hscos = H*cosxe;
Htsin = sinxe'.*H;
Htcos = cosxe'.*H;
HtDt = (Htcos./Hscos + Htsin.*(Hssin./Hscos.^2))./( Hssin.^2./Hscos.^2 + 1 )
The line with HtDt takes a long time to solve, even though im using sparse matrices and vectorized my inputs. Any ideas?
0 Comments
Answers (1)
Steven Lord
on 10 May 2023
The resulting matrix won't be very sparse. Anywhere your Hscos has a 0 (including the implicitly stored 0's) the result will contain a nonfinite, non-zero value.
Hscos = speye(5)
y = sparse(1)./Hscos
fullY = full(y); % for comparison
whos Hscos y fullY
You're consuming more memory storing y (every element of which is non-zero) as a sparse matrix than you would be if you stored it as a full matrix!
One potential approach would be to extract the non-zero elements from your sparse matrix Hscos, divide by the elements in the corresponding location in the other matrix, and treat anything with a 0 in Hscos as giving a 0 in the result. I don't know if that's the best approach for the underlying problem you're trying to solve, but if those 0's are actually important then you're going to need to operate on the full versions of your matrices.
3 Comments
Steven Lord
on 10 May 2023
Can you generate and show us what phi, x, and H look like for a small value of N (say N = 5 or N = 10)? And what is the order of magnitude for the elements of x and for p? I want to make sure you're not overflowing to Inf or underflowing to 0.
See Also
Categories
Find more on Sparse 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!