
SymPy – simplify - ptype
http://docs.sympy.org/latest/tutorial/simplification.html#simplify
======
ivan_ah
Sympy is pretty awesome. I use live.sympy.org a lot with my students because
you can bookmark an entire interactive session as a (long) URL. For example,
suppose you had to answer the following question. _Find the position of the
object at t=3 seconds, if it starts from x_i=20[m], with v_i=10[m /s] and
undergoes a constant acceleration of a=5[m/s^2]._ Ans: 72.5[m]. Sol.:
[http://bit.ly/1hJS9P3](http://bit.ly/1hJS9P3)

Below are some more examples of cool stuff sympy can do.

Expand, factor, and solve polynomials:

    
    
      >>> P = (x-1)*(x-2)*(x-3)
      >>> P.expand()
      x**3 - 6*x**2 + 11*x - 6
      
      >>> P.factor()
      (x - 1)*(x - 2)*(x - 3)
      
      >>> roots = solve(P,x)
      >>> roots
      [1, 2, 3]
    

Derive the solutions of a quadratic equation:

    
    
      >>> solve( a*x**2 + b*x + c, x)
      [(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
    

Compute symbolic outputs of trig functions:

    
    
      >>> sin(pi/6)
      1/2
      >>> cos(pi/6)
      sqrt(3)/2
    
    

Sympy knows about trig identities:

    
    
      >>> sin(x) == cos(x - pi/2)
      True
      
      >>> simplify( sin(x)*cos(y)+cos(x)*sin(y) )
      sin(x + y)
      
      >>> e = sin(x)**2 + cos(x)**2
      >>> trigsimp(e)
      1 
    
    

Find the solution to the simple harmonic oscillator differential equation
(x''(t)+w^2x(t)=0):

    
    
      >>> sol = dsolve( diff(x(t),t,t) + w**2*x(t), x(t) )
      >>> sol
      x(t) == C1*sin(w*t) + C2*cos(w*t)
    
    

For v4.1 of my math book, I'm going to add a short sympy tutorial. I'll post a
printable version of it to HN when I release, so stay tuned ;)

~~~
krastanov
I am certain that the sympy mailing list would be extremely happy to hear more
about your use of live.sympy.org. Especially if you have any suggestions or
requests.

------
Cogito
Is there any way to ensure simplified expressions are only defined over the
same range as the original expression?

My guess is no, but it is often important to understand that, for example, a
polynomial fraction is not defined when the denominator is 0.

To use an example from the page,

    
    
        simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
    

gives

    
    
        x - 1
    

but should give

    
    
        x - 1; x != 1 + sqrt(2), x != 1 - sqrt(2)
    

This is not always what you want to see, but it would be cool if you could
turn it on :)

~~~
krastanov
This is because of a rigorously defined procedure called "extending by
continuity". The function is continuous, your issue is just an artifact of
notation.

~~~
jay-anderson
Can you explain this more? From what I understand the original function has
division by '0' at those specific values of 'x'. So the final reduced form has
a different domain where it is valid.

~~~
krastanov
I will give an example, not the general definition.

Consider the function f: x -> sin(x)/x. At x=0 you indeed have a division by
0, and if you stick to blindly using your notation it does not work. The
function is not defined at x=0. However you usually (not always) are
interested in the function as a whole. Look at the limit from the left, look
at the limit from the right. They have the same values, it makes sense just to
use this limiting value for the value at x=0.

See
[https://en.wikipedia.org/wiki/Removable_singularity](https://en.wikipedia.org/wiki/Removable_singularity)

------
chisophugis
Appears to need a bit more robustification.

    
    
        >>> simplify(sqrt(x^2))
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "/base/data/home/apps/s~sympy-live-hrd/43.373169527249054993/sympy/sympy/functions/elementary/miscellaneous.py", line 110, in sqrt
            return C.Pow(arg, S.Half)
          File "/base/data/home/apps/s~sympy-live-hrd/43.373169527249054993/sympy/sympy/core/cache.py", line 93, in wrapper
            r = func(*args, **kw_args)
          File "/base/data/home/apps/s~sympy-live-hrd/43.373169527249054993/sympy/sympy/core/power.py", line 119, in __new__
            obj = b._eval_power(e)
        AttributeError: 'Not' object has no attribute '_eval_power'

~~~
jmgrosen

        x^2

in Python is x XOR 2 :) You probably want

    
    
        x**2

------
Scene_Cast2
For anyone looking for a symbolic computation environment - check out Maxima,
I find it to be more pleasant than command-line SymPy.

~~~
krastanov
Sympy for interactive use is usually meant to be used with ipython, where it
gets a nicely rendered latex output.

~~~
DougMerritt
Not sure what your point is; Maxima also generates LaTeX.

E.g. randomly googled:
[http://hippasus.com/resources/symmath/maximatypeset.html](http://hippasus.com/resources/symmath/maximatypeset.html)

Since Maxima was once world class, I would be surprised if Sympy has surpassed
it yet, but I'm out of touch.

Here's a comparison I just found: [https://github.com/sympy/sympy/wiki/SymPy-
vs.-Maxima](https://github.com/sympy/sympy/wiki/SymPy-vs.-Maxima)

Sympy is presumably desirable if you're already using Python heavily.

~~~
mkesper
IPython Notebooks come to my mind.
[http://nbviewer.ipython.org/github/ipython/ipython/blob/mast...](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/SymPy%20Examples.ipynb)

------
shmageggy
Great, another tool that decreases my motivation to actually work on
sharpening my lagging calc skills.

~~~
ivan_ah
I'd say you still need the calc skills so you'll know what you're doing: sympy
helps with demos, explorations, and tedious calculations, but not with the
theory.

For example, here's a little demo that shows that integration is the "undo"
operation of differentiation [http://bit.ly/1dDD4dc](http://bit.ly/1dDD4dc)
but that won't make you really _understand_ the fundamental theorem of
calculus[1].

____________________

[1]
[http://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus...](http://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus#Physical_intuition)

------
NamTaf
This is basically the sym() and simple() functions from Matlab, which is
something I really felt wasn't nearly widespread enough. Knowing about this is
fantastic and I'm going to use it heaps. Thanks!

------
sheetjs
If anyone from sympy is reading this:

    
    
        simplify(exp(x)/exp(x - 1))
    

is causing a runtime error:

    
    
        RuntimeError: maximum recursion depth exceeded while calling a Python object

~~~
sn6uv
What version of Sympy are you using?

I get (tested Sympy 0.7.2-0.7.4):

    
    
        >>> simplify(exp(x)/exp(x - 1))
        E

~~~
krastanov
It does fail on live.sympy.org

~~~
ivan_ah
Worked for me:
[http://live.sympy.org/?evaluate=simplify(exp(x)%2Fexp(x%20-%...](http://live.sympy.org/?evaluate=simplify\(exp\(x\)%2Fexp\(x%20-%201\)\)%0A%23--%0A)

~~~
plaes
It sometimes works and sometimes doesn't.

------
rch
Nice. The taylor series capabilities seem pretty alright too.

