Is there a built-in function in maxima to get from a polynomial function a list with its coefficients? And to get the degree of the polynomial?
The most similar function I found is args
, but it also returns the variable together with the coefficient. I could have accepted this, still more when using length
together with args
would return the degree. The problem is that args
doesn't work with zero-degree polynomials.
Is there another function that adjusts better to these purposes? Thanks in advance.
To compute the degree of a polynomial in one variable, you can use the hipow
function.
(%i) p1 : 3*x^5 + x^2 + 1$
(%i) hipow(p1,x);
(%o) 5
For a polynomial with more than one variable, you can map hipow
over the variables returned by the listofvars
function, and then take the maximum of the resulting list.
(%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
The coeff
function returns the coefficient of x^n
, given coeff(p,x,n)
, so to generate a list of the coefficients of a polynomial in one variable, we can iterate through the powers of x, saving the coefficients to a list.
(%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]
And to generate a list of the coefficients of a polynomial in more than one variable, map coeffs1
over the 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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With