Re: Split in Mathematica 2.2
- To: mathgroup at smc.vnet.net
- Subject: [mg12841] Re: Split in Mathematica 2.2
- From: "Carl K.Woll" <carlw at fermi.phys.washington.edu>
- Date: Wed, 17 Jun 1998 00:27:48 -0400
- Organization: Department of Physics
- References: <199806100704.DAA13675@smc.vnet.net.> <6lqqub$o9u@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Andrzej, Here is a faster version of your idea: split[li_,testQ_:SameQ] := Module[{r1,r2}, r1=Flatten[Position[Apply[testQ,Partition[li,2,1],{1}],False]]; r2=Transpose[{Join[{1},r1+1],Join[r1,{Length[li]}]}]; Take[li,#]&/@r2] At least, on my machine, the above version is more than twice as fast as yours. Cheers, Carl Woll Dept of Physics U of Washington Andrzej Kozlowski wrote: > > > >At 12:04 AM -0700 1998.06.10, Andrzej Kozlowski wrote: > >>.. Can anyone suggest a really fast implementation > >>of Split that works in version 2.2? > > > >Here's an emulated Split with speed considerations. Basically, it iterates > >through the list, mark down where the boundaries should be, then map Take > >to the boundary indexes to obtain the new list. > > > >split::"usage"= > > "split simulates the version 3 build-in function Split. Example: > >Split[{1,2,3,4,4,3,2}]"; > > > >split[li_,fQ_:SameQ]:=Module[{liLength,index,i},liLength=Length at li; > >index={};For[i=1,i<liLength,i++, > > If[Not@(fQ@@Part[li,{i,i+1}]),index={index,i}]]; > >(Take[li,#]&)/@(Transpose[{Flatten[{1,#+1}],Flatten[{#,liLength}]}]&)@ > > Flatten at index]; > > > > > >PS for those wondering what happened to my series of Mathematica expositions... I > >got extremely busy with work. I will come back to it sometimes in the > >future. > > > > Xah, xah at best.com > > http://www.best.com/~xah/PageTwo_dir/more.html > > "Perl: all unix's stupidity in one." > > I think I managed to construct an even faster program which avoids the > dreaded For loop: > > split1[l_]:= > Module[{m}, > Map[Take[l,#]&, > Partition[ > Insert[Insert[ > > Flatten[Position[Partition[l,2,1]/.{x_,y_}/;x=!=y->m,m]],1,1], > Length[l],-1],2,1]/.{a_,b_}/;a!=1->{a+1,b}]] > > Here are some test results (Mathematica 3.01, PowerMac G3 266) In[5]:= > w = Table[Random[Integer], {10000}]; In[6]:= > Split[w];//Timing > Out[6]= > {0.25 Second,Null} > In[7]:= > split[w];//Timing > Out[7]= > {1.16667 Second,Null} > In[8]:= > split1[w];//Timing > Out[8]= > {0.766667 Second,Null} > > Split is the built in mathematica 3.0 function, split is Xah's function > and split1 is my new function. I am relieved; I hate the For loop! > > Andrzej > > Dr. Andrzej Kozlowski > Professor of Mathematics > Toyama International University > Toyama, JAPAN > http://sigma.tuins.ac.jp/
- References:
- Split in Mathematica 2.2
- From: Andrzej Kozlowski <andrzej@tuins.ac.jp>
- Split in Mathematica 2.2