nchoosek errors because of max_recursion_depth

Francesco Potortì Potorti at isti.cnr.it
Fri Nov 21 06:04:55 CST 2008


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

-- 
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/


More information about the Bug-octave mailing list