Re: How to evaluate parts of an expression, but not other parts?
- To: mathgroup at smc.vnet.net
- Subject: [mg122694] Re: How to evaluate parts of an expression, but not other parts?
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 6 Nov 2011 05:56:17 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201111050945.EAA10308@smc.vnet.net> <144C59E7-489B-4015-B060-2985B69D3E08@mimuw.edu.pl>
On 5 Nov 2011, at 13:22, Andrzej Kozlowski wrote: > > On 5 Nov 2011, at 10:45, Julian Francis wrote: > >> Dear all, >> >> I'd like to use the TreePlot function to visualise the expression of a >> dynamic programming problem I am working on. >> >> If I have something like: ( (a+b) + (c+d ) >> >> Mathematica helpfully simplifies this to: a + b + c + d >> >> But I'd prefer it to be in the original form. >> >> I can't write Hold[ ( (a+b) + (c+d) )] because I do want a,b,c & d to >> be evaluated. >> >> I want to write something like: >> Hold[ ( (Evaluate[a]+Evaluate[b]) + (Evaluate[c]+Evaluate[d]) ) ] >> >> But this just leaves the Evaluate expressions unevaluated. >> >> Any help greatly appreciated. >> >> Thanks, >> Julian. >> > > > Well, there may be a simpler way, but here is one that comes to my mind: > > a = x; b = y; c = z; d = w; > > HoldForm @@ Apply[Hold, Hold[(a + b) + (c + d)], {2}] /. p_Hold :> With[{w = Plus @@ p}, w /; True] > > HoldForm[(x + y) + (w + z)] > > Andrzej Kozlowski A slightly simpler way (using the same idea) is: HoldForm[Hold[a + b] + Hold[c + d]] /. p_Hold :> With[{w = Plus @@ p}, w /; True] HoldForm[(x + y) + (w + z)]
- References:
- How to evaluate parts of an expression, but not other parts?
- From: Julian Francis <julian.w.francis@gmail.com>
- How to evaluate parts of an expression, but not other parts?