Status: Valid
Owner: [email protected]
Labels: Type-Defect Priority-Medium

New issue 3923 by [email protected]: Calling cancel() on a Piecewise() instance.
http://code.google.com/p/sympy/issues/detail?id=3923

from sympy import init_session
init_session()
p1 = Piecewise(((x**2 - 1)/(x + 1), x > 1), (0, True))
p1
/ 2
|x  - 1
|------  for x > 1
<x + 1
|
|  0     otherwise
\
cancel(p1)
/   2
|  x       1
|----- - -----  for x > 1
<x + 1   x + 1
|
|      0        otherwise
\

Notice that it did not simplify (x**2 - 1)/(x + 1) to (x - 1). But there is worse:

A = Symbol('A', commutative=False)
p2 = Piecewise((A*(x**2 - 1)/(x + 1), x > 1), (0, True))
p2
// 2    \
|\x  - 1/*A
|----------  for x > 1
<  x + 1
|
|    0       otherwise
\
cancel(p2)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "sympy/polys/polytools.py", line 5988, in cancel
    return f.xreplace(dict(reps))
  File "sympy/core/basic.py", line 1100, in xreplace
    return self.func(*args)
  File "sympy/functions/elementary/piecewise.py", line 96, in __new__
    pair = ExprCondPair(*ec)
TypeError: __new__() takes exactly 3 arguments (4 given)

The problem is that cancel can take either an Expr or a 2-Tuple as input, and does something completely different according to the input. But when given the noncommutative Piecewise, it recurses throught the expression tree and treats the ExprCondPair as a 2-Tuple and does something very unexpected with it.

--
You received this message because this project is configured to send all issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

--
You received this message because you are subscribed to the Google Groups 
"sympy-issues" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy-issues.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to