Split in Mathematica 2.2
- To: mathgroup at smc.vnet.net
- Subject: [mg12777] Split in Mathematica 2.2
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Wed, 10 Jun 1998 03:04:37 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Although I have a personal copy of Mathematica 3.0, the university where I teach still has not upgraded from version 2.2. The result is that I often find in my classes that certain useful functions are not available. One of the most useful of them is Split. I therefore decided to program it myself and the first thing that came to my mind was to use Fold. I created the following code. presplit[{x___, l_List}, a_] := If[MemberQ[l, a], {x, Append[l, a]}, {x, l, {a}}] presplit[{}, d_] := {{d}} split[l_List] := Fold[presplit, {}, l] This works fine: In[103]:= split[{1, 1, 1, 5, 7, 11, 11, 12, 14, 14}] Out[103]= {{1,1,1},{5},{7},{11,11},{12},{14,14}} Unfortunatelly it is very slow (all tests in Mathematica 3.0 on PowerMac G3 266): In[104]:= w = Table[Random[Integer], {10000}]; In[105]:= split[w];//Timing Out[105]= {76.05 Second,Null} By comparison the built in function Split gives In[106]:= Split[w];//Timing Out[106]= {0.483333 Second,Null} Having read David Wagner's "Power Programming with Mathematica" I thought I knew the culprit. I decided to get rid of Append and try using linked lists instead. So I wrote: In[107]:= presplit1[{x___, l_List}, a_] := If[Not[FreeQ[Last[l], a]], {x, {l, {a}}}, {x, l, {a}}] presplit1[{}, d_] := {{d}} split1[l_List] := Map[#/.List -> Sequence&,Fold[presplit1, {}, l], {2}] I tried my new split1: In[108]:= split1[w];//Timing Out[108]= {68.9167 Second,Null} An improvement, but very slight. What is making this so slow? Can anyone suggest a really fast implementation of Split that works in version 2.2? Andrzej Kozlowski
- Follow-Ups:
- Re: Split in Mathematica 2.2
- From: Andrzej Kozlowski <andrzej@tuins.ac.jp>
- Re: Split in Mathematica 2.2