MathGroup Archive 2009

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

Search the Archive

Re: How to map a list on function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97575] Re: How to map a list on function
  • From: Raffy <raffy at mac.com>
  • Date: Mon, 16 Mar 2009 04:22:19 -0500 (EST)
  • References: <gpg18t$ceh$1@smc.vnet.net> <gpilb2$595$1@smc.vnet.net>

On Mar 15, 3:29 am, Ray Koopman <koop... at sfu.ca> wrote:
> On Mar 14, 3:35 am, buts <mange... at yahoo.com> wrote:
>
>
>
>
>
> > Hello,
>
> > Could anyone explain me how to do the following:
> > I have a long list of integer numbers in groups of four:
>
> > list= {{10,3,5,7},{4,6,8,9},{0,8,3,6}, ...... }
>
> > or its Flatten version.
>
> > How to write a fast function g[list] which does this:
>
> > g[list] =x^10*y^3*z^5*u^7 + x^4*y^6*z^8*u^9 + y^8*z^3*u^6+ ...
>
> > It should be done many times (say, 10^4-6) on lists with
> > length > 1000, so taking parts is not a good idea.
> > How to use Map or similar ?
> > Thanks.
>
> Here are five ways to do it. For lists of 1000 and 2000 4-tuples,
> g4 and g5 are best and about equally fast, but for longer lists
> g5 is fastest.
>
> list = {{10,3,5,7},{4,6,8,9},{0,8,3,6}};
> g1[list_] := Tr[Times@@({x,y,z,u}^#)& /@ list];
> g2[list_] := Tr[Inner[Power,{x,y,z,u},#,Times]& /@ list];
> g3[list_] := Tr@Inner[#2^#1&,list,{x,y,z,u},Times];
> g4[list_] := Tr[Times@@({x,y,z,u}^Transpose@list)];
> g5[list_] := Tr@Inner[Power,{x,y,z,u},Transpose@list,Times];
> SameQ @@ (#@list& /@ {g1,g2,g3,g4,g5})
> g5[list]//InputForm
>
> True
> u^6*y^8*z^3 + u^7*x^10*y^3*z^5 + u^9*x^4*y^6*z^8
>
> try[length_,reps_] := Block[{list = Table[Random[Integer,9],
> {length},{4}]}, {length, First/@{Timing@Do[g1[list],{reps}],
> Timing@Do[g2[list],{reps}], Timing@Do[g3[list],{reps}],
> Timing@Do[g4[list],{reps}], Timing@Do[g5[list],{reps}]}/.Second->1}]
>
> try[1000#,64/#]&/@{1,2,4,8}
>
> {{1000, {1.93, 1.56, 1.50, 1.21, 1.23}},
>  {2000, {2.10, 1.75, 1.73, 1.46, 1.47}},
>  {4000, {2.45, 2.13, 2.16, 1.95, 1.88}},
>  {8000, {2.72, 2.44, 2.42, 2.36, 2.17}}}

Wow, the speed gain of Tr over Total or Plus@@ is interesting.  Thanks
for that tidbit!


  • Prev by Date: identical rows in tables
  • Next by Date: Re: Two Notebooks Open at the Same Time
  • Previous by thread: Re: How to map a list on function
  • Next by thread: Re: How to map a list on function