In [1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
import numpy as np
In [5]:
D = np.random.random((10000, 10))
A = np.random.random((10, 1000))
B = np.random.random((1000, 5))
C = np.random.random((5, 50))
In [6]:
from mdot import mdot, print_optimal_chain_order
from mdot import _mdot, _mdot_three
In [7]:
%timeit mdot(A, B, C, optimize=True)
%timeit np.dot(np.dot(A, B), C)
%timeit np.dot(A, np.dot(B, C))
(AB)C
is the optimal solution. mdot
yields basically the same performace (plus some overhead).
Let's determine the optimal solution to use it later:
In [10]:
print_optimal_chain_order(D, A, B, C)
print_optimal_chain_order(D, A, B, C, names=list("DABC"))
Test 4 arguments
In [6]:
print "inline optimization:"
%timeit mdot(D, A, B, C, optimize=True)
print "precalculated optimal parens"
%timeit np.dot(np.dot(D, np.dot(A, B)), C)
%timeit np.dot(np.dot(np.dot(D, A), B), C)
%timeit np.dot(np.dot(D, A), np.dot(B, C))
%timeit np.dot(D, np.dot(A, np.dot(B, C)))
%timeit np.dot(D, np.dot(np.dot(A, B), C))
%timeit np.dot(np.dot(D, np.dot(A, B)), C)