# Reshaping a matrix for Reverse Kronecker product or Schmidt decomposition

**URL:** <https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702>\
**Category:** General Usage\
**Tags:** question, linearalgebra\
**Created:** [September 8, 2018, 1:53pm UTC](https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702 "2018-09-08T13:53:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CNOT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnot/32/5190_2.png) [@CNOT](https://discourse.julialang.org/u/CNOT)\
**Post date:** [September 8, 2018, 1:53pm UTC](https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702/1 "2018-09-08T13:53:55Z")

</div>

Before I begin, I hope MathJax works on Discourse.  
I am trying to do a generalization of Schmidt decomposition on a matrix, which means writing down a matrix H of size m\times n, where m = m\_1 \times m\_2 and n = n\_1\times n\_2, as

H = \sum\_j \lambda\_j H\_{1,j}\otimes H\_{2,j}\ ,

using the minimum number of terms possible. Here, H\_{1,j} and H\_{2,j} are matrices of sizes m\_1 \times n\_1 and m\_2 \times n\_2 respectively and \otimes is a Kronecker product. What I was trying to do is similar to what is explained in [this answer](https://math.stackexchange.com/a/321424/78741), with a tiny difference at the end.

There are three main steps in this algorithm:

1. Rearranging elements of H into a matrix of size m\_1 n\_1 \times m\_2 n\_2
2. Doing a Singular Value Decomposition on the result from previous step
3. Rearranging the outcome of SVD into the form we wanted

The second and third step are straightforward in Julia. For the first step what I initially tried was to reshape the Matrix H into a tensor of rank m\_1\times m\_2 \times n\_1 \times n\_2 using `reshape` and then swap the second and third index using `permutedims` and then rearrange it back into a matrix using `reshape` again.

```julia
H1 = reshape(H,(m1,m2,n1,n2))
H2 = permutedims(H1,(1,3,2,4))
H3 = reshape(H2,(m1*n1,m2*n2))

```

However, `reshape` doesn’t rearrange the elements as I want them to. For example, if I run the procedure above on the example given in the [math.stackexchange](https://math.stackexchange.com/a/321424/78741) answer, I get:

\begin{bmatrix} a\_{11} & a\_{41} & a\_{13} & a\_{43} \\ a\_{21} & a\_{51} & a\_{23} & a\_{53} \\ a\_{31} & a\_{61} & a\_{33} & a\_{63} \\ a\_{12} & a\_{42} & a\_{14} & a\_{44} \\ a\_{22} & a\_{52} & a\_{24} & a\_{54} \\ a\_{32} & a\_{62} & a\_{34} & a\_{64} \end{bmatrix}

instead of what is needed:

\begin{bmatrix} a\_{11}& a\_{21} & a\_{12} & a\_{22} \\ a\_{31}& a\_{41} & a\_{32} & a\_{42} \\ a\_{51}& a\_{61} & a\_{52} & a\_{62} \\ a\_{13}& a\_{23} & a\_{14} & a\_{24} \\ a\_{33}& a\_{43} & a\_{34} & a\_{44} \\ a\_{53}& a\_{63} & a\_{54} & a\_{64} \end{bmatrix}

So, my question is: Is there an easy/obvious way of doing this transformation or am I stuck with writing a depth 4 nested `for` loop?

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [September 8, 2018, 8:09pm UTC](https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702/2 "2018-09-08T20:09:53Z")

</div>

The Kronecker product orders dimensions inversely than how Julia orders tensors. The first dimension in julia corresponds to the last dimension of the Kronecker product. The simplest way to fix you problem would therefore be to swap the order of the two matrices.

In a way, this actually makes sense: it’s like a binary number (or any number really) were the first number is on the right.

Also see [https://github.com/JuliaLang/julia/pull/28697](https://github.com/JuliaLang/julia/pull/28697) for more discussion. Note that I don’t believe that there is any problem with how things work currently. It’s just a matter of choosing which dimension is “first”.

---

<div class="post-metadata">

**Author:** ![CNOT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cnot/32/5190_2.png) [@CNOT](https://discourse.julialang.org/u/CNOT)\
**Post date:** [September 8, 2018, 8:34pm UTC](https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702/4 "2018-09-08T20:34:34Z")

</div>

Thanks. I see. So I should do something like this instead:

```julia
H1 = reshape(H,(m2,m1,n2,n1))
H2 = permutedims(H1,(1,3,2,4))
H3 = permutedims(reshape(H2,(m2*n2,m1*n1)),(2,1))

```

As was mentioned in the thread you linked it might be wise for me to write my own tensor product function. Luckily ⊗ is not binded.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [September 8, 2018, 9:17pm UTC](https://discourse.julialang.org/t/reshaping-a-matrix-for-reverse-kronecker-product-or-schmidt-decomposition/14702/5 "2018-09-08T21:17:12Z")

</div>

> [@CNOT](#):
>
> Thanks. I see. So I should do something like this instead:

Try it out! I’ll admit I haven’t looked at exactly what you needed to do, but I figured that the dimension ordering was the problem.
