Evaluate Only Part of a Function
Update: Selecting from a list expressions those that do not contain subexpressions that divide by 1 or subtract 0:
list = {HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]],
HoldForm[Plus[7, Times[2, Divide[16, Subtract[9, Plus[4, 1]]]]]],
HoldForm[Plus[3, Times[7, 9], 29]]};
condition = FreeQ[#, (s_Subtract /; s == 0) | (d_Divide /; d == 1), ∞] &;
Select[list, condition]
{7 + 2 16/(9 - (4 + 1)), 3 + 7 9 + 29}
Original answer:
exp = Inactivate[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]];
Activate[exp, Divide | Subtract]
3 + 7*(9 - (4 + 5))*29
An alternative way to evaluate completely subexpressions with head Divide
or Subtract
using RuleCondition
(from WReach's answer in the q/a linked in Sjoerd's answer):
HoldForm[Plus[3, Times[7, Subtract[9, Plus[4, 5]], Divide[29, Subtract[4, 3]]]]] /.
e : _Subtract | _Divide :> RuleCondition[e]
3 + 7 0 29
The answer of kglr works if you only want to do the evaluations associated with Divide
and Subtract
. If instead you want every subexpression with these head to evaluate completely, use the following trick:
Hold[
Plus[
3 + Times[7, Subtract[9, Plus[4, 5]],
Divide[29, Subtract[4, 3]]]]
] /. {
expr : (_Divide | _Subtract) :> With[{x = expr}, x /; True]
}
See also the following answer:
Replacement inside held expression