nchoosek errors because of max_recursion_depth

David Bateman David.Bateman at motorola.com
Fri Nov 21 06:38:14 CST 2008


Francesco Potortì 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.
>
> @@ -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
>
>   
The reseting of the recursion depth should be inside the unwind_protect 
block.

D.


-- 
David Bateman                                David.Bateman at motorola.com
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary



More information about the Bug-octave mailing list