Re: Re: Split in Mathematica 2.2/ $MaxRecursion crash
- To: mathgroup at smc.vnet.net
- Subject: [mg13032] Re: [mg12979] Re: Split in Mathematica 2.2/ $MaxRecursion crash
- From: Daniel Lichtblau <danl>
- Date: Tue, 30 Jun 1998 00:26:29 -0400
- References: <6msqgp$i7j$3@dragonfly.wolfram.com> <199806280652.CAA24350@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
Matthias Weber wrote:
>
> Hello
>
> I can confirm the slow behaviour of Split on a PowerMac 6100/66 (which
> has a 601 PPC, vm turned on) reported recently here, and the correct
> fast behaviour on an Irix.
>
> One of the proposed
> user-implemented alternatives however is even crashing my Mac:
>
> split2[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}]];
> (Head at li)@@((Take[li,#]&)/@(
>
> Transpose[{Flatten[{1,#+1}],Flatten[{#,liLength}]}]&)@index)];
>
> This being highly recursive, to execute (say)
>
> w = Table[Random[Integer], {10000}]; split2[w];//Timing
>
> I need to increase
>
> $RecursionLimit=10000;
>
> This works on our Irix but crashes my poor little Mac (Bus error...,
> restart required) after one error message $RecursionLimit::"reclim":
> with a lower $RecursionLimit, I either get the usual bunch of error
> messages or just one of them, and the Mac crashes. Enjoy.
>
> Matthias Weber
In these circumstances it is useful to rewrite your code so that
$RecursionLimit is not an issue. This is because otherwise you run the
risk of segmentation fault by blowing up the recursion stack.
After some experimentation I used the code below. I changed the For loop
to a Do to get a slight speed improvement. Note that I do NOT change
$RecursionLimit to a non-default setting.
split2[li_, fQ_:SameQ] := Module[{liLength, index, i},
liLength = Length[li];
index = {};
Do [If[!fQ @@ li[[{i,i+1}]], index = {index,i}], {i,liLength-1}];
index = Flatten[index];
index = (Transpose[{Flatten[{1, index + 1}],
Flatten[{index, liLength}]}] & )[index];
Head[li] @@ (Take[li, #1] & ) /@ index
]
Timing[s2 = split2[w];]
Unfortunatwly there are still recursion pitfalls lurking. For example,
try
w = Table[Random[Integer,10], {100000}];
which will give rise to a deeply nested index set in the course of
running split2. This will seg fault in versions 3.0.x and prior of
Mathematica due to a recursive implementation of Flatten. We have fixed
this problem in our development version.
Daniel Lichtblau
Wolfram Research
- References:
- Re: Split in Mathematica 2.2/ $MaxRecursion crash
- From: weber@math.uni-bonn.de (Matthias Weber)
- Re: Split in Mathematica 2.2/ $MaxRecursion crash