Group Abstract Group Abstract

Message Boards Message Boards

0
|
5.8K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to stop evaluation of an expression after pattern replacement?

Posted 10 years ago

Hi,

I want to know how to stop the evaluation of expressions after substituting in a value using pattern replacement. I have tried combinations of Unevaluated, Hold, etc. however they seem only to be effective at halting the evaluation of the pattern rule and/or replacement before the substitution happens (that is, in the testing I have tried). A first simple example:

In[26]:= exp1 = a^2
Out[26]= a^2

In[27]:= exp1 /. a -> 2
Out[27]= 4

Semi-solution: Ugly because the Hold remains part of the output. I fear any attempt to remove the Hold (using pattern replacement) will cause the expression to evaluate. Any suggestions on solving this sort of problem would be great.

In[34]:= exp1 /. a -> Hold[2]
Out[34]= Hold[2]^2

Second example (mainly included because of my lack of experience with how matching needs to be done on the alternate forms of representing the Derivative of a function; i.e. Derivative[1][y][x] vs. D[y[x],x] vs y'[x])

In[41]:= D[y[x], x] /. y[x] -> x^2
Out[41]= Derivative[1][y][x]

In[47]:= FullForm[D[y[x], x]]
Out[47]//FullForm= Derivative[1][y][x]

In[36]:= ReleaseHold[D[Hold[y[x]], x] /. y[x] -> Hold[x^2]]
Out[36]=  PartialD[x, Hold[x^2]]     (Note: Not correct formatting; code below will type format correctly in Mathematica)

\!\(
\*SubscriptBox[\(\[PartialD]\), \(x\)]\(Hold[
\*SuperscriptBox[\(x\), \(2\)]]\)\)

This example is motivated by my wanting to create a tool to help go through the steps of checking whether solutions to algebraic and differential equations are correct. I am aware of using Trace to go through the evaluation of the expressions; which incidently keeps the 2^2 unevaluated without a Hold[2]^2 in the expression tree for the code below (Unfortunately, Out[48] needs to be copy and pasted into Mathematica to get the correct typesetting.)

In[48]:= Trace[exp1 /. a -> 2]

Out[48]= {{\!(* TagBox["exp1", HoldForm]), \!(* TagBox[ SuperscriptBox["a", "2"], HoldForm])}, {\!(* TagBox[ RowBox[{"a", "->", "2"}], HoldForm]), \!(* TagBox[ RowBox[{"a", "->", "2"}], HoldForm])}, \!(* TagBox[ RowBox[{ SuperscriptBox["a", "2"], "/.", " ", RowBox[{"a", "->", "2"}]}], HoldForm]), \!(* TagBox[ SuperscriptBox["2", "2"], HoldForm]), \!(* TagBox["4", HoldForm])}

I however at some point I would like to increase the granularity of the number of steps to show during the evaluation process, because showing all levels of the trace for complicated expressions can get unwieldy (for instance if I included every level with Plus and Times). If anyone has any comments or suggestions (or know if this is already done in some open source code/example), it would be greatly appreciated.

Thanks in advance, Joe

POSTED BY: joseph pantina
2 Replies
Posted 10 years ago

Thanks Eric,

I had noticed that would be a possible solution when I saw the full form of the trace cause it wasnt type setting correctly in the post, but didn't try it until a few minutes after I posted.

Now I am just wondering on the proper pattern matching for things such as the derivative of a function. Whether or not they should be expanded to full-form or if there is a pattern to match the the "short" form such as y'[x]?

Joe

POSTED BY: joseph pantina

Hi Joseph,

Try using HoldForm instead of Hold. "HoldForm[expr] prints as the expression expr, with expr maintained in an unevaluated form."

In[208]:= y=x^2
Out[208]= x^2
In[216]:= y/.x->HoldForm[2]
Out[216]= 2^2

Eric

POSTED BY: Eric Johnstone
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard