Re: coefficient of a polynomial term
- To: mathgroup at smc.vnet.net
- Subject: [mg59382] Re: coefficient of a polynomial term
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sun, 7 Aug 2005 03:47:25 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 8/6/05 at 1:29 AM, tma at nus.edu.sg (Tun Myint Aung) wrote: >One more question again! How to extract the coefficient of a >polynomial term. For example: >poly = 2 x^2 +3 x*y +4 y^2+x^3 >I would like to get 2 for first term, 3 for second term, 4 for >third term and so on.. It is very easy to extract the coefficients of a polynomial using CoefficientList, i.e., CoefficientList[poly, x] {4*y^2, 3*y, 2, 1} Or in two variables CoefficientList[poly, {x, y}] {{0, 0, 4}, {0, 3, 0}, {2, 0, 0}, {1, 0, 0}} But note, Mathematica first puts the polynomial in cannonical order before extracting the coefficients. Possibly something a bit closer to what you want would be done by first converting the polynomial to a list, i.e., List@@poly {2*x^2, x^3, 3*x*y, 4*y^2} then using rules to set x,y to one, i.e. List@@poly /. {x -> 1, y -> 1} {2, 1, 3, 4} But this still doesn't put them in the order you specified and I really don't recommend this approach. The problem with doing this or trying to get the specific order you specified is information loss. Unless the list of coefficients is kept in a standard order there is no way to recover the polynomial from the list of coefficients. With the order output by CoefficientList, recovery of the polynomial can be done by Plus@@MapIndexed[#1*x^(First[#2] - 1)&, CoefficientList[poly, x]] x^3 + 2*x^2 + 3*y*x + 4*y^2 -- To reply via email subtract one hundred and four