LoginSignup
4
4

More than 5 years have passed since last update.

sympy で 多項式の係数を取り出す

Last updated at Posted at 2018-02-19

===========================

$$
f = 2 x^3 + x + 5
$$
のような多項式があって、$x^n$ の係数を知りたい。

coef

方法1: coef メソッド

import sympy

sympy.var('x')
f = 2*x**3 + x + 5

f.coeff(x, 3) # => 2; x^3 の係数 2 を得る

polynomial

方法2: Polynomial manipulation tools 使う


import sympy

sympy.var('x')
f = 2*x**3 + x + 5

poly = sympy.poly(f)

poly.coeffs() # => [2, 1, 5] 
poly.all_coeffs() # => [2, 0, 1, 5]  x**2 の係数も出る
poly.degree() # => 3 次数
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