nchoosek errors because of max_recursion_depth

Jaroslav Hajek highegg at gmail.com
Fri Nov 21 06:28:10 CST 2008


On Fri, Nov 21, 2008 at 1:04 PM, Francesco Potortì <Potorti at isti.cnr.it> wrote:
> If nchoosek is called with a vector argument whose length N is greater
> than max_recursion_depth, it will print N-max_recursion_depth errors.
>
> This is not very smart, as nchoosek knows in advance which recursion
> depth it will need.  This patch corrects this behaviour at a reasonable
> performance penalty (less than 10% for a 40 elements vector, less than
> 3% for a 400 elements vector).  If you think it is the right way to go,
> I will send a changeset.
>

I think a better option is to avoid the recursion entirely, if the
limit can be reached for realistic inputs.


> @@ -75,9 +75,20 @@ function A = nchoosek (v, k)
>   elseif (k == n)
>      A = v(:).';
>   else
> +    if (max_recursion_depth() < n)
> +      oldmax = max_recursion_depth (n);
> +    else
> +      oldmax = false;
> +    endif
> +    unwind_protect
>     m = round (exp (sum (log (k:n-1)) - sum (log (2:n-k))));
>     A = [v(1)*ones(m,1), nchoosek(v(2:n),k-1);
>         nchoosek(v(2:n),k)];
> +    unwind_protect_cleanup
> +      if (oldmax)
> +       max_recursion_depth (oldmax);
> +      endif
> +    end_unwind_protect
>   endif
>
>  endfunction
>
> --
> Francesco Potortì (ricercatore)        Voice: +39 050 315 3058 (op.2111)
> ISTI - Area della ricerca CNR          Fax:   +39 050 315 2040
> via G. Moruzzi 1, I-56124 Pisa         Email: Potorti at isti.cnr.it
> (entrance 20, 1st floor, room C71)     Web:   http://fly.isti.cnr.it/
> _______________________________________________
> Bug-octave mailing list
> Bug-octave at cae.wisc.edu
> https://www-old.cae.wisc.edu/mailman/listinfo/bug-octave
>



-- 
RNDr. Jaroslav Hajek
computing expert
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



More information about the Bug-octave mailing list