Python C extension to compute the permanent.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
vor 10 Jahren
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Functions to compute 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. // Forward function declaration
  8. static PyObject *permanent(PyObject *self, PyObject *args);
  9. // Method list
  10. static PyMethodDef methods[] = {
  11. { "permanent", permanent, METH_VARARGS, "Computes the permanent of a numpy using the most appropriate method available"},
  12. { NULL, NULL, 0, NULL } // Sentinel
  13. };
  14. // Module initialization
  15. PyMODINIT_FUNC initpermanent(void) {
  16. (void) Py_InitModule("permanent", methods);
  17. import_array();
  18. }
  19. // Ryser's algorithm
  20. static npy_complex128 ryser(PyArrayObject *submatrix) {
  21. int n = (int) PyArray_DIM(submatrix, 0);
  22. npy_complex128 rowsum, rowsumprod;
  23. npy_complex128 perm = complex_zero;
  24. int exp = 1 << n;
  25. int i, y, z;
  26. for (i=0; i<exp; ++i) {
  27. rowsumprod = complex_one;
  28. for (y=0; y<n; ++y) {
  29. rowsum = complex_zero;
  30. for (z=0; z<n; ++z) {
  31. if ((i & (1 << z)) != 0) { complex_inc(&rowsum, SM(z, y)); }
  32. }
  33. complex_multiply(&rowsumprod, rowsum);
  34. }
  35. complex_inc(&perm, complex_float_prod(rowsumprod, bitparity(i)));
  36. }
  37. if (n%2 == 1) { perm=complex_float_prod(perm, -1); }
  38. return perm;
  39. }
  40. // This is a wrapper which chooses the optimal permanent function
  41. static PyObject *permanent(PyObject *self, PyObject *args) {
  42. // Parse the input
  43. PyArrayObject *submatrix;
  44. if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &submatrix)) {return NULL;}
  45. // Compute the permanent
  46. npy_complex128 p = ryser(submatrix);
  47. return PyComplex_FromDoubles(p.real, p.imag);
  48. }