Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2343 > unrolled thread
| Started by | Antonio De Juárez <adejuarez@gmail.com> |
|---|---|
| First post | 2011-05-13 10:29 +0000 |
| Last post | 2011-05-15 11:05 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Replace unevaluated symbols Antonio De Juárez <adejuarez@gmail.com> - 2011-05-13 10:29 +0000
Re: Replace unevaluated symbols David Reiss <dbreiss@gmail.com> - 2011-05-14 07:10 +0000
Re: Replace unevaluated symbols Albert Retey <awnl@gmx-topmail.de> - 2011-05-14 07:15 +0000
Re: Replace unevaluated symbols Antonio De Juárez <adejuarez@gmail.com> - 2011-05-15 11:05 +0000
| From | Antonio De Juárez <adejuarez@gmail.com> |
|---|---|
| Date | 2011-05-13 10:29 +0000 |
| Subject | Replace unevaluated symbols |
| Message-ID | <iqj160$rh8$1@smc.vnet.net> |
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.
[toc] | [next] | [standalone]
| From | David Reiss <dbreiss@gmail.com> |
|---|---|
| Date | 2011-05-14 07:10 +0000 |
| Message-ID | <iql9tl$9t6$1@smc.vnet.net> |
| In reply to | #2343 |
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...@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.
[toc] | [prev] | [next] | [standalone]
| From | Albert Retey <awnl@gmx-topmail.de> |
|---|---|
| Date | 2011-05-14 07:15 +0000 |
| Message-ID | <iqla68$a0s$1@smc.vnet.net> |
| In reply to | #2343 |
Am 13.05.2011 12:29, schrieb Antonio De Ju=E1rez:
> 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.
so what you want to do is to hold the pattern in your replacement rule
-- and what a surprise: there is HoldPattern :-)
expr /. HoldPattern[x] -> 3
hth,
albert
[toc] | [prev] | [next] | [standalone]
| From | Antonio De Juárez <adejuarez@gmail.com> |
|---|---|
| Date | 2011-05-15 11:05 +0000 |
| Message-ID | <iqoc2k$ma4$1@smc.vnet.net> |
| In reply to | #2377 |
Thanks Albert. I think HoldPattern may solve the problem.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web