6
$\begingroup$

Finite difference problem

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

enter image description here

How to extract the constant term?

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

$\endgroup$
1
  • 3
    $\begingroup$ expr /. _Subscript :> 0 $\endgroup$
    – Bob Hanlon
    Commented Apr 15, 2022 at 22:45

3 Answers 3

6
$\begingroup$

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)*)
$\endgroup$
5
$\begingroup$
Part[CoefficientArrays[
  1/4 (-1 - Subscript[φ, 49, 50] - Subscript[φ, 50, 49]) + 
   Subscript[φ, 50, 50]], 1]

-(1/4)

$\endgroup$
3
$\begingroup$
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

$\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.