Updates:
Labels: -Priority-Medium Priority-HighComment #2 on issue 978 by [email protected]: extract (-1) from all expressions canonically
http://code.google.com/p/sympy/issues/detail?id=978This issue should really be fixed. Without it, zeros can hide in expressions and lead to undue simplification difficulties. Consider:
m = Matrix([[ 0, -1, 0, 0, 0, 0, -1, 0, 0],
... [-1, x, -1, 0, 0, 0, 0, -1, 0], ... [ 0, -1, x, 0, 0, 0, 0, 0, -1], ... [ 0, 0, 0, x, -1, 0, -1, 0, 0], ... [ 0, 0, 0, -1, x, -1, 0, -1, 0], ... [ 0, 0, 0, 0, -1, 0, 0, 0, -1], ... [-1, 0, 0, -1, 0, 0, x, -1, 0], ... [ 0, -1, 0, 0, -1, 0, -1, 0, -1], ... [ 0, 0, -1, 0, 0, -1, 0, -1, x]])
r=m.row_join(eye(9)).rref()
When you simplify (with cse) some of the terms in the "inverted" matrix (which is singular) you will find that they are NaN, but if you factor such expressions you come up with some interesting results:
for j in range(9):
... for k in range(9,18): ... rjk=r[0][j,k] ... rep,e=cse(rjk) ... if e[0] is S.NaN: ... print j,k,factor(rjk) ... 0 11 -1/(2*x) 0 13 nan 0 14 oo*(x - 1)**2*(x + 1)**2 0 15 -1/2 0 17 0 1 9 -1/2 etc... Let's look at the first one:
z=r[0][0,11]
In its current form it is not NaN:
z.count_ops()
611I've picked one of the arguments of z that is infinity (another one is a symbolic zero and that, I suppose, is why cse is able to determine that z is NaN).
1/(-(-x + 1/x)/(x - 1/x)**2 - (-(-x + 1/x)/(x*(x - 1/x)**2) - 1/(x*(x - 1/x)))** 2/(2*x - 2/x - (-x + 1/x)/(x**2*(x - 1/x)**2) - 1/(x**2*(x - 1/x))) - 1/(x - 1/xz1=z.args[1].args[2]
)) If this argument is sympified the infinity shows up:
S(str(_))
ooHow is a symbolic infinity getting into an expression? I ask somewhat rhetorically. If the expression had been sign simplified (made canonical wrt sign) then perhaps this situation wouldn't have arisen.
zz=z.args[1].args[2]zz.subs([(a,Mul(-1,a,evaluate=False) if a.could_extract_minus_sign() else a)
for a in zz.atoms(Add)]) ooBut an interesting thing is that factor gets into trouble (giving -1/(2*x)) as the factorization of this expression that cse can determine quickly is nan...and simplify takes about two minutes to do nothing to the expression:
t=time();s=simplify(z);print time()-t
124.861000061
s==z
TrueHere's a little simpler expression that doesn't respond well to simple attempts at simplification:
n
-1 + 1/x
z=n/x/(-n)**2-1/n/x z.expand()
-1/(x*(1 - 2/x + x**(-2))) - 1/(x*(-1 + 1/x)) + 1/(x**2*(1 - 2/x + x**(-2)))
1/(x**2*(1 - 2/x + x**(-2))) + 1/(x**2*(-1 + 1/x)) + 2/(x**3*(1 - 3/x + 3/x**2 - 1/x**3)) - 3/(x**3*(1 - 2/x + x**(-2))) - 2/(x**4*(1 - 3/x + 3/x**2 - 1/x**3))z.diff(x).expand()
factor_terms(z)
((-x + 1)**2 - (x - 1)**2)/((-x + 1)*(x - 1)**2)
cancel(z)
0 Again, sign simplification quickly resolves this expression:
z.subs([(a,Mul(-1,a,evaluate=False) if a.could_extract_minus_sign() else a) for a in z.atoms(Add)])
0 -- You received this message because you are subscribed to the Google Groups "sympy-issues" 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-issues?hl=en.
