Re: Extracting polynomial coefficients?
- To: mathgroup@smc.vnet.net
- Subject: [mg11499] Re: Extracting polynomial coefficients?
- From: Bob McDonald <bob@nsgsun.aae.uiuc.edu>
- Date: Fri, 13 Mar 1998 12:21:49 -0500
- Organization: University of Illinois at Urbana-Champaign
- References: <6e87es$nlo@smc.vnet.net>
Thomas Bell wrote: > > I have a very long polynomial that's a function of four variables, a, b, > c, d. I would like Mathematica to (1) tell me what combinations of a, > b, c, and d are in my polynomial, and (2) tell me what the coefficients > are for each of the variable terms. > > For example, say I have constant terms r and q. My polynomial might > look something like > > 4 a^2 b^3 c + r q d^5 + a d^3 + q c^2 a^2 + r^2 a d^3 > > I would like to have Mathematica output something like: > > a^2 b^3 => 4 > d^5 => r q > a d^3 => 1 + r^2 > c^2 a^2 => q I've done something similar. Here's some ideas. 1. First, a function to find all the monomials of a certain order. powersN[order_,varlist_] := Module[ {outerNest,x}, outerNest[x_] := Outer[Times,x,varlist]; Union[Flatten[Nest[outerNest,varlist,order-1]]]]; For example, powersN[3,{x1,x2}] gives {x1^3, x1^2 x2, x1 x2^2 x2^3} In your case, varlist = {a,b,c,d} Let mono[n] = powersN[n] for some integer n be the list of all monomials of order n in the variables in varlist 2. Try something like (poly is your given polynomial) poly = 4 a^2 b^3 c + r q d^5 + a d^3 + q c^2 a^2 + r^2 a d^3 mono[4] = powersN[4,varlist]; coeffs[4] = Coefficient[poly,mono[4]] /. {a->0,b->0,c->0,d->0} The substitution part at the end of Coefficient is so that you don't get, say 4 a^2 as the coefficient of b^3 c 3. Now, you have a list of all the coefficients of each of the monomials at order n. The list is in the same order as the list of monomials you have. I'm not sure what you want to do from there, but for example, you could search in coeffs for all non-zero entries, and print their coefficients. coeff_list[4] = {} Do[If[coeffs[4][[j]] =!= 0, AppendTo[coeff_list[4],{mono[4][[j]],coeffs[4][[j]]}]], {j,1,Length[mono[4]]}] In your example above, this printed out {{a^2 c^2 ,q},{a d^3, 1+r^2}} You could then do this for any order you wished. Hope this helps. Not sure if its the most efficient, but it seems to work for me. __________________________________________________________ |Bob McDonald | Nonlinear Systems Group| |bob@nsgsun.aae.uiuc.edu | Aero/Astro Engineering | | University of Illinois at Urbana-Champaign | \________________________________________________________/