[Numpy-discussion] add elementwise addition & subtraction to einsum

Matthew Harrigan harrigan.matthew at gmail.com
Sun Oct 16 08:47:38 EDT 2016


This is a follow on for issue 8139
<https://github.com/numpy/numpy/issues/8139>.  I propose adding elementwise
addition and subtraction functionality to einsum.  I love einsum as it
clearly and concisely defines complex linear algebra.  However elementwise
addition is a very common linear algebra operation and einsum does not
currently support it.  The Einstein field equations
what the notation was originally developed to document, contain that
functionality.  It is fairly common in stress analysis (my background), for
example see these lectures notes

Specifically I propose adding "+" and "-" characters which separate current
einsum statements which are then combined elementwise.  An example is A =
np.einsum('ij,jk+ij,jk', B, C, D, E), which is A = B * C + D * E.  I wrote
a crude function
to demonstrate the functionality.

I believe the functionality is useful, in keeping with the spirit of a
clean concise API, and doesn't break the existing API, which could warrant

Additionally I believe it opens the possibility of many interesting
performance optimizations.  For instance, many of the optimizations in this
NEP <http://docs.scipy.org/doc/numpy/neps/deferred-ufunc-evaluation.html>
could be done internally to the einsum function, which may be easier to
accomplish given the narrower scope (but I am ignorant of all the low level
C internals of numpy).  The example in the beginning could become A =
np.einsum('...+...+...', B, C, D).

Thank you for your time and consideration.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20161016/6f24b5b8/attachment.html>

More information about the NumPy-Discussion mailing list