Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #6230

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

From "David Park" <djmpark@comcast.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to evaluate parts of an expression, but not other parts?
Date 2011-11-06 11:02 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j95pg2$m87$1@smc.vnet.net> (permalink)
References <32289202.13251.1320484280567.JavaMail.root@m06>

Show all headers | View raw


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 @@ 
  MapThread[
   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@comcast.net
http://home.comcast.net/~djmpark/  


From: Julian Francis [mailto:julian.w.francis@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.

Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: How to evaluate parts of an expression, but not other parts? "David Park" <djmpark@comcast.net> - 2011-11-06 11:02 +0000
  Re: How to evaluate parts of an expression, but not other parts? "andre.robin3" <andre.robin3@wanadoo.fr> - 2011-11-07 10:51 +0000
    Re: How to evaluate parts of an expression, but not other parts? "andre.robin3" <andre.robin3@wanadoo.fr> - 2011-11-08 12:20 +0000
  Re: How to evaluate parts of an expression, but not other parts? Andrzej Kozlowski <akoz@mimuw.edu.pl> - 2011-11-08 12:15 +0000

csiph-web