Re: Replace unevaluated symbols
- To: mathgroup at smc.vnet.net
- Subject: [mg118897] Re: Replace unevaluated symbols
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Sat, 14 May 2011 03:10:12 -0400 (EDT)
- References: <iqj160$rh8$1@smc.vnet.net>
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