[3570545] | 1 | /* This program is public domain. */ |
---|
| 2 | |
---|
| 3 | #include <iostream> |
---|
| 4 | #include <iomanip> |
---|
| 5 | #include <Python.h> |
---|
| 6 | |
---|
| 7 | extern "C" void |
---|
| 8 | resolution(int Nin, const double Qin[], const double Rin[], |
---|
| 9 | int N, const double Q[], const double dQ[], double R[]); |
---|
| 10 | |
---|
| 11 | #define INVECTOR(obj,buf,len) \ |
---|
| 12 | do { \ |
---|
| 13 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 14 | if (err < 0) return NULL; \ |
---|
| 15 | len /= sizeof(*buf); \ |
---|
| 16 | } while (0) |
---|
| 17 | |
---|
| 18 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 19 | do { \ |
---|
| 20 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 21 | if (err < 0) return NULL; \ |
---|
| 22 | len /= sizeof(*buf); \ |
---|
| 23 | } while (0) |
---|
| 24 | |
---|
| 25 | PyObject* Pconvolve(PyObject *obj, PyObject *args) |
---|
| 26 | { |
---|
| 27 | PyObject *Qi_obj,*Ri_obj,*Q_obj,*dQ_obj,*R_obj; |
---|
| 28 | const double *Qi, *Ri, *Q, *dQ; |
---|
| 29 | double *R; |
---|
| 30 | Py_ssize_t nQi, nRi, nQ, ndQ, nR; |
---|
| 31 | |
---|
| 32 | if (!PyArg_ParseTuple(args, "OOOOO:resolution", |
---|
| 33 | &Qi_obj,&Ri_obj,&Q_obj,&dQ_obj,&R_obj)) return NULL; |
---|
| 34 | INVECTOR(Qi_obj,Qi,nQi); |
---|
| 35 | INVECTOR(Ri_obj,Ri,nRi); |
---|
| 36 | INVECTOR(Q_obj,Q,nQ); |
---|
| 37 | INVECTOR(dQ_obj,dQ,ndQ); |
---|
| 38 | OUTVECTOR(R_obj,R,nR); |
---|
| 39 | if (nQi != nRi) { |
---|
| 40 | #ifndef BROKEN_EXCEPTIONS |
---|
| 41 | PyErr_SetString(PyExc_ValueError, "_librefl.convolve: Qi and Ri have different lengths"); |
---|
| 42 | #endif |
---|
| 43 | return NULL; |
---|
| 44 | } |
---|
| 45 | if (nQ != ndQ || nQ != nR) { |
---|
| 46 | #ifndef BROKEN_EXCEPTIONS |
---|
| 47 | PyErr_SetString(PyExc_ValueError, "_librefl.convolve: Q, dQ and R have different lengths"); |
---|
| 48 | #endif |
---|
| 49 | return NULL; |
---|
| 50 | } |
---|
| 51 | resolution(nQi,Qi,Ri,nQ,Q,dQ,R); |
---|
| 52 | return Py_BuildValue(""); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | static PyMethodDef methods[] = { |
---|
| 56 | {"convolve", |
---|
| 57 | Pconvolve, |
---|
| 58 | METH_VARARGS, |
---|
| 59 | "convolve(Qi,Ri,Q,dQ,R): compute convolution of width dQ[k] at points Q[k], returned in R[k]"}, |
---|
| 60 | {0} |
---|
| 61 | } ; |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | #if defined(WIN32) && !defined(__MINGW32__) |
---|
| 65 | __declspec(dllexport) |
---|
| 66 | #endif |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | extern "C" void init_modeling(void) { |
---|
| 70 | Py_InitModule4("_modeling", |
---|
| 71 | methods, |
---|
| 72 | "Modeling C Library", |
---|
| 73 | 0, |
---|
| 74 | PYTHON_API_VERSION |
---|
| 75 | ); |
---|
| 76 | } |
---|