extracting the monomials from a multivariable polynomial
Subject: extracting the monomials from a multivariable polynomial
From: Stavros Macrakis
Date: Sun, 27 Jan 2008 23:31:55 -0500
>
> Is there a function which will extract the list of monomials in a
> multivariable polynomial? For instance...:
>
> p: 2*x^2*y^2 -> [2*x^2*y^2]
> p: 2 + x^2 + y --> [2, x^2, y]
> p: x*y + 3*x^3 - y^4 --> [x*y, 3*x^3, - y^4]
>
In these cases, the monomials are simply the arguments to the "+" function,
so "args" will work:
args( 2 + x^2 + y ) => [2, x^2, y]
However, this won't work in cases like
args( x + %pi + 1) => [1, %pi, x]
args( sqrt(2)*x + x ) => [sqrt(2)*x, x]
args( a*x + x ) => [a*x, x] <<< where a is a parameter, not
a variable
...where you'll first need to collect coefficients using facsum, check
whether the result is a sum, and collect the constant terms (by multiplying
the whole thing by a dummy variable):
monomials(p,vars) :=
block([inflag:true],
p: apply('facsum,cons('?zwzwz * p, vars)),
if mapatom(p) or op(p)#"+" then [p / '?zwzwz]
else args( p ) / '?zwzwz )
monomials((x+y+a+%pi+1)^2,[x,y]) =>
[ (a+%pi+1)^2, 2*(a+%pi+1)*x, x^2, 2*(a+%pi+1)*y, 2*x*y, y^2 ]
Is this what you had in mind? You might also want to look at the ratcoef
function for a different way of cutting up polynomials.
-s