       • Subject: [mg2516] Final comments on CoefficientList
• From: sherod at boussinesq.Colorado.EDU (Scott Herod)
• Date: Wed, 15 Nov 1995 07:01:53 GMT
• Approved: usenet@wri.com
• Distribution: local
• Newsgroups: wri.mathgroup
• Organization: University of Colorado at Boulder
• Sender: daemon at wri.com ( )

```I'm pretty sure that CoefficientList has a bug in it.  The simplest
example is

In:= CoefficientList[0,x]

Out= {}

But,

In:= PolynomialQ[0,x]

Out= True

This causes more problems when you try to create the list of coefficients
of a polynomial in multiple variables.

In:= CoefficientList[ x y + x y^2, {y,x}]

Out= {{}, {0, 1}, {0, 1}}

In:= CoefficientList[x y + x y^2, {x,y}]

Out= {{}, {0, 1, 1}}

Below are two functions which (I think) act correctly.  Neither makes use
of caching as discussed by David Wagner in the most recent Mathematica
Journal but such could be added easily.  I would appreciate comments on
improvements of these because I need to run them MANY times.

myCoefList[eqn_, x_Symbol] := myCoefList[eqn,{x}];
myCoefList[{eqn_}, x__] := myCoefList[eqn, x];
myCoefList[{eqn1_, eqn2__}, x__] :=
{myCoefList[eqn1, x], myCoefList[{eqn2}, x]};

myCoefList[eqn_, {x_}] :=
Table[Coefficient[eqn,x,i], {i,0, Max[Exponent[eqn, x],0]}];
myCoefList[eqn_, {x_, y__}] :=
Table[Coefficient[myCoefList[eqn,y],x,i],
{i,0,Max[Exponent[eqn,x],0]}];

myCoefList2[eqn_, var_] := Module[{coef, spam, a, b, c},
myCoefList::polynomial = "The equation is not a polynomial
in the variables";
coef[spam_, {a_, b_}] := Coefficient[spam, a, b];
coef[spam_, {a_, b_}, c__] := Coefficient[coef[spam, c], a, b];
If[PolynomialQ[eqn, var],
Array[
coef[eqn, Inner[List, var, List[##], Sequence]] &,
(Exponent[eqn,#] + 1 &) /@ var, 0]
,
Message[myCoefList::polynomial];
]
]

Scott A. Herod
Applied Mathematics