1
$\begingroup$

I am having an issue where the kernel spontaneously quits, deep in a long code that I have used for quite some time without this issue. I've tracked it down to an assignment of the form:

a[[All, All, All, -1]] = b;

Where the left and right hand side are both lists of machine precision complex numbers, both of dimension {2,3,9}, and the left hand side all zeroes. So nothing crazy there, and I can also print both sides before the assignment without issues. Neither is a packed array. Some entries were real rather than complex, but adding 0. I didn't help.

The problem goes away by either of these methods (independently):

  1. use arbitrary precision instead of machine precision
  2. Modify the input so as to make some elements of the left hand side above nonzero (and it matters where)
  3. I said above neither a nor b are packed arrays, but a was constructed from a packed array, i.e. an expression containing Developer`ToPackedArray[c], and changing that to just c also fixes it.

So it seems the issue has to do with packed arrays, as 3 gets rid of that explicitly, 1 indirectly, and 2 perhaps affects whether/how/which part was converted in a packed array in the first place. Also, using On["Packing"], methods 1 and 3 get rid of unpacking warnings (occuring in construction of a, not in the line above itself.), method 2 does not.

Does this make sense? Is there an easy way to check if an expression contains rather than is a packed array? And how to avoid this? Is the rule not to use packed arrays when still assigning to parts of the expression?

$\endgroup$
4
  • 3
    $\begingroup$ Catch[expr /. x_?Developer`PackedArrayQ :> Throw[True]]? Not sure about the rest. Code to reproduce the problem may be needed. $\endgroup$
    – Michael E2
    Nov 25 '20 at 18:06
  • $\begingroup$ @MichaelE2 Thanks, that returns True, but when I try to find all occurrences using Cases[expr,x_?DeveloperPackedArrayQ,All], I don't get anything (nor with the option Heads->True`). Any idea why? $\endgroup$
    – Jansen
    Nov 26 '20 at 10:26
  • $\begingroup$ I noticed that. The same thing happens with FreeQ. They unpack the packed arrays before scanning for expressions that match the pattern., I suppose they do it because they check all subexpressions from bottom up (at least that’s the order in which Cases returns expressions). ReplaceAll goes top down. ``Reap[expr /. x_?DeveloperPackedArrayQ :> Sow[x]][[2,1]] might be an alternative to Cases. Plus it won’t return subarrays of packed arrays, which I surmise would be desirable. $\endgroup$
    – Michael E2
    Nov 26 '20 at 13:36
  • 2
    $\begingroup$ Could you post a MCE (minimal crashing example)? $\endgroup$ Nov 26 '20 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.