[12011] | 1 | /*\file FetchData.cpp:
|
---|
| 2 | * \brief: general I/O interface to fetch data in python
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | #ifdef HAVE_CONFIG_H
|
---|
[23708] | 6 | #include <config.h>
|
---|
[12011] | 7 | #else
|
---|
| 8 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
| 9 | #endif
|
---|
| 10 |
|
---|
| 11 | #define PY_ARRAY_UNIQUE_SYMBOL PythonIOSymbol
|
---|
| 12 | #define NO_IMPORT
|
---|
| 13 |
|
---|
[13749] | 14 | #include "./pythonio.h"
|
---|
| 15 | #include "../../c/shared/shared.h"
|
---|
[12011] | 16 |
|
---|
[12073] | 17 | /*Primitive data types*/
|
---|
[12365] | 18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
[12011] | 19 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
| 20 |
|
---|
[14093] | 21 | double dscalar;
|
---|
[12011] | 22 |
|
---|
| 23 | /*return internal value: */
|
---|
[23708] | 24 | #if _PYTHON_MAJOR_ == 3
|
---|
| 25 | if (PyFloat_Check(py_float))
|
---|
[14093] | 26 | dscalar=PyFloat_AsDouble(py_float);
|
---|
[23708] | 27 | else if (PyLong_Check(py_float))
|
---|
[19895] | 28 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[23708] | 29 | else if (PyBool_Check(py_float))
|
---|
| 30 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
| 31 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 32 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 33 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 34 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 35 | else
|
---|
| 36 | _error_("unrecognized float type in input!");
|
---|
| 37 |
|
---|
| 38 | #else
|
---|
| 39 | if (PyFloat_Check(py_float))
|
---|
| 40 | dscalar=PyFloat_AsDouble(py_float);
|
---|
| 41 | else if (PyLong_Check(py_float))
|
---|
[14093] | 42 | dscalar=PyLong_AsDouble(py_float);
|
---|
[19896] | 43 | else if (PyInt_Check(py_float))
|
---|
[23231] | 44 | dscalar=(double)PyInt_AsLong(py_float);
|
---|
[14097] | 45 | else if (PyBool_Check(py_float))
|
---|
[14093] | 46 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
[14097] | 47 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 48 | FetchData(&dscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 49 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 50 | FetchData(&dscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[14093] | 51 | else
|
---|
| 52 | _error_("unrecognized float type in input!");
|
---|
[23708] | 53 | #endif
|
---|
[12011] | 54 | /*output: */
|
---|
[14093] | 55 | *pscalar=dscalar;
|
---|
[12011] | 56 | }
|
---|
| 57 | /*}}}*/
|
---|
[22674] | 58 | /*FUNCTION FetchData(float* pscalar,PyObject* py_float){{{*/
|
---|
| 59 | void FetchData(float* pscalar,PyObject* py_float){
|
---|
| 60 |
|
---|
| 61 | float fscalar;
|
---|
| 62 |
|
---|
| 63 | /*return internal value: */
|
---|
[23708] | 64 | #if _PYTHON_MAJOR_ == 3
|
---|
[22674] | 65 | if (PyFloat_Check(py_float))
|
---|
| 66 | fscalar=PyFloat_AsDouble(py_float);
|
---|
[23708] | 67 | else if (PyLong_Check(py_float))
|
---|
[22674] | 68 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[23708] | 69 | else if (PyBool_Check(py_float))
|
---|
| 70 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
| 71 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
| 72 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 73 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
| 74 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
| 75 | else
|
---|
| 76 | _error_("unrecognized float type in input!");
|
---|
| 77 | #else
|
---|
| 78 | if (PyFloat_Check(py_float))
|
---|
| 79 | fscalar=PyFloat_AsDouble(py_float);
|
---|
| 80 | else if (PyLong_Check(py_float))
|
---|
[22674] | 81 | fscalar=(float)PyLong_AsDouble(py_float);
|
---|
| 82 | else if (PyInt_Check(py_float))
|
---|
[23708] | 83 | fscalar=(float)PyInt_AsLong(py_float);
|
---|
[22674] | 84 | else if (PyBool_Check(py_float))
|
---|
[23708] | 85 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
[22674] | 86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
[23708] | 87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
[23708] | 89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
[22674] | 90 | else
|
---|
[23708] | 91 | _error_("unrecognized float type in input!");
|
---|
| 92 | #endif
|
---|
[22674] | 93 | /*output: */
|
---|
| 94 | *pscalar=fscalar;
|
---|
| 95 | }
|
---|
| 96 | /*}}}*/
|
---|
[14093] | 97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
---|
| 98 | void FetchData(int* pscalar, PyObject* py_long){
|
---|
[12011] | 99 |
|
---|
[14093] | 100 | int iscalar;
|
---|
[12011] | 101 |
|
---|
| 102 | /*return internal value: */
|
---|
[23708] | 103 | #if _PYTHON_MAJOR_ == 3
|
---|
| 104 | if (PyLong_Check(py_long))
|
---|
[19895] | 105 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[23708] | 106 | else if (PyFloat_Check(py_long))
|
---|
| 107 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
| 108 | else if (PyBool_Check(py_long))
|
---|
| 109 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
| 110 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 111 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 112 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 113 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 114 | else
|
---|
| 115 | _error_("unrecognized long type in input!");
|
---|
| 116 |
|
---|
| 117 | #else
|
---|
| 118 | if (PyLong_Check(py_long))
|
---|
| 119 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[19896] | 120 | else if (PyInt_Check(py_long))
|
---|
| 121 | iscalar=(int)PyInt_AsLong(py_long);
|
---|
[14097] | 122 | else if (PyFloat_Check(py_long))
|
---|
[14093] | 123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
[14097] | 124 | else if (PyBool_Check(py_long))
|
---|
[14093] | 125 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
[14097] | 126 | else if (PyTuple_Check(py_long) && (int)PyTuple_Size(py_long)==1)
|
---|
| 127 | FetchData(&iscalar,PyTuple_GetItem(py_long,(Py_ssize_t)0));
|
---|
| 128 | else if (PyList_Check(py_long) && (int)PyList_Size(py_long)==1)
|
---|
| 129 | FetchData(&iscalar,PyList_GetItem(py_long,(Py_ssize_t)0));
|
---|
[14093] | 130 | else
|
---|
| 131 | _error_("unrecognized long type in input!");
|
---|
[23708] | 132 | #endif
|
---|
[12011] | 133 | /*output: */
|
---|
[14093] | 134 | *pscalar=iscalar;
|
---|
[12011] | 135 | }
|
---|
| 136 | /*}}}*/
|
---|
[14093] | 137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
---|
| 138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
---|
[12011] | 139 |
|
---|
[14093] | 140 | bool bscalar;
|
---|
[13622] | 141 |
|
---|
[14097] | 142 | /*return internal value: */
|
---|
[23708] | 143 | #if _PYTHON_MAJOR_ == 3
|
---|
[14097] | 144 | if (PyBool_Check(py_boolean))
|
---|
| 145 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 146 | else if (PyLong_Check(py_boolean))
|
---|
| 147 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19895] | 148 | else if (PyLong_Check(py_boolean))
|
---|
| 149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[23708] | 150 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 151 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 152 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 153 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 154 | else
|
---|
| 155 | _error_("unrecognized boolean type in input!");
|
---|
| 156 |
|
---|
| 157 | #else
|
---|
| 158 | if (PyBool_Check(py_boolean))
|
---|
| 159 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 160 | else if (PyLong_Check(py_boolean))
|
---|
| 161 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
| 162 | else if (PyLong_Check(py_boolean))
|
---|
| 163 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
[19896] | 164 | else if (PyInt_Check(py_boolean))
|
---|
| 165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
---|
[14097] | 166 | else if (PyTuple_Check(py_boolean) && (int)PyTuple_Size(py_boolean)==1)
|
---|
| 167 | FetchData(&bscalar,PyTuple_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 168 | else if (PyList_Check(py_boolean) && (int)PyList_Size(py_boolean)==1)
|
---|
| 169 | FetchData(&bscalar,PyList_GetItem(py_boolean,(Py_ssize_t)0));
|
---|
| 170 | else
|
---|
| 171 | _error_("unrecognized boolean type in input!");
|
---|
[23708] | 172 | #endif
|
---|
[14097] | 173 | /*output: */
|
---|
[14093] | 174 | *pscalar=bscalar;
|
---|
[12011] | 175 | }
|
---|
| 176 | /*}}}*/
|
---|
[12365] | 177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
[12112] | 178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
[12073] | 179 |
|
---|
[12112] | 180 | /*output: */
|
---|
[13372] | 181 | double* dmatrix=NULL;
|
---|
[12112] | 182 | double* matrix=NULL;
|
---|
[23231] | 183 | int M=0;
|
---|
| 184 | int N=0;
|
---|
[13373] | 185 | int ndim;
|
---|
[12112] | 186 | npy_intp* dims=NULL;
|
---|
| 187 |
|
---|
[13990] | 188 | /*intermediary:*/
|
---|
| 189 | long* lmatrix=NULL;
|
---|
| 190 | bool* bmatrix=NULL;
|
---|
[14317] | 191 | int i;
|
---|
| 192 | PyObject* py_matrix2=NULL;
|
---|
[13990] | 193 |
|
---|
[14093] | 194 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 195 | /*retrieve dimensions: */
|
---|
| 196 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[14098] | 197 | if (ndim==2) {
|
---|
| 198 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 199 | M=dims[0]; N=dims[1];
|
---|
| 200 | }
|
---|
| 201 | else if (ndim==1) {
|
---|
| 202 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 203 | M=dims[0]; N=1;
|
---|
| 204 | }
|
---|
| 205 | else
|
---|
| 206 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 207 |
|
---|
[14317] | 208 | if (M && N) {
|
---|
| 209 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 210 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_DOUBLE,ndim,ndim);
|
---|
| 211 | py_matrix=py_matrix2;
|
---|
| 212 | }
|
---|
[14316] | 213 |
|
---|
[14093] | 214 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 215 | /*retrieve internal value: */
|
---|
| 216 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12112] | 217 |
|
---|
[14093] | 218 | /*copy matrix: */
|
---|
| 219 | matrix=xNew<double>(M*N);
|
---|
[14317] | 220 | // if (PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
[14316] | 221 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
[14317] | 222 | // }
|
---|
| 223 |
|
---|
| 224 | // else {
|
---|
| 225 | // int j,k,ipt=0;
|
---|
| 226 | // int mstride,nstride;
|
---|
| 227 | // mstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,0)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
|
---|
| 228 | // if (ndim > 1)
|
---|
| 229 | // nstride=(int)PyArray_STRIDE((PyArrayObject*)py_matrix,1)/PyArray_ITEMSIZE((PyArrayObject*)py_matrix);
|
---|
| 230 | // else
|
---|
| 231 | // nstride=1;
|
---|
| 232 | // for (i=0; i<M; i++) {
|
---|
| 233 | // k=i*mstride;
|
---|
| 234 | // for (j=0; j<N; j++) {
|
---|
| 235 | // matrix[ipt++]=dmatrix[k];
|
---|
| 236 | // k+=nstride;
|
---|
| 237 | // }
|
---|
| 238 | // }
|
---|
| 239 | // }
|
---|
[23708] | 240 |
|
---|
[14093] | 241 | }
|
---|
[13990] | 242 |
|
---|
[14316] | 243 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 244 | /*retrieve internal value: */
|
---|
| 245 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 246 |
|
---|
[14093] | 247 | /*transform into double matrix: */
|
---|
| 248 | matrix=xNew<double>(M*N);
|
---|
| 249 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
| 250 | }
|
---|
[13990] | 251 |
|
---|
[14093] | 252 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 253 | /*retrieve internal value: */
|
---|
| 254 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 255 |
|
---|
[14093] | 256 | /*transform into double matrix: */
|
---|
| 257 | matrix=xNew<double>(M*N);
|
---|
| 258 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | else
|
---|
[14234] | 262 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 263 |
|
---|
[19683] | 264 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
[13990] | 265 | }
|
---|
| 266 | else
|
---|
[14093] | 267 | matrix=NULL;
|
---|
[13484] | 268 | }
|
---|
[14093] | 269 |
|
---|
[14097] | 270 | else {
|
---|
[14093] | 271 | M=1;
|
---|
| 272 | N=1;
|
---|
| 273 | matrix=xNew<double>(M*N);
|
---|
| 274 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 275 | }
|
---|
| 276 |
|
---|
[12112] | 277 | /*output: */
|
---|
| 278 | if(pM)*pM=M;
|
---|
| 279 | if(pN)*pN=N;
|
---|
| 280 | if(pmatrix)*pmatrix=matrix;
|
---|
| 281 | }
|
---|
| 282 | /*}}}*/
|
---|
[12776] | 283 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 284 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 285 |
|
---|
| 286 | /*output: */
|
---|
| 287 | int* matrix=NULL;
|
---|
[23231] | 288 | int M=0;
|
---|
| 289 | int N=0;
|
---|
[13990] | 290 | int ndim;
|
---|
| 291 | npy_intp* dims=NULL;
|
---|
[12776] | 292 |
|
---|
| 293 | /*intermediary:*/
|
---|
[13990] | 294 | double* dmatrix=NULL;
|
---|
| 295 | long* lmatrix=NULL;
|
---|
| 296 | bool* bmatrix=NULL;
|
---|
[12776] | 297 | int i;
|
---|
[14317] | 298 | PyObject* py_matrix2=NULL;
|
---|
[12776] | 299 |
|
---|
[14093] | 300 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 301 | /*retrieve dimensions: */
|
---|
| 302 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
[14098] | 303 | if (ndim==2) {
|
---|
| 304 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 305 | M=dims[0]; N=dims[1];
|
---|
| 306 | }
|
---|
| 307 | else if (ndim==1) {
|
---|
| 308 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 309 | M=dims[0]; N=1;
|
---|
| 310 | }
|
---|
| 311 | else
|
---|
| 312 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
[13622] | 313 |
|
---|
[14093] | 314 | if (M && N) {
|
---|
[14317] | 315 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 316 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
---|
| 317 | py_matrix=py_matrix2;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[14093] | 320 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 321 | /*retrieve internal value: */
|
---|
| 322 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[12776] | 323 |
|
---|
[14093] | 324 | /*transform into integer matrix: */
|
---|
| 325 | matrix=xNew<int>(M*N);
|
---|
| 326 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
| 327 | }
|
---|
[13990] | 328 |
|
---|
[14316] | 329 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14093] | 330 | /*retrieve internal value: */
|
---|
| 331 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 332 |
|
---|
[14093] | 333 | /*transform into integer matrix: */
|
---|
| 334 | matrix=xNew<int>(M*N);
|
---|
| 335 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
| 336 | }
|
---|
[13990] | 337 |
|
---|
[14093] | 338 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 339 | /*retrieve internal value: */
|
---|
| 340 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
[13990] | 341 |
|
---|
[14093] | 342 | /*transform into integer matrix: */
|
---|
| 343 | matrix=xNew<int>(M*N);
|
---|
| 344 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | else
|
---|
[14234] | 348 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 349 |
|
---|
[21201] | 350 | /* These lines are causing a segfault
|
---|
[14317] | 351 | if (py_matrix2)
|
---|
| 352 | delete(py_matrix2);
|
---|
[21201] | 353 | */
|
---|
[13990] | 354 | }
|
---|
| 355 | else
|
---|
[14093] | 356 | matrix=NULL;
|
---|
[13484] | 357 | }
|
---|
[14093] | 358 |
|
---|
[14097] | 359 | else {
|
---|
[14093] | 360 | M=1;
|
---|
| 361 | N=1;
|
---|
| 362 | matrix=xNew<int>(M*N);
|
---|
| 363 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[12776] | 366 | /*output: */
|
---|
| 367 | if(pM)*pM=M;
|
---|
| 368 | if(pN)*pN=N;
|
---|
| 369 | if(pmatrix)*pmatrix=matrix;
|
---|
| 370 | }
|
---|
| 371 | /*}}}*/
|
---|
[14234] | 372 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
| 373 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
| 374 |
|
---|
| 375 | /*output: */
|
---|
| 376 | bool* bmatrix=NULL;
|
---|
| 377 | bool* matrix=NULL;
|
---|
[23231] | 378 | int M=0;
|
---|
| 379 | int N=0;
|
---|
[14234] | 380 | int ndim;
|
---|
| 381 | npy_intp* dims=NULL;
|
---|
| 382 |
|
---|
| 383 | /*intermediary:*/
|
---|
| 384 | double* dmatrix=NULL;
|
---|
| 385 | long* lmatrix=NULL;
|
---|
| 386 | int i;
|
---|
[14317] | 387 | PyObject* py_matrix2=NULL;
|
---|
[14234] | 388 |
|
---|
| 389 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
| 390 | /*retrieve dimensions: */
|
---|
| 391 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
| 392 | if (ndim==2) {
|
---|
| 393 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 394 | M=dims[0]; N=dims[1];
|
---|
| 395 | }
|
---|
| 396 | else if (ndim==1) {
|
---|
| 397 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
| 398 | M=dims[0]; N=1;
|
---|
| 399 | }
|
---|
| 400 | else
|
---|
| 401 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
| 402 |
|
---|
| 403 | if (M && N) {
|
---|
[14317] | 404 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
| 405 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
---|
| 406 | py_matrix=py_matrix2;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[14234] | 409 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
| 410 | /*retrieve internal value: */
|
---|
| 411 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 412 |
|
---|
| 413 | /*transform into bool matrix: */
|
---|
| 414 | matrix=xNew<bool>(M*N);
|
---|
| 415 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[14316] | 418 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
[14234] | 419 | /*retrieve internal value: */
|
---|
| 420 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 421 |
|
---|
| 422 | /*transform into bool matrix: */
|
---|
| 423 | matrix=xNew<bool>(M*N);
|
---|
| 424 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
| 428 | /*retrieve internal value: */
|
---|
| 429 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
| 430 |
|
---|
| 431 | /*copy matrix: */
|
---|
| 432 | matrix=xNew<bool>(M*N);
|
---|
| 433 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | else
|
---|
| 437 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 438 |
|
---|
| 439 | if (py_matrix2)
|
---|
| 440 | delete(py_matrix2);
|
---|
[14234] | 441 | }
|
---|
| 442 | else
|
---|
| 443 | matrix=NULL;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | else {
|
---|
| 447 | M=1;
|
---|
| 448 | N=1;
|
---|
| 449 | matrix=xNew<bool>(M*N);
|
---|
| 450 | FetchData(&(matrix[0]),py_matrix);
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /*output: */
|
---|
| 454 | if(pM)*pM=M;
|
---|
| 455 | if(pN)*pN=N;
|
---|
| 456 | if(pmatrix)*pmatrix=matrix;
|
---|
| 457 | }
|
---|
| 458 | /*}}}*/
|
---|
[12365] | 459 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
[12120] | 460 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
[12112] | 461 |
|
---|
[12120] | 462 | /*output: */
|
---|
[13372] | 463 | double* dvector=NULL;
|
---|
[12120] | 464 | double* vector=NULL;
|
---|
[23231] | 465 | int M=0;
|
---|
[13373] | 466 | int ndim;
|
---|
[12120] | 467 | npy_intp* dims=NULL;
|
---|
| 468 |
|
---|
[13990] | 469 | /*intermediary:*/
|
---|
| 470 | long* lvector=NULL;
|
---|
| 471 | bool* bvector=NULL;
|
---|
| 472 | int i;
|
---|
[14317] | 473 | PyObject* py_vector2=NULL;
|
---|
[13990] | 474 |
|
---|
[14236] | 475 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 476 | /*retrieve dimensions: */
|
---|
| 477 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 478 | if (ndim==1) {
|
---|
| 479 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 480 | M=dims[0];
|
---|
[14236] | 481 | }
|
---|
| 482 | else if (ndim==2) {
|
---|
| 483 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 484 | if (dims[1]==1)
|
---|
[23708] | 485 | M=dims[0];
|
---|
[14236] | 486 | else
|
---|
| 487 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 488 | }
|
---|
| 489 | else
|
---|
| 490 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[13622] | 491 |
|
---|
[14236] | 492 | if (M) {
|
---|
[14317] | 493 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 494 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_DOUBLE,ndim,ndim);
|
---|
| 495 | py_vector=py_vector2;
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[14236] | 498 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 499 | /*retrieve internal value: */
|
---|
| 500 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[12120] | 501 |
|
---|
[14236] | 502 | /*copy vector: */
|
---|
| 503 | vector=xNew<double>(M);
|
---|
| 504 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
| 505 | }
|
---|
[13990] | 506 |
|
---|
[14316] | 507 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 508 | /*retrieve internal value: */
|
---|
| 509 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 510 |
|
---|
[14236] | 511 | /*transform into double vector: */
|
---|
| 512 | vector=xNew<double>(M);
|
---|
| 513 | for(i=0;i<M;i++)vector[i]=(double)lvector[i];
|
---|
| 514 | }
|
---|
[13990] | 515 |
|
---|
[14236] | 516 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 517 | /*retrieve internal value: */
|
---|
| 518 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[13990] | 519 |
|
---|
[14236] | 520 | /*transform into double vector: */
|
---|
| 521 | vector=xNew<double>(M);
|
---|
| 522 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | else
|
---|
| 526 | _error_("unrecognized double pyarray type in input!");
|
---|
[14317] | 527 |
|
---|
[21201] | 528 | /* Causing a seg fault.
|
---|
[14317] | 529 | if (py_vector2)
|
---|
| 530 | delete(py_vector2);
|
---|
[21201] | 531 | */
|
---|
[13990] | 532 | }
|
---|
| 533 | else
|
---|
[14236] | 534 | vector=NULL;
|
---|
[13484] | 535 | }
|
---|
[13372] | 536 |
|
---|
[14236] | 537 | else {
|
---|
| 538 | M=1;
|
---|
| 539 | vector=xNew<double>(M);
|
---|
| 540 | FetchData(&(vector[0]),py_vector);
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[12120] | 543 | /*output: */
|
---|
| 544 | if(pM)*pM=M;
|
---|
| 545 | if(pvector)*pvector=vector;
|
---|
| 546 | }
|
---|
| 547 | /*}}}*/
|
---|
[22674] | 548 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 549 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
---|
| 550 |
|
---|
| 551 | /*output: */
|
---|
| 552 | float* vector=NULL;
|
---|
[23231] | 553 | int M=0;
|
---|
[22674] | 554 | int ndim;
|
---|
| 555 | npy_intp* dims=NULL;
|
---|
| 556 |
|
---|
| 557 | /*intermediary:*/
|
---|
| 558 | long* lvector=NULL;
|
---|
| 559 | bool* bvector=NULL;
|
---|
| 560 | double* dvector=NULL;
|
---|
| 561 | int i;
|
---|
| 562 | PyObject* py_vector2=NULL;
|
---|
| 563 |
|
---|
| 564 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 565 | /*retrieve dimensions: */
|
---|
| 566 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 567 | if (ndim==1) {
|
---|
| 568 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 569 | M=dims[0];
|
---|
[22674] | 570 | }
|
---|
| 571 | else if (ndim==2) {
|
---|
| 572 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 573 | if (dims[1]==1)
|
---|
[23708] | 574 | M=dims[0];
|
---|
[22674] | 575 | else
|
---|
| 576 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 577 | }
|
---|
| 578 | else
|
---|
| 579 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 580 |
|
---|
| 581 | if (M) {
|
---|
| 582 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 583 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 584 | py_vector=py_vector2;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
| 587 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 588 | /*retrieve internal value: */
|
---|
| 589 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 590 |
|
---|
| 591 | /*transform into int vector: */
|
---|
| 592 | vector=xNew<float>(M);
|
---|
[23231] | 593 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
---|
[22674] | 594 | }
|
---|
| 595 |
|
---|
| 596 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
| 597 | /*retrieve internal value: */
|
---|
| 598 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 599 |
|
---|
| 600 | /*transform into int vector: */
|
---|
| 601 | vector=xNew<float>(M);
|
---|
| 602 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 606 | /*retrieve internal value: */
|
---|
| 607 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
| 608 |
|
---|
| 609 | /*transform into int vector: */
|
---|
| 610 | vector=xNew<float>(M);
|
---|
| 611 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
---|
| 612 | }
|
---|
| 613 |
|
---|
| 614 | else
|
---|
| 615 | _error_("unrecognized int pyarray type in input!");
|
---|
| 616 |
|
---|
| 617 | if(py_vector2) delete(py_vector2);
|
---|
| 618 | }
|
---|
| 619 | else
|
---|
[23231] | 620 | vector=NULL;
|
---|
[22674] | 621 | }
|
---|
| 622 | else{
|
---|
| 623 | M=1;
|
---|
| 624 | vector=xNew<float>(M);
|
---|
| 625 | FetchData(&(vector[0]),py_vector);
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /*output: */
|
---|
| 629 | if(pM)*pM=M;
|
---|
| 630 | if(pvector)*pvector=vector;
|
---|
| 631 | }
|
---|
| 632 | /*}}}*/
|
---|
[14221] | 633 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 634 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
[13310] | 635 |
|
---|
[14221] | 636 | /*output: */
|
---|
| 637 | int* vector=NULL;
|
---|
[23231] | 638 | int M=0;
|
---|
[14221] | 639 | int ndim;
|
---|
| 640 | npy_intp* dims=NULL;
|
---|
| 641 |
|
---|
| 642 | /*intermediary:*/
|
---|
| 643 | long* lvector=NULL;
|
---|
| 644 | bool* bvector=NULL;
|
---|
| 645 | double* dvector=NULL;
|
---|
| 646 | int i;
|
---|
[14317] | 647 | PyObject* py_vector2=NULL;
|
---|
[14221] | 648 |
|
---|
[14236] | 649 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 650 | /*retrieve dimensions: */
|
---|
| 651 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 652 | if (ndim==1) {
|
---|
| 653 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 654 | M=dims[0];
|
---|
[14236] | 655 | }
|
---|
| 656 | else if (ndim==2) {
|
---|
| 657 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 658 | if (dims[1]==1)
|
---|
[23708] | 659 | M=dims[0];
|
---|
[14236] | 660 | else
|
---|
| 661 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 662 | }
|
---|
| 663 | else
|
---|
| 664 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14221] | 665 |
|
---|
[14236] | 666 | if (M) {
|
---|
[14317] | 667 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 668 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
| 669 | py_vector=py_vector2;
|
---|
| 670 | }
|
---|
| 671 |
|
---|
[14236] | 672 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 673 | /*retrieve internal value: */
|
---|
| 674 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 675 |
|
---|
[14236] | 676 | /*transform into int vector: */
|
---|
| 677 | vector=xNew<int>(M);
|
---|
[23231] | 678 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
---|
[14236] | 679 | }
|
---|
[14221] | 680 |
|
---|
[14316] | 681 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 682 | /*retrieve internal value: */
|
---|
| 683 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 684 |
|
---|
[14236] | 685 | /*transform into int vector: */
|
---|
| 686 | vector=xNew<int>(M);
|
---|
| 687 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
| 688 | }
|
---|
[14221] | 689 |
|
---|
[14236] | 690 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 691 | /*retrieve internal value: */
|
---|
| 692 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14221] | 693 |
|
---|
[14236] | 694 | /*transform into int vector: */
|
---|
| 695 | vector=xNew<int>(M);
|
---|
| 696 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | else
|
---|
| 700 | _error_("unrecognized int pyarray type in input!");
|
---|
[14317] | 701 |
|
---|
| 702 | if (py_vector2)
|
---|
| 703 | delete(py_vector2);
|
---|
[14221] | 704 | }
|
---|
| 705 | else
|
---|
[14236] | 706 | vector=NULL;
|
---|
[14221] | 707 | }
|
---|
| 708 |
|
---|
[14236] | 709 | else {
|
---|
| 710 | M=1;
|
---|
| 711 | vector=xNew<int>(M);
|
---|
| 712 | FetchData(&(vector[0]),py_vector);
|
---|
| 713 | }
|
---|
| 714 |
|
---|
[14221] | 715 | /*output: */
|
---|
| 716 | if(pM)*pM=M;
|
---|
| 717 | if(pvector)*pvector=vector;
|
---|
| 718 | }
|
---|
| 719 | /*}}}*/
|
---|
[14234] | 720 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
| 721 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
[14221] | 722 |
|
---|
[14234] | 723 | /*output: */
|
---|
| 724 | bool* bvector=NULL;
|
---|
| 725 | bool* vector=NULL;
|
---|
[23231] | 726 | int M=0;
|
---|
[14234] | 727 | int ndim;
|
---|
| 728 | npy_intp* dims=NULL;
|
---|
| 729 |
|
---|
| 730 | /*intermediary:*/
|
---|
| 731 | double* dvector=NULL;
|
---|
| 732 | long* lvector=NULL;
|
---|
| 733 | int i;
|
---|
[14317] | 734 | PyObject* py_vector2=NULL;
|
---|
[14234] | 735 |
|
---|
[14236] | 736 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
| 737 | /*retrieve dimensions: */
|
---|
| 738 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
| 739 | if (ndim==1) {
|
---|
| 740 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
[23708] | 741 | M=dims[0];
|
---|
[14236] | 742 | }
|
---|
| 743 | else if (ndim==2) {
|
---|
| 744 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
| 745 | if (dims[1]==1)
|
---|
[23708] | 746 | M=dims[0];
|
---|
[14236] | 747 | else
|
---|
| 748 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
| 749 | }
|
---|
| 750 | else
|
---|
| 751 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
[14234] | 752 |
|
---|
[14236] | 753 | if (M) {
|
---|
[14317] | 754 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
| 755 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
---|
| 756 | py_vector=py_vector2;
|
---|
| 757 | }
|
---|
| 758 |
|
---|
[14236] | 759 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
| 760 | /*retrieve internal value: */
|
---|
| 761 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 762 |
|
---|
[14236] | 763 | /*transform into bool vector: */
|
---|
| 764 | vector=xNew<bool>(M);
|
---|
| 765 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
| 766 | }
|
---|
[14234] | 767 |
|
---|
[14316] | 768 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
[14236] | 769 | /*retrieve internal value: */
|
---|
| 770 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 771 |
|
---|
[14236] | 772 | /*transform into bool vector: */
|
---|
| 773 | vector=xNew<bool>(M);
|
---|
| 774 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
| 775 | }
|
---|
[14234] | 776 |
|
---|
[14236] | 777 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
| 778 | /*retrieve internal value: */
|
---|
| 779 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
[14234] | 780 |
|
---|
[14236] | 781 | /*copy vector: */
|
---|
| 782 | vector=xNew<bool>(M);
|
---|
| 783 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | else
|
---|
| 787 | _error_("unrecognized bool pyarray type in input!");
|
---|
[14317] | 788 |
|
---|
| 789 | if (py_vector2)
|
---|
| 790 | delete(py_vector2);
|
---|
[14234] | 791 | }
|
---|
| 792 | else
|
---|
[14236] | 793 | vector=NULL;
|
---|
[14234] | 794 | }
|
---|
| 795 |
|
---|
[14236] | 796 | else {
|
---|
| 797 | M=1;
|
---|
| 798 | vector=xNew<bool>(M);
|
---|
| 799 | FetchData(&(vector[0]),py_vector);
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[14234] | 802 | /*output: */
|
---|
| 803 | if(pM)*pM=M;
|
---|
| 804 | if(pvector)*pvector=vector;
|
---|
| 805 | }
|
---|
| 806 | /*}}}*/
|
---|
| 807 |
|
---|
[13310] | 808 | /*ISSM objects*/
|
---|
| 809 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
| 810 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
| 811 |
|
---|
| 812 | /*Initialize output*/
|
---|
| 813 | BamgGeom* bamggeom=new BamgGeom();
|
---|
| 814 |
|
---|
| 815 | /*Fetch all fields*/
|
---|
| 816 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 817 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 818 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
| 819 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
| 820 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
| 821 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 822 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
| 823 |
|
---|
| 824 | /*Assign output pointers:*/
|
---|
| 825 | *pbamggeom=bamggeom;
|
---|
[13287] | 826 | }
|
---|
| 827 | /*}}}*/
|
---|
[13310] | 828 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
| 829 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
| 830 |
|
---|
| 831 | /*Initialize output*/
|
---|
| 832 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
| 833 |
|
---|
| 834 | /*Fetch all fields*/
|
---|
| 835 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
| 836 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
| 837 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
| 838 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
| 839 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
| 840 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
| 841 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
| 842 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
| 843 |
|
---|
| 844 | /*Assign output pointers:*/
|
---|
| 845 | *pbamgmesh=bamgmesh;
|
---|
[13287] | 846 | }
|
---|
| 847 | /*}}}*/
|
---|
[13310] | 848 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
| 849 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
| 850 |
|
---|
| 851 | /*Initialize output*/
|
---|
| 852 | BamgOpts* bamgopts=new BamgOpts();
|
---|
| 853 |
|
---|
| 854 | /*Fetch all fields*/
|
---|
| 855 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
| 856 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
| 857 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
| 858 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
| 859 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
| 860 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
| 861 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
| 862 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
| 863 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
| 864 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
| 865 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
| 866 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
| 867 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
| 868 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
| 869 |
|
---|
| 870 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
| 871 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
| 872 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
| 873 |
|
---|
| 874 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
| 875 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
| 876 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
| 877 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
| 878 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesSize[0],&bamgopts->hVerticesSize[1],PyDict_GetItemString(py_dict,"hVertices"));
|
---|
| 879 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
| 880 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
| 881 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
| 882 |
|
---|
| 883 | /*Additional checks*/
|
---|
| 884 | bamgopts->Check();
|
---|
| 885 |
|
---|
| 886 | /*Assign output pointers:*/
|
---|
| 887 | *pbamgopts=bamgopts;
|
---|
[13287] | 888 | }
|
---|
| 889 | /*}}}*/
|
---|
[13374] | 890 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
[13373] | 891 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
[12120] | 892 |
|
---|
[13373] | 893 | char *name = NULL;
|
---|
| 894 | Option *option = NULL;
|
---|
| 895 |
|
---|
[12776] | 896 | /*Initialize output*/
|
---|
| 897 | Options* options=new Options();
|
---|
| 898 |
|
---|
[13373] | 899 | /*Fetch all options*/
|
---|
| 900 | for (int i=istart; i<nrhs; i=i+2){
|
---|
[19896] | 901 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 902 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
[19896] | 903 | #else
|
---|
| 904 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
| 905 | #endif
|
---|
[12776] | 906 |
|
---|
[13373] | 907 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
| 908 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
| 909 |
|
---|
[15105] | 910 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
---|
[13373] | 911 |
|
---|
| 912 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
| 913 | // options->AddOption(option);
|
---|
| 914 | // option=NULL;
|
---|
| 915 | }
|
---|
| 916 |
|
---|
[12776] | 917 | /*Assign output pointers:*/
|
---|
| 918 | *poptions=options;
|
---|
| 919 | }
|
---|
| 920 | /*}}}*/
|
---|
[15335] | 921 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
---|
| 922 | void FetchData(Contours** pcontours,PyObject* py_list){
|
---|
[12776] | 923 |
|
---|
[13368] | 924 | int numcontours,test1,test2;
|
---|
| 925 | char *contourname = NULL;
|
---|
[15335] | 926 | Contours *contours = NULL;
|
---|
[13368] | 927 | Contour<double> *contouri = NULL;
|
---|
| 928 | PyObject *py_dicti = NULL;
|
---|
| 929 | PyObject *py_item = NULL;
|
---|
| 930 |
|
---|
[19896] | 931 | #if _PYTHON_MAJOR_ >= 3
|
---|
[19895] | 932 | if (PyUnicode_Check(py_list)){
|
---|
[13368] | 933 | FetchData(&contourname,py_list);
|
---|
[14377] | 934 | contours=ExpRead<double>(contourname);
|
---|
[13368] | 935 | }
|
---|
[19896] | 936 | #else
|
---|
| 937 | if (PyString_Check(py_list)){
|
---|
| 938 | FetchData(&contourname,py_list);
|
---|
| 939 | contours=ExpRead<double>(contourname);
|
---|
| 940 | }
|
---|
| 941 | #endif
|
---|
[13368] | 942 | else if(PyList_Check(py_list)){
|
---|
| 943 |
|
---|
[15335] | 944 | contours=new Contours();
|
---|
[13368] | 945 | numcontours=(int)PyList_Size(py_list);
|
---|
| 946 |
|
---|
| 947 | for(int i=0;i<numcontours;i++){
|
---|
| 948 |
|
---|
[15335] | 949 | contouri=new Contour<double>();
|
---|
[13368] | 950 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
| 951 |
|
---|
| 952 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
| 953 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
| 954 | FetchData(&contouri->nods,py_item);
|
---|
| 955 |
|
---|
| 956 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
| 957 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
| 958 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
| 959 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
| 960 |
|
---|
| 961 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
| 962 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
| 963 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
| 964 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
| 965 |
|
---|
| 966 | contours->AddObject(contouri);
|
---|
| 967 | }
|
---|
| 968 | }
|
---|
| 969 | else{
|
---|
| 970 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 | /*clean-up and assign output pointer*/
|
---|
[13372] | 974 | xDelete<char>(contourname);
|
---|
[13368] | 975 | *pcontours=contours;
|
---|
[13353] | 976 | }
|
---|
[23231] | 977 |
|
---|
| 978 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
---|
| 979 |
|
---|
| 980 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
---|
| 981 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
---|
| 982 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
---|
| 983 |
|
---|
| 984 | /*Fetch adjacency matrix:*/
|
---|
| 985 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
---|
| 986 |
|
---|
| 987 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
---|
| 988 | pyGetJc(a,nvtxs,mwstart);
|
---|
| 989 |
|
---|
| 990 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
---|
| 991 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
---|
[23708] | 992 |
|
---|
[23231] | 993 | int* start = xNew<int>(nvtxs+1);
|
---|
| 994 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
---|
| 995 | int* adjacency = xNew<int>(nzmax);
|
---|
| 996 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
---|
| 997 |
|
---|
| 998 | /*Get edges weights*/
|
---|
| 999 | int nedges = start[nvtxs];
|
---|
| 1000 | float* ewgts = NULL;
|
---|
| 1001 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
---|
| 1002 | //size may be 1 and still be empty;
|
---|
| 1003 | //presumably size of edge_weights input will never be exactly 1
|
---|
| 1004 | if(size != 1 && size != 0){
|
---|
| 1005 | ewgts = xNewZeroInit<float>(nedges);
|
---|
| 1006 | for(int i = 0; i<nedges;i++){
|
---|
| 1007 | ewgts[i] = (float)a[i];
|
---|
| 1008 | }
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | /*Assign output pointers*/
|
---|
| 1012 | *pnvtxs = nvtxs;
|
---|
| 1013 | *padjacency = adjacency;
|
---|
| 1014 | *pstart = start;
|
---|
| 1015 | *pewgts = ewgts;
|
---|
| 1016 | }
|
---|
[13353] | 1017 | /*}}}*/
|
---|
[23231] | 1018 |
|
---|
| 1019 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
---|
| 1020 | /*{{{*/
|
---|
| 1021 | //get the number of non-zero elements in each row, adding to the prior number;
|
---|
| 1022 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
---|
| 1023 | // eg: 0, 1, 3, 6, 8, 13, ...
|
---|
| 1024 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
---|
| 1025 | int c = 0;
|
---|
| 1026 | Jc[c++] = 0;
|
---|
| 1027 |
|
---|
| 1028 | for(int i=0;i<nvtxs;i++){
|
---|
| 1029 | for(int j=0;j<nvtxs;j++){
|
---|
| 1030 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1031 | Jc[c] += 1;
|
---|
| 1032 | }
|
---|
| 1033 | }
|
---|
| 1034 | c++;
|
---|
| 1035 | Jc[c] = Jc[c-1];
|
---|
| 1036 | }
|
---|
| 1037 | return;
|
---|
[22674] | 1038 | }
|
---|
[23231] | 1039 | /*}}}*/
|
---|
[13353] | 1040 |
|
---|
[23231] | 1041 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
---|
| 1042 | /*{{{*/
|
---|
| 1043 | //get the row-wise position of each non-zero element;
|
---|
| 1044 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
---|
| 1045 | // eg: 10, 24, 25, 4, ...
|
---|
| 1046 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
---|
| 1047 | // using pyGetJc to determine which indices correspond to which row
|
---|
| 1048 | int r = 0;
|
---|
| 1049 |
|
---|
| 1050 | for(int i=0;i<nvtxs;i++){
|
---|
| 1051 | for(int j=0;j<nvtxs;j++){
|
---|
| 1052 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
| 1053 | Ir[r] = j;
|
---|
| 1054 | r++;
|
---|
| 1055 | }
|
---|
| 1056 | }
|
---|
| 1057 | }
|
---|
| 1058 | return;
|
---|
| 1059 | }
|
---|
| 1060 | /*}}}*/
|
---|
| 1061 |
|
---|
[12073] | 1062 | /*Python version dependent: */
|
---|
[23708] | 1063 | /* #if _PYTHON_MAJOR_ >= 3 */
|
---|
| 1064 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
---|
| 1065 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
---|
[12073] | 1066 |
|
---|
[23708] | 1067 | /* PyObject* py_bytes; */
|
---|
| 1068 | /* char* string=NULL; */
|
---|
[12073] | 1069 |
|
---|
[23708] | 1070 | /* /\*convert to bytes format: *\/ */
|
---|
| 1071 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
---|
[12073] | 1072 |
|
---|
[23708] | 1073 | /* /\*convert from bytes to string: *\/ */
|
---|
| 1074 | /* string=PyBytes_AsUTF8(py_bytes); */
|
---|
[13622] | 1075 |
|
---|
[23708] | 1076 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
---|
| 1077 | /* *pstring=xNew<char>(strlen(string)+1); */
|
---|
| 1078 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
---|
| 1079 | /* } */
|
---|
| 1080 | /* /\*}}}*\/ */
|
---|
| 1081 | /* #else */
|
---|
[12365] | 1082 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
[12073] | 1083 | void FetchData(char** pstring,PyObject* py_string){
|
---|
| 1084 |
|
---|
| 1085 | char* string=NULL;
|
---|
| 1086 |
|
---|
| 1087 | /*extract internal string: */
|
---|
[23708] | 1088 | #if _PYTHON_MAJOR_ == 3
|
---|
| 1089 | string=PyUnicode_AsUTF8(py_string);
|
---|
| 1090 | #else
|
---|
[12073] | 1091 | string=PyString_AsString(py_string);
|
---|
[23708] | 1092 | #endif
|
---|
[13372] | 1093 | /*copy string (note strlen does not include trailing NULL): */
|
---|
| 1094 | *pstring=xNew<char>(strlen(string)+1);
|
---|
| 1095 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
[12073] | 1096 | }
|
---|
| 1097 | /*}}}*/
|
---|
[23708] | 1098 | //#endif
|
---|