[Scipy-svn] r2933 - in trunk/Lib: misc sandbox/pysparse/src

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Apr 18 23:19:20 EDT 2007


Author: oliphant
Date: 2007-04-18 22:19:14 -0500 (Wed, 18 Apr 2007)
New Revision: 2933

Modified:
   trunk/Lib/misc/common.py
   trunk/Lib/sandbox/pysparse/src/ll_mat.c
Log:
Fix up comb, factorial, and factorial2 to use a slightly different (faster?) algorithm.  Fix include problem with pysparse to that it grabs the no-prefix option for backward compatibility.

Modified: trunk/Lib/misc/common.py
===================================================================
--- trunk/Lib/misc/common.py	2007-04-17 19:52:32 UTC (rev 2932)
+++ trunk/Lib/misc/common.py	2007-04-19 03:19:14 UTC (rev 2933)
@@ -32,12 +32,9 @@
     if exact:
         if n < 0:
             return 0L
-        n = long(n)
         val = 1L
-        k = 1L
-        while (k < n+1L):
-            val = val*k
-            k += 1
+        for k in xrange(1,n+1):
+          val *= k
         return val
     else:
         from scipy import special
@@ -64,12 +61,9 @@
             return 0L
         if n <= 0:
             return 1L
-        n = long(n)
         val = 1L
-        k = n
-        while (k > 0):
-            val = val*k
-            k -= 2
+        for k in xrange(n,0,-2):
+          val *= k
         return val
     else:
         from scipy import special
@@ -94,12 +88,9 @@
             return 0L
         if n<=0:
             return 1L
-        n = long(n)
         val = 1L
-        j = n
-        while (j > 0):
+        for j in xrange(n,0,-k):
             val = val*j
-            j -= k
         return val
     else:
         raise NotImplementedError
@@ -118,16 +109,9 @@
     if exact:
         if (k > N) or (N < 0) or (k < 0):
             return 0L
-        N,k = map(long,(N,k))
-        top = N
         val = 1L
-        while (top > (N-k)):
-            val *= top
-            top -= 1
-        n = 1L
-        while (n < k+1L):
-            val /= n
-            n += 1
+        for j in xrange(min(k, N-k)):
+          val = (val*(N-j))//(j+1)
         return val
     else:
         from scipy import special

Modified: trunk/Lib/sandbox/pysparse/src/ll_mat.c
===================================================================
--- trunk/Lib/sandbox/pysparse/src/ll_mat.c	2007-04-17 19:52:32 UTC (rev 2932)
+++ trunk/Lib/sandbox/pysparse/src/ll_mat.c	2007-04-19 03:19:14 UTC (rev 2933)
@@ -9,7 +9,7 @@
 #include "pysparse/spmatrix.h"
 
 #define PY_ARRAY_UNIQUE_SYMBOL spmatrix
-#include "numpy/arrayobject.h"
+#include "numpy/noprefix.h"
 
 #define INCREASE_FACTOR   1.5	/* increase rate for memory reallocation of ll_mat arrays */
 #define PPRINT_ROW_THRESH 500	/* row threshold for choosing between print formats */




More information about the Scipy-svn mailing list