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.

69 lines
2.3KB

  1. /* Computes the permanent, given a numpy array */
  2. #include <Python.h>
  3. #include <numpy/arrayobject.h>
  4. #include <math.h>
  5. #include <complex.h>
  6. // Forward function declaration.
  7. static PyObject *permanent(PyObject *self, PyObject *args);
  8. // Boilerplate: method list.
  9. static PyMethodDef methods[] = {
  10. { "permanent", permanent, METH_VARARGS, "Compute the permanent"},
  11. { NULL, NULL, 0, NULL } /* Sentinel */
  12. };
  13. // Boilerplate: Module initialization.
  14. PyMODINIT_FUNC initpermanent(void) {
  15. (void) Py_InitModule("permanent", methods);
  16. import_array();
  17. }
  18. /*****************************************************************************
  19. * Array access macros. *
  20. *****************************************************************************/
  21. #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
  22. #define m(x0) (*(npy_float64*)((PyArray_DATA(py_m) + \
  23. (x0) * PyArray_STRIDES(py_m)[0])))
  24. #define m_shape(i) (py_m->dimensions[(i)])
  25. #define r(x0, x1) (*(npy_float64*)((PyArray_DATA(py_r) + \
  26. (x0) * PyArray_STRIDES(py_r)[0] + \
  27. (x1) * PyArray_STRIDES(py_r)[1])))
  28. #define r_shape(i) (py_r->dimensions[(i)])
  29. #define v(x0, x1) (*(npy_float64*)((PyArray_DATA(py_v) + \
  30. (x0) * PyArray_STRIDES(py_v)[0] + \
  31. (x1) * PyArray_STRIDES(py_v)[1])))
  32. #define v_shape(i) (py_v->dimensions[(i)])
  33. #define F(x0, x1) (*(npy_float64*)((PyArray_DATA(py_F) + \
  34. (x0) * PyArray_STRIDES(py_F)[0] + \
  35. (x1) * PyArray_STRIDES(py_F)[1])))
  36. #define F_shape(i) (py_F->dimensions[(i)])
  37. static PyObject *permanent(PyObject *self, PyObject *args) {
  38. // Declare variables.
  39. npy_int64 d, i, j;
  40. double complex sum;
  41. PyArrayObject *submatrix;
  42. // Parse variables.
  43. if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &submatrix)) {
  44. return NULL;
  45. }
  46. // Compute the permanent
  47. for(i = 0; i < d; ++i) {
  48. for(j = 0; j<d; ++j) {
  49. sum+=1j;
  50. }
  51. }
  52. // Convert to a python complex number
  53. /*Py_complex psum = struct {};*/ //fromdouble
  54. PyObject *output=PyComplex_FromCComplex(psum);
  55. return output;
  56. }