# Extracting the constant term

### Finite difference problem

There are a large number of variables, interacting at most five at a time. A sample term is:

### How to extract the constant term?

In this example, the constant term is -1/4.

• expr /. _Subscript :> 0 Apr 15, 2022 at 22:45

Try this:

Total[Map[If[Total[Grad[#, Variables[expr]]] === 0, #, Nothing] &, MonomialList[expr]]]
(*-(1/4)*)


In general, when there are no subscripts, we proceed as Bob Hanlon indicates:

expr/. Thread[Variables[expr] -> 0]


Another way to do it is as follows:

Total[Map[If[Total[Exponent[#, Variables[expr]]] === 0, #, Nothing] &,MonomialList[expr]]]
(*-(1/4)*)

Part[CoefficientArrays[
1/4 (-1 - Subscript[φ, 49, 50] - Subscript[φ, 50, 49]) +
Subscript[φ, 50, 50]], 1]


-(1/4)

expr =
1/4 (-1 - Subscript[φ, 49, 50] - Subscript[φ, 50, 49]) + Subscript[φ, 50, 50]];


Two positional solutions

For subscripted variables

p = Position[expr, _Subscript]


{{1, 2, 2, 2}, {1, 2, 3, 2}, {2}}

For subscripted and unsubscripted variables

p = Position[expr, Alternatives @@ Variables[expr]]


{{1, 2, 2, 2}, {1, 2, 3, 2}, {2}}

Using MapAt

MapAt[0 &, p] @ expr


-1/4

Using ReplaceAt (new in 13.1)

ReplaceAt[_ :> 0, p] @ expr


-1/4