Browse Source

Whoops

master
Pete Shadbolt 10 years ago
parent
commit
39aa197cd7
2 changed files with 13 additions and 31 deletions
  1. +11
    -8
      run-tests.py
  2. +2
    -23
      src/permanent.c

+ 11
- 8
run-tests.py View File

@@ -16,20 +16,23 @@ def perm_ryser(a):
terms=map(get_term, indeces) terms=map(get_term, indeces)
return np.sum(terms)*((-1)**n) return np.sum(terms)*((-1)**n)


dimension=5
dimension=9
real=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension)) real=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
imag=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension)) imag=np.random.uniform(-1, 1, dimension*dimension).reshape((dimension, dimension))
submatrix=real+1j*imag submatrix=real+1j*imag


print lib.permanent(submatrix), perm_ryser(submatrix)
#print lib.permanent(submatrix), perm_ryser(submatrix)


sys.exit(0)
t=time.clock()
for i in range(1000):
perm_ryser(submatrix)
print time.clock()-t
#sys.exit(0)
#t=time.clock()
#for i in range(1000):
#perm_ryser(submatrix)
#t1=time.clock()-t


t=time.clock() t=time.clock()
for i in range(1000): for i in range(1000):
lib.permanent(submatrix) lib.permanent(submatrix)
print time.clock()-t
t2=time.clock()-t
print t2

#print t1/t2

+ 2
- 23
src/permanent.c View File

@@ -2,8 +2,9 @@
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h> #include <Python.h>
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
#include "bithacks.h"
#include "npy_util.h" #include "npy_util.h"
#include "bithacks.h"
#include "permanents.h"


// Forward function declaration // Forward function declaration
static PyObject *permanent(PyObject *self, PyObject *args); static PyObject *permanent(PyObject *self, PyObject *args);
@@ -20,28 +21,6 @@ PyMODINIT_FUNC initpermanent(void) {
import_array(); import_array();
} }


// Ryser's algorithm
static npy_complex128 perm_ryser(PyArrayObject *submatrix) {
int n = (int) PyArray_DIM(submatrix, 0);
npy_complex128 rowsum, rowsumprod;
npy_complex128 perm = complex_zero;
int exp = 1 << n;
int i, y, z;
for (i=0; i<exp; ++i) {
rowsumprod = complex_one;
for (y=0; y<n; ++y) {
rowsum = complex_zero;
for (z=0; z<n; ++z) {
if ((i & (1 << z)) != 0) { complex_inc(&rowsum, SM(z, y)); }
}
complex_multiply(&rowsumprod, rowsum);
}
complex_inc(&perm, complex_float_prod(rowsumprod, bitparity(i)));
}
if (n%2 == 1) {perm=complex_float_prod(perm, -1);}
return perm;
}

// This is a wrapper which chooses the optimal permanent function // This is a wrapper which chooses the optimal permanent function
static PyObject *permanent(PyObject *self, PyObject *args) { static PyObject *permanent(PyObject *self, PyObject *args) {
// Parse the input // Parse the input


Loading…
Cancel
Save