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.

71 lines
1.9KB

  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 <math.h>
  6. #include <complex.h>
  7. // Forward function declaration.
  8. static PyObject *permanent(PyObject *self, PyObject *args);
  9. // Boilerplate: method list.
  10. static PyMethodDef methods[] = {
  11. { "permanent", permanent, METH_VARARGS, "Compute the permanent"},
  12. { NULL, NULL, 0, NULL } /* Sentinel */
  13. };
  14. // Boilerplate: Module initialization.
  15. PyMODINIT_FUNC initpermanent(void) {
  16. (void) Py_InitModule("permanent", methods);
  17. import_array();
  18. }
  19. /*****************************************************************************
  20. * Array access macros. *
  21. *****************************************************************************/
  22. #define r(x0, x1) (*(npy_float64*)((PyArray_DATA(py_r) + \
  23. (x0) * PyArray_STRIDES(py_r)[0] + \
  24. (x1) * PyArray_STRIDES(py_r)[1])))
  25. #define r_shape(i) (py_r->dimensions[(i)])
  26. static PyObject *permanent(PyObject *self, PyObject *args) {
  27. // Parse input
  28. PyArrayObject *submatrix;
  29. if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &submatrix)) {
  30. return NULL;
  31. }
  32. // Testing algebra
  33. npy_complex128 a;
  34. /*a.real=1; a.imag=1;*/
  35. /*b.real=1; b.imag=1;*/
  36. /*a.real=a.real+b.real;*/
  37. /*a.imag=a.imag+b.imag;*/
  38. // Let's see if we can add to a number from the numpy array
  39. int xpos=0; int ypos=0;
  40. a=(*(npy_complex128*)((PyArray_DATA(submatrix) + \
  41. (xpos) * PyArray_STRIDES(submatrix)[0] + \
  42. (ypos) * PyArray_STRIDES(submatrix)[1])));
  43. // Convert to a python complex number and return
  44. PyObject *output=PyComplex_FromDoubles(a.real,a.imag);
  45. return output;
  46. /*
  47. // Compute the permanent
  48. for(i = 0; i < d; ++i) {
  49. for(j = 0; j<d; ++j) {
  50. sum+=1j;
  51. }
  52. }
  53. */
  54. }