Symbolic Algebra with sympy


In [1]:
from sympy import *

In [2]:
from sympy import init_session
init_session()


IPython console for SymPy 0.7.6.1 (Python 3.5.1-64-bit) (ground types: python)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

Documentation can be found at http://www.sympy.org

Basics


In [3]:
from sympy.stats import *
E(Die('X', 6))


Out[3]:
$$\frac{7}{2}$$

In [4]:
sqrt(8)


Out[4]:
$$2 \sqrt{2}$$

In [5]:
expr = x + 2*y

In [6]:
expr2 = x*expr

In [7]:
expr2


Out[7]:
$$x \left(x + 2 y\right)$$

In [8]:
expand(expr2)


Out[8]:
$$x^{2} + 2 x y$$

In [9]:
factor(expand(expr2))


Out[9]:
$$x \left(x + 2 y\right)$$

In [10]:
diff(sin(x) * exp(x), x)


Out[10]:
$$e^{x} \sin{\left (x \right )} + e^{x} \cos{\left (x \right )}$$

In [11]:
integrate(exp(x)*sin(x) + exp(x)*cos(x), x)


Out[11]:
$$e^{x} \sin{\left (x \right )}$$

In [12]:
integrate(sin(x**2), (x, -oo, oo))


Out[12]:
$$\frac{\sqrt{2} \sqrt{\pi}}{2}$$

In [13]:
dsolve(Eq(f(t).diff(t, t) - f(t), exp(t)), f(t))


Out[13]:
$$f{\left (t \right )} = C_{2} e^{- t} + \left(C_{1} + \frac{t}{2}\right) e^{t}$$

In [14]:
Matrix([[1,2],[2,2]]).eigenvals()


Out[14]:
$$\left \{ \frac{3}{2} + \frac{\sqrt{17}}{2} : 1, \quad - \frac{\sqrt{17}}{2} + \frac{3}{2} : 1\right \}$$

In [15]:
nu = symbols('nu')
besselj(nu, z).rewrite(jn)


Out[15]:
$$\frac{\sqrt{2} \sqrt{z}}{\sqrt{\pi}} j_{\nu - \frac{1}{2}}\left(z\right)$$

In [16]:
latex(Integral(cos(x)**2, (x, 0, pi)))


Out[16]:
'\\int_{0}^{\\pi} \\cos^{2}{\\left (x \\right )}\\, dx'

In [17]:
expr = cos(x) + 1

In [18]:
expr.subs(x, y)


Out[18]:
$$\cos{\left (y \right )} + 1$$

In [19]:
expr = x**y
expr = expr.subs(y, x**y)
expr = expr.subs(y, x**y)
expr = expr.subs(x, x**x)

In [20]:
expr


Out[20]:
$$\left(x^{x}\right)^{\left(x^{x}\right)^{\left(x^{x}\right)^{y}}}$$

In [21]:
expr = sin(2*x) + cos(2*x)
expand_trig(expr)


Out[21]:
$$2 \sin{\left (x \right )} \cos{\left (x \right )} + 2 \cos^{2}{\left (x \right )} - 1$$

In [22]:
expr.subs(sin(2*x), 2*sin(x)*cos(x))


Out[22]:
$$2 \sin{\left (x \right )} \cos{\left (x \right )} + \cos{\left (2 x \right )}$$

In [23]:
expr = x**4 - 4*x**3 + 4*x**2 - 2*x +3

In [24]:
replacements = [(x**i, y**i) for i in range(5) if i%2 == 0]
expr.subs(replacements)


Out[24]:
$$- 4 x^{3} - 2 x + y^{4} + 4 y^{2} + 3$$

In [25]:
sexpr = "x**4 - 4*x**3 + 4*x**2 - 2*x +3"
sympify(sexpr)


Out[25]:
$$x^{4} - 4 x^{3} + 4 x^{2} - 2 x + 3$$

In [26]:
sqrt(8).evalf()


Out[26]:
$$2.82842712474619$$

In [27]:
pi.evalf(100)


Out[27]:
$$3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068$$

In [28]:
expr = cos(2*x)
expr.evalf(subs = {x: 2.4})


Out[28]:
$$0.0874989834394464$$

In [29]:
expr = cos(x)**2 + sin(x)**2
expr.evalf(subs = {x: 1}, chop=True)


Out[29]:
$$1.0$$

In [30]:
a = range(10)
expr = sin(x)
f = lambdify(x, expr, "numpy")
f(a)


Out[30]:
array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
       -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])

Simplify


In [31]:
simplify(sin(x)**2 + cos(x)**2)


Out[31]:
$$1$$

In [32]:
simplify(gamma(x) / gamma(x-2))


Out[32]:
$$\left(x - 2\right) \left(x - 1\right)$$

In [33]:
expand((x + y)**3)


Out[33]:
$$x^{3} + 3 x^{2} y + 3 x y^{2} + y^{3}$$

In [34]:
factor(x**3 - x**2 + x - 1)


Out[34]:
$$\left(x - 1\right) \left(x^{2} + 1\right)$$

In [35]:
factor_list(x**3 - x**2 + x - 1)


Out[35]:
$$\left ( 1, \quad \left [ \left ( x - 1, \quad 1\right ), \quad \left ( x^{2} + 1, \quad 1\right )\right ]\right )$$

In [36]:
expand((cos(x) + sin(x))**2)


Out[36]:
$$\sin^{2}{\left (x \right )} + 2 \sin{\left (x \right )} \cos{\left (x \right )} + \cos^{2}{\left (x \right )}$$

In [37]:
expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3
cexpr = collect(expr, x)
cexpr


Out[37]:
$$x^{3} + x^{2} \left(- z + 2\right) + x \left(y + 1\right) - 3$$

In [38]:
cexpr.coeff(x**2)


Out[38]:
$$- z + 2$$

In [39]:
cancel((x**2 + 2*x + 1) / (x**2 + x))


Out[39]:
$$\frac{1}{x} \left(x + 1\right)$$

In [40]:
expr = (x*y**2 - 2*x*y*z + x*z**2 + y**2 - 2*y*z + z**2)/(x**2 - 1)

In [41]:
cancel(expr)


Out[41]:
$$\frac{1}{x - 1} \left(y^{2} - 2 y z + z^{2}\right)$$

In [42]:
factor(expr)


Out[42]:
$$\frac{\left(y - z\right)^{2}}{x - 1}$$

In [43]:
expr = (4*x**3 + 21*x**2 + 10*x + 12)/(x**4 + 5*x**3 + 5*x**2 + 4*x)

In [44]:
apart(expr)


Out[44]:
$$\frac{2 x - 1}{x^{2} + x + 1} - \frac{1}{x + 4} + \frac{3}{x}$$

In [45]:
trigsimp(sin(x)*tan(x)/sec(x))


Out[45]:
$$\sin^{2}{\left (x \right )}$$

In [46]:
trigsimp(cosh(x)**2 + sinh(x)**2)


Out[46]:
$$\cosh{\left (2 x \right )}$$

In [47]:
expand_trig(sin(x + y))


Out[47]:
$$\sin{\left (x \right )} \cos{\left (y \right )} + \sin{\left (y \right )} \cos{\left (x \right )}$$

In [48]:
x, y = symbols('x y', positive=True)
a, b = symbols('a b', real=True)
z, t, c = symbols('z t c')

In [49]:
powsimp(x**a*x**b)


Out[49]:
$$x^{a + b}$$

In [50]:
powsimp(x**a*y**a)


Out[50]:
$$\left(x y\right)^{a}$$

In [51]:
powsimp(t**c*z**c)


Out[51]:
$$t^{c} z^{c}$$

In [52]:
powsimp(t**c*z**c, force=True)


Out[52]:
$$\left(t z\right)^{c}$$

In [53]:
expand_power_exp(x**(a + b))


Out[53]:
$$x^{a} x^{b}$$

In [54]:
expand_power_base((x*y)**a)


Out[54]:
$$x^{a} y^{a}$$

In [55]:
powdenest((x**a)**b)


Out[55]:
$$x^{a b}$$

In [56]:
n = symbols('n', real=True)

In [57]:
expand_log(log(x*y))


Out[57]:
$$\log{\left (x \right )} + \log{\left (y \right )}$$

In [58]:
expand_log(log(x**n))


Out[58]:
$$n \log{\left (x \right )}$$

In [59]:
logcombine(n*log(x))


Out[59]:
$$\log{\left (x^{n} \right )}$$

In [60]:
x, y, z = symbols('x y z')
k, m, n = symbols('k m n')

In [61]:
factorial(n)


Out[61]:
$$n!$$

In [62]:
binomial(n, k)


Out[62]:
$${\binom{n}{k}}$$

In [63]:
hyper([1,2], [3], z)


Out[63]:
$${{}_{2}F_{1}\left(\begin{matrix} 1, 2 \\ 3 \end{matrix}\middle| {z} \right)}$$

In [64]:
factorial(x).rewrite(gamma)


Out[64]:
$$\Gamma{\left(x + 1 \right)}$$

In [65]:
tan(x).rewrite(sin)


Out[65]:
$$\frac{2 \sin^{2}{\left (x \right )}}{\sin{\left (2 x \right )}}$$

In [66]:
expand_func(gamma(x + 3))


Out[66]:
$$x \left(x + 1\right) \left(x + 2\right) \Gamma{\left(x \right)}$$

In [67]:
hyperexpand(hyper([1, 1], [2], z))


Out[67]:
$$- \frac{1}{z} \log{\left (- z + 1 \right )}$$

In [68]:
expr =  meijerg([[1],[1]], [[1],[]], -z)

In [69]:
expr


Out[69]:
$${G_{2, 1}^{1, 1}\left(\begin{matrix} 1 & 1 \\1 & \end{matrix} \middle| {- z} \right)}$$

In [70]:
hyperexpand(expr)


Out[70]:
$$e^{\frac{1}{z}}$$

In [71]:
combsimp(binomial(n+1, k+1)/binomial(n, k))


Out[71]:
$$\frac{n + 1}{k + 1}$$

In [72]:
def list_to_frac(l):
    expr = Integer(0)
    for i in reversed(l[1:]):
        expr += i
        expr = 1/expr
    return l[0] + expr

In [73]:
list_to_frac([1,2,3,4])


Out[73]:
$$\frac{43}{30}$$

In [74]:
syms = symbols('a0:5')

In [75]:
syms


Out[75]:
$$\left ( a_{0}, \quad a_{1}, \quad a_{2}, \quad a_{3}, \quad a_{4}\right )$$

In [76]:
frac = list_to_frac(syms)

In [77]:
frac


Out[77]:
$$a_{0} + \frac{1}{a_{1} + \frac{1}{a_{2} + \frac{1}{a_{3} + \frac{1}{a_{4}}}}}$$

In [78]:
frac = cancel(frac)

In [79]:
frac


Out[79]:
$$\frac{a_{0} a_{1} a_{2} a_{3} a_{4} + a_{0} a_{1} a_{2} + a_{0} a_{1} a_{4} + a_{0} a_{3} a_{4} + a_{0} + a_{2} a_{3} a_{4} + a_{2} + a_{4}}{a_{1} a_{2} a_{3} a_{4} + a_{1} a_{2} + a_{1} a_{4} + a_{3} a_{4} + 1}$$

In [80]:
from sympy.printing import print_ccode
print_ccode(frac)


(a0*a1*a2*a3*a4 + a0*a1*a2 + a0*a1*a4 + a0*a3*a4 + a0 + a2*a3*a4 + a2 + a4)/(a1*a2*a3*a4 + a1*a2 + a1*a4 + a3*a4 + 1)

Calculus


In [81]:
diff(cos(x), x)


Out[81]:
$$- \sin{\left (x \right )}$$

In [82]:
diff(x**4, x, 3)


Out[82]:
$$24 x$$

In [83]:
expr = exp(x*y*z)
diff(expr, x, y, 2, z, 4)


Out[83]:
$$x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$$

In [84]:
deriv = Derivative(expr, x, y, 2, z, 4)
deriv


Out[84]:
$$\frac{\partial^{7}}{\partial x\partial y^{2}\partial z^{4}} e^{x y z}$$

In [85]:
deriv.doit()


Out[85]:
$$x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$$

In [86]:
integrate(cos(x), x)


Out[86]:
$$\sin{\left (x \right )}$$

In [87]:
integrate(exp(-x), (x, 0, oo))


Out[87]:
$$1$$

In [88]:
integrate(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))


Out[88]:
$$\pi$$

In [89]:
integrate(x**x, x)


Out[89]:
$$\int x^{x}\, dx$$

In [90]:
expr = Integral(log(x)**2, x)
expr


Out[90]:
$$\int \log^{2}{\left (x \right )}\, dx$$

In [91]:
expr.doit()


Out[91]:
$$x \log^{2}{\left (x \right )} - 2 x \log{\left (x \right )} + 2 x$$

In [92]:
integrate(sin(x**2), x)


Out[92]:
$$\frac{3 \sqrt{2} \sqrt{\pi} S\left(\frac{\sqrt{2} x}{\sqrt{\pi}}\right)}{8 \Gamma{\left(\frac{7}{4} \right)}} \Gamma{\left(\frac{3}{4} \right)}$$

In [93]:
integrate(x**y*exp(-x), (x, 0, oo))


Out[93]:
$$\begin{cases} \Gamma{\left(y + 1 \right)} & \text{for}\: - \Re{y} < 1 \\\int_{0}^{\infty} x^{y} e^{- x}\, dx & \text{otherwise} \end{cases}$$

In [94]:
limit(sin(x)/x, x, 0)


Out[94]:
$$1$$

In [95]:
expr = Limit((cos(x) - 1)/x, x, 0)
expr


Out[95]:
$$\lim_{x \to 0^+}\left(\frac{1}{x} \left(\cos{\left (x \right )} - 1\right)\right)$$

In [96]:
expr.doit()


Out[96]:
$$0$$

In [97]:
limit(1/x, x, 0, '-')


Out[97]:
$$-\infty$$

In [98]:
expr = exp(sin(x))
expr.series(x, 0, 4)


Out[98]:
$$1 + x + \frac{x^{2}}{2} + \mathcal{O}\left(x^{4}\right)$$

In [99]:
x + x**3 + x**6 + O(x**4)


Out[99]:
$$x + x^{3} + \mathcal{O}\left(x^{4}\right)$$

In [100]:
x * O(1)


Out[100]:
$$\mathcal{O}\left(x\right)$$

In [101]:
expr.series(x, 0, 4).removeO()


Out[101]:
$$\frac{x^{2}}{2} + x + 1$$

In [102]:
exp(x - 6).series(x, 6)


Out[102]:
$$-5 + \frac{1}{2} \left(x - 6\right)^{2} + \frac{1}{6} \left(x - 6\right)^{3} + \frac{1}{24} \left(x - 6\right)^{4} + \frac{1}{120} \left(x - 6\right)^{5} + x + \mathcal{O}\left(\left(x - 6\right)^{6}; x\rightarrow6\right)$$

In [103]:
exp(x - 6).series(x, 6).removeO().subs(x, x - 6)


Out[103]:
$$x + \frac{1}{120} \left(x - 12\right)^{5} + \frac{1}{24} \left(x - 12\right)^{4} + \frac{1}{6} \left(x - 12\right)^{3} + \frac{1}{2} \left(x - 12\right)^{2} - 11$$

Working with matrices


In [104]:
M = Matrix([[1,2,3],[3,2,1]])
P = Matrix([0,1,1])
M*P


Out[104]:
$$\left[\begin{matrix}5\\3\end{matrix}\right]$$

In [105]:
M


Out[105]:
$$\left[\begin{matrix}1 & 2 & 3\\3 & 2 & 1\end{matrix}\right]$$

In [106]:
M.shape


Out[106]:
$$\left ( 2, \quad 3\right )$$

In [107]:
M.col(-1)


Out[107]:
$$\left[\begin{matrix}3\\1\end{matrix}\right]$$

In [108]:
M = Matrix([[1,3], [-2,3]])
M**-1


Out[108]:
$$\left[\begin{matrix}\frac{1}{3} & - \frac{1}{3}\\\frac{2}{9} & \frac{1}{9}\end{matrix}\right]$$

In [109]:
M.T


Out[109]:
$$\left[\begin{matrix}1 & -2\\3 & 3\end{matrix}\right]$$

In [110]:
eye(3)


Out[110]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 1 & 0\\0 & 0 & 1\end{matrix}\right]$$

In [111]:
diag(1,2,3)


Out[111]:
$$\left[\begin{matrix}1 & 0 & 0\\0 & 2 & 0\\0 & 0 & 3\end{matrix}\right]$$

In [112]:
M.det()


Out[112]:
$$9$$

In [113]:
M= Matrix([[1,0,1,3],[2,3,4,7],[-1,-3,-3,-4]])
M


Out[113]:
$$\left[\begin{matrix}1 & 0 & 1 & 3\\2 & 3 & 4 & 7\\-1 & -3 & -3 & -4\end{matrix}\right]$$

In [114]:
M.rref()


Out[114]:
$$\left ( \left[\begin{matrix}1 & 0 & 1 & 3\\0 & 1 & \frac{2}{3} & \frac{1}{3}\\0 & 0 & 0 & 0\end{matrix}\right], \quad \left [ 0, \quad 1\right ]\right )$$

In [115]:
M = Matrix([[1,2,3,0,0],[4,10,0,0,1]])
M


Out[115]:
$$\left[\begin{matrix}1 & 2 & 3 & 0 & 0\\4 & 10 & 0 & 0 & 1\end{matrix}\right]$$

In [116]:
M.nullspace()


Out[116]:
$$\left [ \left[\begin{matrix}-15\\6\\1\\0\\0\end{matrix}\right], \quad \left[\begin{matrix}0\\0\\0\\1\\0\end{matrix}\right], \quad \left[\begin{matrix}1\\- \frac{1}{2}\\0\\0\\1\end{matrix}\right]\right ]$$

In [117]:
M = Matrix([[3, -2, 4, -2], [5,3,-3,-2], [5,-2,2,-2], [5,-2,-3,3]])
M


Out[117]:
$$\left[\begin{matrix}3 & -2 & 4 & -2\\5 & 3 & -3 & -2\\5 & -2 & 2 & -2\\5 & -2 & -3 & 3\end{matrix}\right]$$

In [118]:
M.eigenvals()


Out[118]:
$$\left \{ -2 : 1, \quad 3 : 1, \quad 5 : 2\right \}$$

In [119]:
M.eigenvects()


Out[119]:
$$\left [ \left ( -2, \quad 1, \quad \left [ \left[\begin{matrix}0\\1\\1\\1\end{matrix}\right]\right ]\right ), \quad \left ( 3, \quad 1, \quad \left [ \left[\begin{matrix}1\\1\\1\\1\end{matrix}\right]\right ]\right ), \quad \left ( 5, \quad 2, \quad \left [ \left[\begin{matrix}1\\1\\1\\0\end{matrix}\right], \quad \left[\begin{matrix}0\\-1\\0\\1\end{matrix}\right]\right ]\right )\right ]$$

In [120]:
P, D = M.diagonalize()

In [121]:
P


Out[121]:
$$\left[\begin{matrix}0 & 1 & 1 & 0\\1 & 1 & 1 & -1\\1 & 1 & 1 & 0\\1 & 1 & 0 & 1\end{matrix}\right]$$

In [122]:
D


Out[122]:
$$\left[\begin{matrix}-2 & 0 & 0 & 0\\0 & 3 & 0 & 0\\0 & 0 & 5 & 0\\0 & 0 & 0 & 5\end{matrix}\right]$$

In [123]:
P*D*P**-1


Out[123]:
$$\left[\begin{matrix}3 & -2 & 4 & -2\\5 & 3 & -3 & -2\\5 & -2 & 2 & -2\\5 & -2 & -3 & 3\end{matrix}\right]$$

In [124]:
lamda = symbols('lamda')
p = M.charpoly(lamda)
p


Out[124]:
$$\operatorname{PurePoly}{\left( \lambda^{4} - 11 \lambda^{3} + 29 \lambda^{2} + 35 \lambda - 150, \lambda, domain=\mathbb{Z} \right)}$$

In [125]:
factor(p)


Out[125]:
$$\left(\lambda - 5\right)^{2} \left(\lambda - 3\right) \left(\lambda + 2\right)$$

Solving Algebraic and Differential Equations


In [126]:
solve(x**2 - 1, x)


Out[126]:
$$\left [ -1, \quad 1\right ]$$

In [127]:
solve((x - y + 2, x + y -3), (x, y))


Out[127]:
$$\left \{ x : \frac{1}{2}, \quad y : \frac{5}{2}\right \}$$

In [128]:
solve(x**3 - 6*x**2 + 9*x, x)


Out[128]:
$$\left [ 0, \quad 3\right ]$$

In [129]:
roots(x**3 - 6*x**2 + 9*x, x)


Out[129]:
$$\left \{ 0 : 1, \quad 3 : 2\right \}$$

In [130]:
f, g = symbols('f g', cls=Function)

In [131]:
f(x).diff(x)


Out[131]:
$$\frac{d}{d x} f{\left (x \right )}$$

In [132]:
diffeq = Eq(f(x).diff(x, 2) - 2*f(x).diff(x) + f(x), sin(x))

In [133]:
diffeq


Out[133]:
$$f{\left (x \right )} - 2 \frac{d}{d x} f{\left (x \right )} + \frac{d^{2}}{d x^{2}} f{\left (x \right )} = \sin{\left (x \right )}$$

In [134]:
dsolve(diffeq, f(x))


Out[134]:
$$f{\left (x \right )} = \left(C_{1} + C_{2} x\right) e^{x} + \frac{1}{2} \cos{\left (x \right )}$$

In [135]:
dsolve(f(x).diff(x)*(1 - sin(f(x))), f(x))


Out[135]:
$$f{\left (x \right )} + \cos{\left (f{\left (x \right )} \right )} = C_{1}$$

In [136]:
a, t = symbols('a t')
f(t).diff(t)
diffeq = Eq(f(t).diff(t), a*t)

In [137]:
diffeq


Out[137]:
$$\frac{d}{d t} f{\left (t \right )} = a t$$

In [138]:
dsolve(diffeq, f(t))


Out[138]:
$$f{\left (t \right )} = C_{1} + \frac{a t^{2}}{2}$$

In [139]:
x = symbols('x', cls=Function)
diffeq = Eq(x(t).diff(t), a*x(t))
diffeq


Out[139]:
$$\frac{d}{d t} x{\left (t \right )} = a x{\left (t \right )}$$

In [140]:
dsolve(diffeq, x(t))


Out[140]:
$$x{\left (t \right )} = C_{1} e^{a t}$$

Numerics


In [141]:
N(pi, 10)


Out[141]:
$$3.141592654$$

In [142]:
x = symbols('x')

In [143]:
expr = Integral(sin(x)/(x**2), (x, 1, oo))

In [144]:
expr.evalf()


Out[144]:
$$0.5$$

In [145]:
expr.evalf(maxn=20)


Out[145]:
$$0.5$$

In [146]:
expr.evalf(quad='osc')


Out[146]:
$$0.504067061906928$$

In [147]:
expr.evalf(20, quad='osc')


Out[147]:
$$0.50406706190692837199$$

In [148]:
expr = Integral(sin(1/x), (x, 0, 1))
expr


Out[148]:
$$\int_{0}^{1} \sin{\left (\frac{1}{x} \right )}\, dx$$

In [149]:
expr.evalf()


Out[149]:
$$0.5$$

In [150]:
expr = expr.transform(x, 1/x)
expr


Out[150]:
$$\int_{1}^{\infty} \frac{1}{x^{2}} \sin{\left (x \right )}\, dx$$

In [151]:
expr.evalf(quad='osc')


Out[151]:
$$0.504067061906928$$

In [152]:
nsimplify(pi, tolerance=0.001)


Out[152]:
$$\frac{355}{113}$$

In [153]:
expr = sin(x)/x

In [154]:
%timeit expr.evalf(subs={x: 3.14})


1000 loops, best of 3: 397 µs per loop

In [155]:
f1 = lambdify(x, expr)
%timeit f1(3.14)


The slowest run took 13.44 times longer than the fastest. This could mean that an intermediate result is being cached 
1000000 loops, best of 3: 308 ns per loop

In [156]:
f2 = lambdify(x, expr, 'numpy')
%timeit f2(3.14)


The slowest run took 16.41 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 2.2 µs per loop

In [157]:
%timeit f2(np.linspace(1, 10, 10000))


1000 loops, best of 3: 306 µs per loop

In [158]:
%timeit [f1(x) for x in np.linspace(1, 10, 10000)]


100 loops, best of 3: 5.19 ms per loop

In [159]:
from mpmath import *

In [160]:
f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
for x in [0, 1, 2.5, 10]:
    nprint(f(x), 15)
    nprint([cos(x), sin(x)], 15)


[1.0, 0.0]
[1.0, 0.0]
[0.54030230586814, 0.841470984807897]
[0.54030230586814, 0.841470984807897]
[-0.801143615546934, 0.598472144103957]
[-0.801143615546934, 0.598472144103957]
[-0.839071529076452, -0.54402111088937]
[-0.839071529076452, -0.54402111088937]

In [161]:
from sympy.plotting import plot
%matplotlib inline

plot(x*y**3 - y*x**3)
pass



In [162]:
from sympy.plotting import plot3d_parametric_surface
from sympy import sin, cos
u, v = symbols('u v')
plot3d_parametric_surface(cos(u + v), sin(u - v), u-v, (u, -5, 5), (v, -5, 5))
pass


Statistics


In [163]:
from sympy.stats import *

In [164]:
k = Symbol("k", positive=True)
theta = Symbol("theta", positive=True)
z = Symbol("z")
X = Gamma("x", k, theta)

In [165]:
D = density(X)(z)
D


Out[165]:
$$\frac{z^{k - 1} e^{- \frac{z}{\theta}}}{\theta^{k} \Gamma{\left(k \right)}}$$

In [166]:
C = cdf(X, meijerg=True)(z)
C


Out[166]:
$$\begin{cases} - \frac{k \gamma\left(k, 0\right)}{\Gamma{\left(k + 1 \right)}} + \frac{k \gamma\left(k, \frac{z}{\theta}\right)}{\Gamma{\left(k + 1 \right)}} & \text{for}\: z \geq 0 \\0 & \text{otherwise} \end{cases}$$

In [167]:
E(X)


Out[167]:
$$\frac{\theta}{\Gamma{\left(k \right)}} \Gamma{\left(k + 1 \right)}$$

In [168]:
V = variance(X)
V


Out[168]:
$$\frac{\theta^{3} \theta^{k - 1}}{\theta^{k} \Gamma^{2}{\left(k \right)}} \Gamma^{2}{\left(k + 1 \right)} - \frac{2 \theta^{2}}{\Gamma^{2}{\left(k \right)}} \Gamma^{2}{\left(k + 1 \right)} + \frac{\theta \theta^{k + 1}}{\theta^{k} \Gamma{\left(k \right)}} \Gamma{\left(k + 2 \right)}$$

In [169]:
simplify(V)


Out[169]:
$$k \theta^{2}$$

In [170]:
N = Normal('Gaussian', 10, 5)
density(N)(z)


Out[170]:
$$\frac{\sqrt{2}}{10 \sqrt{\pi}} e^{- \frac{1}{50} \left(z - 10\right)^{2}}$$

In [171]:
density(N)(3).evalf()


Out[171]:
$$0.029945493127149$$

In [172]:
simplify(cdf(N)(z))


Out[172]:
$$\frac{1}{2} \operatorname{erf}{\left (\frac{\sqrt{2}}{10} \left(z - 10\right) \right )} + \frac{1}{2}$$

In [173]:
P(N > 10)


Out[173]:
$$\frac{1}{2}$$

In [174]:
sample(N)


Out[174]:
$$6.30254802079348$$