# 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.

• It also crashes for me on Windows 10 v12.1. May 27 '20 at 22:07
• macos version 12.0 crashes, too. Weird. May 27 '20 at 22:39
• Please report this to support. May 27 '20 at 23:04
• If you're using DiagonalMatrix[], perhaps just have it generate a SparseArray[] directly: test1 = DiagonalMatrix[{1., 0}, 0, 2, SparseArray]; test2 = DiagonalMatrix[{0, I}, 0, 2, SparseArray];. May 28 '20 at 1:14
• I'll ensure this is reported internally.
– ktm
May 28 '20 at 15:09

This seemed to work for me...

test1 = DiagonalMatrix[SparseArray[{1. + 0. I, 0. I}]];
test2 = DiagonalMatrix[SparseArray[{0. I, 1. I}]];
test1.test2

• This is better. It still works for me even if I leave the exact {1., 0} and {0, I} in place. May 27 '20 at 22:45

Using N[...] didn't work for me either. If you can tolerate the really tiny error on the order of $$10^{-308}$$ then here's a workaround which adds the $MinMachineNumber to the first matrix elements: test1 = SparseArray[DiagonalMatrix[{1., 0} +$MinMachineNumber]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2


This seems to work, and returns an empty SparseArray as desired

test1 = SparseArray[DiagonalMatrix[SetPrecision[{1., 0}, \$MachinePrecision]]]
test2 = SparseArray[DiagonalMatrix[{0, I}]]
test1.test2