Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

Re: Final comments on CoefficientList

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2541] Re: [mg2516] Final comments on CoefficientList
  • From: Allan Hayes <hay%haystack at smc.vnet.net>
  • Date: Fri, 17 Nov 1995 00:20:22 -0500

Scott Herod <sherod at boussinesq.Colorado.EDU>
in [mg2516] Final comments on CoefficientList

defines two new coefficient list functions (attached) that always  
give cuboidal arrays of the coefficients, unlike the built-in  
function, CoefficientList.

Here is another such function that is quicker and simpler.

   CoefficientList3[poly_, vars_]:=
      Fold[
         Outer[Coefficient[#1,#2]&,#1,#2]&,
         {Expand[(Times@@vars) poly]},
         vars^Range[Exponent[poly,vars]+1]
      ]//First
	
The function for getting back to the polynomial now simpler:
	
   CoeffsToPoly[c_,v_] :=
      Dot[c,Sequence@@Reverse[v^Range[0,Dimensions[c]-1]]]

Timings:
First construct a test polynomial (this one,pp, is of degree 4 in  
each of the variables x,y,z,u,v)

   ri := Random[Integer,{1,9}];
   vars = {x,y,z,u};
   pp =
     Plus@@Flatten[Array[Times@@(rivars^{##})&,{4,4,4,4},{0,0,0,0}]];

   (cl3 = CoefficientList3[pp, vars]);//Timing
      {0.9 Second, Null}

Scott's two functions
   (cl1= myCoefList[pp, vars]);//Timing
      {31.0167 Second, Null}

   (cl2= myCoefList2[pp, vars]);//Timing
      {29.7167 Second, Null}

The built-in function
   (cl= CoefficientList[pp, vars]);//Timing
      {2.2 Second, Null}

Checks
   cl1 == cl2 ==cl3
      True

   (pp - CoeffsToPoly[cl3,vars])//Expand
      0

Allan Hayes
hay at haystack.demon.co.uk

*********************8

Scott's Code
( Scott sent me a correction to the last definition in the code for  
myCoefList)

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];
  ]
]



  • Prev by Date: Mathematica Training
  • Next by Date: Re: turn off symbols in MultipleListPlot?
  • Previous by thread: Re: Final comments on CoefficientList
  • Next by thread: Q: Good book on programming...