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.

23 lines
731B

  1. // Ryser's algorithm
  2. static npy_complex128 perm_ryser(PyArrayObject *submatrix) {
  3. int n = (int) PyArray_DIM(submatrix, 0);
  4. npy_complex128 rowsum, rowsumprod;
  5. npy_complex128 perm = complex_zero;
  6. int exp = 1 << n;
  7. int i, y, z;
  8. for (i=0; i<exp; ++i) {
  9. rowsumprod = complex_one;
  10. for (y=0; y<n; ++y) {
  11. rowsum = complex_zero;
  12. for (z=0; z<n; ++z) {
  13. if ((i & (1 << z)) != 0) { complex_inc(&rowsum, SM(z, y)); }
  14. }
  15. complex_multiply(&rowsumprod, rowsum);
  16. }
  17. complex_inc(&perm, complex_float_prod(rowsumprod, bitparity(i)));
  18. }
  19. if (n%2 == 1) {perm=complex_float_prod(perm, -1);}
  20. return perm;
  21. }