[896abb3] | 1 | /** |
---|
| 2 | * C implementation of the P(r) inversion |
---|
| 3 | * Cinvertor is the base class for the Invertor class |
---|
| 4 | * and provides the underlying computations. |
---|
[9a23253e] | 5 | * |
---|
[896abb3] | 6 | */ |
---|
[9e8dc22] | 7 | #include <Python.h> |
---|
| 8 | #include "structmember.h" |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <stdlib.h> |
---|
| 11 | #include <math.h> |
---|
| 12 | #include <time.h> |
---|
| 13 | |
---|
| 14 | #include "invertor.h" |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /// Error object for raised exceptions |
---|
| 18 | static PyObject * CinvertorError = NULL; |
---|
| 19 | |
---|
| 20 | #define INVECTOR(obj,buf,len) \ |
---|
| 21 | do { \ |
---|
| 22 | int err = PyObject_AsReadBuffer(obj, (const void **)(&buf), &len); \ |
---|
| 23 | if (err < 0) return NULL; \ |
---|
| 24 | len /= sizeof(*buf); \ |
---|
| 25 | } while (0) |
---|
[bd30f4a5] | 26 | |
---|
[9e8dc22] | 27 | #define OUTVECTOR(obj,buf,len) \ |
---|
| 28 | do { \ |
---|
| 29 | int err = PyObject_AsWriteBuffer(obj, (void **)(&buf), &len); \ |
---|
| 30 | if (err < 0) return NULL; \ |
---|
| 31 | len /= sizeof(*buf); \ |
---|
| 32 | } while (0) |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | // Class definition |
---|
[896abb3] | 36 | /** |
---|
| 37 | * C implementation of the P(r) inversion |
---|
| 38 | * Cinvertor is the base class for the Invertor class |
---|
| 39 | * and provides the underlying computations. |
---|
[9a23253e] | 40 | * |
---|
[896abb3] | 41 | */ |
---|
[9e8dc22] | 42 | typedef struct { |
---|
[9a23253e] | 43 | PyObject_HEAD |
---|
[896abb3] | 44 | /// Internal data structure |
---|
[9a23253e] | 45 | Invertor_params params; |
---|
[9e8dc22] | 46 | } Cinvertor; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | static void |
---|
| 50 | Cinvertor_dealloc(Cinvertor* self) |
---|
| 51 | { |
---|
| 52 | invertor_dealloc(&(self->params)); |
---|
[9a23253e] | 53 | |
---|
[9e8dc22] | 54 | self->ob_type->tp_free((PyObject*)self); |
---|
| 55 | |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | static PyObject * |
---|
| 59 | Cinvertor_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
---|
| 60 | { |
---|
| 61 | Cinvertor *self; |
---|
[9a23253e] | 62 | |
---|
[9e8dc22] | 63 | self = (Cinvertor *)type->tp_alloc(type, 0); |
---|
[9a23253e] | 64 | |
---|
[9e8dc22] | 65 | return (PyObject *)self; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | static int |
---|
| 69 | Cinvertor_init(Cinvertor *self, PyObject *args, PyObject *kwds) |
---|
| 70 | { |
---|
[9a23253e] | 71 | if (self != NULL) { |
---|
[9e8dc22] | 72 | // Create parameters |
---|
| 73 | invertor_init(&(self->params)); |
---|
| 74 | } |
---|
| 75 | return 0; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | static PyMemberDef Cinvertor_members[] = { |
---|
| 79 | //{"params", T_OBJECT, offsetof(Cinvertor, params), 0, |
---|
| 80 | // "Parameters"}, |
---|
| 81 | {NULL} /* Sentinel */ |
---|
| 82 | }; |
---|
| 83 | |
---|
[9a23253e] | 84 | const char set_x_doc[] = |
---|
[896abb3] | 85 | "Function to set the x data\n" |
---|
| 86 | "Takes an array of doubles as input.\n" |
---|
| 87 | " @return: number of entries found"; |
---|
[9e8dc22] | 88 | |
---|
| 89 | /** |
---|
| 90 | * Function to set the x data |
---|
| 91 | * Takes an array of doubles as input |
---|
| 92 | * Returns the number of entries found |
---|
| 93 | */ |
---|
| 94 | static PyObject * set_x(Cinvertor *self, PyObject *args) { |
---|
| 95 | PyObject *data_obj; |
---|
| 96 | Py_ssize_t ndata; |
---|
| 97 | double *data; |
---|
[2d06beb] | 98 | int i; |
---|
[9a23253e] | 99 | |
---|
[9e8dc22] | 100 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 101 | OUTVECTOR(data_obj,data,ndata); |
---|
[9a23253e] | 102 | |
---|
[2d06beb] | 103 | free(self->params.x); |
---|
| 104 | self->params.x = (double*) malloc(ndata*sizeof(double)); |
---|
[9a23253e] | 105 | |
---|
[2d06beb] | 106 | if(self->params.x==NULL) { |
---|
[9a23253e] | 107 | PyErr_SetString(CinvertorError, |
---|
[2d06beb] | 108 | "Cinvertor.set_x: problem allocating memory."); |
---|
[9a23253e] | 109 | return NULL; |
---|
[2d06beb] | 110 | } |
---|
[9a23253e] | 111 | |
---|
[2d06beb] | 112 | for (i=0; i<ndata; i++) { |
---|
| 113 | self->params.x[i] = data[i]; |
---|
| 114 | } |
---|
[9a23253e] | 115 | |
---|
[2d06beb] | 116 | //self->params.x = data; |
---|
[9e8dc22] | 117 | self->params.npoints = ndata; |
---|
[9a23253e] | 118 | return Py_BuildValue("i", self->params.npoints); |
---|
[9e8dc22] | 119 | } |
---|
| 120 | |
---|
[9a23253e] | 121 | const char get_x_doc[] = |
---|
[896abb3] | 122 | "Function to get the x data\n" |
---|
| 123 | "Takes an array of doubles as input.\n" |
---|
| 124 | " @return: number of entries found"; |
---|
| 125 | |
---|
[9e8dc22] | 126 | static PyObject * get_x(Cinvertor *self, PyObject *args) { |
---|
| 127 | PyObject *data_obj; |
---|
| 128 | Py_ssize_t ndata; |
---|
| 129 | double *data; |
---|
| 130 | int i; |
---|
[9a23253e] | 131 | |
---|
[9e8dc22] | 132 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 133 | OUTVECTOR(data_obj, data, ndata); |
---|
[9a23253e] | 134 | |
---|
[9e8dc22] | 135 | // Check that the input array is large enough |
---|
| 136 | if (ndata < self->params.npoints) { |
---|
[9a23253e] | 137 | PyErr_SetString(CinvertorError, |
---|
[9e8dc22] | 138 | "Cinvertor.get_x: input array too short for data."); |
---|
[9a23253e] | 139 | return NULL; |
---|
[9e8dc22] | 140 | } |
---|
[9a23253e] | 141 | |
---|
[9e8dc22] | 142 | for(i=0; i<self->params.npoints; i++){ |
---|
| 143 | data[i] = self->params.x[i]; |
---|
| 144 | } |
---|
[9a23253e] | 145 | |
---|
| 146 | return Py_BuildValue("i", self->params.npoints); |
---|
[9e8dc22] | 147 | } |
---|
| 148 | |
---|
[9a23253e] | 149 | const char set_y_doc[] = |
---|
[896abb3] | 150 | "Function to set the y data\n" |
---|
| 151 | "Takes an array of doubles as input.\n" |
---|
| 152 | " @return: number of entries found"; |
---|
| 153 | |
---|
[9e8dc22] | 154 | /** |
---|
| 155 | * Function to set the y data |
---|
| 156 | * Takes an array of doubles as input |
---|
| 157 | * Returns the number of entries found |
---|
| 158 | */ |
---|
| 159 | static PyObject * set_y(Cinvertor *self, PyObject *args) { |
---|
| 160 | PyObject *data_obj; |
---|
| 161 | Py_ssize_t ndata; |
---|
| 162 | double *data; |
---|
[2d06beb] | 163 | int i; |
---|
[9a23253e] | 164 | |
---|
[9e8dc22] | 165 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 166 | OUTVECTOR(data_obj,data,ndata); |
---|
[9a23253e] | 167 | |
---|
[2d06beb] | 168 | free(self->params.y); |
---|
| 169 | self->params.y = (double*) malloc(ndata*sizeof(double)); |
---|
[9a23253e] | 170 | |
---|
[2d06beb] | 171 | if(self->params.y==NULL) { |
---|
[9a23253e] | 172 | PyErr_SetString(CinvertorError, |
---|
[2d06beb] | 173 | "Cinvertor.set_y: problem allocating memory."); |
---|
[9a23253e] | 174 | return NULL; |
---|
[2d06beb] | 175 | } |
---|
[9a23253e] | 176 | |
---|
[2d06beb] | 177 | for (i=0; i<ndata; i++) { |
---|
| 178 | self->params.y[i] = data[i]; |
---|
[9a23253e] | 179 | } |
---|
| 180 | |
---|
[2d06beb] | 181 | //self->params.y = data; |
---|
[9e8dc22] | 182 | self->params.ny = ndata; |
---|
[9a23253e] | 183 | return Py_BuildValue("i", self->params.ny); |
---|
[9e8dc22] | 184 | } |
---|
| 185 | |
---|
[9a23253e] | 186 | const char get_y_doc[] = |
---|
[896abb3] | 187 | "Function to get the y data\n" |
---|
| 188 | "Takes an array of doubles as input.\n" |
---|
| 189 | " @return: number of entries found"; |
---|
| 190 | |
---|
[9e8dc22] | 191 | static PyObject * get_y(Cinvertor *self, PyObject *args) { |
---|
| 192 | PyObject *data_obj; |
---|
| 193 | Py_ssize_t ndata; |
---|
| 194 | double *data; |
---|
| 195 | int i; |
---|
[9a23253e] | 196 | |
---|
[9e8dc22] | 197 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 198 | OUTVECTOR(data_obj, data, ndata); |
---|
[9a23253e] | 199 | |
---|
[9e8dc22] | 200 | // Check that the input array is large enough |
---|
| 201 | if (ndata < self->params.ny) { |
---|
[9a23253e] | 202 | PyErr_SetString(CinvertorError, |
---|
[9e8dc22] | 203 | "Cinvertor.get_y: input array too short for data."); |
---|
[9a23253e] | 204 | return NULL; |
---|
[9e8dc22] | 205 | } |
---|
[9a23253e] | 206 | |
---|
[9e8dc22] | 207 | for(i=0; i<self->params.ny; i++){ |
---|
| 208 | data[i] = self->params.y[i]; |
---|
| 209 | } |
---|
[9a23253e] | 210 | |
---|
| 211 | return Py_BuildValue("i", self->params.npoints); |
---|
[9e8dc22] | 212 | } |
---|
| 213 | |
---|
[9a23253e] | 214 | const char set_err_doc[] = |
---|
[896abb3] | 215 | "Function to set the err data\n" |
---|
| 216 | "Takes an array of doubles as input.\n" |
---|
| 217 | " @return: number of entries found"; |
---|
| 218 | |
---|
[9e8dc22] | 219 | /** |
---|
| 220 | * Function to set the x data |
---|
| 221 | * Takes an array of doubles as input |
---|
| 222 | * Returns the number of entries found |
---|
| 223 | */ |
---|
| 224 | static PyObject * set_err(Cinvertor *self, PyObject *args) { |
---|
| 225 | PyObject *data_obj; |
---|
| 226 | Py_ssize_t ndata; |
---|
| 227 | double *data; |
---|
[2d06beb] | 228 | int i; |
---|
[9a23253e] | 229 | |
---|
[9e8dc22] | 230 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 231 | OUTVECTOR(data_obj,data,ndata); |
---|
[9a23253e] | 232 | |
---|
[2d06beb] | 233 | free(self->params.err); |
---|
| 234 | self->params.err = (double*) malloc(ndata*sizeof(double)); |
---|
[9a23253e] | 235 | |
---|
[2d06beb] | 236 | if(self->params.err==NULL) { |
---|
[9a23253e] | 237 | PyErr_SetString(CinvertorError, |
---|
[2d06beb] | 238 | "Cinvertor.set_err: problem allocating memory."); |
---|
[9a23253e] | 239 | return NULL; |
---|
[2d06beb] | 240 | } |
---|
[9a23253e] | 241 | |
---|
[2d06beb] | 242 | for (i=0; i<ndata; i++) { |
---|
| 243 | self->params.err[i] = data[i]; |
---|
| 244 | } |
---|
[9a23253e] | 245 | |
---|
[2d06beb] | 246 | //self->params.err = data; |
---|
[9e8dc22] | 247 | self->params.nerr = ndata; |
---|
[9a23253e] | 248 | return Py_BuildValue("i", self->params.nerr); |
---|
[9e8dc22] | 249 | } |
---|
| 250 | |
---|
[9a23253e] | 251 | const char get_err_doc[] = |
---|
[896abb3] | 252 | "Function to get the err data\n" |
---|
| 253 | "Takes an array of doubles as input.\n" |
---|
| 254 | " @return: number of entries found"; |
---|
| 255 | |
---|
[9e8dc22] | 256 | static PyObject * get_err(Cinvertor *self, PyObject *args) { |
---|
| 257 | PyObject *data_obj; |
---|
| 258 | Py_ssize_t ndata; |
---|
| 259 | double *data; |
---|
| 260 | int i; |
---|
[9a23253e] | 261 | |
---|
[9e8dc22] | 262 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[2d06beb] | 263 | OUTVECTOR(data_obj, data, ndata); |
---|
[9a23253e] | 264 | |
---|
[9e8dc22] | 265 | // Check that the input array is large enough |
---|
| 266 | if (ndata < self->params.nerr) { |
---|
[9a23253e] | 267 | PyErr_SetString(CinvertorError, |
---|
[9e8dc22] | 268 | "Cinvertor.get_err: input array too short for data."); |
---|
[9a23253e] | 269 | return NULL; |
---|
[9e8dc22] | 270 | } |
---|
[9a23253e] | 271 | |
---|
[9e8dc22] | 272 | for(i=0; i<self->params.nerr; i++){ |
---|
| 273 | data[i] = self->params.err[i]; |
---|
| 274 | } |
---|
[9a23253e] | 275 | |
---|
| 276 | return Py_BuildValue("i", self->params.npoints); |
---|
[9e8dc22] | 277 | } |
---|
| 278 | |
---|
[9a23253e] | 279 | const char is_valid_doc[] = |
---|
[896abb3] | 280 | "Check the validity of the stored data\n" |
---|
| 281 | " @return: Returns the number of points if it's all good, -1 otherwise"; |
---|
| 282 | |
---|
[9e8dc22] | 283 | /** |
---|
| 284 | * Check the validity of the stored data |
---|
| 285 | * Returns the number of points if it's all good, -1 otherwise |
---|
| 286 | */ |
---|
| 287 | static PyObject * is_valid(Cinvertor *self, PyObject *args) { |
---|
| 288 | if(self->params.npoints==self->params.ny && |
---|
| 289 | self->params.npoints==self->params.nerr) { |
---|
| 290 | return Py_BuildValue("i", self->params.npoints); |
---|
| 291 | } else { |
---|
| 292 | return Py_BuildValue("i", -1); |
---|
[9a23253e] | 293 | } |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | const char set_has_bck_doc[] = |
---|
| 297 | "Sets background flag\n"; |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Sets the maximum distance |
---|
| 301 | */ |
---|
| 302 | static PyObject * set_has_bck(Cinvertor *self, PyObject *args) { |
---|
| 303 | int has_bck; |
---|
| 304 | |
---|
| 305 | if (!PyArg_ParseTuple(args, "i", &has_bck)) return NULL; |
---|
| 306 | self->params.has_bck = has_bck; |
---|
| 307 | return Py_BuildValue("i", self->params.has_bck); |
---|
[9e8dc22] | 308 | } |
---|
| 309 | |
---|
[9a23253e] | 310 | const char get_has_bck_doc[] = |
---|
| 311 | "Gets background flag\n"; |
---|
| 312 | |
---|
| 313 | /** |
---|
| 314 | * Gets the maximum distance |
---|
| 315 | */ |
---|
| 316 | static PyObject * get_has_bck(Cinvertor *self, PyObject *args) { |
---|
| 317 | return Py_BuildValue("i", self->params.has_bck); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | const char set_dmax_doc[] = |
---|
[896abb3] | 321 | "Sets the maximum distance\n"; |
---|
| 322 | |
---|
[9e8dc22] | 323 | /** |
---|
| 324 | * Sets the maximum distance |
---|
| 325 | */ |
---|
| 326 | static PyObject * set_dmax(Cinvertor *self, PyObject *args) { |
---|
| 327 | double d_max; |
---|
[9a23253e] | 328 | |
---|
[9e8dc22] | 329 | if (!PyArg_ParseTuple(args, "d", &d_max)) return NULL; |
---|
| 330 | self->params.d_max = d_max; |
---|
[9a23253e] | 331 | return Py_BuildValue("d", self->params.d_max); |
---|
[9e8dc22] | 332 | } |
---|
| 333 | |
---|
[9a23253e] | 334 | const char get_dmax_doc[] = |
---|
[896abb3] | 335 | "Gets the maximum distance\n"; |
---|
| 336 | |
---|
[9e8dc22] | 337 | /** |
---|
| 338 | * Gets the maximum distance |
---|
| 339 | */ |
---|
| 340 | static PyObject * get_dmax(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 341 | return Py_BuildValue("d", self->params.d_max); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | const char set_slit_height_doc[] = |
---|
| 345 | "Sets the slit height in units of q [A-1]\n"; |
---|
| 346 | |
---|
| 347 | /** |
---|
| 348 | * Sets the slit height |
---|
| 349 | */ |
---|
| 350 | static PyObject * set_slit_height(Cinvertor *self, PyObject *args) { |
---|
| 351 | double slit_height; |
---|
| 352 | |
---|
| 353 | if (!PyArg_ParseTuple(args, "d", &slit_height)) return NULL; |
---|
| 354 | self->params.slit_height = slit_height; |
---|
| 355 | return Py_BuildValue("d", self->params.slit_height); |
---|
[9e8dc22] | 356 | } |
---|
| 357 | |
---|
[9a23253e] | 358 | const char get_slit_height_doc[] = |
---|
| 359 | "Gets the slit height\n"; |
---|
| 360 | |
---|
| 361 | /** |
---|
| 362 | * Gets the slit height |
---|
| 363 | */ |
---|
| 364 | static PyObject * get_slit_height(Cinvertor *self, PyObject *args) { |
---|
| 365 | return Py_BuildValue("d", self->params.slit_height); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | const char set_slit_width_doc[] = |
---|
| 369 | "Sets the slit width in units of q [A-1]\n"; |
---|
| 370 | |
---|
| 371 | /** |
---|
| 372 | * Sets the slit width |
---|
| 373 | */ |
---|
| 374 | static PyObject * set_slit_width(Cinvertor *self, PyObject *args) { |
---|
| 375 | double slit_width; |
---|
| 376 | |
---|
| 377 | if (!PyArg_ParseTuple(args, "d", &slit_width)) return NULL; |
---|
| 378 | self->params.slit_width = slit_width; |
---|
| 379 | return Py_BuildValue("d", self->params.slit_width); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | const char get_slit_width_doc[] = |
---|
| 383 | "Gets the slit width\n"; |
---|
| 384 | |
---|
| 385 | /** |
---|
| 386 | * Gets the slit width |
---|
| 387 | */ |
---|
| 388 | static PyObject * get_slit_width(Cinvertor *self, PyObject *args) { |
---|
| 389 | return Py_BuildValue("d", self->params.slit_width); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | |
---|
| 393 | const char set_qmin_doc[] = |
---|
[896abb3] | 394 | "Sets the minimum q\n"; |
---|
| 395 | |
---|
[f71287f4] | 396 | /** |
---|
| 397 | * Sets the minimum q |
---|
| 398 | */ |
---|
| 399 | static PyObject * set_qmin(Cinvertor *self, PyObject *args) { |
---|
| 400 | double q_min; |
---|
[9a23253e] | 401 | |
---|
[f71287f4] | 402 | if (!PyArg_ParseTuple(args, "d", &q_min)) return NULL; |
---|
| 403 | self->params.q_min = q_min; |
---|
[9a23253e] | 404 | return Py_BuildValue("d", self->params.q_min); |
---|
[f71287f4] | 405 | } |
---|
| 406 | |
---|
[9a23253e] | 407 | const char get_qmin_doc[] = |
---|
[896abb3] | 408 | "Gets the minimum q\n"; |
---|
| 409 | |
---|
[f71287f4] | 410 | /** |
---|
| 411 | * Gets the minimum q |
---|
| 412 | */ |
---|
| 413 | static PyObject * get_qmin(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 414 | return Py_BuildValue("d", self->params.q_min); |
---|
[f71287f4] | 415 | } |
---|
| 416 | |
---|
[9a23253e] | 417 | const char set_qmax_doc[] = |
---|
[896abb3] | 418 | "Sets the maximum q\n"; |
---|
[f71287f4] | 419 | |
---|
| 420 | /** |
---|
| 421 | * Sets the maximum q |
---|
| 422 | */ |
---|
| 423 | static PyObject * set_qmax(Cinvertor *self, PyObject *args) { |
---|
| 424 | double q_max; |
---|
[9a23253e] | 425 | |
---|
[f71287f4] | 426 | if (!PyArg_ParseTuple(args, "d", &q_max)) return NULL; |
---|
| 427 | self->params.q_max = q_max; |
---|
[9a23253e] | 428 | return Py_BuildValue("d", self->params.q_max); |
---|
[f71287f4] | 429 | } |
---|
| 430 | |
---|
[9a23253e] | 431 | const char get_qmax_doc[] = |
---|
[896abb3] | 432 | "Gets the maximum q\n"; |
---|
| 433 | |
---|
[f71287f4] | 434 | /** |
---|
| 435 | * Gets the maximum q |
---|
| 436 | */ |
---|
| 437 | static PyObject * get_qmax(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 438 | return Py_BuildValue("d", self->params.q_max); |
---|
[f71287f4] | 439 | } |
---|
| 440 | |
---|
[9a23253e] | 441 | const char set_alpha_doc[] = |
---|
[896abb3] | 442 | "Sets the alpha parameter\n"; |
---|
[f71287f4] | 443 | |
---|
[eca05c8] | 444 | static PyObject * set_alpha(Cinvertor *self, PyObject *args) { |
---|
| 445 | double alpha; |
---|
[9a23253e] | 446 | |
---|
[eca05c8] | 447 | if (!PyArg_ParseTuple(args, "d", &alpha)) return NULL; |
---|
| 448 | self->params.alpha = alpha; |
---|
[9a23253e] | 449 | return Py_BuildValue("d", self->params.alpha); |
---|
[eca05c8] | 450 | } |
---|
| 451 | |
---|
[9a23253e] | 452 | const char get_alpha_doc[] = |
---|
[896abb3] | 453 | "Gets the alpha parameter\n"; |
---|
| 454 | |
---|
[eca05c8] | 455 | /** |
---|
| 456 | * Gets the maximum distance |
---|
| 457 | */ |
---|
| 458 | static PyObject * get_alpha(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 459 | return Py_BuildValue("d", self->params.alpha); |
---|
[eca05c8] | 460 | } |
---|
| 461 | |
---|
[9a23253e] | 462 | const char get_nx_doc[] = |
---|
[896abb3] | 463 | "Gets the number of x points\n"; |
---|
| 464 | |
---|
[9e8dc22] | 465 | /** |
---|
| 466 | * Gets the number of x points |
---|
| 467 | */ |
---|
| 468 | static PyObject * get_nx(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 469 | return Py_BuildValue("i", self->params.npoints); |
---|
[9e8dc22] | 470 | } |
---|
| 471 | |
---|
[9a23253e] | 472 | const char get_ny_doc[] = |
---|
[896abb3] | 473 | "Gets the number of y points\n"; |
---|
| 474 | |
---|
[9e8dc22] | 475 | /** |
---|
| 476 | * Gets the number of y points |
---|
| 477 | */ |
---|
| 478 | static PyObject * get_ny(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 479 | return Py_BuildValue("i", self->params.ny); |
---|
[9e8dc22] | 480 | } |
---|
| 481 | |
---|
[9a23253e] | 482 | const char get_nerr_doc[] = |
---|
[896abb3] | 483 | "Gets the number of err points\n"; |
---|
| 484 | |
---|
[9e8dc22] | 485 | /** |
---|
| 486 | * Gets the number of error points |
---|
| 487 | */ |
---|
| 488 | static PyObject * get_nerr(Cinvertor *self, PyObject *args) { |
---|
[9a23253e] | 489 | return Py_BuildValue("i", self->params.nerr); |
---|
[9e8dc22] | 490 | } |
---|
| 491 | |
---|
| 492 | |
---|
[9a23253e] | 493 | const char residuals_doc[] = |
---|
[896abb3] | 494 | "Function to call to evaluate the residuals\n" |
---|
| 495 | "for P(r) inversion\n" |
---|
| 496 | " @param args: input parameters\n" |
---|
| 497 | " @return: list of residuals"; |
---|
| 498 | |
---|
[9e8dc22] | 499 | /** |
---|
| 500 | * Function to call to evaluate the residuals |
---|
[eca05c8] | 501 | * @param args: input parameters |
---|
| 502 | * @return: list of residuals |
---|
[9e8dc22] | 503 | */ |
---|
| 504 | static PyObject * residuals(Cinvertor *self, PyObject *args) { |
---|
| 505 | double *pars; |
---|
| 506 | PyObject* residuals; |
---|
| 507 | PyObject* temp; |
---|
| 508 | double *res; |
---|
| 509 | int i; |
---|
| 510 | double residual, diff; |
---|
| 511 | // Regularization factor |
---|
[eca05c8] | 512 | double regterm = 0.0; |
---|
| 513 | double tmp = 0.0; |
---|
[abad620] | 514 | // Number of slices in regularization term estimate |
---|
| 515 | int nslice = 25; |
---|
[9a23253e] | 516 | |
---|
[9e8dc22] | 517 | PyObject *data_obj; |
---|
| 518 | Py_ssize_t npars; |
---|
[9a23253e] | 519 | |
---|
[9e8dc22] | 520 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[9a23253e] | 521 | |
---|
[9e8dc22] | 522 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 523 | |
---|
[9e8dc22] | 524 | // PyList of residuals |
---|
| 525 | // Should create this list only once and refill it |
---|
| 526 | residuals = PyList_New(self->params.npoints); |
---|
[eca05c8] | 527 | |
---|
[abad620] | 528 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[9a23253e] | 529 | |
---|
[9e8dc22] | 530 | for(i=0; i<self->params.npoints; i++) { |
---|
| 531 | diff = self->params.y[i] - iq(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 532 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
[eca05c8] | 533 | tmp = residual; |
---|
[9a23253e] | 534 | |
---|
[eca05c8] | 535 | // regularization term |
---|
| 536 | residual += self->params.alpha * regterm; |
---|
[9a23253e] | 537 | |
---|
[eca05c8] | 538 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
[9a23253e] | 539 | PyErr_SetString(CinvertorError, |
---|
[eca05c8] | 540 | "Cinvertor.residuals: error setting residual."); |
---|
| 541 | return NULL; |
---|
| 542 | }; |
---|
[9a23253e] | 543 | |
---|
[eca05c8] | 544 | } |
---|
[9a23253e] | 545 | |
---|
[eca05c8] | 546 | return residuals; |
---|
| 547 | } |
---|
[896abb3] | 548 | |
---|
[9a23253e] | 549 | const char pr_residuals_doc[] = |
---|
[896abb3] | 550 | "Function to call to evaluate the residuals\n" |
---|
| 551 | "for P(r) minimization (for testing purposes)\n" |
---|
| 552 | " @param args: input parameters\n" |
---|
| 553 | " @return: list of residuals"; |
---|
| 554 | |
---|
[eca05c8] | 555 | /** |
---|
| 556 | * Function to call to evaluate the residuals |
---|
| 557 | * for P(r) minimization (for testing purposes) |
---|
| 558 | * @param args: input parameters |
---|
| 559 | * @return: list of residuals |
---|
| 560 | */ |
---|
| 561 | static PyObject * pr_residuals(Cinvertor *self, PyObject *args) { |
---|
| 562 | double *pars; |
---|
| 563 | PyObject* residuals; |
---|
| 564 | PyObject* temp; |
---|
| 565 | double *res; |
---|
| 566 | int i; |
---|
| 567 | double residual, diff; |
---|
| 568 | // Regularization factor |
---|
| 569 | double regterm = 0.0; |
---|
| 570 | double tmp = 0.0; |
---|
[abad620] | 571 | // Number of slices in regularization term estimate |
---|
| 572 | int nslice = 25; |
---|
[9a23253e] | 573 | |
---|
[eca05c8] | 574 | PyObject *data_obj; |
---|
| 575 | Py_ssize_t npars; |
---|
[9a23253e] | 576 | |
---|
[eca05c8] | 577 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
[9a23253e] | 578 | |
---|
[eca05c8] | 579 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 580 | |
---|
[eca05c8] | 581 | // Should create this list only once and refill it |
---|
| 582 | residuals = PyList_New(self->params.npoints); |
---|
| 583 | |
---|
[abad620] | 584 | regterm = reg_term(pars, self->params.d_max, npars, nslice); |
---|
[eca05c8] | 585 | |
---|
[9a23253e] | 586 | |
---|
[eca05c8] | 587 | for(i=0; i<self->params.npoints; i++) { |
---|
| 588 | diff = self->params.y[i] - pr(pars, self->params.d_max, npars, self->params.x[i]); |
---|
| 589 | residual = diff*diff / (self->params.err[i]*self->params.err[i]); |
---|
| 590 | tmp = residual; |
---|
[9a23253e] | 591 | |
---|
[eca05c8] | 592 | // regularization term |
---|
| 593 | residual += self->params.alpha * regterm; |
---|
[9a23253e] | 594 | |
---|
[9e8dc22] | 595 | if (PyList_SetItem(residuals, i, Py_BuildValue("d",residual) ) < 0){ |
---|
[9a23253e] | 596 | PyErr_SetString(CinvertorError, |
---|
[9e8dc22] | 597 | "Cinvertor.residuals: error setting residual."); |
---|
| 598 | return NULL; |
---|
| 599 | }; |
---|
[9a23253e] | 600 | |
---|
[9e8dc22] | 601 | } |
---|
[9a23253e] | 602 | |
---|
[9e8dc22] | 603 | return residuals; |
---|
| 604 | } |
---|
| 605 | |
---|
[9a23253e] | 606 | const char get_iq_doc[] = |
---|
[896abb3] | 607 | "Function to call to evaluate the scattering intensity\n" |
---|
| 608 | " @param args: c-parameters, and q\n" |
---|
| 609 | " @return: I(q)"; |
---|
| 610 | |
---|
[9e8dc22] | 611 | /** |
---|
| 612 | * Function to call to evaluate the scattering intensity |
---|
| 613 | * @param args: c-parameters, and q |
---|
| 614 | * @return: I(q) |
---|
| 615 | */ |
---|
| 616 | static PyObject * get_iq(Cinvertor *self, PyObject *args) { |
---|
| 617 | double *pars; |
---|
| 618 | double q, iq_value; |
---|
| 619 | PyObject *data_obj; |
---|
| 620 | Py_ssize_t npars; |
---|
[9a23253e] | 621 | |
---|
[9e8dc22] | 622 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
| 623 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 624 | |
---|
[9e8dc22] | 625 | iq_value = iq(pars, self->params.d_max, npars, q); |
---|
[9a23253e] | 626 | return Py_BuildValue("f", iq_value); |
---|
[9e8dc22] | 627 | } |
---|
| 628 | |
---|
[f168d02] | 629 | const char get_iq_smeared_doc[] = |
---|
| 630 | "Function to call to evaluate the scattering intensity.\n" |
---|
| 631 | "The scattering intensity is slit-smeared." |
---|
| 632 | " @param args: c-parameters, and q\n" |
---|
| 633 | " @return: I(q)"; |
---|
| 634 | |
---|
| 635 | /** |
---|
| 636 | * Function to call to evaluate the scattering intensity |
---|
| 637 | * The scattering intensity is slit-smeared. |
---|
| 638 | * @param args: c-parameters, and q |
---|
| 639 | * @return: I(q) |
---|
| 640 | */ |
---|
| 641 | static PyObject * get_iq_smeared(Cinvertor *self, PyObject *args) { |
---|
| 642 | double *pars; |
---|
| 643 | double q, iq_value; |
---|
| 644 | PyObject *data_obj; |
---|
| 645 | Py_ssize_t npars; |
---|
| 646 | |
---|
| 647 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &q)) return NULL; |
---|
| 648 | OUTVECTOR(data_obj,pars,npars); |
---|
| 649 | |
---|
| 650 | iq_value = iq_smeared(pars, self->params.d_max, npars, |
---|
| 651 | self->params.slit_height, self->params.slit_width, |
---|
| 652 | q, 21); |
---|
| 653 | return Py_BuildValue("f", iq_value); |
---|
| 654 | } |
---|
| 655 | |
---|
[9a23253e] | 656 | const char get_pr_doc[] = |
---|
[896abb3] | 657 | "Function to call to evaluate P(r)\n" |
---|
| 658 | " @param args: c-parameters and r\n" |
---|
| 659 | " @return: P(r)"; |
---|
| 660 | |
---|
[9e8dc22] | 661 | /** |
---|
| 662 | * Function to call to evaluate P(r) |
---|
| 663 | * @param args: c-parameters and r |
---|
| 664 | * @return: P(r) |
---|
| 665 | */ |
---|
| 666 | static PyObject * get_pr(Cinvertor *self, PyObject *args) { |
---|
| 667 | double *pars; |
---|
| 668 | double r, pr_value; |
---|
| 669 | PyObject *data_obj; |
---|
| 670 | Py_ssize_t npars; |
---|
[9a23253e] | 671 | |
---|
[9e8dc22] | 672 | if (!PyArg_ParseTuple(args, "Od", &data_obj, &r)) return NULL; |
---|
| 673 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 674 | |
---|
[9e8dc22] | 675 | pr_value = pr(pars, self->params.d_max, npars, r); |
---|
[9a23253e] | 676 | return Py_BuildValue("f", pr_value); |
---|
[9e8dc22] | 677 | } |
---|
| 678 | |
---|
[9a23253e] | 679 | const char get_pr_err_doc[] = |
---|
[896abb3] | 680 | "Function to call to evaluate P(r) with errors\n" |
---|
| 681 | " @param args: c-parameters and r\n" |
---|
| 682 | " @return: (P(r),dP(r))"; |
---|
| 683 | |
---|
[eca05c8] | 684 | /** |
---|
| 685 | * Function to call to evaluate P(r) with errors |
---|
| 686 | * @param args: c-parameters and r |
---|
| 687 | * @return: P(r) |
---|
| 688 | */ |
---|
| 689 | static PyObject * get_pr_err(Cinvertor *self, PyObject *args) { |
---|
| 690 | double *pars; |
---|
| 691 | double *pars_err; |
---|
| 692 | double pr_err_value; |
---|
| 693 | double r, pr_value; |
---|
| 694 | PyObject *data_obj; |
---|
| 695 | Py_ssize_t npars; |
---|
| 696 | PyObject *err_obj; |
---|
| 697 | Py_ssize_t npars2; |
---|
[9a23253e] | 698 | int i; |
---|
| 699 | |
---|
[eca05c8] | 700 | if (!PyArg_ParseTuple(args, "OOd", &data_obj, &err_obj, &r)) return NULL; |
---|
[9a23253e] | 701 | OUTVECTOR(data_obj,pars,npars); |
---|
[eca05c8] | 702 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
[43c0a8e] | 703 | |
---|
[eca05c8] | 704 | pr_err(pars, pars_err, self->params.d_max, npars, r, &pr_value, &pr_err_value); |
---|
[9a23253e] | 705 | return Py_BuildValue("ff", pr_value, pr_err_value); |
---|
[eca05c8] | 706 | } |
---|
| 707 | |
---|
[9a23253e] | 708 | const char basefunc_ft_doc[] = |
---|
[896abb3] | 709 | "Returns the value of the nth Fourier transofrmed base function\n" |
---|
| 710 | " @param args: c-parameters, n and q\n" |
---|
| 711 | " @return: nth Fourier transformed base function, evaluated at q"; |
---|
| 712 | |
---|
[2d06beb] | 713 | static PyObject * basefunc_ft(Cinvertor *self, PyObject *args) { |
---|
| 714 | double d_max, q; |
---|
| 715 | int n; |
---|
[9a23253e] | 716 | |
---|
[2d06beb] | 717 | if (!PyArg_ParseTuple(args, "did", &d_max, &n, &q)) return NULL; |
---|
[9a23253e] | 718 | return Py_BuildValue("f", ortho_transformed(d_max, n, q)); |
---|
| 719 | |
---|
[2d06beb] | 720 | } |
---|
[9e8dc22] | 721 | |
---|
[9a23253e] | 722 | const char oscillations_doc[] = |
---|
[896abb3] | 723 | "Returns the value of the oscillation figure of merit for\n" |
---|
| 724 | "the given set of coefficients. For a sphere, the oscillation\n" |
---|
| 725 | "figure of merit is 1.1.\n" |
---|
| 726 | " @param args: c-parameters\n" |
---|
| 727 | " @return: oscillation figure of merit"; |
---|
| 728 | |
---|
[abad620] | 729 | static PyObject * oscillations(Cinvertor *self, PyObject *args) { |
---|
| 730 | double *pars; |
---|
| 731 | PyObject *data_obj; |
---|
| 732 | Py_ssize_t npars; |
---|
| 733 | double oscill, norm; |
---|
[9a23253e] | 734 | |
---|
[abad620] | 735 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 736 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 737 | |
---|
[abad620] | 738 | oscill = reg_term(pars, self->params.d_max, npars, 100); |
---|
| 739 | norm = int_p2(pars, self->params.d_max, npars, 100); |
---|
[9a23253e] | 740 | return Py_BuildValue("f", sqrt(oscill/norm)/acos(-1.0)*self->params.d_max ); |
---|
| 741 | |
---|
[abad620] | 742 | } |
---|
| 743 | |
---|
[9a23253e] | 744 | const char get_peaks_doc[] = |
---|
[896abb3] | 745 | "Returns the number of peaks in the output P(r) distrubution\n" |
---|
| 746 | "for the given set of coefficients.\n" |
---|
| 747 | " @param args: c-parameters\n" |
---|
| 748 | " @return: number of P(r) peaks"; |
---|
| 749 | |
---|
[4f63160] | 750 | static PyObject * get_peaks(Cinvertor *self, PyObject *args) { |
---|
| 751 | double *pars; |
---|
| 752 | PyObject *data_obj; |
---|
| 753 | Py_ssize_t npars; |
---|
| 754 | int count; |
---|
[9a23253e] | 755 | |
---|
[4f63160] | 756 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 757 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 758 | |
---|
[4f63160] | 759 | count = npeaks(pars, self->params.d_max, npars, 100); |
---|
| 760 | |
---|
[9a23253e] | 761 | return Py_BuildValue("i", count ); |
---|
| 762 | |
---|
[4f63160] | 763 | } |
---|
| 764 | |
---|
[9a23253e] | 765 | const char get_positive_doc[] = |
---|
[896abb3] | 766 | "Returns the fraction of P(r) that is positive over\n" |
---|
| 767 | "the full range of r for the given set of coefficients.\n" |
---|
| 768 | " @param args: c-parameters\n" |
---|
| 769 | " @return: fraction of P(r) that is positive"; |
---|
| 770 | |
---|
[43c0a8e] | 771 | static PyObject * get_positive(Cinvertor *self, PyObject *args) { |
---|
| 772 | double *pars; |
---|
| 773 | PyObject *data_obj; |
---|
| 774 | Py_ssize_t npars; |
---|
| 775 | double fraction; |
---|
[9a23253e] | 776 | |
---|
[43c0a8e] | 777 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 778 | OUTVECTOR(data_obj,pars,npars); |
---|
[9a23253e] | 779 | |
---|
[43c0a8e] | 780 | fraction = positive_integral(pars, self->params.d_max, npars, 100); |
---|
| 781 | |
---|
[9a23253e] | 782 | return Py_BuildValue("f", fraction ); |
---|
| 783 | |
---|
[43c0a8e] | 784 | } |
---|
| 785 | |
---|
[9a23253e] | 786 | const char get_pos_err_doc[] = |
---|
[896abb3] | 787 | "Returns the fraction of P(r) that is 1 standard deviation\n" |
---|
| 788 | "above zero over the full range of r for the given set of coefficients.\n" |
---|
| 789 | " @param args: c-parameters\n" |
---|
| 790 | " @return: fraction of P(r) that is positive"; |
---|
| 791 | |
---|
[43c0a8e] | 792 | static PyObject * get_pos_err(Cinvertor *self, PyObject *args) { |
---|
| 793 | double *pars; |
---|
| 794 | double *pars_err; |
---|
| 795 | PyObject *data_obj; |
---|
| 796 | PyObject *err_obj; |
---|
| 797 | Py_ssize_t npars; |
---|
| 798 | Py_ssize_t npars2; |
---|
| 799 | double fraction; |
---|
[9a23253e] | 800 | |
---|
[43c0a8e] | 801 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &err_obj)) return NULL; |
---|
[9a23253e] | 802 | OUTVECTOR(data_obj,pars,npars); |
---|
[43c0a8e] | 803 | OUTVECTOR(err_obj,pars_err,npars2); |
---|
[9a23253e] | 804 | |
---|
[43c0a8e] | 805 | fraction = positive_errors(pars, pars_err, self->params.d_max, npars, 51); |
---|
| 806 | |
---|
[9a23253e] | 807 | return Py_BuildValue("f", fraction ); |
---|
| 808 | |
---|
| 809 | } |
---|
| 810 | |
---|
| 811 | const char get_rg_doc[] = |
---|
| 812 | "Returns the value of the radius of gyration Rg.\n" |
---|
| 813 | " @param args: c-parameters\n" |
---|
| 814 | " @return: Rg"; |
---|
| 815 | |
---|
| 816 | static PyObject * get_rg(Cinvertor *self, PyObject *args) { |
---|
| 817 | double *pars; |
---|
| 818 | double *pars_err; |
---|
| 819 | PyObject *data_obj; |
---|
| 820 | Py_ssize_t npars; |
---|
| 821 | Py_ssize_t npars2; |
---|
| 822 | double value; |
---|
| 823 | |
---|
| 824 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 825 | OUTVECTOR(data_obj,pars,npars); |
---|
| 826 | |
---|
| 827 | value = rg(pars, self->params.d_max, npars, 101); |
---|
| 828 | |
---|
| 829 | return Py_BuildValue("f", value ); |
---|
| 830 | |
---|
[43c0a8e] | 831 | } |
---|
| 832 | |
---|
[9a23253e] | 833 | const char get_iq0_doc[] = |
---|
| 834 | "Returns the value of I(q=0).\n" |
---|
| 835 | " @param args: c-parameters\n" |
---|
| 836 | " @return: I(q=0)"; |
---|
| 837 | |
---|
| 838 | static PyObject * get_iq0(Cinvertor *self, PyObject *args) { |
---|
| 839 | double *pars; |
---|
| 840 | double *pars_err; |
---|
| 841 | PyObject *data_obj; |
---|
| 842 | Py_ssize_t npars; |
---|
| 843 | Py_ssize_t npars2; |
---|
| 844 | double value; |
---|
| 845 | |
---|
| 846 | if (!PyArg_ParseTuple(args, "O", &data_obj)) return NULL; |
---|
| 847 | OUTVECTOR(data_obj,pars,npars); |
---|
| 848 | |
---|
| 849 | value = 4.0*acos(-1.0)*int_pr(pars, self->params.d_max, npars, 101); |
---|
| 850 | |
---|
| 851 | return Py_BuildValue("f", value ); |
---|
| 852 | |
---|
| 853 | } |
---|
| 854 | |
---|
| 855 | /** |
---|
| 856 | * Check whether a q-value is within acceptabel limits |
---|
| 857 | * Return 1 if accepted, 0 if rejected. |
---|
| 858 | */ |
---|
| 859 | int accept_q(Cinvertor *self, double q) { |
---|
| 860 | if (self->params.q_min>0 && q<self->params.q_min) return 0; |
---|
| 861 | if (self->params.q_max>0 && q>self->params.q_max) return 0; |
---|
| 862 | return 1; |
---|
| 863 | } |
---|
| 864 | |
---|
| 865 | const char get_matrix_doc[] = |
---|
| 866 | "Returns A matrix and b vector for least square problem.\n" |
---|
| 867 | " @param nfunc: number of base functions\n" |
---|
| 868 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
| 869 | " @param a: A array to fill\n" |
---|
| 870 | " @param b: b vector to fill\n" |
---|
| 871 | " @return: 0"; |
---|
| 872 | |
---|
| 873 | static PyObject * get_matrix(Cinvertor *self, PyObject *args) { |
---|
| 874 | double *a; |
---|
| 875 | double *b; |
---|
| 876 | PyObject *a_obj; |
---|
| 877 | PyObject *b_obj; |
---|
| 878 | Py_ssize_t n_a; |
---|
| 879 | Py_ssize_t n_b; |
---|
| 880 | // Number of bins for regularization term evaluation |
---|
| 881 | int nr, nfunc; |
---|
| 882 | int i, j, i_r; |
---|
| 883 | double r, sqrt_alpha, pi; |
---|
| 884 | double tmp; |
---|
| 885 | int offset; |
---|
| 886 | |
---|
| 887 | if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &b_obj)) return NULL; |
---|
| 888 | OUTVECTOR(a_obj,a,n_a); |
---|
| 889 | OUTVECTOR(b_obj,b,n_b); |
---|
| 890 | |
---|
| 891 | assert(n_b>=nfunc); |
---|
[bd30f4a5] | 892 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
[9a23253e] | 893 | |
---|
| 894 | sqrt_alpha = sqrt(self->params.alpha); |
---|
| 895 | pi = acos(-1.0); |
---|
| 896 | offset = (self->params.has_bck==1) ? 0 : 1; |
---|
| 897 | |
---|
| 898 | for (j=0; j<nfunc; j++) { |
---|
| 899 | for (i=0; i<self->params.npoints; i++) { |
---|
| 900 | if (accept_q(self, self->params.x[i])){ |
---|
| 901 | if (self->params.has_bck==1 && j==0) { |
---|
| 902 | a[i*nfunc+j] = 1.0/self->params.err[i]; |
---|
| 903 | } else { |
---|
[f168d02] | 904 | if (self->params.slit_width>0 || self->params.slit_height>0) { |
---|
| 905 | a[i*nfunc+j] = ortho_transformed_smeared(self->params.d_max, |
---|
| 906 | j+offset, self->params.slit_height, self->params.slit_width, |
---|
| 907 | self->params.x[i], 21)/self->params.err[i]; |
---|
| 908 | } else { |
---|
| 909 | a[i*nfunc+j] = ortho_transformed(self->params.d_max, j+offset, self->params.x[i])/self->params.err[i]; |
---|
| 910 | } |
---|
[9a23253e] | 911 | } |
---|
| 912 | } |
---|
| 913 | } |
---|
| 914 | for (i_r=0; i_r<nr; i_r++){ |
---|
| 915 | if (self->params.has_bck==1 && j==0) { |
---|
| 916 | a[(i_r+self->params.npoints)*nfunc+j] = 0.0; |
---|
| 917 | } else { |
---|
| 918 | r = self->params.d_max/nr*i_r; |
---|
| 919 | tmp = pi*(j+offset)/self->params.d_max; |
---|
| 920 | a[(i_r+self->params.npoints)*nfunc+j] = sqrt_alpha * 1.0/nr*self->params.d_max*2.0* |
---|
| 921 | (2.0*pi*(j+offset)/self->params.d_max*cos(pi*(j+offset)*r/self->params.d_max) + |
---|
| 922 | tmp*tmp*r * sin(pi*(j+offset)*r/self->params.d_max)); |
---|
| 923 | } |
---|
| 924 | } |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | for (i=0; i<self->params.npoints; i++) { |
---|
| 928 | if (accept_q(self, self->params.x[i])){ |
---|
| 929 | b[i] = self->params.y[i]/self->params.err[i]; |
---|
| 930 | } |
---|
| 931 | } |
---|
| 932 | |
---|
| 933 | return Py_BuildValue("i", 0); |
---|
| 934 | |
---|
| 935 | } |
---|
| 936 | |
---|
| 937 | const char get_invcov_matrix_doc[] = |
---|
| 938 | " Compute the inverse covariance matrix, defined as inv_cov = a_transposed x a.\n" |
---|
| 939 | " @param nfunc: number of base functions\n" |
---|
| 940 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
| 941 | " @param a: A array to fill\n" |
---|
| 942 | " @param inv_cov: inverse covariance array to be filled\n" |
---|
| 943 | " @return: 0"; |
---|
| 944 | |
---|
| 945 | static PyObject * get_invcov_matrix(Cinvertor *self, PyObject *args) { |
---|
| 946 | double *a; |
---|
| 947 | PyObject *a_obj; |
---|
| 948 | Py_ssize_t n_a; |
---|
| 949 | double *inv_cov; |
---|
| 950 | PyObject *cov_obj; |
---|
| 951 | Py_ssize_t n_cov; |
---|
| 952 | int nr, nfunc; |
---|
| 953 | int i, j, k; |
---|
| 954 | |
---|
| 955 | if (!PyArg_ParseTuple(args, "iiOO", &nfunc, &nr, &a_obj, &cov_obj)) return NULL; |
---|
| 956 | OUTVECTOR(a_obj,a,n_a); |
---|
| 957 | OUTVECTOR(cov_obj,inv_cov,n_cov); |
---|
| 958 | |
---|
| 959 | assert(n_cov>=nfunc*nfunc); |
---|
| 960 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
| 961 | |
---|
| 962 | for (i=0; i<nfunc; i++) { |
---|
| 963 | for (j=0; j<nfunc; j++) { |
---|
| 964 | inv_cov[i*nfunc+j] = 0.0; |
---|
| 965 | for (k=0; k<nr+self->params.npoints; k++) { |
---|
| 966 | inv_cov[i*nfunc+j] += a[k*nfunc+i]*a[k*nfunc+j]; |
---|
| 967 | } |
---|
| 968 | } |
---|
| 969 | } |
---|
| 970 | return Py_BuildValue("i", 0); |
---|
| 971 | } |
---|
| 972 | |
---|
| 973 | const char get_reg_size_doc[] = |
---|
| 974 | " Compute the covariance matrix, defined as inv_cov = a_transposed x a.\n" |
---|
| 975 | " @param nfunc: number of base functions\n" |
---|
| 976 | " @param nr: number of r-points used when evaluating reg term.\n" |
---|
| 977 | " @param a: A array to fill\n" |
---|
| 978 | " @param inv_cov: inverse covariance array to be filled\n" |
---|
| 979 | " @return: 0"; |
---|
| 980 | |
---|
| 981 | static PyObject * get_reg_size(Cinvertor *self, PyObject *args) { |
---|
| 982 | double *a; |
---|
| 983 | PyObject *a_obj; |
---|
| 984 | Py_ssize_t n_a; |
---|
| 985 | int nr, nfunc; |
---|
| 986 | int i, j; |
---|
| 987 | double sum_sig, sum_reg; |
---|
| 988 | |
---|
| 989 | if (!PyArg_ParseTuple(args, "iiO", &nfunc, &nr, &a_obj)) return NULL; |
---|
| 990 | OUTVECTOR(a_obj,a,n_a); |
---|
| 991 | |
---|
| 992 | assert(n_a>=nfunc*(nr+self->params.npoints)); |
---|
| 993 | |
---|
| 994 | sum_sig = 0.0; |
---|
| 995 | sum_reg = 0.0; |
---|
| 996 | for (j=0; j<nfunc; j++){ |
---|
| 997 | for (i=0; i<self->params.npoints; i++){ |
---|
| 998 | if (accept_q(self, self->params.x[i])==1) |
---|
| 999 | sum_sig += (a[i*nfunc+j])*(a[i*nfunc+j]); |
---|
| 1000 | } |
---|
| 1001 | for (i=0; i<nr; i++){ |
---|
| 1002 | sum_reg += (a[(i+self->params.npoints)*nfunc+j])*(a[(i+self->params.npoints)*nfunc+j]); |
---|
| 1003 | } |
---|
| 1004 | } |
---|
| 1005 | return Py_BuildValue("ff", sum_sig, sum_reg); |
---|
| 1006 | } |
---|
[896abb3] | 1007 | |
---|
| 1008 | const char eeeget_qmin_doc[] = "\ |
---|
| 1009 | This is a multiline doc string.\n\ |
---|
| 1010 | \n\ |
---|
| 1011 | This is the second line."; |
---|
[9a23253e] | 1012 | const char eeeset_qmin_doc[] = |
---|
[896abb3] | 1013 | "This is a multiline doc string.\n" |
---|
| 1014 | "\n" |
---|
| 1015 | "This is the second line."; |
---|
| 1016 | |
---|
[9e8dc22] | 1017 | static PyMethodDef Cinvertor_methods[] = { |
---|
[896abb3] | 1018 | {"residuals", (PyCFunction)residuals, METH_VARARGS, residuals_doc}, |
---|
| 1019 | {"pr_residuals", (PyCFunction)pr_residuals, METH_VARARGS, pr_residuals_doc}, |
---|
| 1020 | {"set_x", (PyCFunction)set_x, METH_VARARGS, set_x_doc}, |
---|
| 1021 | {"get_x", (PyCFunction)get_x, METH_VARARGS, get_x_doc}, |
---|
| 1022 | {"set_y", (PyCFunction)set_y, METH_VARARGS, set_y_doc}, |
---|
| 1023 | {"get_y", (PyCFunction)get_y, METH_VARARGS, get_y_doc}, |
---|
| 1024 | {"set_err", (PyCFunction)set_err, METH_VARARGS, set_err_doc}, |
---|
| 1025 | {"get_err", (PyCFunction)get_err, METH_VARARGS, get_err_doc}, |
---|
| 1026 | {"set_dmax", (PyCFunction)set_dmax, METH_VARARGS, set_dmax_doc}, |
---|
| 1027 | {"get_dmax", (PyCFunction)get_dmax, METH_VARARGS, get_dmax_doc}, |
---|
| 1028 | {"set_qmin", (PyCFunction)set_qmin, METH_VARARGS, set_qmin_doc}, |
---|
| 1029 | {"get_qmin", (PyCFunction)get_qmin, METH_VARARGS, get_qmin_doc}, |
---|
| 1030 | {"set_qmax", (PyCFunction)set_qmax, METH_VARARGS, set_qmax_doc}, |
---|
| 1031 | {"get_qmax", (PyCFunction)get_qmax, METH_VARARGS, get_qmax_doc}, |
---|
| 1032 | {"set_alpha", (PyCFunction)set_alpha, METH_VARARGS, set_alpha_doc}, |
---|
| 1033 | {"get_alpha", (PyCFunction)get_alpha, METH_VARARGS, get_alpha_doc}, |
---|
[9a23253e] | 1034 | {"set_slit_width", (PyCFunction)set_slit_width, METH_VARARGS, set_slit_width_doc}, |
---|
| 1035 | {"get_slit_width", (PyCFunction)get_slit_width, METH_VARARGS, get_slit_width_doc}, |
---|
| 1036 | {"set_slit_height", (PyCFunction)set_slit_height, METH_VARARGS, set_slit_height_doc}, |
---|
| 1037 | {"get_slit_height", (PyCFunction)get_slit_height, METH_VARARGS, get_slit_height_doc}, |
---|
| 1038 | {"set_has_bck", (PyCFunction)set_has_bck, METH_VARARGS, set_has_bck_doc}, |
---|
| 1039 | {"get_has_bck", (PyCFunction)get_has_bck, METH_VARARGS, get_has_bck_doc}, |
---|
[896abb3] | 1040 | {"get_nx", (PyCFunction)get_nx, METH_VARARGS, get_nx_doc}, |
---|
| 1041 | {"get_ny", (PyCFunction)get_ny, METH_VARARGS, get_ny_doc}, |
---|
| 1042 | {"get_nerr", (PyCFunction)get_nerr, METH_VARARGS, get_nerr_doc}, |
---|
| 1043 | {"iq", (PyCFunction)get_iq, METH_VARARGS, get_iq_doc}, |
---|
[f168d02] | 1044 | {"iq_smeared", (PyCFunction)get_iq_smeared, METH_VARARGS, get_iq_smeared_doc}, |
---|
[896abb3] | 1045 | {"pr", (PyCFunction)get_pr, METH_VARARGS, get_pr_doc}, |
---|
| 1046 | {"get_pr_err", (PyCFunction)get_pr_err, METH_VARARGS, get_pr_err_doc}, |
---|
| 1047 | {"is_valid", (PyCFunction)is_valid, METH_VARARGS, is_valid_doc}, |
---|
| 1048 | {"basefunc_ft", (PyCFunction)basefunc_ft, METH_VARARGS, basefunc_ft_doc}, |
---|
| 1049 | {"oscillations", (PyCFunction)oscillations, METH_VARARGS, oscillations_doc}, |
---|
| 1050 | {"get_peaks", (PyCFunction)get_peaks, METH_VARARGS, get_peaks_doc}, |
---|
| 1051 | {"get_positive", (PyCFunction)get_positive, METH_VARARGS, get_positive_doc}, |
---|
| 1052 | {"get_pos_err", (PyCFunction)get_pos_err, METH_VARARGS, get_pos_err_doc}, |
---|
[9a23253e] | 1053 | {"rg", (PyCFunction)get_rg, METH_VARARGS, get_rg_doc}, |
---|
| 1054 | {"iq0", (PyCFunction)get_iq0, METH_VARARGS, get_iq0_doc}, |
---|
| 1055 | {"_get_matrix", (PyCFunction)get_matrix, METH_VARARGS, get_matrix_doc}, |
---|
| 1056 | {"_get_invcov_matrix", (PyCFunction)get_invcov_matrix, METH_VARARGS, get_invcov_matrix_doc}, |
---|
| 1057 | {"_get_reg_size", (PyCFunction)get_reg_size, METH_VARARGS, get_reg_size_doc}, |
---|
| 1058 | |
---|
[9e8dc22] | 1059 | {NULL} |
---|
| 1060 | }; |
---|
| 1061 | |
---|
| 1062 | static PyTypeObject CinvertorType = { |
---|
| 1063 | PyObject_HEAD_INIT(NULL) |
---|
| 1064 | 0, /*ob_size*/ |
---|
| 1065 | "Cinvertor", /*tp_name*/ |
---|
| 1066 | sizeof(Cinvertor), /*tp_basicsize*/ |
---|
| 1067 | 0, /*tp_itemsize*/ |
---|
| 1068 | (destructor)Cinvertor_dealloc, /*tp_dealloc*/ |
---|
| 1069 | 0, /*tp_print*/ |
---|
| 1070 | 0, /*tp_getattr*/ |
---|
| 1071 | 0, /*tp_setattr*/ |
---|
| 1072 | 0, /*tp_compare*/ |
---|
| 1073 | 0, /*tp_repr*/ |
---|
| 1074 | 0, /*tp_as_number*/ |
---|
| 1075 | 0, /*tp_as_sequence*/ |
---|
| 1076 | 0, /*tp_as_mapping*/ |
---|
| 1077 | 0, /*tp_hash */ |
---|
| 1078 | 0, /*tp_call*/ |
---|
| 1079 | 0, /*tp_str*/ |
---|
| 1080 | 0, /*tp_getattro*/ |
---|
| 1081 | 0, /*tp_setattro*/ |
---|
| 1082 | 0, /*tp_as_buffer*/ |
---|
| 1083 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
---|
| 1084 | "Cinvertor objects", /* tp_doc */ |
---|
| 1085 | 0, /* tp_traverse */ |
---|
| 1086 | 0, /* tp_clear */ |
---|
| 1087 | 0, /* tp_richcompare */ |
---|
| 1088 | 0, /* tp_weaklistoffset */ |
---|
| 1089 | 0, /* tp_iter */ |
---|
| 1090 | 0, /* tp_iternext */ |
---|
| 1091 | Cinvertor_methods, /* tp_methods */ |
---|
| 1092 | Cinvertor_members, /* tp_members */ |
---|
| 1093 | 0, /* tp_getset */ |
---|
| 1094 | 0, /* tp_base */ |
---|
| 1095 | 0, /* tp_dict */ |
---|
| 1096 | 0, /* tp_descr_get */ |
---|
| 1097 | 0, /* tp_descr_set */ |
---|
| 1098 | 0, /* tp_dictoffset */ |
---|
| 1099 | (initproc)Cinvertor_init, /* tp_init */ |
---|
| 1100 | 0, /* tp_alloc */ |
---|
| 1101 | Cinvertor_new, /* tp_new */ |
---|
| 1102 | }; |
---|
| 1103 | |
---|
| 1104 | |
---|
| 1105 | static PyMethodDef module_methods[] = { |
---|
[9a23253e] | 1106 | {NULL} |
---|
[9e8dc22] | 1107 | }; |
---|
| 1108 | |
---|
| 1109 | /** |
---|
| 1110 | * Function used to add the model class to a module |
---|
| 1111 | * @param module: module to add the class to |
---|
[9a23253e] | 1112 | */ |
---|
[9e8dc22] | 1113 | void addCinvertor(PyObject *module) { |
---|
| 1114 | PyObject *d; |
---|
[9a23253e] | 1115 | |
---|
[9e8dc22] | 1116 | if (PyType_Ready(&CinvertorType) < 0) |
---|
| 1117 | return; |
---|
| 1118 | |
---|
| 1119 | Py_INCREF(&CinvertorType); |
---|
| 1120 | PyModule_AddObject(module, "Cinvertor", (PyObject *)&CinvertorType); |
---|
[9a23253e] | 1121 | |
---|
[9e8dc22] | 1122 | d = PyModule_GetDict(module); |
---|
| 1123 | CinvertorError = PyErr_NewException("Cinvertor.error", NULL, NULL); |
---|
| 1124 | PyDict_SetItemString(d, "CinvertorError", CinvertorError); |
---|
| 1125 | } |
---|
| 1126 | |
---|
| 1127 | |
---|
| 1128 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
---|
| 1129 | #define PyMODINIT_FUNC void |
---|
| 1130 | #endif |
---|
| 1131 | PyMODINIT_FUNC |
---|
[9a23253e] | 1132 | initpr_inversion(void) |
---|
[9e8dc22] | 1133 | { |
---|
| 1134 | PyObject* m; |
---|
| 1135 | |
---|
| 1136 | m = Py_InitModule3("pr_inversion", module_methods, |
---|
| 1137 | "C extension module for inversion to P(r)."); |
---|
[9a23253e] | 1138 | |
---|
[9e8dc22] | 1139 | addCinvertor(m); |
---|
| 1140 | } |
---|