要计算一个变量中多项式的次数,可以使用hipow 函数。
(%i) p1 : 3*x^5 + x^2 + 1$
(%i) hipow(p1,x);
(%o) 5
对于具有多个变量的多项式,您可以将hipow 映射到listofvars 函数返回的变量上,然后取结果列表中的最大值。
(%i) p2 : 4*y^8 - 3*x^5 + x^2 + 1$
(%i) degree(p) := if integerp(p) then 0 else
lmax(map (lambda([u], hipow(p,u)),listofvars(p)))$
(%i) degree(p1);
(%o) 5
(%i) degree(p2);
(%o) 8
(%i) degree(1);
(%o) 0
coeff 函数返回x^n 的系数,给定coeff(p,x,n),因此要生成一个变量中多项式的系数列表,我们可以遍历 x 的幂,将系数保存到列表。
(%i) coeffs1(p,x) := block([l], l : [],
for i from 0 thru hipow(p,x)
do (l : cons(coeff(p,x,i),l)), l)$
(%i) coeffs1(p1,x);
(%o) [3, 0, 0, 1, 0, 1]
要生成多个变量中多项式的系数列表,请将coeffs1 映射到listofvars。
(%i) coeffs(p) := map(lambda([u], coeffs1(p, u)), listofvars(p))$
(%i) coeffs(p2);
(%o) [[- 3, 0, 0, 1, 0, 4 y^8 + 1],
[4, 0, 0, 0, 0, 0, 0, 0, - 3 x^5 + x^2 + 1]]