Loading [MathJax]/jax/output/PreviewHTML/jax.js
##// END OF EJS Templates
use ROUTER/DEALER sockets for stdin...
r4952:a2ac298e
Show More
sympy.ipynb
267 lines | 36.2 KiB | text/plain | TextLexer

SymPy: Open Source Symbolic Mathematics

In [1]:
%load_ext sympyprinting
In [2]:
from __future__ import division
from sympy import *
x, y, z = symbols("x y z")
k, m, n = symbols("k m n", integer=True)
f, g, h = map(Function, 'fgh')

Elementary operations

In [3]:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)
Out[3]:
In [4]:
exp(I*x).subs(x,pi).evalf()
Out[4]:
-1.0
In [5]:
e = x + 2*y
In [6]:
srepr(e)
Out[6]:
Add(Symbol('x'), Mul(Integer(2), Symbol('y')))
In [7]:
exp(pi * sqrt(163)).evalf(50)
Out[7]:
262537412640768743.99999999999925007259719818568888

Algebra

In [8]:
((x+y)**2 * (x+1)).expand()
Out[8]:
x^{3} + 2 x^{2} y + x^{2} + x y^{2} + 2 x y + y^{2}
In [9]:
a = 1/x + (x*sin(x) - 1)/x
display(a)
simplify(a)
\frac{x \operatorname{sin}\left(x\right) -1}{x} + \frac{1}{x}
Out[9]:
\operatorname{sin}\left(x\right)
In [10]:
solve(Eq(x**3 + 2*x**2 + 4*x + 8, 0), x)
Out[10]:
[-2⋅ⅈ, 2⋅ⅈ, -2]
In [11]:
a, b = symbols('a b')
Sum(6*n**2 + 2**n, (n, a, b))
Out[11]:
\sum_{n=a}^{b} \left(2^{n} + 6 n^{2}\right)

Calculus

In [12]:
limit((sin(x)-x)/x**3, x, 0)
Out[12]:
- \frac{1}{6}
In [13]:
(1/cos(x)).series(x, 0, 6)
Out[13]:
1 + \frac{1}{2} x^{2} + \frac{5}{24} x^{4} + \operatorname{\mathcal{O}}\left(x^{6}\right)
In [14]:
diff(cos(x**2)**2 / (1+x), x)
Out[14]:
- 4 \frac{x \operatorname{sin}\left(x^{2}\right) \operatorname{cos}\left(x^{2}\right)}{x + 1} - \frac{\operatorname{cos}^{2}\left(x^{2}\right)}{\left(x + 1\right)^{2}}
In [15]:
integrate(x**2 * cos(x), (x, 0, pi/2))
Out[15]:
-2 + \frac{1}{4} \pi^{2}
In [16]:
eqn = Eq(Derivative(f(x),x,x) + 9*f(x), 1)
display(eqn)
dsolve(eqn, f(x))
9 \operatorname{f}\left(x\right) + \frac{\partial^{2}}{\partial^{2} x} \operatorname{f}\left(x\right) = 1
Out[16]:
\operatorname{f}\left(x\right) = C_{1} \operatorname{cos}\left(3 x\right) + C_{2} \operatorname{sin}\left(3 x\right) + \frac{1}{9}