MathGroup Archive 2011

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

Search the Archive

Re: Replace unevaluated symbols

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118871] Re: Replace unevaluated symbols
  • From: David Reiss <dbreiss at gmail.com>
  • Date: Sat, 14 May 2011 03:05:28 -0400 (EDT)
  • References: <iqj160$rh8$1@smc.vnet.net>

The reason why this happens can be gleaned by Tracing an example:

z = 0;

THen

TracePrint[
 Hold[z] /. z -> 7
 ]

gives

 Hold[z]/. z->7

  ReplaceAll

  Hold[z]

   Hold

  z->7

   Rule

   z

   0

   7

  0->7

  0->7

   Rule

   0

   7

 Hold[z]/. 0->7

 Hold[z]




>From this you can see that the replacement rule z->7 is evaluated to 0-
>7 and hence the result is correct.  This is all a consequence of
Mathematica's standard evaluation process.

So indeed an approach using Block or Module for example allows you to
by pass this:

Block[{z},
 TracePrint[
  Hold[z] /. z -> 7
  ]

 ]

gives


 Hold[z]/. z->7

  ReplaceAll

  Hold[z]

   Hold

  z->7

   Rule

   z

   7

  z->7

   Rule

   z

   7

 Hold[z]/. z->7

 Hold[7]

  Hold


In essence, if you wish to bypass  Mathematica's standard evaluation
order, you will need to localize the variable as you suggest.

Best,
David


On May 13, 6:29 am, Antonio De Ju=E1rez <adejua... at gmail.com> wrote:
> Given an expression like
>
> {x+y,2*x-y}
>
> I would like to replace x and y with some values even if these
> variables have some value assigned. For example, the code
>
> expr = Hold[x+y,2x-y]
> expr/.x->3
>
> produces
>
> Hold[3 + y, 2*3 - y]
>
> but the code
>
> x=2.5;
> expr = Hold[x+y,2x-y]
> expr/.x->3
>
> produces the wrong result
>
> Hold[x + y, 2 x - y]
>
> I know this can be done using Block; however, I don't know the
> variables to be replaced beforehand. I would like to have a function
> with attribute HoldAll like
>
> fun[expr,var1,value1,var2,value2,...]
>
> that replaces the variables var1, var2, ... by the corresponding
> values value1, value2,..., even if any of the variables var1, var2,...
> has a preassigned value.



  • Prev by Date: Re: color-coding two sets of plotted curves?
  • Next by Date: Re: Equation numbering
  • Previous by thread: Re: Replace unevaluated symbols
  • Next by thread: Re: Replace unevaluated symbols