Prev: PDE coupling boundary problem
Next: matrix equation
From: Fred Klingener on 7 May 2010 06:24 Here, the second execution of f fails because variable 'a' evaluates to Pi and can't be reassigned: ClearAll[f, a, b, c] f[x_, y_, z_] := x = Pi; 3 y + 2 z f[a, b, c] f[a, b, c] I have come to understand that the canonical approach to control this is to assign the HoldFirst attribute to f: ClearAll[f, a, b, c] SetAttributes[f, HoldFirst] f[x_, y_, z_] := x = Pi; 3 y + 2 z f[a, b, c] f[a, b, c] a and, indeed, this works as intended. Plan B, using an explicit Unevaluated also works: ClearAll[f, a, b, c] f[x_, y_, z_] := x = Pi; 3 y + 2 z f[Unevaluated[a], b, c] f[Unevaluated[a], b, c] a An alternate function formulation fails, evidently in the same way as the first example: ClearAll[f, a, b, c] SetAttributes[f, HoldFirst] f = (#1 = Pi; 3 #2 + 2 #3) & f[a, b, c] f[a, b, c] a while ClearAll[f, a, b, c] f = (#1 = Pi; 3 #2 + 2 #3) & f[Unevaluated[a], b, c] f[Unevaluated[a], b, c] a does. What am I missing? TIA, Fred Klingener
From: Raffy on 8 May 2010 07:07 On May 7, 3:24 am, Fred Klingener <gigabitbuc...(a)BrockEng.com> wrote: > Here, the second execution of f fails because variable 'a' evaluates > to Pi and can't be reassigned: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > > I have come to understand that the canonical approach to control this > is to assign the HoldFirst attribute to f: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > a > > and, indeed, this works as intended. > > Plan B, using an explicit Unevaluated also works: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > An alternate function formulation fails, evidently in the same way as > the first example: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[a, b, c] > f[a, b, c] > a > > while > > ClearAll[f, a, b, c] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > does. > > What am I missing? > > TIA, > Fred Klingener Function[{a, b, c}, a = Pi; 3 b + 2 c, HoldFirst]
From: Albert Retey on 8 May 2010 07:09 Am 07.05.2010 12:24, schrieb Fred Klingener: > Here, the second execution of f fails because variable 'a' evaluates > to Pi and can't be reassigned: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > > I have come to understand that the canonical approach to control this > is to assign the HoldFirst attribute to f: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[a, b, c] > f[a, b, c] > a > > and, indeed, this works as intended. > > Plan B, using an explicit Unevaluated also works: > > ClearAll[f, a, b, c] > f[x_, y_, z_] := x = Pi; 3 y + 2 z > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > An alternate function formulation fails, evidently in the same way as > the first example: > > ClearAll[f, a, b, c] > SetAttributes[f, HoldFirst] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[a, b, c] > f[a, b, c] > a You probably want this: ClearAll[f, a, b, c] f = Function[Null,#1 = Pi; 3 #2 + 2 #3,HoldFirst] > > ClearAll[f, a, b, c] > f = (#1 = Pi; 3 #2 + 2 #3) & > f[Unevaluated[a], b, c] > f[Unevaluated[a], b, c] > a > > does. > > What am I missing? Attributes of Symbols affect only the downvalues you have defined for them, in the case that did not work, you did assign an ownvalue, which happens to be a function. Thus the HoldFirt did not have an effect, since the Function still did evaluate its arguments. You can set attributes to Functions as shown above... hth, albert
|
Pages: 1 Prev: PDE coupling boundary problem Next: matrix equation |