Skip to main content
Source Link
Michael E2
  • 262.2k
  • 21
  • 371
  • 838

CoefficientArrays[] is another algebraic operator that may be used for this algebraic operation, since it gives a list of {constant, linear, quadratic,...} coefficients:

Replace[
 CoefficientArrays[A, Variables[A]],
 {{const_} :> const,
  {const_, linear_, ___} :> const + linear . Variables[A]}]

(*  -1 + a + b  *)

Variation: getting the coefficients of a dummy variable, replacing each variable $v \mapsto vt$ (as in my other Series[] answer):

Total[CoefficientArrays[
   A /. v : Alternatives @@ Variables[A] :> v $t, $t][[;; UpTo[2]]],
 All]
(*  -1 + a + b  *)