Einstein Summation in Numpy
obilaniu6266h16.wordpress.com
obilaniu6266h16.wordpress.com
I made a C++ operator overloading implementation of Ricci calculus once upon a time. That way you could define your tensors and then write statements like:
T = (rho + p)*u("^a")u("^b") + p * g("^a^b");
traceT = T("^a_a");
Anyways, I find the domain specific language of this particular numpy function to be a bit abhorrent. Why not write "ik,kj->ij" as "M_ij = A_ik * B_kj" and make it much more obvious what is happening instead of requiring this understanding. Heck, if you used kwargs, you could even avoid passing the same argument multiple times like in the quadratic form example: x = np.ricci_calculus("M_ij v^i v^j", M, v)
Here I am leaving out that "x = ..." prefix in the DSL since you can take an ordering convention and a scalar has no slots anyways. @einsum M[i,j] := A[i,k] * B[k,j]
Or faster with @tensor from https://github.com/Jutho/TensorOperations.jl & the same notation. I'm not aware of any attempt to distinguish co-vectors; you can have g = Diagonal([-1,1,1,1]) and insert A[i,k]g[k,k′]B[k′,j] and this is handled efficiently.Well to distinguish covectors you need to define something like a manifold or just any canonical mapping between the tangent and cotangent space and then when you define your first few tensors you define them in the scope of that mapping.
There is some generalization to gauge fields where a bijective canonical mapping may not be possible in general, but I never studied that extensively.
If you had reason to do so, it would be easy to bolt something onto the front of @tensor etc. which understood say A^[i]_[j] * B^[j,k] . Something similar would not be so hard to do to np.einsum either, just messing with the string... it could take exactly your "email Latex notation". In fact I'm a bit surprised that I can't find someone who's done this, in a few minutes' googling. I did find that opt-einsum has a notation in which the indices at least follow the array:
https://optimized-einsum.readthedocs.io/en/latest/input_form...
You have honestly piqued my curiosity towards Julia. I will need to find an opportunity to dig in.
Vector inner product: "a,a->" (Assumes two vectors of same length)
Vector element-wise product: "a,a->a" (Assumes two vectors of same length)
Vector outer product: "a,b->ab" (Vectors not necessarily same length.)
Matrix transposition: "ab->ba"
Matrix diagonal: "ii->i"
Matrix trace: "ii->"
1-D Sum: "a->"
2-D Sum: "ab->"
3-D Sum: "abc->"
Matrix inner product "ab,ab->" (If you pass twice the same argument, it becomes a matrix L2 norm)
Left-multiplication Matrix-Vector: "ab,b->a"
Right-multiplication Vector-Matrix: "a,ab->b"
Matrix Multiply: "ab,bc->ac"
Batch Matrix Multiply: "Yab,Ybc->Yac"
Quadratic form / Mahalanobis Distance: "a,ab,b->"The same convention as in NumPy, but IMHO much nicer visual explanation for beginners. Also, think about tensor diagrams, as in the Feynman diagram in "Simple diagrams of convolutional neural networks" https://medium.com/inbrowserai/simple-diagrams-of-convoluted....
[1] https://github.com/michaelklachko/CapsNet/blob/master/capsne...
OptEinsum is also worth checking out if you use Einsum a lot, it has additional features to what is in Numpy.
I will try to update the title. There are no changes since then that diminish this document's value - the einsum notation is still the same. The biggest change since then is that the numpy.einsum implementation has been tremendously optimized and has become the benchmark in capabilities that other libraries follow.
Edit: Opt_Einsum is amazing and was a big part of the optimization efforts - it has been merged fully into numpy since v1.12. See https://github.com/dgasmith/opt_einsum#news-opt_einsum-will-...
Disclaimer: I am the author of the `opt_einsum` package.
Using the Optimize kewarg is a must for any summation involving a few inputs, which is why I brought it up. It is one of the things which is not discussed in a post from 2016.
Arrays of numbers should be a native type of a programming language used for serious numerical computing. Strings and dictionaries are nice, but I can do without. However, arrays of numbers are fundamental and it is very sad that they are accessible only via idiosyncratic libraries such as numpy or pytorch.
It’s not used much for scientific computing because numpy made a better tool.
Also, the list data structure in Python is a contiguous memory array... it’s just designed to store a contiguous array of PyObject pointers, and the use case for list is exactly when the flexibility and abstraction of using PyObject data structures is more important than raw performance. You would use numpy arrays or special jitted functions or custom C-level data structures you write in Cython, etc. etc., when raw performance is the higher priority.
I agree with this philosophy! But that is certainly not at all the case with python, where fancy strings and even fancier dictionaries are readily available at the language level, instead of being implemented as separate libraries that you have to import.
If Python did not support strings nor dictionaries as basic data types, you might have a point. But it does, and it chooses to ignore multidimensional arrays and their basic operations. This is weird, and even a bit offensive when all you need to do is dealing with such arrays of numbers. You always feel like an "outsider", unlike when you program in octave or julia, for example.
Even though I use Python every day, and I have used scipy often, I still reach for Octave whenever I need a quick calculation that I don't feel like turning into a big project.
And yeah, it's nice that matrices are first-class objects. Sometimes I miss 1-dimensional arrays or sensible strings, but it's not a big deal, and for quick, short things, the Octave syntax is still a lot easier to deal with than Python.
Most of Octave's users describe themselves as "not a programmer", despite using a programming language, so I think there's still room for this kind of software in the world.
- the existence of tools for working with strings and dictionaries doesn't take away from numpy at all
- I have never been offended by a programming language or felt like an outsider while using numpy
Most numeric libraries can be Described as functions of/on ndarrays that return ndarrays, not having a language wide ndarray is the equivalent of having every library carry it's own unique list interface/implementation.
indeed! a sizeable part of numpy is written in fortran
See also: https://stackoverflow.com/questions/1303182/how-does-blas-ge...
This is not a library problem, it is a focus problem of the language. The language supports strings and dictionaries natively, as basic data types, but not numeric arrays.
Einsum will also make you more youthful, you’ll grow back missing hair, be a big success on the dating scene, get a promotion at work, and feel a new lease on life!