Instantly share code, notes, and snippets.

# mblondel/einsum.py

Created May 22, 2015 05:36
Show Gist options
• Save mblondel/379b404de355a095d448 to your computer and use it in GitHub Desktop.
Einstein sum notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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 print "Dot product" a = rng.rand(5) b = rng.rand(5) print np.dot(a, b) print np.einsum("i,i", a, b) print print "Outer product" print np.outer(a, b) print np.einsum("i,j", a, b) print print "Identity" B = rng.rand(3, 2) print B print np.einsum("ij", B) print print "Transpose" print B.T print np.einsum("ji", B) print print "Sum" print np.einsum('i->', a) print np.sum(a) print print "Diag" print np.einsum("ii->i", A) print np.diag(A) print print "Matrix product" print np.einsum("ij,jk", A, B) print np.dot(A, B) print

### saanusman commented Jul 29, 2015

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

### goulu commented Oct 2, 2015

it's Python ! with NumPy's excellent einsum function ( see http://ajcr.net/Basic-guide-to-einsum/ )