Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2377

Re: Replace unevaluated symbols

From Albert Retey <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Replace unevaluated symbols
Date 2011-05-14 07:15 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iqla68$a0s$1@smc.vnet.net> (permalink)
References <iqj160$rh8$1@smc.vnet.net>

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web