Discrepancy in sparse matrix math, when NaN's present

97 views (last 30 days)
I expect result1 and result2 below to be identical, but they aren't. The discrepancy must be a bug, right? I'm working in R2024b, but the Run output below shows the issue exists as well in whatever the Matlab online engine is now running.
n = 5; m = 3;
S = sparse(n, m);
v=nan(n, 1);
D=sparse(diag(v));
result1=full(v.*S) %correct
result1 = 5×3
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
result2=full(D*S) %incorrect
result2 = 5×3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  8 Comments
Matt J
Matt J ungefär en timme ago
Edited: Matt J ungefär en timme ago
@dpb Yes, I think there's little doubt that it has always been there. It's clear, from a performance optimization point of view, why you mgiht want sparse mtimes() to ignore NaNs. The whole idea of sparse type is to exploit the fact that 0*somethingFinite=0.If you have to worry about "somethingFinite" being non-finite, I wonder how much of the intended performance can be salvaged.
dpb
dpb ungefär 3 timmar ago
There was a similar thread on sparse performance here just a week or so ago where it ended up being full for similar reasons. I looked but didn't find it in a cursory search, iirc, it was @Steven Lord who pointed out the issue about timing.

Sign in to comment.

Accepted Answer

Matt J
Matt J ungefär 6 timmar ago
Edited: Matt J ungefär 6 timmar ago
The response I seem to be getting from Tech Support is that this is expected behavior. NaNs are not guaranteed to propagate the same in sparse matrix multiplication as in full. In particular, sparse mtimes does not try to detect 0*NaN scenarios when the zero is sparse/implicit.
  2 Comments
Paul
Paul ungefär 6 timmar ago
Is this expected behavior documented anywhere? I poked around a bit but didn't see anything; admittedly didn't try very hard (shouldn't have to).
Matt J
Matt J ungefär 6 timmar ago
Edited: Matt J ungefär 6 timmar ago
No, Tech Support seems to acknowledge that it isn't documented:
"I apologize for any confusion caused by the lack of documentation regarding NaN propagation in these operations.
To clarify, the time differences you are seeing between “.*” and “*” are due to underlying algorithms which handle NaN propagation differently. The same concept applies to the result of the second example that you provided. Specifically, the propagation rules between a sparse-sparse matrix multiply are different from a sparse-full matrix multiply.
So, although theoretically the results should be the same for both cases, the underlying code goes through a different algorithm and propagates NaN differently."

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!