[PYTHON] How to use SymPy

background

Recently, I have sometimes needed an analytical solution of a polynomial that satisfies certain boundary conditions. Finding an analytical solution seemed to be difficult, so I tried to find out if it could be made easier using SymPy.

SymPy

SymPy is a python library for performing symbolic operations. You can do the following:

For example, suppose you want to find the analytical solution of the following simultaneous equations. $ bx + ay = ab $ $ x - cy = c$ The solution of this simultaneous equation is

import sympy
a = sympy.symbols("a")
b = sympy.symbols("b")
c = sympy.symbols("c")
x = sympy.symbols("x")
y = sympy.symbols("y")
sympy.solve([b*x+a*y-b*a,x-c*y-c],(x,y))

The result is $ \ frac {ac \ left (b + 1 \ right)} {a + bc}, \ frac {b \ left (a --c \ right)} {a + bc} $. It can be obtained.

Coefficient of polynomial

Here, the polynomial

f(x) = A_{0}+A_{1} x+A_{1} x^{2} + A_{3} x^{3} + A_{4} x^{4} + A_{5} x^{5}

among,

f(0) = 0 f(1) = S_{1} f'(0) = 0 f'(1) = 0 f''(0) = 0 f''(1) = 0 Suppose you want to find a coefficient that satisfies.

for i in range(6):
    exec(f"A{i} = sympy.Symbol(\"A{i}\")")
S1 = sympy.Symbol("S1")
x = sympy.Symbol("x")

f = A0 + A1*(x) + A2*(x)**2 + A3*(x)**3 + A4*(x)**4 + A5*(x)**5
f1 = sympy.diff(f, x)
f2 = sympy.diff(f1, x)

First, prepare the variables and the function you want to find as above,

The condition of $ f (0) = 0 $ is

cond0 = f.subs(x, 0) - 0

It is expressed as. Similarly, all the remaining conditions

cond1 = f.subs(x, 1) - S1
cond2 = f1.subs(x, 0) - 0
cond3 = f1.subs(x, 1) - 0
cond4 = f2.subs(x, 0) - 0
cond5 = f2.subs(x, 1) - 0

Defined in.

$ A_ {0}, A_ {1}, A_ {2}, A_ {3}, A_ {4}, A_ {5} $ that satisfy all of these conditions

solution = sympy.solve([cond0, cond1, cond2, cond3, cond4, cond5],(A0,A1,A2,A3,A4,A5))

Then the answer is (A_{0},A_{1},A_{2},A_{3},A_{4},A_{5}) = (0, 0, 0, 10 S_{1}, - 15 S_{1}, 6 S_{1}) I was asked.

Substituting this result into $ f (x) $

for i in range(6):
    exec(f"f = f.subs(A{i}, solution[A{i}])")

f(x) = 10S_{1}x^{3} -15S_{1}x^{4} + 6S_{1}x^{5} f'(x) = 30S_{1}x^{2} -60S_{1}x^{3} + 30S_{1}x^{4} f''(x) = 60S_{1}x -180S_{1}x^{2} + 120S_{1}x^{3}

And now we know the function $ f (x) $ we want to find.

Recommended Posts

How to use SymPy
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook
How to use Pandas Rolling
[Note] How to use virtualenv
How to use redis-py Dictionaries
Python: How to use pydub
[Python] How to use checkio
[Go] How to use "... (3 periods)"
How to use Django's GeoIp2
[Python] How to use input ()
How to use the decorator
[Introduction] How to use open3d
How to use Python lambda
How to use Jupyter Notebook
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Google Colaboratory
How to use Python bytes
How to use cron (personal memo)