Advertisement
cirossmonteiro

python c extension

Feb 27th, 2025 (edited)
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.92 KB | None | 0 0
  1. #include <Python.h>
  2.  
  3. // source: https://stackoverflow.com/questions/8001923/python-extension-module-with-variable-number-of-arguments
  4. // source: https://codedamn.com/news/python/implementing-custom-python-c-extensions-step-by-step-guide
  5. // source: a lot from chatgpt
  6.  
  7. void _compute_tensor_index(
  8.     unsigned int order,
  9.     unsigned int *dimensions,
  10.     unsigned long index,
  11.     unsigned int *final
  12. ) {
  13.     unsigned int i;
  14.     unsigned long p = 1;
  15.     unsigned int r = 0;
  16.  
  17.     for(i = 0; i < order; i++) {
  18.         p *= dimensions[i];
  19.     }
  20.  
  21.     for(i = 0; i < order; i++) {
  22.         p /= dimensions[i];
  23.         final[i] = (index - r) / p;
  24.         r += final[i] * p;
  25.     }
  26. }
  27.  
  28. unsigned long _compute_linear_index(
  29.     unsigned int order,
  30.     unsigned int *dimensions,
  31.     unsigned int *index
  32. ) {
  33.     unsigned long p = 1;
  34.     unsigned long final = 0;
  35.     unsigned int i;
  36.  
  37.     for(i = 0; i < order; i++) {
  38.         final += index[order-i-1] * p;
  39.         p *= dimensions[order-i-1];
  40.     }
  41.  
  42.     return final;
  43. }
  44.  
  45. static PyObject* contraction(PyObject* self, PyObject* args) {
  46.     unsigned long order = PyLong_AsUnsignedLong(PyTuple_GetItem(args, 0));
  47.     unsigned long new_order = order - 2;
  48.     unsigned long I = PyLong_AsUnsignedLong(PyTuple_GetItem(args, 1));
  49.     unsigned long J = PyLong_AsUnsignedLong(PyTuple_GetItem(args, 2));
  50.     unsigned int *dimensions;
  51.     unsigned int *new_dimensions;
  52.     unsigned int *index;
  53.     unsigned int *new_index;
  54.     unsigned long i;
  55.     unsigned int j;
  56.     unsigned int current_i;
  57.     unsigned long p = 1;
  58.     unsigned long new_p = 1;
  59.     unsigned long new_pos;
  60.     double *values;
  61.     double *new_values;
  62.     PyObject *result;
  63.    
  64.  
  65.     dimensions = malloc(order*sizeof(unsigned int));
  66.     new_dimensions = malloc(new_order*sizeof(unsigned int));
  67.     for(i = 0; i < order; i++) {
  68.         dimensions[i] = PyLong_AsUnsignedLong(PyTuple_GetItem(args, i+3));
  69.         p *= dimensions[i];
  70.     }
  71.     current_i = 0;
  72.     for(i = 0; i < order; i++) {
  73.         if (i == I || i == J) {
  74.             continue;
  75.         } else {
  76.             new_dimensions[current_i] = dimensions[i];
  77.             new_p *= dimensions[i];
  78.             current_i++;
  79.         }
  80.     }
  81.  
  82.     values = malloc(p*sizeof(double));
  83.     for(i = 0; i < p; i++) {
  84.         values[i] = PyFloat_AsDouble(PyTuple_GetItem(args, i+order+3));
  85.     }
  86.  
  87.     new_values = malloc(new_p*sizeof(double));
  88.     for(i = 0; i < new_p; i++) {
  89.         new_values[i] = 0;
  90.     }
  91.  
  92.     // core
  93.     index = malloc(order*sizeof(unsigned int));
  94.     new_index = malloc(new_order*sizeof(unsigned int));
  95.     for(i = 0; i < p; i++) {
  96.         _compute_tensor_index(order, dimensions, i, index);
  97.         if (index[I] == index[J]) {
  98.  
  99.             // computing new_index
  100.             current_i = 0;
  101.             for(j = 0; j < order; j++) {
  102.                 if (j == I || j == J) {
  103.                     continue;
  104.                 } else {
  105.                     new_index[current_i] = index[j];
  106.                     current_i++;
  107.                 }
  108.             }
  109.  
  110.             new_pos = _compute_linear_index(new_order, new_dimensions, new_index);
  111.             new_values[new_pos] += values[i];
  112.         }
  113.     }
  114.  
  115.     result = PyList_New(new_p);
  116.     for(i = 0; i < new_p; i++) {
  117.         PyList_SetItem(result, i, PyFloat_FromDouble(new_values[i]));
  118.     }
  119.  
  120.     free(dimensions);
  121.     free(new_dimensions);
  122.     free(index);
  123.     free(new_index);
  124.     free(values);
  125.     free(new_values);
  126.  
  127.     return result;
  128. }
  129.  
  130. static PyObject* compute_tensor_index(PyObject* self, PyObject* args) {
  131.     Py_ssize_t order = (PyTuple_Size(args) - 1);
  132.     PyObject *result = PyList_New(order);
  133.     unsigned int i;
  134.     unsigned int *dimensions;
  135.     unsigned int *final;
  136.     unsigned long p = 1;
  137.     unsigned int r = 0;
  138.     unsigned long index = PyLong_AsUnsignedLong(PyTuple_GetItem(args, 0));
  139.    
  140.     final = malloc(order*sizeof(unsigned int));
  141.     dimensions = malloc(order*sizeof(unsigned int));
  142.     for(i = 0; i < order; i++) {
  143.         dimensions[i] = PyLong_AsUnsignedLong(PyTuple_GetItem(args, i+1));
  144.         p *= dimensions[i];
  145.     }
  146.  
  147.     // core
  148.     for(i = 0; i < order; i++) {
  149.         p /= dimensions[i];
  150.         final[i] = (index - r) / p;
  151.         r += final[i] * p;
  152.     }
  153.    
  154.     for(i = 0; i < order; i++) {
  155.         PyList_SetItem(result, i, PyLong_FromLong(final[i]));
  156.     }
  157.  
  158.     free(dimensions);
  159.     free(final);
  160.  
  161.     return result;
  162. }
  163.  
  164. static PyObject* compute_linear_index(PyObject* self, PyObject* args) {
  165.     Py_ssize_t order = PyTuple_Size(args) / 2;
  166.     Py_ssize_t i;
  167.     unsigned int *dimensions;
  168.     unsigned int *index;
  169.     long p = 1;
  170.     long final = 0;
  171.    
  172.     dimensions = malloc(order*sizeof(unsigned int));
  173.     for(i = 0; i < order; i++) {
  174.         dimensions[i] = PyLong_AsUnsignedLong(PyTuple_GetItem(args, i));
  175.     }
  176.  
  177.     index = malloc(order*sizeof(unsigned int));
  178.     for(i = 0; i < order; i++) {
  179.         index[i] = PyLong_AsUnsignedLong(PyTuple_GetItem(args, i+order));
  180.     };
  181.  
  182.     // core
  183.     for(i = 0; i < order; i++) {
  184.         final += index[order-i-1]*p;
  185.         p *= dimensions[order-i-1];
  186.     }
  187.  
  188.     free(dimensions);
  189.     free(index);
  190.  
  191.     return PyLong_FromLong(final);
  192. }
  193.  
  194. static PyMethodDef TensorMethods[] = {
  195.     {"contraction", contraction, METH_VARARGS, "Compute contraction."},
  196.     {"compute_tensor_index", compute_tensor_index, METH_VARARGS, "Compute tensor index."},
  197.     {"compute_linear_index", compute_linear_index, METH_VARARGS, "Compute linear index."},
  198.     {NULL, NULL, 0, NULL} /* Sentinel */
  199. };
  200.  
  201. static struct PyModuleDef tensor_module = {
  202.     PyModuleDef_HEAD_INIT,
  203.     "tensor", /* name of module */
  204.     NULL,           /* module documentation, may be NULL */
  205.     -1,             /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
  206.     TensorMethods
  207. };
  208.  
  209. PyMODINIT_FUNC PyInit_tensor(void) {
  210.     return PyModule_Create(&tensor_module);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement