Python C extension to compute the permanent.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

65 lignes
2.1KB

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