In [2]:
from IPython.display import display

from sympy.interactive import printing
printing.init_printing(use_latex='mathjax')

from __future__ import division
from sympy import *

Best Tutorial

https://github.com/sympy/sympy/wiki/Tutorial

Something about numbers in SymPy

In [3]:
a = Rational(1,2)
In [4]:
a
Out[4]:
$$\frac{1}{2}$$
In [7]:
a*2
Out[7]:
$$1$$
In [8]:
a.evalf()
Out[8]:
$$0.5$$
In [10]:
a**2
Out[10]:
$$\frac{1}{4}$$
In [11]:
(a**2).evalf()
Out[11]:
$$0.25$$
In [12]:
pi
Out[12]:
$$\pi$$
In [13]:
pi.evalf()
Out[13]:
$$3.14159265358979$$
In [18]:
oo
Out[18]:
$$\infty$$
In [19]:
oo+1
Out[19]:
$$\infty$$
In [20]:
oo > 1e35
Out[20]:
True
In [23]:
I**2
Out[23]:
$$-1$$
In [24]:
c=3*I+5
In [25]:
c
Out[25]:
$$5 + 3 i$$
In [29]:
c.adjoint()
Out[29]:
$$5 - 3 i$$

Symbols Definition

In [31]:
x = Symbol('x')
In [33]:
x
Out[33]:
$$x$$
In [35]:
y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-35-009520053b00> in <module>()
----> 1 y

NameError: name 'y' is not defined
In [37]:
x, y = symbols('x y')
In [42]:
x,y
Out[42]:
$$\begin{pmatrix}x, & y\end{pmatrix}$$
In [44]:
t=Symbol('2')
In [48]:
2*t
Out[48]:
$$2 \cdot 2$$

Limits

In [50]:
limit(sin(x)/x, x, 0)
Out[50]:
$$1$$
In [51]:
limit(1/x,x,oo)
Out[51]:
$$0$$

Derivative

In [53]:
diff(sin(x),x)
Out[53]:
$$\cos{\left (x \right )}$$
In [56]:
limit((sin(x+y)-sin(x))/y,y,0) # Derivative defined as limit
Out[56]:
$$\cos{\left (x \right )}$$

Integral

In [59]:
integrate(cos(x),x)
Out[59]:
$$\sin{\left (x \right )}$$
In [68]:
#s=Symbol('sigma')
s=Symbol('sigma', nonnegative=true)

There are many possible assumptions for a variable. A reference is here: http://docs.sympy.org/dev/modules/assumptions/index.html

In [69]:
s
Out[69]:
$$\sigma$$
In [70]:
g=1/(s*sqrt(2*pi))*exp(-x**2/(2*s**2))
In [71]:
g
Out[71]:
$$\frac{\sqrt{2} e^{- \frac{x^{2}}{2 \sigma^{2}}}}{2 \sqrt{\pi} \sigma}$$
In [72]:
integrate(g,x)
Out[72]:
$$\frac{1}{2} \operatorname{erf}{\left (\frac{\sqrt{2} x}{2 \sigma} \right )}$$
In [73]:
integrate(g,(x,-1,1))
Out[73]:
$$\operatorname{erf}{\left (\frac{\sqrt{2}}{2 \sigma} \right )}$$
In [74]:
integrate(g,(x,-s,s))
Out[74]:
$$\operatorname{erf}{\left (\frac{\sqrt{2}}{2} \right )}$$
In [75]:
N(integrate(g,(x,-s,s)))
Out[75]:
$$0.682689492137086$$
In [76]:
integrate(g,(x,-oo,oo))
Out[76]:
$$\begin{cases} 1 & \text{for}\: \left\lvert{\operatorname{periodic_{argument}}{\left (\frac{1}{\operatorname{polar\_lift}^{2}{\left (\sigma \right )}},\infty \right )}}\right\rvert \leq \frac{\pi}{2} \\\int_{-\infty}^{\infty} \frac{\sqrt{2} e^{- \frac{x^{2}}{2 \sigma^{2}}}}{2 \sqrt{\pi} \sigma}\, dx & \text{otherwise} \end{cases}$$

Taylor Expansion

In [77]:
cos(x)
Out[77]:
$$\cos{\left (x \right )}$$
In [78]:
cos(x).series(x,0,10)
Out[78]:
$$1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} + \mathcal{O}\left(x^{10}\right)$$
In [79]:
sin(x).series(x,0,10)
Out[79]:
$$x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)$$
In [80]:
exp(x).series(x,0,10)
Out[80]:
$$1 + x + \frac{x^{2}}{2} + \frac{x^{3}}{6} + \frac{x^{4}}{24} + \frac{x^{5}}{120} + \frac{x^{6}}{720} + \frac{x^{7}}{5040} + \frac{x^{8}}{40320} + \frac{x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)$$
In [81]:
exp(I*x).series(x,0,10)
Out[81]:
$$1 + i x - \frac{x^{2}}{2} - \frac{i x^{3}}{6} + \frac{x^{4}}{24} + \frac{i x^{5}}{120} - \frac{x^{6}}{720} - \frac{i x^{7}}{5040} + \frac{x^{8}}{40320} + \frac{i x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)$$
In [41]:
cos(x).series(x,0,10)+I*sin(x).series(x,0,10)
Out[41]:
$$1 + i \left(x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)\right) - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} + \mathcal{O}\left(x^{10}\right)$$
In [82]:
exp(I*x).series(x,0,10)-(cos(x).series(x,0,10)+I*sin(x).series(x,0,10))
Out[82]:
$$- i \left(x - \frac{x^{3}}{6} + \frac{x^{5}}{120} - \frac{x^{7}}{5040} + \frac{x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)\right) + i x - \frac{i x^{3}}{6} + \frac{i x^{5}}{120} - \frac{i x^{7}}{5040} + \frac{i x^{9}}{362880} + \mathcal{O}\left(x^{10}\right)$$
In [83]:
simplify(exp(I*x).series(x,0,10)-(cos(x).series(x,0,10)+I*sin(x).series(x,0,10)))
Out[83]:
$$\mathcal{O}\left(x^{10}\right)$$

Differential equations

In [84]:
F = Function('f')
In [85]:
DiffF = Eq(F(x).diff(x,x)+F(x),0)
In [92]:
F(x).diff(x,x)+F(x)
Out[92]:
$$f{\left (x \right )} + \frac{d^{2}}{d x^{2}} f{\left (x \right )}$$
In [95]:
dsolve(DiffF, F(x))
Out[95]:
$$f{\left (x \right )} = C_{1} \sin{\left (x \right )} + C_{2} \cos{\left (x \right )}$$

Algebraic equations

In [96]:
Alg = Eq(x**4, 1)
In [97]:
Alg
Out[97]:
$$x^{4} = 1$$
In [98]:
solve(Alg,x)
Out[98]:
$$\begin{bmatrix}-1, & 1, & - i, & i\end{bmatrix}$$
In [99]:
Eq1=Eq(x + 5*y, 2)
Eq2=Eq(-3*x + 6*y, 15)
In [100]:
Eq1, Eq2
Out[100]:
$$\begin{pmatrix}x + 5 y = 2, & - 3 x + 6 y = 15\end{pmatrix}$$
In [101]:
solve([Eq1, Eq2], [x,y])
Out[101]:
$$\begin{Bmatrix}x : -3, & y : 1\end{Bmatrix}$$

2D Geometry

In [102]:
px = Point(0,0)
py = Point(1,1)
pz = Point(1,0)
In [103]:
t = Triangle(px, py, pz)
In [106]:
t.area
Out[106]:
$$- \frac{1}{2}$$
In [107]:
t.medians[px]
Out[107]:
$$Segment(Point(0, 0), Point(1, 1/2))$$
In [114]:
t.medians
Out[114]:
$$\begin{Bmatrix}Point(0, 0) : Segment(Point(0, 0), Point(1, 1/2)), & Point(1, 0) : Segment(Point(1/2, 1/2), Point(1, 0)), & Point(1, 1) : Segment(Point(1/2, 0), Point(1, 1))\end{Bmatrix}$$
In [116]:
intersection(t.medians[px], t.medians[py], t.medians[pz])
Out[116]:
$$\begin{bmatrix}Point(2/3, 1/3)\end{bmatrix}$$
In [117]:
t.bisectors()[px]
Out[117]:
$$Segment(Point(0, 0), Point(1, -1 + sqrt(2)))$$
In [118]:
intersection(t.bisectors()[px], t.bisectors()[py], t.bisectors()[pz])
Out[118]:
$$\begin{bmatrix}Point(sqrt(2)/2, -sqrt(2)/2 + 1)\end{bmatrix}$$
In [119]:
center=intersection(t.bisectors()[px], t.bisectors()[py], t.bisectors()[pz])[0]
In [121]:
center
Out[121]:
$$Point(sqrt(2)/2, -sqrt(2)/2 + 1)$$
In [122]:
L=Line(px,py)
In [124]:
radius=(L.distance(center))
In [126]:
C=Circle(center, radius)
In [127]:
C
Out[127]:
$$Circle(Point(sqrt(2)/2, -sqrt(2)/2 + 1), sqrt(2)*(-1 + sqrt(2))/2)$$
In [128]:
C.is_tangent(L)
Out[128]:
True
In [21]:
#Plot(C)

Linear Algebra

In [138]:
Matrix([[1,x],[5,y]])
Out[138]:
$$\left[\begin{matrix}1 & x\\5 & y\end{matrix}\right]$$
In [140]:
Matrix(2,2,[1,x,5,y])
Out[140]:
$$\left[\begin{matrix}1 & x\\5 & y\end{matrix}\right]$$
In [141]:
diag(1,2,3,4,5,6)
Out[141]:
$$\left[\begin{matrix}1 & 0 & 0 & 0 & 0 & 0\\0 & 2 & 0 & 0 & 0 & 0\\0 & 0 & 3 & 0 & 0 & 0\\0 & 0 & 0 & 4 & 0 & 0\\0 & 0 & 0 & 0 & 5 & 0\\0 & 0 & 0 & 0 & 0 & 6\end{matrix}\right]$$
In [142]:
ones(4,3)
Out[142]:
$$\left[\begin{matrix}1 & 1 & 1\\1 & 1 & 1\\1 & 1 & 1\\1 & 1 & 1\end{matrix}\right]$$
In [143]:
zeros(2,5)
Out[143]:
$$\left[\begin{matrix}0 & 0 & 0 & 0 & 0\\0 & 0 & 0 & 0 & 0\end{matrix}\right]$$
In [144]:
M=Matrix([[1,x],[5,y]])
In [145]:
M[0,1]
Out[145]:
$$x$$
In [146]:
M[:,1]
Out[146]:
$$\left[\begin{matrix}x\\y\end{matrix}\right]$$
In [150]:
M*M
Out[150]:
$$\left[\begin{matrix}5 x + 1 & x y + x\\5 y + 5 & 5 x + y^{2}\end{matrix}\right]$$
In [151]:
(M*M-M)/M
Out[151]:
$$\left[\begin{matrix}5 x & x y\\5 y & 5 x + y^{2} - y\end{matrix}\right] \left(\left[\begin{matrix}1 & x\\5 & y\end{matrix}\right]\right)^{-1}$$
In [152]:
exp(M)
Out[152]:
$$\left[\begin{matrix}- \frac{x e^{\frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{- \frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}} \left(\frac{1}{2 x} \left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) - \frac{\left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right)^{2} \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right)}{8 x \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}}\right) + \frac{\left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) e^{\frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{4 \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}} & - \frac{x \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) e^{\frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{2 \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}} - \frac{x \left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) e^{\frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{4 \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \left(- \frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}}\\\left(\frac{1}{2 x} \left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) - \frac{\left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right)^{2} \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right)}{8 x \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}}\right) e^{\frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}} - \frac{e^{\frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{4 x \sqrt{20 x + y^{2} - 2 y + 1}} \left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) & \frac{e^{\frac{y}{2} + \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{2 \sqrt{20 x + y^{2} - 2 y + 1}} \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) + \frac{\left(y - \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) \left(y + \sqrt{20 x + y^{2} - 2 y + 1} - 1\right) e^{\frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}}}{4 \left(- \frac{y}{2} - \frac{1}{2} \sqrt{20 x + y^{2} - 2 y + 1} + \frac{1}{2}\right) \sqrt{20 x + y^{2} - 2 y + 1}}\end{matrix}\right]$$
In [153]:
v1=Matrix([3,5])
v2=Matrix([7,2])
In [154]:
M*v1
Out[154]:
$$\left[\begin{matrix}5 x + 3\\5 y + 15\end{matrix}\right]$$
In [155]:
v1*M
---------------------------------------------------------------------------
ShapeError                                Traceback (most recent call last)
<ipython-input-155-49731853e5e1> in <module>()
----> 1 v1*M

/usr/local/lib/python2.7/site-packages/sympy/core/decorators.pyc in binary_op_wrapper(self, other)
    116                     else:
    117                         return f(self)
--> 118             return func(self, other)
    119         return binary_op_wrapper
    120     return priority_decorator

/usr/local/lib/python2.7/site-packages/sympy/matrices/dense.pyc in __mul__(self, other)
    567     @call_highest_priority('__rmul__')
    568     def __mul__(self, other):
--> 569         return super(DenseMatrix, self).__mul__(_force_mutable(other))
    570 
    571     @call_highest_priority('__mul__')

/usr/local/lib/python2.7/site-packages/sympy/matrices/matrices.pyc in __mul__(self, other)
    511             B = other
    512             if A.cols != B.rows:
--> 513                 raise ShapeError("Matrices size mismatch.")
    514             if A.cols == 0:
    515                 return classof(A, B)._new(A.rows, B.cols, lambda i, j: 0)

ShapeError: Matrices size mismatch.
In [156]:
v1.T*M
Out[156]:
$$\left[\begin{matrix}28 & 3 x + 5 y\end{matrix}\right]$$
In [157]:
v1.T
Out[157]:
$$\left[\begin{matrix}3 & 5\end{matrix}\right]$$
In [162]:
v1.T*v2
Out[162]:
$$\left[\begin{matrix}31\end{matrix}\right]$$
In [161]:
(v1.T*v2)[0]
Out[161]:
$$31$$
In [163]:
M.det()
Out[163]:
$$- 5 x + y$$
In [164]:
M.inv()
Out[164]:
$$\left[\begin{matrix}\frac{5 x}{- 5 x + y} + 1 & - \frac{x}{- 5 x + y}\\- \frac{5}{- 5 x + y} & \frac{1}{- 5 x + y}\end{matrix}\right]$$
In [165]:
M.inv()*M
Out[165]:
$$\left[\begin{matrix}1 & - \frac{x y}{- 5 x + y} + x \left(\frac{5 x}{- 5 x + y} + 1\right)\\0 & - \frac{5 x}{- 5 x + y} + \frac{y}{- 5 x + y}\end{matrix}\right]$$
In [166]:
simplify(M.inv()*M)
Out[166]:
$$\left[\begin{matrix}1 & 0\\0 & 1\end{matrix}\right]$$
In [167]:
v1.norm()
Out[167]:
$$\sqrt{34}$$
In [168]:
M.norm()
Out[168]:
$$\sqrt{\left\lvert{x}\right\rvert^{2} + \left\lvert{y}\right\rvert^{2} + 26}$$
In [169]:
M
Out[169]:
$$\left[\begin{matrix}1 & x\\5 & y\end{matrix}\right]$$
In []: