Skip to content

Instantly share code, notes, and snippets.

Created May 22, 2015 05:36
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Einstein sum notation
import numpy as np
rng = np.random.RandomState(0)
print "Trace"
A = rng.rand(3, 3)
print np.trace(A)
print np.einsum("ii", A)
print "Dot product"
a = rng.rand(5)
b = rng.rand(5)
print, b)
print np.einsum("i,i", a, b)
print "Outer product"
print np.outer(a, b)
print np.einsum("i,j", a, b)
print "Identity"
B = rng.rand(3, 2)
print B
print np.einsum("ij", B)
print "Transpose"
print B.T
print np.einsum("ji", B)
print "Sum"
print np.einsum('i->', a)
print np.sum(a)
print "Diag"
print np.einsum("ii->i", A)
print np.diag(A)
print "Matrix product"
print np.einsum("ij,jk", A, B)
print, B)
Copy link

is it a matlab code? if not, than what is the name of this software

Copy link

goulu commented Oct 2, 2015

it's Python ! with NumPy's excellent einsum function ( see )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment