MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Final comments on CoefficientList


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

In[1]:= CoefficientList[0,x]

Out[1]= {}

But,

In[2]:= PolynomialQ[0,x]

Out[2]= True

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

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

Out[4]= {{}, {0, 1}, {0, 1}}

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

Out[5]= {{}, {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
University of Colorado, Boulder


  • Prev by Date: Limitations of NDSolve
  • Next by Date: Q: Good book on programming...
  • Previous by thread: Re: Limitations of NDSolve
  • Next by thread: Final comments on CoefficientList