import sympy and other library¶
In [1]:
import sympy
import numpy as np
import matplotlib.pyplot as plt
sympy.plotting
%matplotlib inline
pretty printing (as latex) setting¶
In [2]:
sympy.init_printing()
define variables¶
In [3]:
x, y, k, t = sympy.symbols('x y k t')
symbolic expression¶
sqrt example¶
In [4]:
sympy.sqrt(9)
Out[4]:
In [5]:
sympy.sqrt(8)
Out[5]:
derivative¶
In [6]:
expr = sympy.sin(k*x)
expr
Out[6]:
In [7]:
sympy.diff(expr, x)
Out[7]:
In [8]:
sympy.diff(expr, x, x)
Out[8]:
integrate¶
In [9]:
expr = sympy.exp(x)*sympy.sin(x) + sympy.exp(x)*sympy.cos(x)
expr
Out[9]:
In [10]:
sympy.integrate(expr, x)
Out[10]:
In [11]:
expr = sympy.sin(sympy.pi*x)
expr
Out[11]:
In [12]:
sympy.integrate(expr, (x, 0, 1))
Out[12]:
limit¶
In [13]:
sympy.limit(sympy.sin(2*x)/x, x, 0)
Out[13]:
slove equation¶
In [14]:
sympy.solve(x**2 - 2, x)
Out[14]:
ODE¶
In [15]:
sympy.Eq(y(t).diff(t, t) - y(t))
Out[15]:
In [16]:
sympy.dsolve(sympy.Eq(y(t).diff(t, t) - y(t), sympy.exp(t)), y(t))
Out[16]:
In [17]:
eq = sympy.Eq(y(t).diff(t) - y(t))
In [18]:
sympy.dsolve(eq, y(t))
Out[18]:
print latex forma¶
In [19]:
sympy.latex(sympy.Integral(sympy.cos(x)**2, (x, 0, sympy.pi)))
Out[19]:
In [20]:
%%latex
$\int_{0}^{\pi} \cos^{2}{\left (x \right )}\, dx$
In [21]:
pwd
Out[21]:
substitute symbol with value¶
In [22]:
expr = x+y**2
expr
Out[22]:
method 1¶
In [23]:
expr.subs({x:2, y:3})
Out[23]:
method 2¶
In [24]:
expr.subs(x, 2)
Out[24]:
method 3¶
In [25]:
expr.subs([(x, 2), (y, 3)])
Out[25]:
In [26]:
eq1 = sympy.Eq(x**2, 1)
eq1
Out[26]:
In [27]:
sympy.solve(eq1, x)
Out[27]:
Equals signs¶
In [28]:
eq1 = (x+y)**2
eq1
Out[28]:
In [29]:
eq2 = x**2+2*x*y+y**2
eq2
Out[29]:
In [30]:
sympy.simplify(eq1-eq2)
Out[30]:
In [31]:
eq1.equals(eq2)
Out[31]:
Cover string to SymPy expressions¶
In [32]:
# a and b must define first
a, b = sympy.symbols('a b')
express_string = "a**2+b**2+5"
expr = sympy.simplify(express_string)
In [33]:
expr
Out[33]:
In [34]:
expr.subs(a, 2)
Out[34]:
Evaluate SymPy expressions into float number¶
In [35]:
expr.subs([(a, 2), (b, 3)]).evalf()
Out[35]:
In [36]:
sympy.pi.evalf(100)
Out[36]:
lambdify¶
In [37]:
expr = a**2+b
expr
Out[37]:
In [38]:
f = sympy.lambdify([a, b], expr, "numpy")
In [39]:
a = np.arange(5)
b = 1
print(a)
print(b)
f(a, b)
Out[39]:
plotting function¶
In [40]:
p1 = sympy.plotting.plot(x**3)
p2 = sympy.plotting.plot(sympy.sin(x))
p3 = sympy.plotting.plot(sympy.sin(x), sympy.cos(x))
p3[0].line_color = 'green'
p3[1].line_color = 'red'
p3.xlabel='x'
p3.ylabel='y'
p3.show()
random variable¶
In [41]:
from sympy.stats import Die, density, Normal, Uniform
from sympy import symbols, Symbol
In [42]:
D6 = Die('D6', 6)
density(D6).dict
Out[42]:
In [43]:
z, mu = symbols('z mu')
sigma = Symbol("sigma", positive=True)
X = Normal("X", mu, sigma)
expr = density(X)(z)
expr
Out[43]:
In [44]:
ee = expr.subs([(mu, 0), (sigma, 5)])
sympy.plot(ee, (z, -30, 30))
ee.subs(z, 5)
Out[44]:
In [45]:
sympy.integrate(expr, (z, -sympy.oo, mu))
Out[45]:
Uniform distrubution¶
In [46]:
a = Symbol("a", negative=True)
b = Symbol("b", positive=True)
X = Uniform("x", a, b)
expr = density(X)(x)
expr
Out[46]:
In [47]:
sympy.integrate(expr, (x, a, b))
Out[47]:
In [48]:
sympy.simplify(sympy.integrate(expr, (x, a, b)))
Out[48]:
In [49]:
expr2 = expr.subs([(a, 0), (b, 10)])
expr2
Out[49]:
In [50]:
sympy.simplify(expr2)
print(type(expr2))
沒有留言:
張貼留言