Re: How to evaluate parts of an expression, but not other parts?

• To: mathgroup at smc.vnet.net
• Subject: [mg122697] Re: How to evaluate parts of an expression, but not other parts?
• From: "David Park" <djmpark at comcast.net>
• Date: Sun, 6 Nov 2011 05:56:49 -0500 (EST)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• References: <32289202.13251.1320484280567.JavaMail.root@m06>

```I'm sure that you will obtain some answers to do this with plain
Mathematica, but the Presentations package does have routines that allow
selective manipulation of expressions.

Along with HoldForm your can use EvaluateAt or EvaluateAtPattern to do
selective evaluations of held expressions. You can also use
CreateSubexpression, OperateSubexlression and ReleaseSubexpressions to tag
and group things together to prevent Mathematica from mixing there elements
with other elements outside the subexpressions. Tagged Subexpressions also
show the tag in a tooltip when the mouse hovers over the Subexpression. We
also have MapLevelParts that allows an operation to be performed on selected
level parts in an expression (usually a sum, product or list).

So, as a simple example we could do:

<<Presentations`

a = 1; b = 2; c = 3; d = 4;
HoldForm[a + b] + HoldForm[c + d]
% // EvaluateAt[{1, 1}]
% // EvaluateAt[{2, 1}]
% // ReleaseHold

(a+b)+(c+d)

3+(c+d)

3+7

10

Using tagged Subexpressions we could do the following. We can also specify
that a subexpression should always show parentheses.

a = 1; b = 2; c = 3; d = 4;
CreateSubexpression[HoldForm[a + b], True, tag1] +
CreateSubexpression[HoldForm[c + d], True, tag2]
% // OperateSubexpression[ReleaseHold, tag1]
% // OperateSubexpression[ReleaseHold, tag2]
% // ReleaseSubexpressions[All]

(a+b)+(c+d)

(3)+(c+d)

(3)+(7)

10

If we want to show the individual values before they are combined in a
Subexpression we could use nested Subexpressions and the following more
complicated construction.

Clear[a, b, c, d]
step1 = Plus @@
CreateSubexpression[#1, #2] &, {HoldForm /@ {a, b, c, d}, {taga,
tagb, tagc, tagd}}]
a = 1; b = 2; c = 3; d = 4;
step2 = step1 //
MapLevelParts[CreateSubexpression[#, tagcd] &, {{3, 4}}];
step3 = step2 //
MapLevelParts[CreateSubexpression[#, tagab] &, {{1, 2}}]
step4 = Fold[OperateSubexpression[ReleaseHold, #2][#1] &,
step3, {taga, tagb, tagc, tagd}]
step5 = Fold[ReleaseSubexpressions[#2][#1] &,
step4, {taga, tagb, tagc, tagd}]
FixedPoint[ReleaseSubexpressions[All], step5]

(a)+(b)+(c)+(d)

((a)+(b))+((c)+(d))

((1)+(2))+((3)+(4))

(3)+(7)

10

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: Julian Francis [mailto:julian.w.francis at gmail.com]

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.

```

• Prev by Date: Re: Impossible to move objects in dynamic graphics
• Next by Date: Re: How to evaluate parts of an expression, but not other parts?
• Previous by thread: Re: How to evaluate parts of an expression, but not other parts?
• Next by thread: Re: How to evaluate parts of an expression, but not other parts?