Suppose I have a quadratic polynomial in two variables x and y in which the squares with respect to x and y have already been completed:

 q = -72 + 9 (-2 + x)^2 + 4 (3 + y)^2 ;


How might I extract the "constant" part -72 of this expression?

(Note that I am not asking how to find the constant term of the polynomial; I know you can do that by simply replacing x and y with 0.)

♯ = # & @@ # &
♯ @ q


-72

tl;dr

♯♯ = # & @ ## & @@ # &

♯♯ @ q


-72

• Sharp! :-P -- +1 May 20, 2015 at 14:27
• # & @@ q suffices ;-} +1
– ciao
May 21, 2015 at 5:14
• @MichaelE2, thank you..
– kglr
May 21, 2015 at 8:10
• @ciao, what was i thinking:)
– kglr
May 21, 2015 at 8:11
• +2 for cleverness, -1 for giving me headaches. ;P May 21, 2015 at 8:55
q /. Power -> (0 &)


-72

Notes:

Since

0&[x,y]


0

then

Power[x, y]/.Power->(0&)


0

and

q /. Power -> (0 &)


-72

Additional ways using ReplaceAll for OP's specific example:

q /. _Power -> 0 (* thanks: @Guesswhoitis. *)
q /. Times -> (0&)
q /. _Times -> 0


and using Block (temporarily re-defining Power or Times as 0&):

Block[{Power = 0 &}, q]
Block[{Times = 0 &}, q]


Note that none of the above would work for

q1 = -72 (a^2) + 9 (-2 + x)^2 + 4 (3 + y)^2


since the constant term in q1 contains elements with head Times and Power.

• Would q /. _Power -> 0 work also? May 20, 2015 at 0:46
• @J. M., yes... that's better.
– kglr
May 20, 2015 at 0:53
• @Bob Hanlon, thank you for the edit.
– kglr
May 20, 2015 at 0:53
• One could argue that -72(a - 0)^2 is part of the "square" terms… May 20, 2015 at 8:38

OK, I think this is easier than I thought:

   c = First[List @@ q]
(* -72 *)


Is there a yet easier way, or some alternate ways?

(Sorry, I had not intended to post something I could so quickly answer myself!)

• Assuming poly in form of question, First@q suffices, as does e.g. Cases[q, _?NumericQ]...
– ciao
May 19, 2015 at 22:59
• @ciao: ah yes, the default sort order will put the constant term first! May 20, 2015 at 1:01

Perhaps use Cases DeleteCases on the default level 1 (I overlooked an assumption in my original use of Cases):

q = -72 + 9 (-2 + x)^2 + 4 (3 + y)^2;
DeleteCases[q, _?(!FreeQ[#, Alternatives @@ Variables[q]] &)]
(*  -72  *)


A few other methods and examples. One problem with using First is that it assumes the constant is first, which I believe will be true if the constant is a simple number. It seems likely the OP has in mind that the constant term will be a single integer, but the scope of the problem is naturally larger. Aside from some numeric expressions not being sorted so that they appear before terms containing variables, there is also the possibility of symbolic constants. Here are some approaches to problems for which First fails. They use DeleteCases in the same way as above; the differences are in how constants are determined.

q = GoldenRatio^2 + (a + 2)^2 + 4 (b + 1)^2;  (* < System symbolic constant *)
DeleteCases[q, _?(! FreeQ[#, Alternatives @@ Variables[q]] &)]
First@q
(*
GoldenRatio^2
(2 + a)^2                                   (* < First failed *)
*)

q = 1 + Sqrt[2] + (a + 2)^2 + 4 (b + 1)^2;    (* < more than one constant term *)
DeleteCases[q, _?(FreeQ[#, Alternatives @@ Variables[q]] &)]
First@q
(*
(2 + a)^2 + 4 (1 + b)^2
1
*)

q = (a + 2)^2 + 4 (b + 1)^2 + C[1] - 5;
myvars = {a, b};                              (* < user-specified variables *)
DeleteCases[q, _?(! FreeQ[#, Alternatives @@ myvars] &)]
First[q]
(*
-5 + C[1]
-5
*)

SetAttributes[c, Constant];                   (* < user-specified Constants *)
q = (a + 2)^2 + 4 (b + 1)^2 + (c - 1)^2;
DeleteCases[q, _?(Dt[#] =!= 0 &)]
First@q
ClearAll[c];
(*
(-1 + c)^2
(2 + a)^2
*)

• thanks for this suggestion. It does, though, seem more complicated than my method. May 20, 2015 at 1:03
• @murray It is certainly more complicated, but as for correctness, I suppose it depends on what you may assume about the polynomials. May 20, 2015 at 12:17

Apparently, nobody brought up this possibility:

SeriesCoefficient[-72 + 9 (-2 + x)^2 + 4 (3 + y)^2, {x, 2, 0}, {y, -3, 0}]
-72


where it is assumed that you know the terms subtracted from the corresponding variables.