MathGroup Archive 2009

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

Search the Archive

Re: Replacement rules that contain function arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105343] Re: [mg105317] Replacement rules that contain function arguments
  • From: Carl Woll <carlw at wolfram.com>
  • Date: Sun, 29 Nov 2009 05:08:24 -0500 (EST)
  • References: <200911280604.BAA01286@smc.vnet.net>

Leo Alekseyev wrote:
> Dear Mathgroup,
>
> I am trying to understand what happens when a pattern from function
> definition appears in the list of replacement rules.  In particular,
> consider this:
>
> (* Example (A): *)
> Clear[foo,kij,k];
> foo[k_] := kij /. {kij :> Table[k[[i]] k[[j]], {i, 1, 3}, {j, 1, 3}]}
> foo[{1, 1, 1}]
> (* gives {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}} as expected *)
>
> (* But now, let's put replacement rules into a variable: *)
> (* Example (B): *)
> rrules := {kij :> Table[k[[i]] k[[j]], {i, 1, 3}, {j, 1, 3}]}
> foo[k_] := kij /. rrules
> foo[{1, 1, 1}]
> (* rrules gets evaluated first, hence this fails *)
>
> My question is -- is there a good way to control evaluation such that
> even though rules are defined in a variable, evaluation proceeds as in
> Example (A)?
>
> Below are a couple of things I found to work, but they are somewhat
> syntactically cumbersome.
>
> (* Example (C): works, but have to manually turn off messages *)
> foo[k_] := Evaluate[Off[Part::"partd"]; kij /. rrules]; On[
>  Part::"partd"]
> foo[{1, 1, 1}]
>
> (* Example (D): works, but have to rename function argument and wrap
> in a block *)
> foo[kk_] := Block[{k = kk}, kij /. rrules];
> foo[{1, 1, 1}]
>
> Thanks,
> --Leo
>
>   

Why not use:

rrules[k_] := {kij :> Table[k[[i]] k[[j]], {i, 1, 3}, {j, 1, 3}]}
foo[k_] := kij /. rrules[k]

Carl Woll
Wolfram Research


  • Prev by Date: Using GraphPlot to draw an empty graph
  • Next by Date: Re: Bug ??????
  • Previous by thread: Replacement rules that contain function arguments (evaluation order)
  • Next by thread: Re: Replacement rules that contain function arguments