MathGroup Archive 2006

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

Search the Archive

Re: Can anybody help?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63652] Re: Can anybody help?
  • From: Paul <gleam at flashmail.com>
  • Date: Mon, 9 Jan 2006 04:48:28 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

> It boggles my mind that some people call that code
> elegant.
> 
> Short, yes, elegant, no.  It is far from intuitive to
> know what
> Position[v1, #1]&/@v3 does.  Anyone care to enlighten
> me?


You should avail yourself of the help files, which generally do a very good job of explaining Mathematica behavior.  


Position[expr, pattern] gives a list of the positions at which objects matching pattern appear in expr.

Map[f, expr] or f /@ expr applies f to each element on the first level in expr.

Function[body] or body& is a pure function. The formal parameters are # (or #1), #2, etc.


Therefore Position[v1, #1]& /@ v3 gives the positions of each element of v3 within v1.


As for my own solution which in reference to I had the audacity to use the word elegant:

v3 /. Dispatch@Thread[v1 -> v2]


expr /. rules applies a rule or list of rules in an attempt to transform each subpart of an expression expr.

lhs -> rhs represents a rule that transforms lhs to rhs.

Thread[f[args]] "threads" f over any lists that appear in args.

Dispatch[{a->a, a->a, â?¦ }] generates an optimized dispatch table representation of a list of rules. The object produced by Dispatch can be used to give the rules in expr /. rules.


Therefore at its core, it reads: "replace v1(i) with v2(i) for all v3."  Thread is needed to make Rule "go inside" the lists to operate at the element level, and Dispatch is used to speed up application.


Paul


  • Prev by Date: Re: NDSolve::ndsz question
  • Next by Date: Re: Puzzle Challenge
  • Previous by thread: Re: Re: Can anybody help?
  • Next by thread: Re: Can anybody help?