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.

81 lines
2.4KB

  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 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. // Parse input
  39. PyArrayObject *submatrix;
  40. if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &submatrix)) {
  41. return NULL;
  42. }
  43. // Declare variables.
  44. /*npy_int64 d, i, j;*/
  45. Py_complex a;
  46. Py_complex b;
  47. a.real=1; a.imag=1;
  48. b.real=1; b.imag=1;
  49. a.real=a.real+b.real;
  50. a.imag=a.imag+b.imag;
  51. // Convert to a python complex number
  52. PyObject *output=PyComplex_FromDoubles(a.real,a.imag);
  53. return output;
  54. /*
  55. // Compute the permanent
  56. for(i = 0; i < d; ++i) {
  57. for(j = 0; j<d; ++j) {
  58. sum+=1j;
  59. }
  60. }
  61. */
  62. }