MathGroup Archive 2007

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

Search the Archive

Re: Mapping a pure function with 2 conponents.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77385] Re: Mapping a pure function with 2 conponents.
  • From: "Dr. Wolfgang Hintze" <weh at snafu.de>
  • Date: Thu, 7 Jun 2007 04:06:10 -0400 (EDT)
  • Organization: privat
  • References: <f43i20$3hl$1@smc.vnet.net>
  • Reply-to: "Dr. Wolfgang Hintze" <weh at snafu.de>

<phoenix7744 at gmail.com> schrieb im Newsbeitrag 
news:f43i20$3hl$1 at smc.vnet.net...
> I'm trying to get the following code to work:
>
> Map[#1^2 + #2 &, {{0, 1}, {2, 3}, {4, 5}}]
>
> The goal is to use a pure function in order to achieve the result:
> {1, 7, 21}
>
> The issue is that the evaluation involves   #1^2 + #2 & [{0,1}]
> instead of #1^2 + #2 & [0,1]
>
> Thanks, in advance.
>
>
I found two possible solutions

a) using two slots (#1, #2)
#1^2 + #2 & @@@ {{0, 1}, {2, 3}, {4, 5}}
{1, 7, 21}

Timing[Do[#1^2 + #2 & @@@ {{0, 1}, {2, 3}, {4, 5}}, {10^6}]]
{7.094 Second, Null}

Remark: the same construct in input format looks like this
Apply[#1^2 + #2 & , {{0, 1}, {2, 3}, {4, 5}}, {1}]
 {1, 7, 21}

b) using one slot(#) and its components

(#1[[1]]^2 + #1[[2]] & ) /@ {{0, 1}, {2, 3}, {4, 5}}
{1, 7, 21}


Timing[Do[(#1[[1]]^2 + #1[[2]] &) /@ {{0, 1}, {2, 3}, {4, 5}}, {10^6}]]
{9.891 Second, Null}

Hence the two slot version is slightly faster.

Regards,
Wolfgang 



  • Prev by Date: Re: apparently I don't know how to use Map (or Apply or MapThread)
  • Next by Date: Re: apparently I don't know how to use Map (or Apply or MapThread)
  • Previous by thread: Re: Re: Mapping a pure function with 2 conponents.
  • Next by thread: DIfference of error functions with complex arguments