Hi,

On Tue, May 05, 2009 at 03:00:45AM -0700, Colin Gillespie wrote:
> 
> Hi,
> 
> I think that the expected behaviour would be for pow(x,2) to return
> x^2, as opposed to a general function. Otherwise, it would be
> inconsistent with how 'sin' and 'exp' are handled (for example).
> 

I also think the current behaviour is inconsistent and, what is more
important, counterintuitive for Python users. I attached a patch that
fixes this issue. It passes all tests when using py.test, but fails a
few tests with sympy's test(), e.g.:

sympy/integrals/tests/test_integrals.py[38] .............................E......
..                                                                        [FAIL]
sympy/integrals/tests/test_lineintegrals.py[1] .                            [OK]
sympy/integrals/tests/test_rationaltools.py[1] .                            [OK]
sympy/integrals/tests/test_risch.py[19] ........^C interrupted by user

________________________________________________________________________________
_________ sympy/integrals/tests/test_integrals.py:test_evalf_integrals _________
  File "/home/matt/repo/git/sympy/sympy/integrals/tests/test_integrals.py", 
line 255, in test_evalf_integrals
    assert NS(pi - 4*Integral('sqrt(1-x**2)', (x, 0, 1)), 15, maxprec=30, 
chop=True) in ('0.0', '0')
  File "./sympy/integrals/integrals.py", line 20, in __new__
    function = sympify(function)
  File "./sympy/core/sympify.py", line 121, in sympify
    return ast_parser.parse_expr(a, locals)
  File "./sympy/core/ast_parser.py", line 96, in parse_expr
    return eval(e, local_dict, global_dict)
  File "<string>", line 1, in <module>
  File "./sympy/galgebra/GA.py", line 1876, in __pow__
    return(MV.outer_product(self,mv))
  File "./sympy/galgebra/GA.py", line 1507, in outer_product
    product = mv1.scalar_mul(mv2)
  File "./sympy/galgebra/GA.py", line 1899, in scalar_mul
    if isinstance(self.mv[i],numpy.ndarray):
IndexError: list index out of range

After removing galgebra/tests everything started to work fine. Any ideas
what is wrong here? (test(), py.test, galgebra/tests or something else?)

-- 
Mateusz

From c94d48872ca4b4054e30b40885d943f59f93c639 Mon Sep 17 00:00:00 2001
From: Mateusz Paprocki <matt...@gmail.com>
Date: Thu, 30 Apr 2009 19:26:17 +0200
Subject: [PATCH] Don't convert __builtin__ attribute to a Symbol

This patch makes:

 sympify('pow(x, 2)')

return x**2 as opposed to Function('pow')(x, 2).
---
 sympy/core/ast_parser.py          |    5 ++++-
 sympy/core/ast_parser_python24.py |    5 ++++-
 sympy/core/tests/test_sympify.py  |    4 ++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/sympy/core/ast_parser.py b/sympy/core/ast_parser.py
index e633ab5..0f0e4c6 100644
--- a/sympy/core/ast_parser.py
+++ b/sympy/core/ast_parser.py
@@ -25,6 +25,8 @@
 
 from sympy import Integer, Real, Basic
 
+import __builtin__
+
 try:
     import ast
     ast_enabled = True
@@ -59,7 +61,7 @@ def visit_Name(self, node):
 
                 if isinstance(name_obj, (Basic, type)) or callable(name_obj):
                     return node
-            elif node.id in ['True', 'False']:
+            elif hasattr(__builtin__, node.id):
                 return node
             return fix_missing_locations(Call(Name('Symbol', Load()),
                     [Str(node.id)], [], None, None))
@@ -100,3 +102,4 @@ def parse_expr(s, local_dict):
             return SymPyParser(local_dict=local_dict).parse_expr(s)
         except SyntaxError:
             raise SympifyError("sorry")
+
diff --git a/sympy/core/ast_parser_python24.py 
b/sympy/core/ast_parser_python24.py
index ed7e41b..e4ef30b 100644
--- a/sympy/core/ast_parser_python24.py
+++ b/sympy/core/ast_parser_python24.py
@@ -8,6 +8,8 @@
 from basic import Basic
 from symbol import Symbol
 
+import __builtin__
+
 _is_integer = re.compile(r'\A\d+(l|L)?\Z').match
 
 class SymPyTransformer(Transformer):
@@ -42,7 +44,7 @@ def atom_name(self, nodelist):
 
             if isinstance(name_obj, (Basic, type)) or callable(name_obj):
                 return Const(name_obj, lineno=lineno)
-        elif name in ['True', 'False']:
+        elif hasattr(__builtin__, name):
             return Const(eval(name), lineno=lineno)
 
         symbol_obj = Symbol(name)
@@ -97,3 +99,4 @@ def parse_expr(self, ws_expression):
         parsed_expr = eval(code, self.local_dict, self.global_dict) #Changed 
order to prefer sympy objects to  user defined
 
         return parsed_expr
+
diff --git a/sympy/core/tests/test_sympify.py b/sympy/core/tests/test_sympify.py
index 63094f5..33749d2 100644
--- a/sympy/core/tests/test_sympify.py
+++ b/sympy/core/tests/test_sympify.py
@@ -60,6 +60,9 @@ def test_sympify_function():
     assert sympify('factor(x**2-1, x)') == -(1-x)*(x+1)
     assert sympify('sin(pi/2)*cos(pi)') == -Integer(1)
 
+def test_builtin():
+    assert sympify('pow(x, 2)') == x**2
+
 def test_sympify_poly():
     p = Poly(x**2+x+1, x)
 
@@ -245,3 +248,4 @@ def test_issue883():
 def test_S_sympify():
     assert S(1)/2 == sympify(1)/2
     assert (-2)**(S(1)/2) == sqrt(2)*I
+
-- 
1.6.2.3

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to