11
$\begingroup$

Bug introduced in 11.0 and fixed in 11.3.0


I am trying to reshape a $2 \times 2 \times 2$ array into a $n \times m$ array. It seems that if the matrix is defined via SparseArray, the ArrayReshape command doesn't care what $n$ and $m$ I choose, it always reshapes it to a $4 \times 2$ array.

In this code I try to reshape to $1 \times 8$:

Clear["Global`*"]
m = SparseArray[{i_, i_, i_} -> 1., {2, 2, 2}];
mt = ArrayReshape[m, {1, 8}];
MatrixForm[mt]

$\begin{pmatrix}1.&0.\\ 0.&0.\\0.&0.\\0.&1.\end{pmatrix}$

Why does it work like that? Am I doing something wrong?

$\endgroup$
4
  • $\begingroup$ Seems to have something to do with SparseArray, despite what the docs say. Dimensions@ArrayReshape[m, {1, 8}] gives {4, 2}, but Dimensions@ArrayReshape[Normal@m, {1, 8}] gives {1, 8}. $\endgroup$ Oct 28, 2017 at 12:42
  • $\begingroup$ In Mathematica 11 Miguels code produces a list with 8 entries ({1,8} dimensional) $\endgroup$
    – M. Stern
    Oct 28, 2017 at 14:51
  • $\begingroup$ Mi edition is 11.2 ... $\endgroup$
    – MBolin
    Oct 28, 2017 at 15:59
  • $\begingroup$ bug introduced in 11.2? $\endgroup$
    – M. Stern
    Oct 28, 2017 at 16:37

3 Answers 3

8
$\begingroup$
ArrayReshape[SparseArray`SparseArrayFlatten[m], {1, 8}]
(* or ArrayReshape[Flatten[m], {1, 8}] *)

SparseArray[<2>,{1,8}]

Normal @ %

{{1, 0, 0, 0, 0, 0, 0, 1}}

For the special case where the desired shape is a list with Length equal to Times@@Dimensions[m], you can also use SparseArray`SparseArrayFlatten:

SparseArray`SparseArrayFlatten[m]

SparseArray[<2>,{8}]

Normal @ %

{1, 0, 0, 0, 0, 0, 0, 1}

$\endgroup$
4
  • $\begingroup$ That's right @kglr but my aim is not a $1 \times 8$ array, but any $n \times m$ array. The $1 \times 8$ in the code was just an example. $\endgroup$
    – MBolin
    Oct 28, 2017 at 16:01
  • $\begingroup$ @Miguel, this approach works for any n and m. For example, ArrayReshape[SparseArray`SparseArrayFlatten[m], {5, 3}, x] gives SparseArray[<9>,{5,3}] as it should $\endgroup$
    – kglr
    Oct 28, 2017 at 16:29
  • $\begingroup$ Sorry, you're right. Thank you @kglr $\endgroup$
    – MBolin
    Oct 29, 2017 at 2:07
  • $\begingroup$ @MiguelBolín, my pleasure. Thank you for the accept. And welcome to mma.se. $\endgroup$
    – kglr
    Oct 29, 2017 at 2:08
5
$\begingroup$

It seems one have to make SparseArray to Normal first to reshape it.

m=SparseArray[{i_,i_,i_}->1,{2,2,2}];
ArrayReshape[m,{1,8}]//Normal

Mathematica graphics

But now

ArrayReshape[Normal@m,{1,8}]

Mathematica graphics

Not sure if this is by design or not.

$\endgroup$
6
  • 1
    $\begingroup$ Okay but then I loose the computation saving of SparseArray, right? When you make it Normal it adds all the zeros and you carry them along $\endgroup$
    – MBolin
    Oct 28, 2017 at 13:29
  • $\begingroup$ @MiguelBolín that is right. THat was the only way I could find to get it to work. I suggest you use Kglr solution using SparseArray`SparseArrayFlatten which I did not know about. $\endgroup$
    – Nasser
    Oct 28, 2017 at 15:21
  • 1
    $\begingroup$ mt = SparseArray@ArrayReshape[Normal@m, {1, 8}][[1]] $\endgroup$
    – Bob Hanlon
    Oct 28, 2017 at 15:22
  • $\begingroup$ Or just mt = SparseArray@ArrayReshape[Normal@m, {1, 8}] for the 1 x 8 $\endgroup$
    – Bob Hanlon
    Oct 28, 2017 at 15:37
  • $\begingroup$ I guess that has the same problem as Nasser's, right @BobHanlon ? You are adding the zeros and carrying them along. $\endgroup$
    – MBolin
    Oct 28, 2017 at 16:04
3
$\begingroup$

Comparing the timings for the approaches proposed by kglr and Nasser (modified):

$HistoryLength = 0;

m = SparseArray[{i_, i_, i_} -> 1., {16, 16, 16}];

(kglr = ArrayReshape[SparseArray`SparseArrayFlatten[m], #] & /@ {{1, 
      16^3}, {16^3, 1}, {128, 32}, {32, 128}}) // RepeatedTiming

enter image description here

(nasser = SparseArray@ArrayReshape[Normal@m, #] & /@ {{1, 16^3}, {16^3, 
      1}, {128, 32}, {32, 128}}) // RepeatedTiming

enter image description here

Verifying their equivalence

kglr === nasser

True
$\endgroup$

Your Answer

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

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