2
$\begingroup$

I have to write a script in which I have to work with a very simple tensor network but I can't find a nice scalable way to make this work.

The basic object I am working with is a tensor more or less of this form (using the Einstein summation convention) $A_{i_1....i_n}^{j_1...j_n} = T^{[1]}_{i_1k_1}(T^{[2]})^{k_1j_1}_{i_2k_2}(T^{[3]})^{k_2j_2}_{i_3k_3}...$ etc., i.e. some contraction of $\bigotimes_kT^{[k]}$.

I then need to change the representation of this tensor by collecting all lower and upper indices $I={i_1,i_2,...,i_n}$, $J={j_1,...,j_n}$ to effectively obtain a matrix $A_I^J$.

To achieve the first part I can use the TensorProduct function followed by TensorContract, but then I cannot think of a smart scalable way (i.e. code that doesn't depend on $n$) to group indices together. Mathematica has the KroneckerProduct function to represent tensor products of matrices as matrices, but is there some analogue for the cases in which I don't have a tensor product but a more generic tensor? Or is there another smart way to achieve this?

If you look at it in MatrixForm, at the end of the day I only have to remove internal parenthesis. I can't believe that there is no smart automated way to do this :)

$\endgroup$
1
  • 1
    $\begingroup$ "at the end of the day I only have to remove internal parenthesis" - can you use Flatten with an adequate level specification then? You did not include your actual expression, so I can't try it myself, unfortunately. $\endgroup$
    – MarcoB
    Jun 17, 2020 at 15:39

1 Answer 1

2
$\begingroup$

Probably Transpose and ArrayReshape can do the conversion into a matrix for you.

A = Array[a, {2, 2, 2, 2, 3, 3, 3, 3}];
i = Range[1, TensorRank[A], 2];
j = Range[2, TensorRank[A], 2];
B = ArrayReshape[ Transpose[A, Join[i, j]], {Times @@ Dimensions[A][[i]], Times @@ Dimensions[A][[j]]}];
B //Dimensions

{36, 36}

$\endgroup$
2

Your Answer

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

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