[9e8dc22] | 1 | |
---|
| 2 | #include <Python.h> |
---|
| 3 | #include "structmember.h" |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <math.h> |
---|
| 7 | #include <time.h> |
---|
| 8 | |
---|
| 9 | #include "invertor.h" |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | /// Error object for raised exceptions |
---|
| 13 | static PyObject * CinvertorError = NULL; |
---|
| 14 | |
---|
| 15 | #define INVECTOR(obj,buf,len) \ |
---|
| 16 | do { \ |
---|
| 17 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 18 | if (err < 0) return NULL; \ |
---|
| 19 | len /= sizeof(*buf); \ |
---|
| 20 | } while (0) |
---|
| 21 | |
---|
| 22 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 23 | do { \ |
---|
| 24 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 25 | if (err < 0) return NULL; \ |
---|
| 26 | len /= sizeof(*buf); \ |
---|
| 27 | } while (0) |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | // Class definition |
---|
| 31 | typedef struct { |
---|
| 32 | PyObject_HEAD |
---|
| 33 | Invertor_params params; |
---|
| 34 | } Cinvertor; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | static void |
---|
| 38 | Cinvertor_dealloc(Cinvertor* self) |
---|
| 39 | { |
---|
| 40 | invertor_dealloc(&(self->params)); |
---|
| 41 | |
---|
| 42 | self->ob_type->tp_free((PyObject*)self); |
---|
| 43 | |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | static PyObject * |
---|
| 47 | Cinvertor_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 48 | { |
---|
| 49 | Cinvertor *self; |
---|
| 50 | |
---|
| 51 | self = (Cinvertor *)type->tp_alloc(type, 0); |
---|
| 52 | |
---|
| 53 | return (PyObject *)self; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | static int |
---|
| 57 | Cinvertor_init(Cinvertor *self, PyObject *args, PyObject *kwds) |
---|
| 58 | { |
---|
| 59 | if (self != NULL) { |
---|
| 60 | // Create parameters |
---|
| 61 | invertor_init(&(self->params)); |
---|
| 62 | } |
---|
| 63 | return 0; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | static PyMemberDef Cinvertor_members[] = { |
---|
| 67 | //{"params", T_OBJECT, offsetof(Cinvertor, params), 0, |
---|
| 68 | // "Parameters"}, |
---|
| 69 | {NULL} /* Sentinel */ |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Function to set the x data |
---|
| 75 | * Takes an array of doubles as input |
---|
| 76 | * Returns the number of entries found |
---|
| 77 | */ |
---|
| 78 | static PyObject * set_x(Cinvertor *self, PyObject *args) { |
---|
| 79 | PyObject *data_obj; |
---|
| 80 | Py_ssize_t ndata; |
---|
| 81 | double *data; |
---|
[2d06beb] | 82 | int i; |
---|
[9e8dc22] | 83 | |
---|
| 84 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 85 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 86 | |
---|
| 87 | free(self->params.x); |
---|
| 88 | self->params.x = (double*) malloc(ndata*sizeof(double)); |
---|
| 89 | |
---|
| 90 | if(self->params.x==NULL) { |
---|
| 91 | PyErr_SetString(CinvertorError, |
---|
| 92 | "Cinvertor.set_x: problem allocating memory."); |
---|
| 93 | return NULL; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | for (i=0; i<ndata; i++) { |
---|
| 97 | self->params.x[i] = data[i]; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | //self->params.x = data; |
---|
[9e8dc22] | 101 | self->params.npoints = ndata; |
---|
| 102 | return Py_BuildValue("i", self->params.npoints); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | static PyObject * get_x(Cinvertor *self, PyObject *args) { |
---|
| 106 | PyObject *data_obj; |
---|
| 107 | Py_ssize_t ndata; |
---|
| 108 | double *data; |
---|
| 109 | int i; |
---|
| 110 | |
---|
| 111 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 112 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 113 | |
---|
| 114 | // Check that the input array is large enough |
---|
| 115 | if (ndata < self->params.npoints) { |
---|
| 116 | PyErr_SetString(CinvertorError, |
---|
| 117 | "Cinvertor.get_x: input array too short for data."); |
---|
| 118 | return NULL; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | for(i=0; i<self->params.npoints; i++){ |
---|
| 122 | data[i] = self->params.x[i]; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | return Py_BuildValue("i", self->params.npoints); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /** |
---|
| 129 | * Function to set the y data |
---|
| 130 | * Takes an array of doubles as input |
---|
| 131 | * Returns the number of entries found |
---|
| 132 | */ |
---|
| 133 | static PyObject * set_y(Cinvertor *self, PyObject *args) { |
---|
| 134 | PyObject *data_obj; |
---|
| 135 | Py_ssize_t ndata; |
---|
| 136 | double *data; |
---|
[2d06beb] | 137 | int i; |
---|
[9e8dc22] | 138 | |
---|
| 139 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 140 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 141 | |
---|
| 142 | free(self->params.y); |
---|
| 143 | self->params.y = (double*) malloc(ndata*sizeof(double)); |
---|
| 144 | |
---|
| 145 | if(self->params.y==NULL) { |
---|
| 146 | PyErr_SetString(CinvertorError, |
---|
| 147 | "Cinvertor.set_y: problem allocating memory."); |
---|
| 148 | return NULL; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | for (i=0; i<ndata; i++) { |
---|
| 152 | self->params.y[i] = data[i]; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | //self->params.y = data; |
---|
[9e8dc22] | 156 | self->params.ny = ndata; |
---|
| 157 | return Py_BuildValue("i", self->params.ny); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | static PyObject * get_y(Cinvertor *self, PyObject *args) { |
---|
| 161 | PyObject *data_obj; |
---|
| 162 | Py_ssize_t ndata; |
---|
| 163 | double *data; |
---|
| 164 | int i; |
---|
| 165 | |
---|
| 166 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 167 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 168 | |
---|
| 169 | // Check that the input array is large enough |
---|
| 170 | if (ndata < self->params.ny) { |
---|
| 171 | PyErr_SetString(CinvertorError, |
---|
| 172 | "Cinvertor.get_y: input array too short for data."); |
---|
| 173 | return NULL; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | for(i=0; i<self->params.ny; i++){ |
---|
| 177 | data[i] = self->params.y[i]; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | return Py_BuildValue("i", self->params.npoints); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Function to set the x data |
---|
| 185 | * Takes an array of doubles as input |
---|
| 186 | * Returns the number of entries found |
---|
| 187 | */ |
---|
| 188 | static PyObject * set_err(Cinvertor *self, PyObject *args) { |
---|
| 189 | PyObject *data_obj; |
---|
| 190 | Py_ssize_t ndata; |
---|
| 191 | double *data; |
---|
[2d06beb] | 192 | int i; |
---|
[9e8dc22] | 193 | |
---|
| 194 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 195 | OUTVECTOR(data_obj,data,ndata); |
---|
[2d06beb] | 196 | |
---|
| 197 | free(self->params.err); |
---|
| 198 | self->params.err = (double*) malloc(ndata*sizeof(double)); |
---|
| 199 | |
---|
| 200 | if(self->params.err==NULL) { |
---|
| 201 | PyErr_SetString(CinvertorError, |
---|
| 202 | "Cinvertor.set_err: problem allocating memory."); |
---|
| 203 | return NULL; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | for (i=0; i<ndata; i++) { |
---|
| 207 | self->params.err[i] = data[i]; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | //self->params.err = data; |
---|
[9e8dc22] | 211 | self->params.nerr = ndata; |
---|
| 212 | return Py_BuildValue("i", self->params.nerr); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | static PyObject * get_err(Cinvertor *self, PyObject *args) { |
---|
| 216 | PyObject *data_obj; |
---|
| 217 | Py_ssize_t ndata; |
---|
| 218 | double *data; |
---|
| 219 | int i; |
---|
| 220 | |
---|
| 221 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 222 | OUTVECTOR(data_obj, data, ndata); |
---|
[9e8dc22] | 223 | |
---|
| 224 | // Check that the input array is large enough |
---|
| 225 | if (ndata < self->params.nerr) { |
---|
| 226 | PyErr_SetString(CinvertorError, |
---|
| 227 | "Cinvertor.get_err: input array too short for data."); |
---|
| 228 | return NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | for(i=0; i<self->params.nerr; i++){ |
---|
| 232 | data[i] = self->params.err[i]; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | return Py_BuildValue("i", self->params.npoints); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Check the validity of the stored data |
---|
| 240 | * Returns the number of points if it's all good, -1 otherwise |
---|
| 241 | */ |
---|
| 242 | static PyObject * is_valid(Cinvertor *self, PyObject *args) { |
---|
| 243 | if(self->params.npoints==self->params.ny && |
---|
| 244 | self->params.npoints==self->params.nerr) { |
---|
| 245 | return Py_BuildValue("i", self->params.npoints); |
---|
| 246 | } else { |
---|
| 247 | return Py_BuildValue("i", -1); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Sets the maximum distance |
---|
| 253 | */ |
---|
| 254 | static PyObject * set_dmax(Cinvertor *self, PyObject *args) { |
---|
| 255 | double d_max; |
---|
| 256 | |
---|
| 257 | if (!PyArg_ParseTuple(args, "d", &d_max)) return NULL; |
---|
| 258 | self->params.d_max = d_max; |
---|
| 259 | return Py_BuildValue("d", self->params.d_max); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Gets the maximum distance |
---|
| 264 | */ |
---|
| 265 | static PyObject * get_dmax(Cinvertor *self, PyObject *args) { |
---|
| 266 | return Py_BuildValue("d", self->params.d_max); |
---|
| 267 | } |
---|
| 268 | |
---|
[f71287f4] | 269 | /** |
---|
| 270 | * Sets the minimum q |
---|
| 271 | */ |
---|
| 272 | static PyObject * set_qmin(Cinvertor *self, PyObject *args) { |
---|
| 273 | double q_min; |
---|
| 274 | |
---|
| 275 | if (!PyArg_ParseTuple(args, "d", &q_min)) return NULL; |
---|
| 276 | self->params.q_min = q_min; |
---|
| 277 | return Py_BuildValue("d", self->params.q_min); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Gets the minimum q |
---|
| 282 | */ |
---|
| 283 | static PyObject * get_qmin(Cinvertor *self, PyObject *args) { |
---|
| 284 | return Py_BuildValue("d", self->params.q_min); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | |
---|
| 288 | /** |
---|
| 289 | * Sets the maximum q |
---|
| 290 | */ |
---|
| 291 | static PyObject * set_qmax(Cinvertor *self, PyObject *args) { |
---|
| 292 | double q_max; |
---|
| 293 | |
---|
| 294 | if (!PyArg_ParseTuple(args, "d", &q_max)) return NULL; |
---|
| 295 | self->params.q_max = q_max; |
---|
| 296 | return Py_BuildValue("d", self->params.q_max); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Gets the maximum q |
---|
| 301 | */ |
---|
| 302 | static PyObject * get_qmax(Cinvertor *self, PyObject *args) { |
---|
| 303 | return Py_BuildValue("d", self->params.q_max); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
[eca05c8] | 307 | static PyObject * set_alpha(Cinvertor *self, PyObject *args) { |
---|
| 308 | double alpha; |
---|
| 309 | |
---|
| 310 | if (!PyArg_ParseTuple(args, "d", &alpha)) return NULL; |
---|
| 311 | self->params.alpha = alpha; |
---|
| 312 | return Py_BuildValue("d", self->params.alpha); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * Gets the maximum distance |
---|
| 317 | */ |
---|
| 318 | static PyObject * get_alpha(Cinvertor *self, PyObject *args) { |
---|
| 319 | return Py_BuildValue("d", self->params.alpha); |
---|
| 320 | } |
---|
| 321 | |
---|
[9e8dc22] | 322 | /** |
---|
| 323 | * Gets the number of x points |
---|
| 324 | */ |
---|
| 325 | static PyObject * get_nx(Cinvertor *self, PyObject *args) { |
---|
| 326 | return Py_BuildValue("i", self->params.npoints); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
| 330 | * Gets the number of y points |
---|
| 331 | */ |
---|
| 332 | static PyObject * get_ny(Cinvertor *self, PyObject *args) { |
---|
| 333 | return Py_BuildValue("i", self->params.ny); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /** |
---|
| 337 | * Gets the number of error points |
---|
| 338 | */ |
---|
| 339 | static PyObject * get_nerr(Cinvertor *self, PyObject *args) { |
---|
| 340 | return Py_BuildValue("i", self->params.nerr); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * Function to call to evaluate the residuals |
---|
[eca05c8] | 346 | * @param args: input parameters |
---|
| 347 | * @return: list of residuals |
---|
[9e8dc22] | 348 | */ |
---|
| 349 | static PyObject * residuals(Cinvertor *self, PyObject *args) { |
---|
| 350 | double *pars; |
---|
| 351 | PyObject* residuals; |
---|
| 352 | PyObject* temp; |
---|
| 353 | double *res; |
---|
| 354 | int i; |
---|
| 355 | double residual, diff; |
---|
| 356 | // Regularization factor |
---|
[eca05c8] | 357 | double regterm = 0.0; |
---|
| 358 | double tmp = 0.0; |
---|
[abad620] | 359 | // Number of slices in regularization term estimate |
---|
| 360 | int nslice = 25; |
---|
[9e8dc22] | 361 | |
---|
| 362 | PyObject *data_obj; |
---|
| 363 | Py_ssize_t npars; |
---|
| 364 | |
---|
| 365 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 366 | |
---|
| 367 | OUTVECTOR(data_obj,pars,npars); |
---|
| 368 | |
---|
| 369 | // PyList of residuals |
---|
| 370 | // Should create this list only once and refill it |
---|
| 371 | residuals = PyList_New(self->params.npoints); |
---|
[eca05c8] | 372 | |
---|
[abad620] | 373 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[9e8dc22] | 374 | |
---|
| 375 | for(i=0; i<self->params.npoints; i++) { |
---|
| 376 | diff = self->params.y[i] - iq(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 377 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
[eca05c8] | 378 | tmp = residual; |
---|
| 379 | |
---|
| 380 | // regularization term |
---|
| 381 | residual += self->params.alpha * regterm; |
---|
| 382 | |
---|
| 383 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
| 384 | PyErr_SetString(CinvertorError, |
---|
| 385 | "Cinvertor.residuals: error setting residual."); |
---|
| 386 | return NULL; |
---|
| 387 | }; |
---|
| 388 | |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | return residuals; |
---|
| 392 | } |
---|
| 393 | /** |
---|
| 394 | * Function to call to evaluate the residuals |
---|
| 395 | * for P(r) minimization (for testing purposes) |
---|
| 396 | * @param args: input parameters |
---|
| 397 | * @return: list of residuals |
---|
| 398 | */ |
---|
| 399 | static PyObject * pr_residuals(Cinvertor *self, PyObject *args) { |
---|
| 400 | double *pars; |
---|
| 401 | PyObject* residuals; |
---|
| 402 | PyObject* temp; |
---|
| 403 | double *res; |
---|
| 404 | int i; |
---|
| 405 | double residual, diff; |
---|
| 406 | // Regularization factor |
---|
| 407 | double regterm = 0.0; |
---|
| 408 | double tmp = 0.0; |
---|
[abad620] | 409 | // Number of slices in regularization term estimate |
---|
| 410 | int nslice = 25; |
---|
[eca05c8] | 411 | |
---|
| 412 | PyObject *data_obj; |
---|
| 413 | Py_ssize_t npars; |
---|
| 414 | |
---|
| 415 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 416 | |
---|
| 417 | OUTVECTOR(data_obj,pars,npars); |
---|
| 418 | |
---|
| 419 | // Should create this list only once and refill it |
---|
| 420 | residuals = PyList_New(self->params.npoints); |
---|
| 421 | |
---|
[abad620] | 422 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[eca05c8] | 423 | |
---|
| 424 | |
---|
| 425 | for(i=0; i<self->params.npoints; i++) { |
---|
| 426 | diff = self->params.y[i] - pr(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 427 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
| 428 | tmp = residual; |
---|
| 429 | |
---|
| 430 | // regularization term |
---|
| 431 | residual += self->params.alpha * regterm; |
---|
| 432 | |
---|
[9e8dc22] | 433 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
| 434 | PyErr_SetString(CinvertorError, |
---|
| 435 | "Cinvertor.residuals: error setting residual."); |
---|
| 436 | return NULL; |
---|
| 437 | }; |
---|
| 438 | |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | return residuals; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | /** |
---|
| 445 | * Function to call to evaluate the scattering intensity |
---|
| 446 | * @param args: c-parameters, and q |
---|
| 447 | * @return: I(q) |
---|
| 448 | */ |
---|
| 449 | static PyObject * get_iq(Cinvertor *self, PyObject *args) { |
---|
| 450 | double *pars; |
---|
| 451 | double q, iq_value; |
---|
| 452 | PyObject *data_obj; |
---|
| 453 | Py_ssize_t npars; |
---|
| 454 | |
---|
| 455 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
| 456 | OUTVECTOR(data_obj,pars,npars); |
---|
| 457 | |
---|
| 458 | iq_value = iq(pars, self->params.d_max, npars, q); |
---|
| 459 | return Py_BuildValue("f", iq_value); |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | /** |
---|
| 463 | * Function to call to evaluate P(r) |
---|
| 464 | * @param args: c-parameters and r |
---|
| 465 | * @return: P(r) |
---|
| 466 | */ |
---|
| 467 | static PyObject * get_pr(Cinvertor *self, PyObject *args) { |
---|
| 468 | double *pars; |
---|
| 469 | double r, pr_value; |
---|
| 470 | PyObject *data_obj; |
---|
| 471 | Py_ssize_t npars; |
---|
| 472 | |
---|
| 473 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &r)) return NULL; |
---|
| 474 | OUTVECTOR(data_obj,pars,npars); |
---|
| 475 | |
---|
| 476 | pr_value = pr(pars, self->params.d_max, npars, r); |
---|
| 477 | return Py_BuildValue("f", pr_value); |
---|
| 478 | } |
---|
| 479 | |
---|
[eca05c8] | 480 | /** |
---|
| 481 | * Function to call to evaluate P(r) with errors |
---|
| 482 | * @param args: c-parameters and r |
---|
| 483 | * @return: P(r) |
---|
| 484 | */ |
---|
| 485 | static PyObject * get_pr_err(Cinvertor *self, PyObject *args) { |
---|
| 486 | double *pars; |
---|
| 487 | double *pars_err; |
---|
| 488 | double pr_err_value; |
---|
| 489 | double r, pr_value; |
---|
| 490 | PyObject *data_obj; |
---|
| 491 | Py_ssize_t npars; |
---|
| 492 | PyObject *err_obj; |
---|
| 493 | Py_ssize_t npars2; |
---|
| 494 | |
---|
| 495 | if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; |
---|
| 496 | OUTVECTOR(data_obj,pars,npars); |
---|
| 497 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
| 498 | |
---|
| 499 | pr_err(pars, pars_err, self->params.d_max, npars, r, &pr_value, &pr_err_value); |
---|
| 500 | return Py_BuildValue("ff", pr_value, pr_err_value); |
---|
| 501 | } |
---|
| 502 | |
---|
[2d06beb] | 503 | static PyObject * basefunc_ft(Cinvertor *self, PyObject *args) { |
---|
| 504 | double d_max, q; |
---|
| 505 | int n; |
---|
| 506 | |
---|
| 507 | if (!PyArg_ParseTuple(args, "did", &d_max, &n, &q)) return NULL; |
---|
| 508 | return Py_BuildValue("f", ortho_transformed(d_max, n, q)); |
---|
| 509 | |
---|
| 510 | } |
---|
[9e8dc22] | 511 | |
---|
[abad620] | 512 | static PyObject * oscillations(Cinvertor *self, PyObject *args) { |
---|
| 513 | double *pars; |
---|
| 514 | PyObject *data_obj; |
---|
| 515 | Py_ssize_t npars; |
---|
| 516 | double oscill, norm; |
---|
| 517 | |
---|
| 518 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 519 | OUTVECTOR(data_obj,pars,npars); |
---|
| 520 | |
---|
| 521 | oscill = reg_term(pars, self->params.d_max, npars, 100); |
---|
| 522 | norm = int_p2(pars, self->params.d_max, npars, 100); |
---|
| 523 | return Py_BuildValue("f", sqrt(oscill/norm)/acos(-1.0)*self->params.d_max ); |
---|
| 524 | |
---|
| 525 | } |
---|
| 526 | |
---|
[4f63160] | 527 | static PyObject * get_peaks(Cinvertor *self, PyObject *args) { |
---|
| 528 | double *pars; |
---|
| 529 | PyObject *data_obj; |
---|
| 530 | Py_ssize_t npars; |
---|
| 531 | int count; |
---|
| 532 | |
---|
| 533 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 534 | OUTVECTOR(data_obj,pars,npars); |
---|
| 535 | |
---|
| 536 | count = npeaks(pars, self->params.d_max, npars, 100); |
---|
| 537 | |
---|
| 538 | return Py_BuildValue("i", count ); |
---|
| 539 | |
---|
| 540 | } |
---|
| 541 | |
---|
[9e8dc22] | 542 | static PyMethodDef Cinvertor_methods[] = { |
---|
| 543 | {"residuals", (PyCFunction)residuals, METH_VARARGS, "Get the list of residuals"}, |
---|
[eca05c8] | 544 | {"pr_residuals", (PyCFunction)pr_residuals, METH_VARARGS, "Get the list of residuals"}, |
---|
[9e8dc22] | 545 | {"set_x", (PyCFunction)set_x, METH_VARARGS, ""}, |
---|
| 546 | {"get_x", (PyCFunction)get_x, METH_VARARGS, ""}, |
---|
| 547 | {"set_y", (PyCFunction)set_y, METH_VARARGS, ""}, |
---|
| 548 | {"get_y", (PyCFunction)get_y, METH_VARARGS, ""}, |
---|
| 549 | {"set_err", (PyCFunction)set_err, METH_VARARGS, ""}, |
---|
| 550 | {"get_err", (PyCFunction)get_err, METH_VARARGS, ""}, |
---|
| 551 | {"set_dmax", (PyCFunction)set_dmax, METH_VARARGS, ""}, |
---|
| 552 | {"get_dmax", (PyCFunction)get_dmax, METH_VARARGS, ""}, |
---|
[f71287f4] | 553 | {"set_qmin", (PyCFunction)set_qmin, METH_VARARGS, ""}, |
---|
| 554 | {"get_qmin", (PyCFunction)get_qmin, METH_VARARGS, ""}, |
---|
| 555 | {"set_qmax", (PyCFunction)set_qmax, METH_VARARGS, ""}, |
---|
| 556 | {"get_qmax", (PyCFunction)get_qmax, METH_VARARGS, ""}, |
---|
[eca05c8] | 557 | {"set_alpha", (PyCFunction)set_alpha, METH_VARARGS, ""}, |
---|
| 558 | {"get_alpha", (PyCFunction)get_alpha, METH_VARARGS, ""}, |
---|
[9e8dc22] | 559 | {"get_nx", (PyCFunction)get_nx, METH_VARARGS, ""}, |
---|
| 560 | {"get_ny", (PyCFunction)get_ny, METH_VARARGS, ""}, |
---|
| 561 | {"get_nerr", (PyCFunction)get_nerr, METH_VARARGS, ""}, |
---|
| 562 | {"iq", (PyCFunction)get_iq, METH_VARARGS, ""}, |
---|
| 563 | {"pr", (PyCFunction)get_pr, METH_VARARGS, ""}, |
---|
[eca05c8] | 564 | {"get_pr_err", (PyCFunction)get_pr_err, METH_VARARGS, ""}, |
---|
[9e8dc22] | 565 | {"is_valid", (PyCFunction)is_valid, METH_VARARGS, ""}, |
---|
[2d06beb] | 566 | {"basefunc_ft", (PyCFunction)basefunc_ft, METH_VARARGS, ""}, |
---|
[abad620] | 567 | {"oscillations", (PyCFunction)oscillations, METH_VARARGS, ""}, |
---|
[4f63160] | 568 | {"get_peaks", (PyCFunction)get_peaks, METH_VARARGS, ""}, |
---|
[9e8dc22] | 569 | |
---|
| 570 | {NULL} |
---|
| 571 | }; |
---|
| 572 | |
---|
| 573 | static PyTypeObject CinvertorType = { |
---|
| 574 | PyObject_HEAD_INIT(NULL) |
---|
| 575 | 0, /*ob_size*/ |
---|
| 576 | "Cinvertor", /*tp_name*/ |
---|
| 577 | sizeof(Cinvertor), /*tp_basicsize*/ |
---|
| 578 | 0, /*tp_itemsize*/ |
---|
| 579 | (destructor)Cinvertor_dealloc, /*tp_dealloc*/ |
---|
| 580 | 0, /*tp_print*/ |
---|
| 581 | 0, /*tp_getattr*/ |
---|
| 582 | 0, /*tp_setattr*/ |
---|
| 583 | 0, /*tp_compare*/ |
---|
| 584 | 0, /*tp_repr*/ |
---|
| 585 | 0, /*tp_as_number*/ |
---|
| 586 | 0, /*tp_as_sequence*/ |
---|
| 587 | 0, /*tp_as_mapping*/ |
---|
| 588 | 0, /*tp_hash */ |
---|
| 589 | 0, /*tp_call*/ |
---|
| 590 | 0, /*tp_str*/ |
---|
| 591 | 0, /*tp_getattro*/ |
---|
| 592 | 0, /*tp_setattro*/ |
---|
| 593 | 0, /*tp_as_buffer*/ |
---|
| 594 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 595 | "Cinvertor objects", /* tp_doc */ |
---|
| 596 | 0, /* tp_traverse */ |
---|
| 597 | 0, /* tp_clear */ |
---|
| 598 | 0, /* tp_richcompare */ |
---|
| 599 | 0, /* tp_weaklistoffset */ |
---|
| 600 | 0, /* tp_iter */ |
---|
| 601 | 0, /* tp_iternext */ |
---|
| 602 | Cinvertor_methods, /* tp_methods */ |
---|
| 603 | Cinvertor_members, /* tp_members */ |
---|
| 604 | 0, /* tp_getset */ |
---|
| 605 | 0, /* tp_base */ |
---|
| 606 | 0, /* tp_dict */ |
---|
| 607 | 0, /* tp_descr_get */ |
---|
| 608 | 0, /* tp_descr_set */ |
---|
| 609 | 0, /* tp_dictoffset */ |
---|
| 610 | (initproc)Cinvertor_init, /* tp_init */ |
---|
| 611 | 0, /* tp_alloc */ |
---|
| 612 | Cinvertor_new, /* tp_new */ |
---|
| 613 | }; |
---|
| 614 | |
---|
| 615 | |
---|
| 616 | static PyMethodDef module_methods[] = { |
---|
| 617 | {NULL} |
---|
| 618 | }; |
---|
| 619 | |
---|
| 620 | /** |
---|
| 621 | * Function used to add the model class to a module |
---|
| 622 | * @param module: module to add the class to |
---|
| 623 | */ |
---|
| 624 | void addCinvertor(PyObject *module) { |
---|
| 625 | PyObject *d; |
---|
| 626 | |
---|
| 627 | if (PyType_Ready(&CinvertorType) < 0) |
---|
| 628 | return; |
---|
| 629 | |
---|
| 630 | Py_INCREF(&CinvertorType); |
---|
| 631 | PyModule_AddObject(module, "Cinvertor", (PyObject *)&CinvertorType); |
---|
| 632 | |
---|
| 633 | d = PyModule_GetDict(module); |
---|
| 634 | CinvertorError = PyErr_NewException("Cinvertor.error", NULL, NULL); |
---|
| 635 | PyDict_SetItemString(d, "CinvertorError", CinvertorError); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | |
---|
| 639 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
| 640 | #define PyMODINIT_FUNC void |
---|
| 641 | #endif |
---|
| 642 | PyMODINIT_FUNC |
---|
| 643 | initpr_inversion(void) |
---|
| 644 | { |
---|
| 645 | PyObject* m; |
---|
| 646 | |
---|
| 647 | m = Py_InitModule3("pr_inversion", module_methods, |
---|
| 648 | "C extension module for inversion to P(r)."); |
---|
| 649 | |
---|
| 650 | addCinvertor(m); |
---|
| 651 | } |
---|