Re: Constant term in polynomial?

*Subject*: [mg3386] Re: Constant term in polynomial?*From*: espen.haslund at fys.uio.no (Espen Haslund)*Date*: 3 Mar 1996 13:44:01 -0600*Approved*: usenet@wri.com*Distribution*: local*Newsgroups*: wri.mathgroup*Organization*: Universitet i Oslo*Sender*: daemon at wri.com

In article <4gmhdc$gmv at dragonfly.wolfram.com>, bruck at mtha.usc.edu says... > >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 ? > >I suppose one way would be to use > > Coefficient[Coefficient[7 + 3 x y + y^2,x,0],y,0]. > >But that's incredibly clunky, especially since I may have fifty or more >variables in my real-life problem. > >I could evaluate the expression under the rule {x->0, y->0}, with the same >problem: for fifty variables that's awkward. I could build the rule using >Variables[expr], but that's clumsy and seems inefficient. > >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? > >Or I can introduce an auxiliary variable "one", refer to the polynomial as >"7 one + 3 x y + y^2", and ask for Coefficient[expr, one]. Gag! If I ever >want to EVALUATE it, I have to remember to use the rule one->1. > >There MUST be a standard way to do this, but I can't think of what it could be! > >--Ron Bruck > Dear, Ron: Interesting problem. I have read some of the answers to your question (including our own), and taken some of the ideas. If you want to be sure that your function gives the same as the [[1, 1, ..., 1]] element of what CoefficientList returns then you should probably use something like: constantCoefficient[poly_, vars_] := First[Flatten[CoefficientList[poly, vars] ] ] This is quite slow for long polynomials. Your idea of a set of rules like {x->0, y->0} gives a much faster function: coeff0[poly_, vars_] := Module[{r}, r = Map[Rule[#, 0] &, Flatten[{vars}] ]; poly /. r ] Also you are right that looking only on the first part of the expression is even more efficient. If you know that your constant coefficient is an integer, rational, or approximate real number then the following should avoid failing for the special cases discussed by several people. I think it may also work if your coefficient is a complex number. c0[poly_] := Module[{d}, If[Length[poly] < 2, Return[Select[d + d^2 + poly, NumberQ] ], Return[Select[d + Take[poly, 2], NumberQ] ] ] ] The dummy d and d^2 is to avoid some of the special cases. If you need also irrational coefficients, this modified version should work, but is significantly slower. c0Irrational[poly_] := Module[{d}, Select[d + d^2 + poly, NumberQ[N[#]] &] ] To se some of the problems (inconsistencies) that may occur you can try all definitions, and also CoefficientList[poly, vars] and PolynomialQ[poly, vars], with poly = Log[x](and vars = x). Hope this is of some help - Espen

**Re: Constant term in polynomial?**

**FAQ for comp.soft-sys.math.mathematica ?**

**Re: Constant term in polynomial?**

**ConstantCoefficient of polynomial**