       Re: Constant term in polynomial?

• Subject: [mg3367] Re: Constant term in polynomial?
• From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
• Date: 2 Mar 1996 14:23:02 -0600
• Approved: usenet@wri.com
• Distribution: local
• Newsgroups: wri.mathgroup
• Organization: University of Colorado, Boulder
• Sender: daemon at wri.com

```In article <4gro41\$1d6 at dragonfly.wolfram.com>,
Ronald Bruck <bruck at pacificnet.net> wrote:
>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 ?
>
>... 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],
>   (* else head != plus *)
>      If[Variables[poly] == {}, Return[poly], Return]
>   ]
>]

Why don't you use the pattern matcher to check for the head Plus?

ConstantCoefficient[poly_Plus] :=
With[{c = First[poly]}, If[NumberQ[N[c]], c, 0]]

>This won't recognize E + x as having constant term E, but fortunately my
>polynomials all have rational coefficients.

The method given above works for E+x.  It won't work for E + 1 + x,
however.  But the following does:

ConstantCoefficient[poly_Plus] :=
Select[poly, NumberQ[N[#]]&]

Select works on expressions wtih any head, not just list.
What should happen here is that the level-1 parts of the Plus that
are numeric will be returned, _wrapped_in_Plus_:

In:=
ConstantCoefficient[E^2 + Pi + Sqrtx + Sqrt]
Out=
2
Sqrt + E  + Pi

>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!

Indeed, the polynomial I used as an example above will break this technique.

Dave Wagner
Principia Consulting
(303) 786-8371
dbwagner at princon.com
http://www.princon.com/princon

```

• Prev by Date: Re: Constant term in polynomial?
• Next by Date: ConstantCoefficient of polynomial
• Previous by thread: Re: Constant term in polynomial?
• Next by thread: Re: Constant term in polynomial?