# Programmatically contracting multiple tensors

4 messages
Open this post in threaded view
|

## Programmatically contracting multiple tensors

 Greetings,I have something in my code where I can receive an array M of unknown dimensionality and a list of "labels" for each axis.  E.g. perhaps I might get an array of shape (2, 47, 3, 47, 3) with labels ['spin', 'atom', 'coord', 'atom', 'coord'].For every axis that is labeled "coord", I want to multiply in some rotation matrix R.  So, for the above example, this could be done with the following handwritten line:return np.einsum('Cc,Ee,abcde->abCdE', R, R, M)But since I want to do this programmatically, I find myself in the awkward situation of having to construct this string (and e.g. having to arbitrarily limit the number of axes to 26 or something like that).  Is there a more idiomatic way to do this that would let me supply integer labels for summation indices?  Or should I just bite the bullet and start generating strings?---Michael _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion
Open this post in threaded view
|

## Re: Programmatically contracting multiple tensors

 Einsum has a secret integer argument format that appears in the Examples section of the `np.einsum` docs, but appears not to be mentioned at all in the parameter listing.EricOn Sat, 13 Mar 2021 at 00:25, Michael Lamparski <[hidden email]> wrote:Greetings,I have something in my code where I can receive an array M of unknown dimensionality and a list of "labels" for each axis.  E.g. perhaps I might get an array of shape (2, 47, 3, 47, 3) with labels ['spin', 'atom', 'coord', 'atom', 'coord'].For every axis that is labeled "coord", I want to multiply in some rotation matrix R.  So, for the above example, this could be done with the following handwritten line:return np.einsum('Cc,Ee,abcde->abCdE', R, R, M)But since I want to do this programmatically, I find myself in the awkward situation of having to construct this string (and e.g. having to arbitrarily limit the number of axes to 26 or something like that).  Is there a more idiomatic way to do this that would let me supply integer labels for summation indices?  Or should I just bite the bullet and start generating strings?---Michael _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion _______________________________________________ NumPy-Discussion mailing list [hidden email] https://mail.python.org/mailman/listinfo/numpy-discussion