I've implemented an evaluate=False option for Add, Mul, Pow and
functions (see attachment). This could be useful to suppress default
behavior like Sub(x,y) -> Add(x,Mul(-1,y)) for code generation etc. As
it happens, I need something like this for evalf testing as well.
Fredrik
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
# HG changeset patch
# User Fredrik Johansson <[EMAIL PROTECTED]>
# Date 1213795035 -7200
# Node ID 809a34695ade804a9d3efce6ad8f100f05dafe73
# Parent 0842457787a92e2132778d3a379dd0f40dea6333
implement evaluate=False option for Add, Mul, Pow and functions
diff -r 0842457787a9 -r 809a34695ade sympy/core/function.py
--- a/sympy/core/function.py Tue Jun 17 18:26:55 2008 +0200
+++ b/sympy/core/function.py Wed Jun 18 15:17:15 2008 +0200
@@ -126,6 +126,8 @@
if opt in options:
del options[opt]
# up to here.
+ if options.get('evaluate') is False:
+ return Basic.__new__(cls, *args, **options)
r = cls.canonize(*args, **options)
if isinstance(r, Basic):
return r
diff -r 0842457787a9 -r 809a34695ade sympy/core/operations.py
--- a/sympy/core/operations.py Tue Jun 17 18:26:55 2008 +0200
+++ b/sympy/core/operations.py Wed Jun 18 15:17:15 2008 +0200
@@ -21,6 +21,8 @@
@cacheit
def __new__(cls, *args, **assumptions):
+ if assumptions.get('evaluate') is False:
+ return Basic.__new__(cls, *map(_sympify, args), **assumptions)
if len(args)==0:
return cls.identity()
if len(args)==1:
diff -r 0842457787a9 -r 809a34695ade sympy/core/power.py
--- a/sympy/core/power.py Tue Jun 17 18:26:55 2008 +0200
+++ b/sympy/core/power.py Wed Jun 18 15:17:15 2008 +0200
@@ -60,6 +60,8 @@
def __new__(cls, a, b, **assumptions):
a = _sympify(a)
b = _sympify(b)
+ if assumptions.get('evaluate') is False:
+ return Basic.__new__(cls, a, b, **assumptions)
if b is S.Zero:
return S.One
if b is S.One:
diff -r 0842457787a9 -r 809a34695ade sympy/core/tests/test_arit.py
--- a/sympy/core/tests/test_arit.py Tue Jun 17 18:26:55 2008 +0200
+++ b/sympy/core/tests/test_arit.py Wed Jun 18 15:17:15 2008 +0200
@@ -1,5 +1,5 @@
from sympy import Symbol, sin, cos, exp, O, sqrt, Rational, Real, re, pi, \
- sympify, sqrt
+ sympify, sqrt, Add, Mul, Pow
from sympy.utilities.pytest import XFAIL
x = Symbol("x")
@@ -941,3 +941,17 @@
e = 2*a + b
f = b + 2*a
assert e == f
+
+def test_suppressed_evaluation():
+ a = Add(1,3,2,evaluate=False)
+ b = Mul(1,3,2,evaluate=False)
+ c = Pow(3,2,evaluate=False)
+ assert a != 6
+ assert a.func is Add
+ assert a.args == (1,3,2)
+ assert b != 6
+ assert b.func is Mul
+ assert b.args == (1,3,2)
+ assert c != 9
+ assert c.func is Pow
+ assert c.args == (3,2)
diff -r 0842457787a9 -r 809a34695ade sympy/core/tests/test_functions.py
--- a/sympy/core/tests/test_functions.py Tue Jun 17 18:26:55 2008 +0200
+++ b/sympy/core/tests/test_functions.py Wed Jun 18 15:17:15 2008 +0200
@@ -277,3 +277,10 @@
assert diff(x**3, x) == 3*x**2
assert diff(x**3, x, evaluate=False) != 3*x**2
assert diff(x**3, x, evaluate=False) == Derivative(x**3, x)
+
+def test_suppressed_evaluation():
+ a = sin(0,evaluate=False)
+ assert a != 0
+ assert str(a) == "sin(0)"
+ assert a.func is sin
+ assert a.args == (0,)