Reputation: 259
For my understanding, tensordot http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.tensordot.html is multiply two tensors and sum given indices. This is precisely einsum does.
Actually what is the difference between this two functions? Is that due to different performance?
Upvotes: 3
Views: 1408
Reputation: 231385
They are different approaches to similar problems. einsum
is more general. Speed can be similar, though you need to check individual cases.
tensordot
works by reshaping and transposing axes, reducing the problem to one that np.dot
can solve. Its code, up to the dot
call is Python, so you can read it for yourself.
einsum
is built 'from-ground-up' to work with the 'Einstein notation' that is used in physics (it was written by scientist for fit his needs and usage). The documentation covers that. It is C code, so is a little harder to study. Basically it parses the indexing string, and builds an nditer
object that will iterate over the input arrays, performing some sort of sum-of-products calculation. It can take short cuts in case where you just want indexing, diagonal, etc.
There have been number of questions asking about either of these functions, or suggesting their use in the answers.
In new versions there is also a np.matmul
that generalized dot
in a different way. It is linked to the new @
operator in Python3.5.
Upvotes: 8