Re: Final comments on CoefficientList
- Subject: [mg2541] Re: [mg2516] Final comments on CoefficientList
- From: hay%haystack at smc.vnet.net (Allan Hayes)
- Date: Fri, 17 Nov 1995 05:20:22 GMT
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
- Sender: daemon at wri.com ( )
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]; ] ]