LoginSignup
4
2

More than 5 years have passed since last update.

sympy で特殊関数

Last updated at Posted at 2018-02-17

sympy は Python で所謂 Matematica の要領で数式計算をしてくれるパッケージ

インストールは普通に

$ pip3 install sympy

など、pip でできる (上の例は python3 と pip3)。

基本

微積分

import sympy

sympy.var('x')

print(sympy.diff(x**2 + x, x)) # 微分
print(sympy.integrate(x**2 + x, x)) # 不定積分
print(sympy.integrate(x**2, (x, 0, 1))) # 定積分

print(sympy.latex(x**2)) # LaTex 表記

分数

0.333 じゃない、$ 1/3 $ が欲しいんだ。

print(sympy.Rational(1, 2))

数値

逆に数値が欲しい場合は evalf() メソッド

print(sympy.pi) # => pi
print(sympy.pi.evalf()) => 3.14159265358979

初等関数

普通に pi, sin, cos, sqrt, erf などなど

特殊関数

このドキュメントを見れば良いみたい。gamma, beta, ベッセル関数、などなど、それなりに揃っている。

Legendre polynomial

$$ P_\ell(x) = \texttt{legendre}(l, x) $$

from sympy import *
var('x')
print(legendre(2, x))

Spherical harmonics

$$ Y_{\ell m}(\theta, \phi) = \texttt{Ynm}(l, m, theta, phi)$$

from sympy import *
var('theta')
var('phi')
print(Ynm(2, 2, theta, phi).expand(func=True))

多項式と Legendre 多項式を行ったり来たりしたかった私はこのくらいで満足。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2