0
0

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 3 years have passed since last update.

分数式から係数を取り出す方法

Posted at

多項式の係数を取り出すときはcoeff()メソッドを使えば良いが、分数式になると単純に使うだけではダメで、expand()メソッドで展開する必要がある。

how_to_get_coeff_from_fractional_expression.py
# coding:utf-8


import sympy as sp


# f(x)/g(y) のx**aの次数の取り方
# sympy の変数を作る
x, y = sp.symbols('x y')
# sympy.Symbol の関数(分数式)を作る
symbolFunc = (x**2 + 2*x + 3) / (4*y + 5)
# x**1 の係数として、 2/(4y + 5) を期待するが 0 になる
print(symbolFunc.coeff(x, 1))
# ==> 0
# expand() メソッドを噛ませるとうまくいく
print(symbolFunc.expand().coeff(x, 1))
# ==> 2/(4y + 5)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?