2
$\begingroup$

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.)

$\endgroup$
0

5 Answers 5

3
$\begingroup$
♯ = # & @@ # &
♯ @ q

-72

tl;dr

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

♯♯ @ q

-72

$\endgroup$
6
  • $\begingroup$ Sharp! :-P -- +1 $\endgroup$
    – Michael E2
    May 20, 2015 at 14:27
  • $\begingroup$ # & @@ q suffices ;-} +1 $\endgroup$
    – ciao
    May 21, 2015 at 5:14
  • $\begingroup$ @MichaelE2, thank you.. $\endgroup$
    – kglr
    May 21, 2015 at 8:10
  • $\begingroup$ @ciao, what was i thinking:) $\endgroup$
    – kglr
    May 21, 2015 at 8:11
  • $\begingroup$ +2 for cleverness, -1 for giving me headaches. ;P $\endgroup$ May 21, 2015 at 8:55
2
$\begingroup$
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.

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

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

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

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
*)
$\endgroup$
2
  • $\begingroup$ thanks for this suggestion. It does, though, seem more complicated than my method. $\endgroup$
    – murray
    May 20, 2015 at 1:03
  • $\begingroup$ @murray It is certainly more complicated, but as for correctness, I suppose it depends on what you may assume about the polynomials. $\endgroup$
    – Michael E2
    May 20, 2015 at 12:17
1
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.