4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Sympy 逆引きリファレンス

Last updated at Posted at 2017-09-08

LaTeX出力

In [59]: h_x
Out[59]: 
       2                          3
(x + 1) ⋅(x + 2)⋅(x + 3) + (x + 4) 

In [60]: latex(h_x)
Out[60]: '\\left(x + 1\\right)^{2} \\left(x + 2\\right) \\left(x + 3\\right) + \\left(x + 4\\right)^{3}'

式の展開

In [50]: f_x
Out[50]: 
       2                
(x + 1) ⋅(x + 2)⋅(x + 3)

In [51]: expand(f_x)
Out[51]: 
 4      3       2           
x  + 7⋅x  + 17⋅x  + 17⋅x + 6

因数分解

In [21]: g_x = x**2 + 2*x + 1

In [22]: factor(g_x,x)
Out[22]: 
       2
(x + 1) 

因数のリストを取得

In [21]: g_x = x**2 + 2*x + 1

In [22]: factor(g_x,x)
Out[22]: 
       2
(x + 1) 

In [23]: factor_list(g_x,x)
Out[23]: (1, [(x + 1, 2)])

積での因子分解

In [35]: f_x
Out[35]: 
 -2⋅x         
ℯ    ⋅sin(2⋅x)

In [39]: f_x.args
Out[39]: 
⎛ -2⋅x          ⎞
⎝ℯ    , sin(2⋅x)⎠

多項式の除算

In [25]: f 
Out[25]: x**2 + 4/x**2

In [26]: g = (x + 2/x)**2

In [27]: div(g, f)
Out[27]: (1, 4)

In [28]: q,r = div(g,f)

In [29]: h = q * f + r

In [30]: h
Out[30]: x**2 + 4 + 4/x**2

In [31]: g == h
Out[31]: False

In [32]: g.expand() == h
Out[32]: True

関数の変換

In [45]: f_x
Out[45]: 
   ⎛    π⎞
sin⎜x + ─⎟
   ⎝    4⎠

In [46]: f_x.rewrite(cos)
Out[46]: 
   ⎛    π⎞
cos⎜x - ─⎟
   ⎝    4⎠

In [47]: f_x.rewrite(tan)
Out[47]: 
       ⎛x   π⎞ 
  2⋅tan⎜─ + ─⎟ 
       ⎝2   8⎠ 
───────────────
   2⎛x   π⎞    
tan ⎜─ + ─⎟ + 1
    ⎝2   8⎠    

In [48]: f_x.rewrite(exp)
Out[48]: 
   ⎛     ⎛     π⎞      ⎛    π⎞⎞ 
   ⎜   ⅈ⋅⎜-x - ─⎟    ⅈ⋅⎜x + ─⎟⎟ 
   ⎜     ⎝     4⎠      ⎝    4⎠⎟ 
-ⅈ⋅⎝- ℯ           + ℯ         ⎠ 
────────────────────────────────
               2    

平面・空間ベクトル

行列で表示する方法もあるみたいだが、高校数学の範囲では Point でなんとかなる?

In [64]: Point([3,4])
Out[64]: Point2D(3, 4)

スカラー倍

scaleで、各変数ごとにかける係数を指定すればいける。

In [65]: a = Point([2,-3])

In [70]: a.scale(3)
Out[70]: Point2D(6, -3)

In [71]: a.scale(3,3)
Out[71]: Point2D(6, -9)

距離

In [104]: a
Out[104]: Point2D(2, -3)

In [105]: a.origin
Out[105]: Point2D(0, 0)

In [106]: a.distance(a.origin)
Out[106]: √13

内積

In [92]: a
Out[92]: Point2D(2, -3)

In [93]: c
Out[93]: Point2D(4, 4)

In [94]: a.dot(c)
Out[94]: -4

直交方向ベクトル

In [108]: a
Out[108]: Point2D(2, -3)

In [109]: a.orthogonal_direction
Out[109]: Point2D(3, 2)

In [110]: a.dot(a.orthogonal_direction)
Out[110]: 0

確率

サイコロの確率変数

In [1]: from sympy.stats import P, E, Die

In [2]: X = Die('X',6)

In [3]: X
Out[3]: X

In [4]: P(X>3)
Out[4]: 1/2

In [5]: E(X)
Out[5]: 7/2

In [6]: Y = Die('Y',6)

In [7]: E(X+Y)
Out[7]: 7

In [8]: P(X+Y>10)
Out[8]: 1/12

そのほかにも、コイン投げの確率分布とか、一様分布とか、だいたいは揃っている

4
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?