Re: Constant term in polynomial?
- To: mathgroup at smc.vnet.net
- Subject: [mg3345] Re: [mg3326] Constant term in polynomial?
- From: brucec (Bruce Carpenter)
- Date: Thu, 29 Feb 1996 03:23:48 -0500
- Sender: owner-wri-mathgroup at wolfram.com
>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
How about the following? For the multivariate polynominal poly,
Select[poly, NumberQ]. For example
In[63]:=
Short[poly = Expand[(2+Sum[a[i]^i, {i,1,50}])^2],3]
Out[63]//Short=
2 2 2 4
4 + 4 a[1] + a[1] + 4 a[2] + 2 a[1] a[2] + a[2] +
48 50 49 50 100
<<1317>> + 2 a[48] a[50] + 2 a[49] a[50] + a[50]
In[64]:=
Timing[Select[poly, NumberQ]]
Out[64]=
{0.05 Second, 4}
Cheers,
Bruce Carpenter
-----------------------
Bruce Carpenter
Courseware Coordinator phone: (217) 398-0700
Wolfram Research, Inc. fax: (217) 398-0747
100 Trade Centre Drive email: brucec at wolfram.com
Champaign, IL 61820 web: http://www.wolfram.com
==== [MESSAGE SEPARATOR] ====