Easy/newbie question on default simplification behavior

26 views
Skip to first unread message

Eric Dennison

unread,
May 13, 2014, 9:08:14 PM5/13/14
to sy...@googlegroups.com
When I type in an expression (x is a symbol):

>>> (x**2-1)/(x-1)
(x**2 - 1)/(x - 1)

One presumes this does not do any automatic cancellation because we don't want to lose undefined behavior at x = 1.

Then,

>>> (x+1)*(x-1)/(x-1)
x + 1

or 

>>> (x-1)/(x-1)
1

This oversimplifies the expression and loses the undefined behavior at x = 1.

Is it possible to create an expression like the latter that does not get oversimplified?

Aaron Meurer

unread,
May 14, 2014, 1:50:15 PM5/14/14
to sy...@googlegroups.com
The noncancellation is also about computational issues here.
Automatically performing a cancel when an object is created could be
potentially be very expensive. However, SymPy automatically cancels
terms. Actually, it just combines exponents, so x*x becomes x**2,
x/x**2 becomes 1/x and x/x becomes x**0 == 1 (these are all the same
because SymPy treats 1/x as x**-1). Of course, anything with a
nonpositive exponent does not make sense if x is 0.

Without knowing more of what you are doing, I can't give the best
advice, but I would suggest just keeping the numerator and denominator
separate.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/0b461aec-c962-407a-b886-48d7d464f4f8%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Richard Fateman

unread,
May 15, 2014, 12:28:20 PM5/15/14
to sy...@googlegroups.com

I think the issue is really one of the computational domain, and whether the
cancellation is always valid in that domain.  In the formal algebraic structure
  (rational field extended by the indeterminate [x])  the cancellation is valid.

The behavior of  (x^2-1)/(x-1)    is always indistinguishable from  x+1.   Even at x=1.

But but but .... you say what about diviision by zero?  Eh, if you want to write
a program that keeps track of such things, go ahead.  You could use sympy
to help you.  But it would be a mistake to require sympy to always keep track.
Too expensive in time and especially space.

RJF
Reply all
Reply to author
Forward
0 new messages