Re: Constant term in polynomial?
- To: mathgroup at smc.vnet.net
- Subject: [mg3333] Re: Constant term in polynomial?
- From: bruck at pacificnet.net (Ronald Bruck)
- Date: Wed, 28 Feb 1996 03:35:12 -0500
- Organization: University of Southern California
- Sender: owner-wri-mathgroup at wolfram.com
In article <4gmhdc$gmv at dragonfly.wolfram.com>, bruck at mtha.usc.edu (Ronald Bruck) wrote: :Arrgh, I feel stupid asking this question, but I can't think how to do it: :how do I find the constant term in a polynomial in several variables in :Mathematica? For example, the "7" in 7 + 3 x y + y^2 ? ... :First[7 + 3 x y + y^2] will work for this one, since the 7 is present and :appears first in the FullForm representation. But it won't work in :First[3 x y + y^2], which returns 3 x y. : :OK, so I can build a command which computes Variables[First[expr]], and :if that's empty, returns 0; otherwise returns First[expr]. Also clunky :IMHO, but it seems the most workable--unless there's some trap I'm missing? Yep, there's a trap -- First[3 x y] returns 3! So I ended up implementing it as follows: ConstantCoefficient[poly_] := Module[{c}, If[Head[poly] === Plus, c = First[poly]; If[Variables[c] == {}, Return[c], Return[0]], (* else head != plus *) If[Variables[poly] == {}, Return[poly], Return[0]] ] ] This won't recognize E + x as having constant term E, but fortunately my polynomials all have rational coefficients. It does seem there ought to be a built-in function to do this. Someone e-mailed me a suggestion to use poly /. x_^e_ -> 0. This almost works (it doesn't recognize the x in 7 + x as a power of x); it does seem rather dangerous to apply the rule x_ -> 0! The suggestion reminds me of another problem: suppose I want to work with multivariable polynomials up through a certain degree p. So when, e.g., I multiply two such polys, I need to get rid of terms like x^p y. This can be done by applying rules like (x^m_ y^n_ /; Positive[m+n-p]) -> 0 (although on one machine I have to use (q__ x^m_ y^n_ /; Positive[m+n-p]) -> 0, don't ask me why), with the annoyance that we also have to build rules for x^m_ y -> 0 and x y^m_ -> 0 and x^m_ -> 0 and y^n_ -> 0! Is there some more elegant way to do this--as well as faster--preferably one which works for general variables, not just x and y? This came up when I was solving an integral equation for a function of two variables, and used undetermined coefficients. --Ron Bruck Now 100% ISDN from this address ==== [MESSAGE SEPARATOR] ====