Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: slot argument weirdness

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83947] Re: slot argument weirdness
  • From: Yaroslav Bulatov <yaroslavvb at gmail.com>
  • Date: Wed, 5 Dec 2007 07:11:44 -0500 (EST)
  • References: <fj37b2$hok$1@smc.vnet.net>

On Dec 4, 1:40 am, Jerry <JLK2... at yahoo.com> wrote:
> I have to produce some bulky matrices which are described by
> two parameters and it seems the easiest way to produce them
> is as follows (I took out all the complexity and just left
> in the slots to illustrate my problem). v is the parameter
> array.
>
> v = {2, 5};
> myMatrix=Apply[{{#1, #2}, {#2, #1}} &, v]
>
> giving     {{2, 5}, {5, 2}}    and all is well.
>
> But since the actual form in the first argument in Apply is
> really a large messy thing, I thought I'd produce it just
> once in the notebook and represent it with:
>
> m = {{#1, #2}, {#2, #1}};
>
> But geez, this doesn't work at all:
>
> MyMatrix2= Apply[m &, v]
>
> gives      {{#1, #2}, {#2, #1}}
>
> I've tried a lot of things to make this work but have failed
> completely. If someone can tell me that there is absolutely
> no representation of the slot configuration that will do
> what I want, then I can quit trying. Or is there? Thanks for
> any info.

The problem is that Function is HoldAll, so that the body of the
function (m) doesn't get substituted until after Apply is evaluated
and Function head is stripped.
So you should tell it explicitly to evaluate the Function body --
  MyMatrix2 = Apply[(m // Evaluate) &, v]

You can use Chris Chiasson's trace routine to see what goes wrong in
things like
recordSteps[MyMatrix2 = Apply[m &, v]]
then look in C:\msgStream.m


SetAttributes[recordSteps, HoldAll];
recordSteps[expr_] :=
 Block[{$Output = List@OpenWrite["C:\\msgStream.m"]},
  TracePrint[Unevaluated[expr], _?(FreeQ[#, Off] &),
   TraceInternal -> True];
  Close /@ $Output;
  Thread[Union@
    Cases[ReadList["C:\\msgStream.m", HoldComplete[Expression]],
     symb_Symbol /;
       AtomQ@Unevaluated@symb &&
        Context@Unevaluated@symb === "System`" :>
      HoldComplete@symb, {0, Infinity}, Heads -> True], HoldComplete]
  ]


  • Prev by Date: Re: Partition a list into sublists of unequal size
  • Next by Date: Re: Controlling evaluation
  • Previous by thread: Re: slot argument weirdness
  • Next by thread: Re: slot argument weirdness