2
$\begingroup$

I have a tensor

    T = Array[Subscript[K, ##] &, {2, 2, 2, 2}]

And its matrix form is $$ \left( \begin{array}{cc} \left( \begin{array}{cc} K_{1,1,1,1} & K_{1,1,1,2} \\ K_{1,1,2,1} & K_{1,1,2,2} \\ \end{array} \right) & \left( \begin{array}{cc} K_{1,2,1,1} & K_{1,2,1,2} \\ K_{1,2,2,1} & K_{1,2,2,2} \\ \end{array} \right) \\ \left( \begin{array}{cc} K_{2,1,1,1} & K_{2,1,1,2} \\ K_{2,1,2,1} & K_{2,1,2,2} \\ \end{array} \right) & \left( \begin{array}{cc} K_{2,2,1,1} & K_{2,2,1,2} \\ K_{2,2,2,1} & K_{2,2,2,2} \\ \end{array} \right) \\ \end{array} \right) $$

I use

    ArrayReshape[T, {4, 4}]

and get the matrix

$$ \left( \begin{array}{cccc} K_{1,1,1,1} & K_{1,1,1,2} & K_{1,1,2,1} & K_{1,1,2,2} \\ K_{1,2,1,1} & K_{1,2,1,2} & K_{1,2,2,1} & K_{1,2,2,2} \\ K_{2,1,1,1} & K_{2,1,1,2} & K_{2,1,2,1} & K_{2,1,2,2} \\ K_{2,2,1,1} & K_{2,2,1,2} & K_{2,2,2,1} & K_{2,2,2,2} \\ \end{array} \right). $$

However, I want the matrix looks exactly like the matrix form of the tensor T. How can I do it in Mathematica?

Ps: This can be done by

    ArrayFlatten[T]

but When the Dimension of T grows, it doesn't works. For example, if

    T = Array[Subscript[K, ##] &, {2, 2, 2, 2, 2, 2}]

then I need to use

    ArrayFlatten[ArrayFlatten[T]]

to get the same elements matrix as the matrixform of the tensor T.

$\endgroup$
0

1 Answer 1

3
$\begingroup$
T = Array[Subscript[K, ##] &, {2, 2, 2, 2}];
Flatten[T, {{1, 3}, {2, 4}}] // MatrixForm

$$ \left( \begin{array}{cccc} K_{1,1,1,1} & K_{1,1,1,2} & K_{1,2,1,1} & K_{1,2,1,2} \\ K_{1,1,2,1} & K_{1,1,2,2} & K_{1,2,2,1} & K_{1,2,2,2} \\ K_{2,1,1,1} & K_{2,1,1,2} & K_{2,2,1,1} & K_{2,2,1,2} \\ K_{2,1,2,1} & K_{2,1,2,2} & K_{2,2,2,1} & K_{2,2,2,2} \\ \end{array} \right) $$

T = Array[Subscript[K, ##] &, {2, 2, 2, 2, 2, 2}];
Flatten[T, {{1, 3, 5}, {2, 4, 6}}] // MatrixForm

$$ \left( \begin{array}{cccccccc} K_{1,1,1,1,1,1} & K_{1,1,1,1,1,2} & K_{1,1,1,2,1,1} & K_{1,1,1,2,1,2} & K_{1,2,1,1,1,1} & K_{1,2,1,1,1,2} & K_{1,2,1,2,1,1} & K_{1,2,1,2,1,2} \\ K_{1,1,1,1,2,1} & K_{1,1,1,1,2,2} & K_{1,1,1,2,2,1} & K_{1,1,1,2,2,2} & K_{1,2,1,1,2,1} & K_{1,2,1,1,2,2} & K_{1,2,1,2,2,1} & K_{1,2,1,2,2,2} \\ K_{1,1,2,1,1,1} & K_{1,1,2,1,1,2} & K_{1,1,2,2,1,1} & K_{1,1,2,2,1,2} & K_{1,2,2,1,1,1} & K_{1,2,2,1,1,2} & K_{1,2,2,2,1,1} & K_{1,2,2,2,1,2} \\ K_{1,1,2,1,2,1} & K_{1,1,2,1,2,2} & K_{1,1,2,2,2,1} & K_{1,1,2,2,2,2} & K_{1,2,2,1,2,1} & K_{1,2,2,1,2,2} & K_{1,2,2,2,2,1} & K_{1,2,2,2,2,2} \\ K_{2,1,1,1,1,1} & K_{2,1,1,1,1,2} & K_{2,1,1,2,1,1} & K_{2,1,1,2,1,2} & K_{2,2,1,1,1,1} & K_{2,2,1,1,1,2} & K_{2,2,1,2,1,1} & K_{2,2,1,2,1,2} \\ K_{2,1,1,1,2,1} & K_{2,1,1,1,2,2} & K_{2,1,1,2,2,1} & K_{2,1,1,2,2,2} & K_{2,2,1,1,2,1} & K_{2,2,1,1,2,2} & K_{2,2,1,2,2,1} & K_{2,2,1,2,2,2} \\ K_{2,1,2,1,1,1} & K_{2,1,2,1,1,2} & K_{2,1,2,2,1,1} & K_{2,1,2,2,1,2} & K_{2,2,2,1,1,1} & K_{2,2,2,1,1,2} & K_{2,2,2,2,1,1} & K_{2,2,2,2,1,2} \\ K_{2,1,2,1,2,1} & K_{2,1,2,1,2,2} & K_{2,1,2,2,2,1} & K_{2,1,2,2,2,2} & K_{2,2,2,1,2,1} & K_{2,2,2,1,2,2} & K_{2,2,2,2,2,1} & K_{2,2,2,2,2,2} \\ \end{array} \right) $$

Et cetera. For example, for a rank-8 tensor,

n = 8;
T = Array[Subscript[K, ##] &, ConstantArray[2, n]];
Flatten[T, {Range[1, n, 2], Range[2, n, 2]}] // MatrixForm

$$ \left( \begin{array}{cccccccccccccccc} K_{1,1,1,1,1,1,1,1} & K_{1,1,1,1,1,1,1,2} & K_{1,1,1,1,1,2,1,1} & K_{1,1,1,1,1,2,1,2} & K_{1,1,1,2,1,1,1,1} & K_{1,1,1,2,1,1,1,2} & K_{1,1,1,2,1,2,1,1} & K_{1,1,1,2,1,2,1,2} & K_{1,2,1,1,1,1,1,1} & K_{1,2,1,1,1,1,1,2} & K_{1,2,1,1,1,2,1,1} & K_{1,2,1,1,1,2,1,2} & K_{1,2,1,2,1,1,1,1} & K_{1,2,1,2,1,1,1,2} & K_{1,2,1,2,1,2,1,1} & K_{1,2,1,2,1,2,1,2} \\ K_{1,1,1,1,1,1,2,1} & K_{1,1,1,1,1,1,2,2} & K_{1,1,1,1,1,2,2,1} & K_{1,1,1,1,1,2,2,2} & K_{1,1,1,2,1,1,2,1} & K_{1,1,1,2,1,1,2,2} & K_{1,1,1,2,1,2,2,1} & K_{1,1,1,2,1,2,2,2} & K_{1,2,1,1,1,1,2,1} & K_{1,2,1,1,1,1,2,2} & K_{1,2,1,1,1,2,2,1} & K_{1,2,1,1,1,2,2,2} & K_{1,2,1,2,1,1,2,1} & K_{1,2,1,2,1,1,2,2} & K_{1,2,1,2,1,2,2,1} & K_{1,2,1,2,1,2,2,2} \\ K_{1,1,1,1,2,1,1,1} & K_{1,1,1,1,2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,2,2,2,2,1,1,2} & K_{2,2,2,2,2,2,1,1} & K_{2,2,2,2,2,2,1,2} \\ K_{2,1,2,1,2,1,2,1} & K_{2,1,2,1,2,1,2,2} & K_{2,1,2,1,2,2,2,1} & K_{2,1,2,1,2,2,2,2} & K_{2,1,2,2,2,1,2,1} & K_{2,1,2,2,2,1,2,2} & K_{2,1,2,2,2,2,2,1} & K_{2,1,2,2,2,2,2,2} & K_{2,2,2,1,2,1,2,1} & K_{2,2,2,1,2,1,2,2} & K_{2,2,2,1,2,2,2,1} & K_{2,2,2,1,2,2,2,2} & K_{2,2,2,2,2,1,2,1} & K_{2,2,2,2,2,1,2,2} & K_{2,2,2,2,2,2,2,1} & K_{2,2,2,2,2,2,2,2} \\ \end{array} \right) $$

$\endgroup$
4
  • $\begingroup$ How to do the inverse operation? From the Matrix to the same arrange tensor? $\endgroup$
    – LX.CC
    Commented Aug 31, 2022 at 7:36
  • $\begingroup$ The inverse can be done by ``` Partition[Flatten[T, {{1, 3}, {2, 4}}] , {2, 2}]```! $\endgroup$
    – LX.CC
    Commented Aug 31, 2022 at 7:47
  • $\begingroup$ But for the high dimension, I need to do Partition[Partition[...]]. Is there any way to do this? $\endgroup$
    – LX.CC
    Commented Aug 31, 2022 at 7:52
  • 2
    $\begingroup$ Please ask a separate question. $\endgroup$
    – Roman
    Commented Aug 31, 2022 at 8:01

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.