# [Numpy-discussion] Tensor contraction

Friedrich Romstedt friedrichromstedt@gmail....
Sat Jun 12 16:15:16 CDT 2010

2010/6/12 Alan Bromborsky <abrombo@verizon.net>:
> If I have a single numpy array, for example with 3 indices T_{ijk} and I
> want to sum over two them in the sense of tensor contraction -
>
> T_{k} = \sum_{i=0}^{n-1} T_{iik}.  Is there an easy way to do this with
> numpy?

Also you can give:

T[I, I, :].sum(axis=0)

a try with:

I = numpy.arange(0, n)  .

This has the benefit to not be limited to two-dimensional diagonals,
like .diagonal() is.

But note that for:
T[:, I, I]
the shape is reversed with respect to that of:
T[I, :, I]  and T[I, I, :]  .

I think it should be written in the docs how the shape is derived.

hth,
Friedrich