Python C extension to compute the permanent.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.0KB

  1. /* Computes the permanent, given a numpy array */
  2. #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  3. #include <Python.h>
  4. #include <numpy/arrayobject.h>
  5. #include "npy_util.h"
  6. #include "bithacks.h"
  7. #include "ryser.h"
  8. // Forward function declaration
  9. static PyObject *permanent(PyObject *self, PyObject *args);
  10. // Method list
  11. static PyMethodDef methods[] = {
  12. { "permanent", permanent, METH_VARARGS, "Compute the permanent"},
  13. { NULL, NULL, 0, NULL } // Sentinel
  14. };
  15. // Module initialization
  16. PyMODINIT_FUNC initpermanent(void) {
  17. (void) Py_InitModule("permanent", methods);
  18. import_array();
  19. }
  20. // This is a wrapper which chooses the optimal permanent function
  21. static PyObject *permanent(PyObject *self, PyObject *args) {
  22. // Parse the input
  23. PyArrayObject *submatrix;
  24. if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &submatrix)) {return NULL;}
  25. // Compute the permanent
  26. npy_complex128 p = ryser(submatrix);
  27. return PyComplex_FromDoubles(p.real, p.imag);
  28. }