Tensor contraction resulting index order

contraction-operatortensor-productstensors

I am trying to implement tensor contraction in C++ through multidimensional arrays, and while the idea of how to sum over the contraction indices is clear, I am uncertain as to the order in which the dimensions should be arranged in the resulting tensor. Assume I have tensors $A$ and $B$ with indices $\alpha_1…\alpha_N$ and $\beta_1…\beta_M$, and assume that $\alpha_{i_1},…,\alpha_{i_p}$ are upper indices, and $\beta_{j_1},…,\beta_{j_p}$ are lower indices, such that the dimension matches between those, and I pair them in that order to sum over those. How would the dimensions in the resulting tensor be arranged? Do I get a tensor with indices arranged as:
$$C_{\alpha_1…\alpha_{i_1-1}\alpha_{i_1+1}…\alpha_{i_p-1}\alpha_{i_p+1}…\alpha_{i_N}\beta_1…\beta_{j_1-1}\beta_{j_1+1}…\beta_{j_p-1}\beta_{j_p+1}…\beta_{M}}$$

I was reading through the following reference: http://www.cs.cornell.edu/cv/SummerSchool/Contractions.pdf

However the way indices are arranged there in the resulting tensor is:

$$C(i,j,p,q) = \sum_{k}\sum_{s}A(i,s,p,k)B(s,j,k,q)$$

Which doesn't seem to match the above.

Best Answer

In mathematics there is no defined order in tensor product or contraction. Index order matters when 1) you want tensor to match some particular definition 2) you want to highlight the symmetry of the result, for example, $A_{ikjl}$ makes sense better if there is symmetry on $ik$ and $jl$, than $ij$ and $kl$. So I would leave this to user (see numpy.einsum ):

multiply(A, B, "ispk,sjkq->ijpq")

Shortly, there is no convention. Tensors are loved because the order of terms doesn't matter and all the syntax information is in indices, so one can use the order of terms to convey some semantic information (like we do with simple multiplication). However, if you insist on fixed order, why use indices at all? You can define tensor product (append dimensions as they are in order) and contraction (cross out dimensions). So $C_{ijkl}=A_{ipjq}B_{pkql}$ is

Tcontract(Tproduct(A,B),2,5,4,7)

Related Question