[Numpy-discussion] Programmatically contracting multiple tensors

Andras Deak deak.andris at gmail.com
Fri Mar 12 20:09:02 EST 2021


On Sat, Mar 13, 2021 at 1:32 AM Eric Wieser <wieser.eric+numpy at gmail.com> wrote:
>
> 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.

It's mentioned (albeit somewhat cryptically) sooner in the Notes:

"einsum also provides an alternative way to provide the subscripts and
operands as einsum(op0, sublist0, op1, sublist1, ..., [sublistout]).
If the output shape is not provided in this format einsum will be
calculated in implicit mode, otherwise it will be performed
explicitly. The examples below have corresponding einsum calls with
the two parameter methods.

New in version 1.10.0."

Not that this helps much, because I definitely wouldn't understand
this API without the examples.
But I'm not sure _where_ this could be highlighted among the
parameters; after all this is all covered by the *operands parameter.

András



> Eric
>
> On Sat, 13 Mar 2021 at 00:25, Michael Lamparski <diagonaldevice at gmail.com> 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
>> NumPy-Discussion at python.org
>> https://mail.python.org/mailman/listinfo/numpy-discussion
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list