MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

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:


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





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]  




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]  






David Park
djmpark at  

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

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.


  • 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?