/* Functions to compute the permanent, given a numpy array */ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include #include #include "npy_util.h" #include "bithacks.h" // Forward function declaration static PyObject *permanent(PyObject *self, PyObject *args); // Method list static PyMethodDef methods[] = { { "permanent", permanent, METH_VARARGS, "Computes the permanent of a numpy using the most appropriate method available"}, { NULL, NULL, 0, NULL } // Sentinel }; #if PY_MAJOR_VERSION >= 3 static struct PyModuleDef cModPyDem = { PyModuleDef_HEAD_INIT, "permanent", "Computes the permanent of a numpy using the most appropriate method available", -1, methods }; PyMODINIT_FUNC PyInit_permanent(void) { import_array(); return PyModule_Create(&cModPyDem); } #else PyMODINIT_FUNC initpermanent(void) { (void) Py_InitModule("permanent", methods); import_array(); } #endif // Ryser's algorithm static npy_complex128 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