Problem:
When I multiply together two sparse matrices that should give back the 0 matrix, where at least one element among the two is complex and at least one is not an exact number, the kernel crashes unexpectedly with no messages generated.
4 workarounds are given at the bottom.
Minimal working example:
test1 = SparseArray[DiagonalMatrix[{1., 0}]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2 (* Crashes kernel with no messages generated *)
Note that at least one element must be complex, at least one must not be an exact number and the final result must have no non-zero elements.
Can anyone reproduce this behavior? Even better, anyone have a workaround? This problem shows up for me deep inside a complex differential equation of $64\times64$ very sparse matrices. Using non-sparse operations gives a $\sim 20$x slowdown.
I'll report to Wolfram as well, thanks!
System:
Version: 12.0.0 for Linux x86 (64-bit) (April 7, 2019). See comments for some other systems affected by this.
Workarounds:
For those who stumble upon this in the future:
test1 = SparseArray[DiagonalMatrix[SetPrecision[{1., 0}, $MachinePrecision]]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2
Gives the desired result of an empty SparseArray. Other workarounds include:
test1 = SparseArray[DiagonalMatrix[{1., 0}]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2
Avoids the crash but gives 2 "specified elements" in the result so it's less sparse than desired.
test1 = SparseArray[DiagonalMatrix[{1., 0} + $MinMachineNumber]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2
Also avoids the crash but does give 1 non-zero element in the result so is technically wrong, albeit by the tiniest possible amount.
test1 = DiagonalMatrix[{1., 0}, 0, 2, SparseArray];
test2 = DiagonalMatrix[{0, I}, 0, 2, SparseArray];
test1.test2
Also avoids the crash and also gives 2 "specified elements" in the result.
DiagonalMatrix[]
, perhaps just have it generate aSparseArray[]
directly:test1 = DiagonalMatrix[{1., 0}, 0, 2, SparseArray]; test2 = DiagonalMatrix[{0, I}, 0, 2, SparseArray];
. $\endgroup$