SymPy – simplify
docs.sympy.org
docs.sympy.org
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 ;)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 :)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.
>>> 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**2E.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.
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...
simplify(exp(x)/exp(x - 1))
is causing a runtime error: RuntimeError: maximum recursion depth exceeded while calling a Python objectI get (tested Sympy 0.7.2-0.7.4):
>>> simplify(exp(x)/exp(x - 1))
E