RE: Replacement question

• To: mathgroup at smc.vnet.net
• Subject: [mg35179] RE: [mg35114] Replacement question
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Fri, 28 Jun 2002 02:31:24 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```> -----Original Message-----
> From: ginak [mailto:gina02122000 at yahoo.com]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, June 25, 2002 9:41 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg35179] [mg35114] Replacement question
>
>
> Suppose I have something like
>
>  In [100] := h[3]/f[2.33342]
>
>  Out[100] := 24.12711
>
> and now I want to evaluate h[5]/f[2.33342], i.e. the same as in
> In[100] but replaceing h[3] with h[5].  This won't work
>
>  In [101] := In[100] /. h[3]->h[5]
>
>  Out[101] := 24.12711
>
> because In[100] is fully evaluated to 24.12711 before the rule is
> applied.  (Generally, the expressions I'm interested in are more of a
> pain to type than h[5]/f[2.33342]).  How do I tell
> Mathematica to evaluate
> In[100] only enough to apply the given substition rule, apply the
> substitution rule, and only then proceed with the evaluation?
>
> In fact, I don't even know how to do a replacement like
>
>  In [102] := h[3]/f[2.33342] /. h[3]->h[5]
>
> for the same reason: the LHS is evaluated before the rule can be
> applied.  (Of course, in this case this replacment task is pointless,
> since it is so easy to type out the desired expression, but there are
> situations in which one can obtain a complicated expression by cutting
> and pasting, and wants to apply a substitution rule to the complicated
> expression before Mathematica evaluates it.)
>
> Thanks!
>
> G.
>
>

Gina,

perhaps this comes close to what you want to attain:

In[1]:= \$Line = 97

In[98]:= f[x_] = x + x^2/3

In[99]:= h[x_] = 24.12711*x*f[2.33342]/3

In[100]:= h[3]/f[2.33342]
Out[100]= 24.1271

Now you want the unevaluated input to In[100] to process further. A way to
do this is to use Block with undefining all current Global symbols (This
might not be enough in the general case, but you may modify the trick), and
Hold the result for further processing:

In[101]:=
Block[Evaluate[Symbol /@ Names["Global`*"]], Hold[Evaluate[In[100]]]] /.
HoldPattern[h[3]] :> h[5] // ReleaseHold

Out[101]= 40.2118

I used RuleDelayed, such that you may continue with that trick.

In[102]:=
Block[Evaluate[Symbol /@ Names["Global`*"]], Hold[Evaluate[In[101]]]]

Out[102]= Hold[h[5]/f[2.33342`]]

As I recognized right now, this is essentially Andrzej's trick.

Also, I must confess, that the method posted by Omega Consulting ...

In[114]:=
Hold[In[100]] /. DownValues[In] /.
HoldPattern[h[3]] :> h[5] // ReleaseHold

Out[114]= 40.2118

...is superior, here an alternative way to get at the unevaluated In[100]

In[122]:=
Cases[DownValues[In],
(Verbatim[HoldPattern[In[100]]] :> s_) :> Hold[s]] // First

Out[122]= Hold[h[3]/f[2.33342]]

This (as well as Omega's method) has the advantage that In[100] is kept
completely unevaluated (such you also may replace numerical factors etc.)

--
Hartmut

```

• Prev by Date: Re: Pretty output
• Next by Date: RE: Assigning to a sublist
• Previous by thread: Re: Replacement question
• Next by thread: Re: Replacement question