In [1]:
import sys

from sympy.simplify import old_cse_main # I've let previous cse_main.py and cse_opts.py files as old_*.py in sympy/simplify/
from sympy.simplify import cse_main
prev_cse = old_cse_main.cse
new_cse = cse_main.cse
import sympy
from sympy.utilities import numbered_symbols
from sympy.core.cache import clear_cache
In [2]:
print("Processors:")
!grep "model name" /proc/cpuinfo

print("\nSystem:")
!uname -a

print("\nPython:\n%s"%sys.version)
    
print("\nSympy fast_cse branch:")
!(cd $my_sympy_path; git show -s)

print("\nbased on Sympy version: %s\n"%sympy.__version__)
Processors:
model name	: Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name	: Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name	: Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name	: Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz

System:
Linux kapa42 3.10.10-1-ARCH #1 SMP PREEMPT Fri Aug 30 11:30:06 CEST 2013 x86_64 GNU/Linux

Python:
3.3.2 (default, May 21 2013, 15:40:45) 
[GCC 4.8.0 20130502 (prerelease)]

Sympy fast_cse branch:
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

based on Sympy version: 0.7.3

Processors:
model name  : Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name  : Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name  : Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz
model name  : Intel(R) Core(TM) i3 CPU       M 380  @ 2.53GHz

System:
Linux kapa42 3.10.10-1-ARCH #1 SMP PREEMPT Fri Aug 30 11:30:06 CEST 2013 x86_64 GNU/Linux

Python:
3.3.2 (default, May 21 2013, 15:40:45) 
[GCC 4.8.0 20130502 (prerelease)]

Sympy fast_cse branch:
fatal: Not a git repository (or any parent up to mount point /home)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

based on Sympy version: 0.7.3
In [2]:
 
In [6]:
def count(exprs):
    ops = '=', '+', '-', '*', '/', '**', 'sin', 'cos'
    s = str(exprs)
    op_counts = {op: s.count(op) for op in ops}
    op_counts['='] = len(exprs[0]) if isinstance(exprs, tuple) else 0
    op_counts['*'] = op_counts['*'] - op_counts['**']
    return '\t'.join([str(op_counts[op]) + ' ' + op for op in ops])

def undo_cse(cse_output):
    from sympy.matrices import Matrix
    from sympy.core.compatibility import iterable
    from sympy import Basic

    subs = dict((s, e) for (s, e) in cse_output[0])
    exprs = cse_output[1]

    def _recreate(expr):
        if iterable(expr):
            out = type(expr)(*map(_recreate, expr))
        elif expr in subs:
            out = _recreate(subs[expr])
        elif isinstance(expr, Basic) and expr.is_Atom:
            out = expr
        else:
            out = type(expr)(*map(_recreate, expr.args))
        return out

    single = False
    if isinstance(exprs, Basic) or isinstance(exprs, Matrix): # if only one expression or one matrix is passed
        exprs = [exprs]
        single = True

    expanded_exprs = []
    for expr in exprs:
        if isinstance(expr, Matrix):
            expanded_exprs.append(expr.applyfunc(_recreate))
        else:
            expanded_exprs.append(_recreate(expr))

    if single:
        expanded_exprs = expanded_exprs[0]

    return expanded_exprs


def verify_cse(orig_exprs, cse_output):
    from sympy import Basic
    
    if isinstance(orig_exprs, Basic):
        orig_exprs = [orig_exprs]
        
    cse_exprs = undo_cse(cse_output)
    
    fail = False
    
    for orig_expr, cse_expr in zip(orig_exprs, cse_exprs):
        diff = orig_expr - cse_expr
        diff = sympy.simplify(diff).expand()
        if diff != 0:
            print(diff)
            fail = True
    
    return not fail
In [6]:
 
In [7]:
large_exprs = sympy.sympify('''[a27*a4 - (a1*a7 + 9.81*a14*cos(a13) + 
a15*(-a25*a26*sin(a13) + a4*cos(a13)) + a22*(-a25*a26*cos(a13) - a4*sin(a13)) - 
a25*(-a1*a25*sin(a13) + a10*a25*cos(a13) + a26*a5)*cos(a13) + a26*(a10*a26 - 
a15*a25*sin(a13) + a21*a25*cos(a13)) - (0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*(-a12*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + 
a7*sin(a18) + (-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) + 
a19*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) + 
a20*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) - 
(-a25*sin(a13)*sin(a18) - a26*cos(a18))*(a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - (-a25*sin(a13)*cos(a18) + 
a26*sin(a18))*(-a19*(-a25*cos(a13) - a6) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))))*sin(a18) - 
0.55*(a12*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) + a20*(-0.045*a25*a6*cos(a13) 
- a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) - 
a23*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) - (a25*cos(a13) + 
a6)*(-a19*(-a25*cos(a13) - a6) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - 
(a25*sin(a13)*sin(a18) + a26*cos(a18))*(-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))))*sin(a18) - 
0.55*(-a19*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) + a20*(0.045*a24 - 
a6*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + 
(-a25*a26*sin(a13) + a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
a23*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - (-a25*cos(a13) - 
a6)*(a12*(-a25*cos(a13) - a6) + a20*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - 
(a25*sin(a13)*cos(a18) - a26*sin(a18))*(-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))))*cos(a18) + 
(-a12*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) + 
a16*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) + a17*(-a24 + a25*a26*sin(a13) - 
a4*cos(a13)) + a23*(0.045*a24 - a6*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + (-a25*a26*sin(a13) + 
a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 0.55*(-a25*a26*cos(a13) - 
a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
a3*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - (a25*cos(a13) + 
a6)*(a16*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a19*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18)) + a2*(a25*sin(a13)*sin(a18) + a26*cos(a18)) - 
a23*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) + a9*(-a25*cos(a13) - a6)) 
- (a25*sin(a13)*sin(a18) + a26*cos(a18))*(a0*(-a25*cos(a13) - a6) + 
a12*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) + 
a17*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) - a19*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a9*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - 
(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) - 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))*(-a19*(-a25*cos(a13) - a6) 
+ a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - (-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)))*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6))*cos(a18) - (a16*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + 
a7*sin(a18) + (-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) + 
a19*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) + 
a2*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) - a23*(-0.045*a25*a6*cos(a13) - 
a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) + 
a9*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) - (-a25*cos(a13) - 
a6)*(-a12*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)) + 
a16*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + a17*(-a25*cos(a13) - a6) + 
a23*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a3*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - (-0.55*a25*sin(a13)*sin(a18) - 
0.55*a26*cos(a18))*(-a12*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + 
a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18))) - (a25*sin(a13)*cos(a18) - 
a26*sin(a18))*(a0*(-a25*cos(a13) - a6) + a12*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) + a17*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) - 
a19*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a9*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - (a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18))*(a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))))*sin(a18))*sin(a13) + (-a0*(-a24 + 
a25*a26*sin(a13) - a4*cos(a13)) + a10*a7 - a12*(-0.045*a25*a6*cos(a13) - 
a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) + 
9.81*a14*sin(a13) + a15*(-a25*a26*cos(a13) - a4*sin(a13)) - 
a17*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) + a19*(0.045*a24 - 
a6*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + 
(-a25*a26*sin(a13) + a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
a21*(-a25*a26*sin(a13) + a4*cos(a13)) - a25*(-a1*a25*sin(a13) + a10*a25*cos(a13) 
+ a26*a5)*sin(a13) - a26*(a1*a26 + a15*a25*cos(a13) - a22*a25*sin(a13)) - 
a9*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) + (-a25*sin(a13)*sin(a18) - 
a26*cos(a18))*(-a12*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18)) + a16*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a17*(-a25*cos(a13) - a6) + a23*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a3*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) + 
(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18))*(-a19*(-a25*cos(a13) - a6) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) + (-a25*sin(a13)*cos(a18) + 
a26*sin(a18))*(a16*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + 
a19*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)) + 
a2*(a25*sin(a13)*sin(a18) + a26*cos(a18)) - a23*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) + a9*(-a25*cos(a13) - a6)) + (0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*(-a19*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) + 
a20*(0.045*a24 - a6*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
0.55*a7*sin(a18) + (-a25*a26*sin(a13) + a4*cos(a13))*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2) - 0.55*(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18) + 
9.81*sin(a13)*sin(a18)) + a23*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + 
a7*sin(a18) + (-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - (-a25*cos(a13) - 
a6)*(a12*(-a25*cos(a13) - a6) + a20*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - 
(a25*sin(a13)*cos(a18) - a26*sin(a18))*(-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)))) + (a12*(-a25*cos(a13) - 
a6) + a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18)))*(-a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) - 0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18) - 
0.045*a6))*cos(a13), a1*(-a25*a26*cos(a13) - a4*sin(a13)) + 
a10*(-a25*a26*sin(a13) + a4*cos(a13)) - 9.81*a11*cos(a13) + a25*(a1*a26 + 
a15*a25*cos(a13) - a22*a25*sin(a13))*cos(a13) + a25*(a10*a26 - a15*a25*sin(a13) 
+ a21*a25*cos(a13))*sin(a13) + a5*a7 - 9.81*a8*sin(a13) + (0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*(-a12*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + 
a7*sin(a18) + (-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) + 
a19*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) + 
a20*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) - 
(-a25*sin(a13)*sin(a18) - a26*cos(a18))*(a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - (-a25*sin(a13)*cos(a18) + 
a26*sin(a18))*(-a19*(-a25*cos(a13) - a6) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))))*cos(a18) + 
0.55*(a12*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) + a20*(-0.045*a25*a6*cos(a13) 
- a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) - 
a23*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) - (a25*cos(a13) + 
a6)*(-a19*(-a25*cos(a13) - a6) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - 
(a25*sin(a13)*sin(a18) + a26*cos(a18))*(-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))))*cos(a18) - 
0.55*(-a19*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) + a20*(0.045*a24 - 
a6*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + 
(-a25*a26*sin(a13) + a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
a23*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - (-a25*cos(a13) - 
a6)*(a12*(-a25*cos(a13) - a6) + a20*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - 
(a25*sin(a13)*cos(a18) - a26*sin(a18))*(-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))))*sin(a18) + 
(-a12*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) + 
a16*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) + a17*(-a24 + a25*a26*sin(a13) - 
a4*cos(a13)) + a23*(0.045*a24 - a6*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + (-a25*a26*sin(a13) + 
a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 0.55*(-a25*a26*cos(a13) - 
a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
a3*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - (a25*cos(a13) + 
a6)*(a16*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a19*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18)) + a2*(a25*sin(a13)*sin(a18) + a26*cos(a18)) - 
a23*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) + a9*(-a25*cos(a13) - a6)) 
- (a25*sin(a13)*sin(a18) + a26*cos(a18))*(a0*(-a25*cos(a13) - a6) + 
a12*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) + 
a17*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) - a19*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a9*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - 
(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) - 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18))*(-a19*(-a25*cos(a13) - a6) 
+ a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - (-a12*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)))*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6))*sin(a18) + (a16*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + 
a7*sin(a18) + (-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) + 
a19*(0.045*a6*(a25*sin(a13)*cos(a18) - a26*sin(a18)) + a7*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18) - (-a25*a26*cos(a13) - 
a4*sin(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a18) - 9.81*cos(a13)) + 
a2*(-a6*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + a7*cos(a18) - 
(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) - a23*(-0.045*a25*a6*cos(a13) - 
a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) + 
a9*(-a24 + a25*a26*sin(a13) - a4*cos(a13)) - (-a25*cos(a13) - 
a6)*(-a12*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)) + 
a16*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + a17*(-a25*cos(a13) - a6) + 
a23*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a3*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) - (-0.55*a25*sin(a13)*sin(a18) - 
0.55*a26*cos(a18))*(-a12*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + 
a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18))) - (a25*sin(a13)*cos(a18) - 
a26*sin(a18))*(a0*(-a25*cos(a13) - a6) + a12*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) + a17*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) - 
a19*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a9*(a25*sin(a13)*sin(a18) + a26*cos(a18))) - (a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18))*(a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))))*cos(a18), -a0*(-a24 + 
a25*a26*sin(a13) - a4*cos(a13)) - a12*(-0.045*a25*a6*cos(a13) - 
a6*(-a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) - 
0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18)) + 0.55*a7*cos(a18) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*sin(a18) - 9.81*sin(a13)*cos(a18)) - 
a17*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - 0.045*a19*(-a24 + a25*a26*sin(a13) 
- a4*cos(a13)) + a19*(0.045*a24 - a6*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + (-a25*a26*sin(a13) + 
a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 0.55*(-a25*a26*cos(a13) - 
a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 0.045*a20*(0.045*a24 - 
a6*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 0.55*a7*sin(a18) + 
(-a25*a26*sin(a13) + a4*cos(a13))*(0.045*sin(a18)**2 + 0.045*cos(a18)**2) - 
0.55*(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18) + 9.81*sin(a13)*sin(a18)) + 
0.045*a23*(-a6*(-a25*sin(a13)*sin(a18) - a26*cos(a18)) + a7*sin(a18) + 
(-a25*a26*cos(a13) - a4*sin(a13))*cos(a18)) - a9*(-a6*(-a25*sin(a13)*cos(a18) + 
a26*sin(a18)) + a7*cos(a18) - (-a25*a26*cos(a13) - a4*sin(a13))*sin(a18)) - 
0.045*(-a25*cos(a13) - a6)*(a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18))) + (-a25*sin(a13)*sin(a18) - 
a26*cos(a18))*(-a12*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18)) + a16*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + 
a17*(-a25*cos(a13) - a6) + a23*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) + 0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 
0.045*a6) + a3*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) + 
(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18))*(-a19*(-a25*cos(a13) - a6) + 
a20*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a13) + 
0.55*a25*sin(a13)*cos(a18) - 0.55*a26*sin(a18) + 0.045*a6) + 
a23*(-a25*sin(a13)*cos(a18) + a26*sin(a18))) + (-a25*sin(a13)*cos(a18) + 
a26*sin(a18))*(a16*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + 
a19*(a25*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*sin(a13)*sin(a18) + 
a26*(0.045*sin(a18)**2 + 0.045*cos(a18)**2)*cos(a18)) + 
a2*(a25*sin(a13)*sin(a18) + a26*cos(a18)) - a23*(0.55*a25*sin(a13)*sin(a18) + 
0.55*a26*cos(a18)) + a9*(-a25*cos(a13) - a6)) - 0.045*(a25*sin(a13)*cos(a18) - 
a26*sin(a18))*(-a12*(-a25*sin(a13)*cos(a18) + a26*sin(a18)) + 
a19*(a25*sin(a13)*sin(a18) + a26*cos(a18)) + a20*(a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*sin(a13)*sin(a18) + a26*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a18))) + (a12*(-a25*cos(a13) - a6) + 
a20*(0.55*a25*sin(a13)*sin(a18) + 0.55*a26*cos(a18)) - 
a23*(a25*sin(a13)*sin(a18) + a26*cos(a18)))*(-a25*(0.045*sin(a18)**2 + 
0.045*cos(a18)**2)*cos(a13) - 0.55*a25*sin(a13)*cos(a18) + 0.55*a26*sin(a18) - 
0.045*a6)]''')
In [8]:
small_exprs = sympy.sympify('[a*b + sin(a*b*c) + cos(a*b*c*d), 1/sin(a*b*c), -a*b*c*d]')
In [9]:
#exprs = small_exprs
exprs = large_exprs
In [9]:
 
In [10]:
clear_cache()
%time exprs_prevcse_opt = prev_cse(exprs)  # external optimizations (cse_opts.py) are done by default
%time exprs_prevcse_opt = prev_cse(exprs)
CPU times: user 2min 18s, sys: 153 ms, total: 2min 18s
Wall time: 2min 18s
CPU times: user 1min 15s, sys: 40 ms, total: 1min 15s
Wall time: 1min 16s
In [11]:
clear_cache()
%time exprs_prevcse_noopt = prev_cse(exprs, optimizations=[])  # force no external optimizations
%time exprs_prevcse_noopt = prev_cse(exprs, optimizations=[])
CPU times: user 1min 16s, sys: 23.3 ms, total: 1min 16s
Wall time: 1min 16s
CPU times: user 38.2 s, sys: 10 ms, total: 38.2 s
Wall time: 38.2 s
In [12]:
clear_cache()
%time exprs_newcse_opt = new_cse(exprs, optimizations='basic')  # uses previous implementation default external optimizations
%time exprs_newcse_opt = new_cse(exprs, optimizations='basic')
CPU times: user 4.86 s, sys: 3.33 ms, total: 4.86 s
Wall time: 4.87 s
CPU times: user 3.76 s, sys: 6.67 ms, total: 3.77 s
Wall time: 3.77 s
In [13]:
clear_cache()
%time exprs_newcse = new_cse(exprs)  # no external optimizations are performed by default in new implementation
%time exprs_newcse = new_cse(exprs)
CPU times: user 723 ms, sys: 0 ns, total: 723 ms
Wall time: 723 ms
CPU times: user 547 ms, sys: 0 ns, total: 547 ms
Wall time: 547 ms
In [14]:
clear_cache()
%time exprs_newcse_noorder = new_cse(exprs, order='none')  # do not order Muls and Adds terms,
%time exprs_newcse_noorder = new_cse(exprs, order='none')  #    machine dependent hash ordering is used instead
CPU times: user 140 ms, sys: 0 ns, total: 140 ms
Wall time: 139 ms
CPU times: user 33.3 ms, sys: 0 ns, total: 33.3 ms
Wall time: 35.4 ms
In [14]:
 
In [15]:
cse_output_names = 'exprs_prevcse_opt', 'exprs_prevcse_noopt', 'exprs_newcse_opt', 'exprs_newcse', 'exprs_newcse_noorder'
cse_outputs = [(name, eval(name)) for name in cse_output_names]
In [15]:
 
In [16]:
print("%22s  %s"%('original expressions', count(exprs)))
for name, cse_output in cse_outputs:
    print("%22s  %s"%(name, count(cse_output)))
  original expressions  0 =	505 +	530 -	1994 *	0 /	186 **	613 sin	489 cos
     exprs_prevcse_opt  58 =	161 +	130 -	392 *	0 /	2 **	2 sin	2 cos
   exprs_prevcse_noopt  50 =	154 +	117 -	378 *	0 /	2 **	2 sin	2 cos
      exprs_newcse_opt  97 =	103 +	85 -	180 *	0 /	2 **	2 sin	2 cos
          exprs_newcse  69 =	80 +	85 -	178 *	0 /	2 **	2 sin	2 cos
  exprs_newcse_noorder  68 =	80 +	84 -	173 *	0 /	2 **	2 sin	2 cos
original expressions  0 = 505 +   530 -   1994 *  0 / 186 **  613 sin 489 cos
    exprs_prevcse_opt  58 =    161 +   130 -   392 *   0 / 2 **    2 sin   2 cos
exprs_prevcse_noopt  50 =    154 +   117 -   378 *   0 / 2 **    2 sin   2 cos
    exprs_newcse_opt  97 =    103 +   85 -    180 *   0 / 2 **    2 sin   2 cos
        exprs_newcse  69 =    80 +    85 -    178 *   0 / 2 **    2 sin   2 cos
exprs_newcse_noorder  68 =    80 +    84 -    173 *   0 / 2 **    2 sin   2 cos
In [17]:
for name, cse_output in cse_outputs:
    print(">>> %22s :"%(name))
    print("  > %s\n"%('Ok' if verify_cse(exprs, cse_output) else 'Fail'))
>>>      exprs_prevcse_opt :
-1.38777878078145e-17*a23*a26**2*sin(a13)*sin(a18)**3*cos(a18)**2
1.38777878078145e-17*a23*a25*a26*sin(a13)*sin(a18)*cos(a18)**4
  > Fail

>>>    exprs_prevcse_noopt :
  > Ok

>>>       exprs_newcse_opt :
-1.38777878078145e-17*a23*a26**2*sin(a13)*sin(a18)**3*cos(a18)**2
1.38777878078145e-17*a23*a25*a26*sin(a13)*sin(a18)*cos(a18)**4
  > Fail

>>>           exprs_newcse :
  > Ok

>>>   exprs_newcse_noorder :
  > Ok

>>>      exprs_prevcse_opt :
-1.38777878078145e-17*a23*a26**2*sin(a13)*sin(a18)**3*cos(a18)**2
1.38777878078145e-17*a23*a25*a26*sin(a13)*sin(a18)*cos(a18)**4
> Fail

>>>    exprs_prevcse_noopt :
> Ok

>>>       exprs_newcse_opt :
-1.38777878078145e-17*a23*a26**2*sin(a13)*sin(a18)**3*cos(a18)**2
1.38777878078145e-17*a23*a25*a26*sin(a13)*sin(a18)*cos(a18)**4
> Fail

>>>           exprs_newcse :
> Ok

>>>   exprs_newcse_noorder :
> Ok
In [14]: