On Sat, Jun 12, 2010 at 4:30 PM, Alan Bromborsky <[email protected]> wrote:
> 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?
looking at numpy docs, diagonal seems to do the trick
>>> a = np.arange(8).reshape(2,2,2)
>>> a
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>> a.diagonal(0,0,1)
array([[0, 6],
[1, 7]])
>>> a[:,:,0]
array([[0, 2],
[4, 6]])
>>> a[:,:,1]
array([[1, 3],
[5, 7]])
>>> a.diagonal(0,0,1).sum(1)
array([6, 8])
Josef
> _______________________________________________
> NumPy-Discussion mailing list
> [email protected]
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion