Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
SymPy – simplify (sympy.org)
69 points by ptype on Feb 11, 2014 | hide | past | favorite | 25 comments


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

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 ;)


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.


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 :)


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


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.


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


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'


    x^2
in Python is x XOR 2 :) You probably want

    x**2


You are using live.sympy.org. It has its own issues. The library itself works well when ran from normal python prompt.


Are you sure it's x^2 and not x2? Perhaps sympy does extra foo to feel more like mathematics.


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


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


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

E.g. randomly googled: 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

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



The sympy version at http://live.sympy.org renders things nicely with in LaTeX (MathJax), though to be honest I prefer the plain ascii output so that I can copy-paste expressions more easily.


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


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 but that won't make you really understand the fundamental theorem of calculus[1].

____________________

[1] http://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus...


Actually it does sharpen my calc skills. Now I can do more experiments, better visualizations, I can try something out and see if I'm wrong. All that helps on accumulating insight. Plus, if knowing your tools makes a good carpenter, why is it different with mathematicians.


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!


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


What version of Sympy are you using?

I get (tested Sympy 0.7.2-0.7.4):

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


It does fail on live.sympy.org



It sometimes works and sometimes doesn't.


Nice. The taylor series capabilities seem pretty alright too.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: