Calculation using Sympy
#Sympy 1 in the current Google Colab.1.Contains 1.
# Sympy 1.Since 1 cannot calculate the "special solution" described later, 1.Upgrade to 3.
!pip install sympy==1.3
Collecting sympy==1.3
[?25l Downloading https://files.pythonhosted.org/packages/dd/f6/ed485ff22efdd7b371d0dbbf6d77ad61c3b3b7e0815a83c89cbb38ce35de/sympy-1.3.tar.gz (5.9MB)
[K |████████████████████████████████| 5.9MB 4.1MB/s
[?25hRequirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.6/dist-packages (from sympy==1.3) (1.1.0)
Building wheels for collected packages: sympy
Building wheel for sympy (setup.py) ... [?25l[?25hdone
Created wheel for sympy: filename=sympy-1.3-cp36-none-any.whl size=5199947 sha256=bc14a07ac6744969566fce4c541612a22adecb5bc83223ccc225ed28f415c38d
Stored in directory: /root/.cache/pip/wheels/6c/59/86/478e3c0f298368c119095cc5985dedac57c0e35a85c737f823
Successfully built sympy
Installing collected packages: sympy
Found existing installation: sympy 1.1.1
Uninstalling sympy-1.1.1:
Successfully uninstalled sympy-1.1.1
Successfully installed sympy-1.3
import sympy as sym
from sympy.plotting import plot
sym.init_printing(use_unicode=True)
%matplotlib inline
#If you are using Google Colab, run it to support TeX display by Sympy
def custom_latex_printer(exp,**options):
from google.colab.output._publish import javascript
url = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/latest.js?config=default"
javascript(url=url)
return sym.printing.latex(exp,**options)
sym.init_printing(use_latex="mathjax",latex_printer=custom_latex_printer)
Treats the specified character as a symbol (character representing a variable).
a = sym.Symbol('a')
If you want to define them all together, do as follows.
a, b, c, x, y = sym.symbols("a b c x y")
Treats the specified character as a function.
f = sym.Function('f')
g = sym.Function('g')
#The formula is in English numerial expression or numerical formula
expr = x**2-12*x+8
expr
#Illustrated the obtained function
plot(expr, (x, -20, 20))
<sympy.plotting.plot.Plot at 0x7faf57ced080>
#Factorization
expr = x**2 + 2*x + 1
sym.factor(expr)
#The equation is in English equation or equality
eq = sym.Eq(expr)
eq
#You may specify the right side like this
eq = sym.Eq(x**2-12*x, -8)
eq
#Solve the equation
sym.solve(eq)
#Can handle expressions using algebra
eq = sym.Eq(a * x ** 2 + b * x + c)
eq
#Solve for x
sym.solve(eq, x)
#Simultaneous equations
eq1 = 3 * x + 5 * y - 29
eq2 = x + y - 7
sym.solve([eq1, eq2])
expr = 2 * x ** 2 + 5 * x - 3
expr
#differential
sym.Derivative(expr)
#Calculate the derivative
sym.Derivative(expr).doit()
#It is the same even if it is written like this
sym.diff(expr)
# sym.Notated as an equation using Eq
sym.Eq(sym.Derivative(expr), sym.diff(expr))
#It has the same meaning as above, but if there is only one variable, it can be omitted as above.
#If there are two or more variables, you must specify what you want to differentiate.
sym.Eq(sym.Derivative(expr, x), sym.diff(expr, x))
#Second derivative
sym.Eq(sym.Derivative(expr, x, 2), sym.diff(expr, x, 2))
#Differentiate with respect to x x=Substitute 1
sym.diff(expr).subs(x, 1)
#Differentiate about x
expr = a * x ** 2 + b * x * y + c * y ** 2
sym.diff(expr, x)
#Differentiate with respect to x x=Substitute 1
sym.diff(expr, x).subs(x, 1)
Complete the following derivative formula using Sympy. [Hint] When using a function such as sin in Sympy, write it as sym.sin.
1-1.
1-2.
1-3.
1-4.
1-5.
1-6.
1-7.
1-8.
1-9.
1-10.
1-11.
1-12.
1-13.
1-14.
Solve $ 2 f'(x) + 5 f (x) = 0 $
#Ordinary differential equation
eq = sym.Eq(2 * f(x).diff(x,1) + 5 * f(x))
eq
#General solution
ans = sym.dsolve(eq)
print(ans)
ans
Eq(f(x), C1*exp(-5*x/2))
#Special solution
ans = sym.dsolve(eq, ics={f(0):1})
print(ans)
ans
Eq(f(x), exp(-5*x/2))
plot(ans.rhs, (x, -10, 10)) #rhs is the right side(Right-hand side)Meaning of
<sympy.plotting.plot.Plot at 0x7faf54dd4320>
#Special solution x=When
print(ans.subs(x, 2))
ans.subs(x, 2)
Eq(f(2), exp(-5))
#If you use a method called evalf, it will expand to floating point
ans.subs(x, 2).evalf()
Answer the following questions about the ordinary differential equation $ f'' (x) + f'(x) + 4 f (x) = 0 $.
Find a general solution.
Find the special solution when $ f (0) = 1 $, $ f'(0) = 1 $.
[Hint] ʻics = {f (0): 1, f () .diff (x, 1) .subs (, _): 1} Write `` and fill in _ appropriately to solve it.
#Integral formula
expr = x ** a
integ = sym.Integral(expr, x)
print(integ)
integ
Integral(x**a, x)
#Perform integration
integ.doit()
#It is the same even if it is written like this
sym.integrate(expr, x)
# sym.Notated as an equation using Eq
eq = sym.Eq(sym.Integral(expr, x), sym.integrate(expr, x))
print(eq)
eq
Eq(Integral(x**a, x), Piecewise((x**(a + 1)/(a + 1), Ne(a, -1)), (log(x), True)))
Complete the following integral formula using Sympy.
3-1.
3-2.
3-3.
3-4.
3-5.
3-6.
3-7.
3-8.
3-9.
3-10.
3-11.
expr = (x-a)*(b-x)
eq = sym.Eq(sym.Integral(expr, (x, a, b)), sym.integrate(expr, (x, a, b))).factor()
print(eq)
eq
Eq(-Integral((-a + x)*(-b + x), (x, a, b)), -(a - b)**3/6)
expr = x/(x**2 + 1)
eq = sym.Eq(sym.Integral(expr, (x, 1, 2)), sym.integrate(expr, (x, 1, 2)))
print(eq)
eq
Eq(Integral(x/(x**2 + 1), (x, 1, 2)), -log(2)/2 + log(5)/2)
sym.integrate(expr, (x, 1, 2)).evalf()
Solve the following definite integral equation using Sympy.
Recommended Posts