On Sun, Jul 6, 2008 at 2:19 PM, Robert Kern <[EMAIL PROTECTED]> wrote:
>
> On Sun, Jul 6, 2008 at 07:13, Ondrej Certik <[EMAIL PROTECTED]> wrote:
>> diff --git a/sympy/simplify/__init__.py b/sympy/simplify/__init__.py
>> --- a/sympy/simplify/__init__.py
>> +++ b/sympy/simplify/__init__.py
>> @@ -10,3 +10,5 @@ from rewrite import cancel, trim, apart
>> from rewrite import cancel, trim, apart
>>
>> from sqrtdenest import sqrtdenest
>> +
>> +from cse import cse
>
> I'd rename the module, in this case. It makes it difficult to import
> the module from the sympy.simplify package. This is necessary if
> someone needs to change the default optimization list at runtime.
That's right. How about this patch:
# HG changeset patch
# User Ondrej Certik <[EMAIL PROTECTED]>
# Date 1215346276 -7200
# Node ID 841260b1596e0adcc1aef4695f78c02fb14c870b
# Parent a899701f1e4f303dcc41fd4a1e00795936a035c5
cse module renamed to cse_main and cse_main.cse() exported by default.
diff --git a/sympy/simplify/__init__.py b/sympy/simplify/__init__.py
--- a/sympy/simplify/__init__.py
+++ b/sympy/simplify/__init__.py
@@ -10,3 +10,5 @@ from rewrite import cancel, trim, apart
from rewrite import cancel, trim, apart
from sqrtdenest import sqrtdenest
+
+from cse_main import cse
diff --git a/sympy/simplify/cse.py b/sympy/simplify/cse_main.py
rename from sympy/simplify/cse.py
rename to sympy/simplify/cse_main.py
diff --git a/sympy/simplify/tests/test_cse.py b/sympy/simplify/tests/test_cse.py
--- a/sympy/simplify/tests/test_cse.py
+++ b/sympy/simplify/tests/test_cse.py
@@ -1,19 +1,19 @@ import itertools
import itertools
-from sympy import Add, Mul, Pow, Symbol, sin, sqrt, symbols, sympify
-from sympy.simplify import cse, cse_opts
+from sympy import Add, Mul, Pow, Symbol, sin, sqrt, symbols, sympify, cse
+from sympy.simplify import cse_main, cse_opts
w,x,y,z = symbols('wxyz')
-x0,x1,x2 = list(itertools.islice(cse.numbered_symbols(), 0, 3))
+x0,x1,x2 = list(itertools.islice(cse_main.numbered_symbols(), 0, 3))
negone = sympify(-1)
def test_numbered_symbols():
- ns = cse.numbered_symbols(prefix='y')
+ ns = cse_main.numbered_symbols(prefix='y')
assert list(itertools.islice(ns, 0, 10)) == [Symbol('y%s'%i) for
i in range(0, 10)]
- ns = cse.numbered_symbols(prefix='y')
+ ns = cse_main.numbered_symbols(prefix='y')
assert list(itertools.islice(ns, 10, 20)) == [Symbol('y%s'%i) for
i in range(10, 20)]
- ns = cse.numbered_symbols()
+ ns = cse_main.numbered_symbols()
assert list(itertools.islice(ns, 0, 10)) == [Symbol('x%s'%i) for
i in range(0, 10)]
# Dummy "optimization" functions for testing.
@@ -24,59 +24,59 @@ def opt2(expr):
return expr*z
def test_preprocess_for_cse():
- assert cse.preprocess_for_cse(x, [(opt1, None)]) == x+y
- assert cse.preprocess_for_cse(x, [(None, opt1)]) == x
- assert cse.preprocess_for_cse(x, [(None, None)]) == x
- assert cse.preprocess_for_cse(x, [(opt1, opt2)]) == x+y
- assert cse.preprocess_for_cse(x, [(opt1, None), (opt2, None)]) == (x+y)*z
+ assert cse_main.preprocess_for_cse(x, [(opt1, None)]) == x+y
+ assert cse_main.preprocess_for_cse(x, [(None, opt1)]) == x
+ assert cse_main.preprocess_for_cse(x, [(None, None)]) == x
+ assert cse_main.preprocess_for_cse(x, [(opt1, opt2)]) == x+y
+ assert cse_main.preprocess_for_cse(x, [(opt1, None), (opt2,
None)]) == (x+y)*z
def test_postprocess_for_cse():
- assert cse.postprocess_for_cse(x, [(opt1, None)]) == x
- assert cse.postprocess_for_cse(x, [(None, opt1)]) == x+y
- assert cse.postprocess_for_cse(x, [(None, None)]) == x
- assert cse.postprocess_for_cse(x, [(opt1, opt2)]) == x*z
+ assert cse_main.postprocess_for_cse(x, [(opt1, None)]) == x
+ assert cse_main.postprocess_for_cse(x, [(None, opt1)]) == x+y
+ assert cse_main.postprocess_for_cse(x, [(None, None)]) == x
+ assert cse_main.postprocess_for_cse(x, [(opt1, opt2)]) == x*z
# Note the reverse order of application.
- assert cse.postprocess_for_cse(x, [(None, opt1), (None, opt2)]) == x*z+y
+ assert cse_main.postprocess_for_cse(x, [(None, opt1), (None,
opt2)]) == x*z+y
def test_cse_single():
# Simple substitution.
e = Add(Pow(x+y,2), sqrt(x+y))
- substs, reduced = cse.cse([e], optimizations=[])
+ substs, reduced = cse([e], optimizations=[])
assert substs == [(x0, x+y)]
assert reduced == [sqrt(x0) + x0**2]
def test_cse_single2():
# Simple substitution, test for being able to pass the expression directly
e = Add(Pow(x+y,2), sqrt(x+y))
- substs, reduced = cse.cse(e, optimizations=[])
+ substs, reduced = cse(e, optimizations=[])
assert substs == [(x0, x+y)]
assert reduced == [sqrt(x0) + x0**2]
def test_cse_not_possible():
# No substitution possible.
e = Add(x,y)
- substs, reduced = cse.cse([e], optimizations=[])
+ substs, reduced = cse([e], optimizations=[])
assert substs == []
assert reduced == [x+y]
def test_nested_substitution():
# Substitution within a substitution.
e = Add(Pow(w*x+y,2), sqrt(w*x+y))
- substs, reduced = cse.cse([e], optimizations=[])
+ substs, reduced = cse([e], optimizations=[])
assert substs == [(x0, w*x), (x1, x0+y)]
assert reduced == [sqrt(x1) + x1**2]
def test_subtraction_opt():
# Make sure subtraction is optimized.
e = (x-y)*(z-y) + sin((x-y)*(z-y))
- substs, reduced = cse.cse([e],
optimizations=[(cse_opts.sub_pre,cse_opts.sub_post)])
+ substs, reduced = cse([e],
optimizations=[(cse_opts.sub_pre,cse_opts.sub_post)])
assert substs == [(x0, z-y), (x1, x-y), (x2, x0*x1)]
assert reduced == [x2 + sin(x2)]
def test_multiple_expressions():
e1 = (x+y)*z
e2 = (x+y)*w
- substs, reduced = cse.cse([e1, e2], optimizations=[])
+ substs, reduced = cse([e1, e2], optimizations=[])
assert substs == [(x0, x+y)]
assert reduced == [x0*z, x0*w]
Ondrej
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---