[Date Index]
[Thread Index]
[Author Index]
Re: Replacement question
*To*: mathgroup at smc.vnet.net
*Subject*: [mg35184] Re: [mg35114] Replacement question
*From*: Andrzej Kozlowski <andrzej at lineone.net>
*Date*: Fri, 28 Jun 2002 02:32:27 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
I am not sure it is right to say the Omega Consulting method is superior
for all such situations, because the two methods are not equivalent and
in certain instances will give different answers. Holding everything
unevaluated till the end is not always the best choice. Here is a very
quickly produced and rather artificial example but I am sure one can
find more natural ones:
In[1]:=
f[x_] := x^3; g[x_] := x^4;
In[2]:=
ExpandAll[f[x]/(f[x] + g[x])^3]
Out[2]=
x^3/(x^9 + 3*x^10 + 3*x^11 + x^12)
In[3]:=
Block[{f, g}, In[2] /. x -> (x + 1)^3]
Out[3]=
(1 + x)^9/((1 + x)^27 + 3*(1 + x)^30 + 3*(1 + x)^33 +
(1 + x)^36)
In[4]:=
ReleaseHold[Hold[In[2]] /. DownValues[In] /. x -> (x + 1)^3]
(Very long output omitted)
On Thursday, June 27, 2002, at 04:07 PM, Wolf, Hartmut wrote:
>
>
> > -----Original Message-----
> > From: ginak [mailto:gina02122000 at yahoo.com]
To: mathgroup at smc.vnet.net
> > Sent: Tuesday, June 25, 2002 9:41 AM
> > Subject: [mg35184] [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
>
>
Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/
Prev by Date:
**RE: Assigning to a sublist**
Next by Date:
**sparse matrix**
Previous by thread:
**RE: Replacement question**
Next by thread:
**Re: Re: Replacement question**
| |