9
$\begingroup$

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.

$\endgroup$
9
  • 1
    $\begingroup$ It also crashes for me on Windows 10 v12.1. $\endgroup$
    – flinty
    Commented May 27, 2020 at 22:07
  • 1
    $\begingroup$ macos version 12.0 crashes, too. Weird. $\endgroup$ Commented May 27, 2020 at 22:39
  • 1
    $\begingroup$ Please report this to support. $\endgroup$ Commented May 27, 2020 at 23:04
  • 2
    $\begingroup$ 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];. $\endgroup$ Commented May 28, 2020 at 1:14
  • 2
    $\begingroup$ I'll ensure this is reported internally. $\endgroup$
    – ktm
    Commented May 28, 2020 at 15:09

3 Answers 3

5
$\begingroup$

This seemed to work for me...

test1 = DiagonalMatrix[SparseArray[{1. + 0. I, 0. I}]];
test2 = DiagonalMatrix[SparseArray[{0. I, 1. I}]];
test1.test2
$\endgroup$
1
  • 1
    $\begingroup$ This is better. It still works for me even if I leave the exact {1., 0} and {0, I} in place. $\endgroup$
    – flinty
    Commented May 27, 2020 at 22:45
4
$\begingroup$

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
$\endgroup$
3
$\begingroup$

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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.