Re: Re: Recombining CoefficientList
- Subject: [mg2565] Re: [mg2531] Re: [mg2501] Recombining CoefficientList
- From: roberto at cce.ufes.br (Roberto Colistete Junior)
- Date: Tue, 21 Nov 1995 09:24:17 -0500
- Approved: usenet@wri.com
- Distribution: local
- Newsgroups: wri.mathgroup
- Organization: Wolfram Research, Inc.
> > Here is a slight modification of tmj 5.3 p24 (In[] and Out[]) > > ToPolynomial[cl_, vars_] := > Plus@@Flatten[ > MapIndexed[#1 Times@@(vars^(#2-1))&,cl,{Depth[cl]-1}] > ] > > Example: > > poly = a x^3 + b x^4 y + c x y + d y^2 + e > > 3 4 2 > e + a x + c x y + b x y + d y > > cl = CoefficientList[poly, {x,y}] > > {{e, 0, d}, {0, c, 0}, {}, {a, 0, 0}, {0, b, 0}} > > ToPolynomial[cl, {x,y}] > > 3 4 2 > e + a x + c x y + b x y + d y > > > Allan Hayes > hay at haystack.demon.co.uk > > > There is a subtle bug in ToPolynomial, which can be seen easily, adding an exponent to e : poly = a x^3 + b x^4 y + c x y + d y^2 + e^2 2 3 4 2 e + a x + c x y + b x y + d y cl = CoefficientList[poly, {x,y}] 2 {{e , 0, d}, {0, c, 0}, {}, {a, 0, 0}, {0, b, 0}} ToPolynomial[cl, {x,y}] Thread::tdlen: Objects of unequal length in {x, y} cannot be combined. Thread::tdlen: Objects of unequal length in {x, y} {0, 0, 0} cannot be combined. Thread::tdlen: Objects of unequal length in e {x, y} {0, 0, 0} cannot be combined. General::stop: Further output of Thread::tdlen will be suppressed during this calculation. The problem is that {Depth[coef] - 1} works fine just to coefficients with depth equal to one. Here is a solution : ToPolynomial2[cl_, vars_] := Plus@@Flatten[ MapIndexed[#1 Times@@(vars^(#2 - 1))&, cl, {Length[vars]}] ] To allow the form ToPolynomial[cl, x], with x not being a list, this is a extended version : ToPolynomial3[cl_, vars_] := Plus@@Flatten[ MapIndexed[#1 Times@@(vars^(#2 - 1))&, cl, {If[Head[vars] === List, Length[vars], 1]} ] ] Repeating the example with poly : ToPolynomial2[cl, {x,y}] 2 3 4 2 e + a x + c x y + b x y + d y ToPolynomial3[cl, {x,y}] 2 3 4 2 e + a x + c x y + b x y + d y