1 | /*\file FetchData.cpp:
|
---|
2 | * \brief: general I/O interface to fetch data in python
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifdef HAVE_CONFIG_H
|
---|
6 | #include <config.h>
|
---|
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 |
|
---|
14 | #include "./pythonio.h"
|
---|
15 | #include "../../c/shared/shared.h"
|
---|
16 |
|
---|
17 | /*Primitive data types*/
|
---|
18 | /*FUNCTION FetchData(double* pscalar,PyObject* py_float){{{*/
|
---|
19 | void FetchData(double* pscalar,PyObject* py_float){
|
---|
20 |
|
---|
21 | double dscalar;
|
---|
22 |
|
---|
23 | /*return internal value: */
|
---|
24 | #if _PYTHON_MAJOR_ == 3
|
---|
25 | if (PyFloat_Check(py_float))
|
---|
26 | dscalar=PyFloat_AsDouble(py_float);
|
---|
27 | else if (PyLong_Check(py_float))
|
---|
28 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
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 py3 in input!");
|
---|
37 |
|
---|
38 | #else
|
---|
39 | if (PyFloat_Check(py_float))
|
---|
40 | dscalar=PyFloat_AsDouble(py_float);
|
---|
41 | else if (PyLong_Check(py_float))
|
---|
42 | dscalar=PyLong_AsDouble(py_float);
|
---|
43 | else if (PyInt_Check(py_float))
|
---|
44 | dscalar=(double)PyInt_AsLong(py_float);
|
---|
45 | else if (PyBool_Check(py_float))
|
---|
46 | dscalar=(double)PyLong_AsLong(py_float);
|
---|
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));
|
---|
51 | else
|
---|
52 | _error_("unrecognized float type in py2 input!");
|
---|
53 | #endif
|
---|
54 | /*output: */
|
---|
55 | *pscalar=dscalar;
|
---|
56 | }
|
---|
57 | /*}}}*/
|
---|
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: */
|
---|
64 | #if _PYTHON_MAJOR_ == 3
|
---|
65 | if (PyFloat_Check(py_float))
|
---|
66 | fscalar=PyFloat_AsDouble(py_float);
|
---|
67 | else if (PyLong_Check(py_float))
|
---|
68 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
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))
|
---|
81 | fscalar=(float)PyLong_AsDouble(py_float);
|
---|
82 | else if (PyInt_Check(py_float))
|
---|
83 | fscalar=(float)PyInt_AsLong(py_float);
|
---|
84 | else if (PyBool_Check(py_float))
|
---|
85 | fscalar=(float)PyLong_AsLong(py_float);
|
---|
86 | else if (PyTuple_Check(py_float) && (int)PyTuple_Size(py_float)==1)
|
---|
87 | FetchData(&fscalar,PyTuple_GetItem(py_float,(Py_ssize_t)0));
|
---|
88 | else if (PyList_Check(py_float) && (int)PyList_Size(py_float)==1)
|
---|
89 | FetchData(&fscalar,PyList_GetItem(py_float,(Py_ssize_t)0));
|
---|
90 | else
|
---|
91 | _error_("unrecognized float type in input!");
|
---|
92 | #endif
|
---|
93 | /*output: */
|
---|
94 | *pscalar=fscalar;
|
---|
95 | }
|
---|
96 | /*}}}*/
|
---|
97 | /*FUNCTION FetchData(int* pscalar,PyObject* py_long){{{*/
|
---|
98 | void FetchData(int* pscalar, PyObject* py_long){
|
---|
99 |
|
---|
100 | int iscalar;
|
---|
101 |
|
---|
102 | /*return internal value: */
|
---|
103 | #if _PYTHON_MAJOR_ == 3
|
---|
104 | if (PyLong_Check(py_long))
|
---|
105 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
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);
|
---|
120 | else if (PyInt_Check(py_long))
|
---|
121 | iscalar=(int)PyInt_AsLong(py_long);
|
---|
122 | else if (PyFloat_Check(py_long))
|
---|
123 | iscalar=(int)PyFloat_AsDouble(py_long);
|
---|
124 | else if (PyBool_Check(py_long))
|
---|
125 | iscalar=(int)PyLong_AsLong(py_long);
|
---|
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));
|
---|
130 | else
|
---|
131 | _error_("unrecognized long type in input!");
|
---|
132 | #endif
|
---|
133 | /*output: */
|
---|
134 | *pscalar=iscalar;
|
---|
135 | }
|
---|
136 | /*}}}*/
|
---|
137 | /*FUNCTION FetchData(bool* pscalar,PyObject* py_boolean){{{*/
|
---|
138 | void FetchData(bool* pscalar,PyObject* py_boolean){
|
---|
139 |
|
---|
140 | bool bscalar;
|
---|
141 |
|
---|
142 | /*return internal value: */
|
---|
143 | #if _PYTHON_MAJOR_ == 3
|
---|
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);
|
---|
148 | else if (PyLong_Check(py_boolean))
|
---|
149 | bscalar=(bool)PyLong_AsLong(py_boolean);
|
---|
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);
|
---|
164 | else if (PyInt_Check(py_boolean))
|
---|
165 | bscalar=(bool)PyInt_AsLong(py_boolean);
|
---|
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!");
|
---|
172 | #endif
|
---|
173 | /*output: */
|
---|
174 | *pscalar=bscalar;
|
---|
175 | }
|
---|
176 | /*}}}*/
|
---|
177 | /*FUNCTION FetchData(double** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
178 | void FetchData(double** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
179 |
|
---|
180 | /*output: */
|
---|
181 | double* dmatrix=NULL;
|
---|
182 | double* matrix=NULL;
|
---|
183 | int M=0;
|
---|
184 | int N=0;
|
---|
185 | int ndim;
|
---|
186 | npy_intp* dims=NULL;
|
---|
187 |
|
---|
188 | /*intermediary:*/
|
---|
189 | long* lmatrix=NULL;
|
---|
190 | bool* bmatrix=NULL;
|
---|
191 | int i;
|
---|
192 | PyObject* py_matrix2=NULL;
|
---|
193 |
|
---|
194 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
195 | /*retrieve dimensions: */
|
---|
196 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
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!");
|
---|
207 |
|
---|
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 | }
|
---|
213 |
|
---|
214 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
215 | /*retrieve internal value: */
|
---|
216 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
217 |
|
---|
218 | /*copy matrix: */
|
---|
219 | matrix=xNew<double>(M*N);
|
---|
220 | memcpy(matrix,dmatrix,(M*N)*sizeof(double));
|
---|
221 | }
|
---|
222 |
|
---|
223 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
224 | /*retrieve internal value: */
|
---|
225 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
226 |
|
---|
227 | /*transform into double matrix: */
|
---|
228 | matrix=xNew<double>(M*N);
|
---|
229 | for(i=0;i<M*N;i++)matrix[i]=(double)lmatrix[i];
|
---|
230 | }
|
---|
231 |
|
---|
232 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
233 | /*retrieve internal value: */
|
---|
234 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
235 |
|
---|
236 | /*transform into double matrix: */
|
---|
237 | matrix=xNew<double>(M*N);
|
---|
238 | for(i=0;i<M*N;i++)matrix[i]=(double)bmatrix[i];
|
---|
239 | }
|
---|
240 |
|
---|
241 | else
|
---|
242 | _error_("unrecognized double pyarray type in input!");
|
---|
243 |
|
---|
244 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
245 | }
|
---|
246 | else
|
---|
247 | matrix=NULL;
|
---|
248 | }
|
---|
249 | else if (PyList_Check(py_matrix)) {
|
---|
250 | /*retrieve dimensions: */
|
---|
251 | M=(int)PyList_Size(py_matrix);
|
---|
252 | N=1
|
---|
253 |
|
---|
254 | if (M) {
|
---|
255 | matrix=xNew<double>(M);
|
---|
256 | for (int index = 0; index < M; index++) {
|
---|
257 | PyObject *item;
|
---|
258 | item = PyList_GetItem(py_matrix, index);
|
---|
259 | if ((int)PyList_Size(item)>1)
|
---|
260 | _error_("2D lists are not suported");
|
---|
261 | FetchData(&(matrix[index]),item);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
265 | else
|
---|
266 | matrix=NULL;
|
---|
267 | }
|
---|
268 | else if (PyTuple_Check(py_matrix)) {
|
---|
269 | /*retrieve dimensions: */
|
---|
270 | M=(int)PyTuple_Size(py_matrix);
|
---|
271 | N=1
|
---|
272 | if (M) {
|
---|
273 | matrix=xNew<double>(M);
|
---|
274 | for (int index = 0; index < M; index++) {
|
---|
275 | PyObject *item;
|
---|
276 | item = PyTuple_GetItem(py_matrix, index);
|
---|
277 | if ((int)PyTuple_Size(item)>1)
|
---|
278 | _error_("2D tuple are not suported");
|
---|
279 | FetchData(&(matrix[index]),item);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
283 | else
|
---|
284 | matrix=NULL;
|
---|
285 | }
|
---|
286 |
|
---|
287 | else {
|
---|
288 | M=1;
|
---|
289 | N=1;
|
---|
290 | matrix=xNew<double>(M*N);
|
---|
291 | FetchData(&(matrix[0]),py_matrix);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*output: */
|
---|
295 | if(pM)*pM=M;
|
---|
296 | if(pN)*pN=N;
|
---|
297 | if(pmatrix)*pmatrix=matrix;
|
---|
298 | }
|
---|
299 | /*}}}*/
|
---|
300 | /*FUNCTION FetchData(int** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
301 | void FetchData(int** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
302 |
|
---|
303 | /*output: */
|
---|
304 | int* matrix=NULL;
|
---|
305 | int M=0;
|
---|
306 | int N=0;
|
---|
307 | int ndim;
|
---|
308 | npy_intp* dims=NULL;
|
---|
309 |
|
---|
310 | /*intermediary:*/
|
---|
311 | double* dmatrix=NULL;
|
---|
312 | long* lmatrix=NULL;
|
---|
313 | bool* bmatrix=NULL;
|
---|
314 | int i;
|
---|
315 | PyObject* py_matrix2=NULL;
|
---|
316 |
|
---|
317 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
318 | /*retrieve dimensions: */
|
---|
319 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
320 | if (ndim==2) {
|
---|
321 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
322 | M=dims[0]; N=dims[1];
|
---|
323 | }
|
---|
324 | else if (ndim==1) {
|
---|
325 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
326 | M=dims[0]; N=1;
|
---|
327 | }
|
---|
328 | else
|
---|
329 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
330 |
|
---|
331 | if (M && N) {
|
---|
332 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
333 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_LONG,ndim,ndim);
|
---|
334 | py_matrix=py_matrix2;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
338 | /*retrieve internal value: */
|
---|
339 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
340 |
|
---|
341 | /*transform into integer matrix: */
|
---|
342 | matrix=xNew<int>(M*N);
|
---|
343 | for(i=0;i<M*N;i++)matrix[i]=(int)dmatrix[i];
|
---|
344 | }
|
---|
345 |
|
---|
346 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
347 | /*retrieve internal value: */
|
---|
348 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
349 |
|
---|
350 | /*transform into integer matrix: */
|
---|
351 | matrix=xNew<int>(M*N);
|
---|
352 | for(i=0;i<M*N;i++)matrix[i]=(int)lmatrix[i];
|
---|
353 | }
|
---|
354 |
|
---|
355 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
356 | /*retrieve internal value: */
|
---|
357 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
358 |
|
---|
359 | /*transform into integer matrix: */
|
---|
360 | matrix=xNew<int>(M*N);
|
---|
361 | for(i=0;i<M*N;i++)matrix[i]=(int)bmatrix[i];
|
---|
362 | }
|
---|
363 |
|
---|
364 | else
|
---|
365 | _error_("unrecognized int pyarray type in input!");
|
---|
366 |
|
---|
367 | /* These lines are causing a segfault
|
---|
368 | if (py_matrix2)
|
---|
369 | delete(py_matrix2);
|
---|
370 | */
|
---|
371 | }
|
---|
372 | else
|
---|
373 | matrix=NULL;
|
---|
374 | }
|
---|
375 | else if (PyList_Check(py_matrix)) {
|
---|
376 | /*retrieve dimensions: */
|
---|
377 | M=(int)PyList_Size(py_matrix);
|
---|
378 | N=1
|
---|
379 | if (M) {
|
---|
380 | matrix=xNew<int>(M);
|
---|
381 | for (int index = 0; index < M; index++) {
|
---|
382 | PyObject *item;
|
---|
383 | item = PyList_GetItem(py_matrix, index);
|
---|
384 | if ((int)PyList_Size(item)>1)
|
---|
385 | _error_("2D lists are not suported");
|
---|
386 | FetchData(&(matrix[index]),item);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | else
|
---|
390 | matrix=NULL;
|
---|
391 | }
|
---|
392 | else if (PyTuple_Check(py_matrix)) {
|
---|
393 | /*retrieve dimensions: */
|
---|
394 | M=(int)PyTuple_Size(py_matrix);
|
---|
395 | N=1
|
---|
396 | if (M) {
|
---|
397 | matrix=xNew<int>(M);
|
---|
398 | for (int index = 0; index < M; index++) {
|
---|
399 | PyObject *item;
|
---|
400 | item = PyTuple_GetItem(py_matrix, index);
|
---|
401 | if ((int)PyTuple_Size(item)>1)
|
---|
402 | _error_("2D tuple are not suported");
|
---|
403 | FetchData(&(matrix[index]),item);
|
---|
404 | }
|
---|
405 | }
|
---|
406 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
407 | else
|
---|
408 | matrix=NULL;
|
---|
409 | }
|
---|
410 |
|
---|
411 | else {
|
---|
412 | M=1;
|
---|
413 | N=1;
|
---|
414 | matrix=xNew<int>(M*N);
|
---|
415 | FetchData(&(matrix[0]),py_matrix);
|
---|
416 | }
|
---|
417 |
|
---|
418 | /*output: */
|
---|
419 | if(pM)*pM=M;
|
---|
420 | if(pN)*pN=N;
|
---|
421 | if(pmatrix)*pmatrix=matrix;
|
---|
422 | }
|
---|
423 | /*}}}*/
|
---|
424 | /*FUNCTION FetchData(bool** pmatrix,int* pM, int* pN, PyObject* py_matrix){{{*/
|
---|
425 | void FetchData(bool** pmatrix,int* pM,int *pN,PyObject* py_matrix){
|
---|
426 |
|
---|
427 | /*output: */
|
---|
428 | bool* bmatrix=NULL;
|
---|
429 | bool* matrix=NULL;
|
---|
430 | int M=0;
|
---|
431 | int N=0;
|
---|
432 | int ndim;
|
---|
433 | npy_intp* dims=NULL;
|
---|
434 |
|
---|
435 | /*intermediary:*/
|
---|
436 | double* dmatrix=NULL;
|
---|
437 | long* lmatrix=NULL;
|
---|
438 | int i;
|
---|
439 | PyObject* py_matrix2=NULL;
|
---|
440 |
|
---|
441 | if (PyArray_Check((PyArrayObject*)py_matrix)) {
|
---|
442 | /*retrieve dimensions: */
|
---|
443 | ndim=PyArray_NDIM((const PyArrayObject*)py_matrix);
|
---|
444 | if (ndim==2) {
|
---|
445 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
446 | M=dims[0]; N=dims[1];
|
---|
447 | }
|
---|
448 | else if (ndim==1) {
|
---|
449 | dims=PyArray_DIMS((PyArrayObject*)py_matrix);
|
---|
450 | M=dims[0]; N=1;
|
---|
451 | }
|
---|
452 | else
|
---|
453 | _error_("expecting an MxN matrix or M vector in input!");
|
---|
454 |
|
---|
455 | if (M && N) {
|
---|
456 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_matrix)) {
|
---|
457 | py_matrix2=PyArray_ContiguousFromAny(py_matrix,NPY_BOOL,ndim,ndim);
|
---|
458 | py_matrix=py_matrix2;
|
---|
459 | }
|
---|
460 |
|
---|
461 | if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_DOUBLE) {
|
---|
462 | /*retrieve internal value: */
|
---|
463 | dmatrix=(double*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
464 |
|
---|
465 | /*transform into bool matrix: */
|
---|
466 | matrix=xNew<bool>(M*N);
|
---|
467 | for(i=0;i<M*N;i++)matrix[i]=(bool)dmatrix[i];
|
---|
468 | }
|
---|
469 |
|
---|
470 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_LONG) {
|
---|
471 | /*retrieve internal value: */
|
---|
472 | lmatrix=(long*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
473 |
|
---|
474 | /*transform into bool matrix: */
|
---|
475 | matrix=xNew<bool>(M*N);
|
---|
476 | for(i=0;i<M*N;i++)matrix[i]=(bool)lmatrix[i];
|
---|
477 | }
|
---|
478 |
|
---|
479 | else if (PyArray_TYPE((PyArrayObject*)py_matrix) == NPY_BOOL) {
|
---|
480 | /*retrieve internal value: */
|
---|
481 | bmatrix=(bool*)PyArray_DATA((PyArrayObject*)py_matrix);
|
---|
482 |
|
---|
483 | /*copy matrix: */
|
---|
484 | matrix=xNew<bool>(M*N);
|
---|
485 | memcpy(matrix,bmatrix,(M*N)*sizeof(bool));
|
---|
486 | }
|
---|
487 |
|
---|
488 | else
|
---|
489 | _error_("unrecognized bool pyarray type in input!");
|
---|
490 |
|
---|
491 | if (py_matrix2)
|
---|
492 | delete(py_matrix2);
|
---|
493 | }
|
---|
494 | else
|
---|
495 | matrix=NULL;
|
---|
496 | }
|
---|
497 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
498 |
|
---|
499 | else if (PyList_Check(py_matrix)) {
|
---|
500 | /*retrieve dimensions: */
|
---|
501 | M=(int)PyList_Size(py_matrix);
|
---|
502 | N=1;
|
---|
503 | if (M) {
|
---|
504 | matrix=xNew<bool>(M);
|
---|
505 | for (int index = 0; index < M; index++) {
|
---|
506 | PyObject *item;
|
---|
507 | item = PyList_GetItem(py_matrix, index);
|
---|
508 | if ((int)PyList_Size(item)>1)
|
---|
509 | _error_("2D lists are not suported");
|
---|
510 | FetchData(&(matrix[index]),item);
|
---|
511 | }
|
---|
512 | }
|
---|
513 | else
|
---|
514 | matrix=NULL;
|
---|
515 | }
|
---|
516 | else if (PyTuple_Check(py_matrix)) {
|
---|
517 | /*retrieve dimensions: */
|
---|
518 | M=(int)PyTuple_Size(py_matrix);
|
---|
519 | N=1
|
---|
520 | if (M) {
|
---|
521 | matrix=xNew<bool>(M);
|
---|
522 | for (int index = 0; index < M; index++) {
|
---|
523 | PyObject *item;
|
---|
524 | item = PyTuple_GetItem(py_matrix, index);
|
---|
525 | if ((int)PyTuple_Size(item)>1)
|
---|
526 | _error_("2D tuples are not suported");
|
---|
527 | FetchData(&(matrix[index]),item);
|
---|
528 | }
|
---|
529 | }
|
---|
530 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
531 | else
|
---|
532 | matrix=NULL;
|
---|
533 | }
|
---|
534 |
|
---|
535 | else {
|
---|
536 | M=1;
|
---|
537 | N=1;
|
---|
538 | matrix=xNew<bool>(M*N);
|
---|
539 | FetchData(&(matrix[0]),py_matrix);
|
---|
540 | }
|
---|
541 |
|
---|
542 | /*output: */
|
---|
543 | if(pM)*pM=M;
|
---|
544 | if(pN)*pN=N;
|
---|
545 | if(pmatrix)*pmatrix=matrix;
|
---|
546 | }
|
---|
547 | /*}}}*/
|
---|
548 | /*FUNCTION FetchData(double** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
549 | void FetchData(double** pvector,int* pM,PyObject* py_vector){
|
---|
550 |
|
---|
551 | /*output: */
|
---|
552 | double* dvector=NULL;
|
---|
553 | double* vector=NULL;
|
---|
554 | int M=0;
|
---|
555 | int ndim;
|
---|
556 | npy_intp* dims=NULL;
|
---|
557 |
|
---|
558 | /*intermediary:*/
|
---|
559 | long* lvector=NULL;
|
---|
560 | bool* bvector=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);
|
---|
569 | M=dims[0];
|
---|
570 | }
|
---|
571 | else if (ndim==2) {
|
---|
572 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
573 | if (dims[1]==1)
|
---|
574 | M=dims[0];
|
---|
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_DOUBLE,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 | /*copy vector: */
|
---|
592 | vector=xNew<double>(M);
|
---|
593 | memcpy(vector,dvector,(M)*sizeof(double));
|
---|
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 double vector: */
|
---|
601 | vector=xNew<double>(M);
|
---|
602 | for(i=0;i<M;i++)vector[i]=(double)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 double vector: */
|
---|
610 | vector=xNew<double>(M);
|
---|
611 | for(i=0;i<M;i++)vector[i]=(double)bvector[i];
|
---|
612 | }
|
---|
613 |
|
---|
614 | else
|
---|
615 | _error_("unrecognized double pyarray type in input!");
|
---|
616 |
|
---|
617 | /* Causing a seg fault.
|
---|
618 | if (py_vector2)
|
---|
619 | delete(py_vector2);
|
---|
620 | */
|
---|
621 | }
|
---|
622 | else
|
---|
623 | vector=NULL;
|
---|
624 | }
|
---|
625 | else if (PyList_Check(py_vector)) {
|
---|
626 | /*retrieve dimensions: */
|
---|
627 | M=(int)PyList_Size(py_vector);
|
---|
628 |
|
---|
629 | if (M) {
|
---|
630 | vector=xNew<double>(M);
|
---|
631 | for (int index = 0; index < M; index++) {
|
---|
632 | PyObject *item;
|
---|
633 | item = PyList_GetItem(py_vector, index);
|
---|
634 | FetchData(&(vector[index]),item);
|
---|
635 | }
|
---|
636 | }
|
---|
637 | else
|
---|
638 | vector=NULL;
|
---|
639 | }
|
---|
640 | else if (PyTuple_Check(py_vector)) {
|
---|
641 | /*retrieve dimensions: */
|
---|
642 | M=(int)PyTuple_Size(py_vector);
|
---|
643 |
|
---|
644 | if (M) {
|
---|
645 | vector=xNew<double>(M);
|
---|
646 | for (int index = 0; index < M; index++) {
|
---|
647 | PyObject *item;
|
---|
648 | item = PyTuple_GetItem(py_vector, index);
|
---|
649 | FetchData(&(vector[index]),item);
|
---|
650 | }
|
---|
651 | }
|
---|
652 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
653 | else
|
---|
654 | vector=NULL;
|
---|
655 | }
|
---|
656 |
|
---|
657 | else {
|
---|
658 | M=1;
|
---|
659 | vector=xNew<double>(M);
|
---|
660 | FetchData(&(vector[0]),py_vector);
|
---|
661 | }
|
---|
662 |
|
---|
663 | /*output: */
|
---|
664 | if(pM)*pM=M;
|
---|
665 | if(pvector)*pvector=vector;
|
---|
666 | }
|
---|
667 | /*}}}*/
|
---|
668 | /*FUNCTION FetchData(float** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
669 | void FetchData(float** pvector,int* pM,PyObject* py_vector){
|
---|
670 |
|
---|
671 | /*output: */
|
---|
672 | float* vector=NULL;
|
---|
673 | int M=0;
|
---|
674 | int ndim;
|
---|
675 | npy_intp* dims=NULL;
|
---|
676 |
|
---|
677 | /*intermediary:*/
|
---|
678 | long* lvector=NULL;
|
---|
679 | bool* bvector=NULL;
|
---|
680 | double* dvector=NULL;
|
---|
681 | int i;
|
---|
682 | PyObject* py_vector2=NULL;
|
---|
683 |
|
---|
684 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
685 | /*retrieve dimensions: */
|
---|
686 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
687 | if (ndim==1) {
|
---|
688 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
689 | M=dims[0];
|
---|
690 | }
|
---|
691 | else if (ndim==2) {
|
---|
692 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
693 | if (dims[1]==1)
|
---|
694 | M=dims[0];
|
---|
695 | else
|
---|
696 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
697 | }
|
---|
698 | else
|
---|
699 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
700 |
|
---|
701 | if (M) {
|
---|
702 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
703 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
704 | py_vector=py_vector2;
|
---|
705 | }
|
---|
706 |
|
---|
707 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
708 | /*retrieve internal value: */
|
---|
709 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
710 |
|
---|
711 | /*transform into int vector: */
|
---|
712 | vector=xNew<float>(M);
|
---|
713 | for(i=0;i<M;i++)vector[i]=(float)dvector[i];
|
---|
714 | }
|
---|
715 |
|
---|
716 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
717 | /*retrieve internal value: */
|
---|
718 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
719 |
|
---|
720 | /*transform into int vector: */
|
---|
721 | vector=xNew<float>(M);
|
---|
722 | for(i=0;i<M;i++)vector[i]=(float)lvector[i];
|
---|
723 | }
|
---|
724 |
|
---|
725 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
726 | /*retrieve internal value: */
|
---|
727 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
728 |
|
---|
729 | /*transform into int vector: */
|
---|
730 | vector=xNew<float>(M);
|
---|
731 | for(i=0;i<M;i++)vector[i]=(float)bvector[i];
|
---|
732 | }
|
---|
733 |
|
---|
734 | else
|
---|
735 | _error_("unrecognized int pyarray type in input!");
|
---|
736 |
|
---|
737 | if(py_vector2) delete(py_vector2);
|
---|
738 | }
|
---|
739 | else
|
---|
740 | vector=NULL;
|
---|
741 | }
|
---|
742 | else if (PyList_Check(py_vector)) {
|
---|
743 | /*retrieve dimensions: */
|
---|
744 | M=(int)PyList_Size(py_vector);
|
---|
745 |
|
---|
746 | if (M) {
|
---|
747 | vector=xNew<float>(M);
|
---|
748 | for (int index = 0; index < M; index++) {
|
---|
749 | PyObject *item;
|
---|
750 | item = PyList_GetItem(py_vector, index);
|
---|
751 | FetchData(&(vector[index]),item);
|
---|
752 | }
|
---|
753 | }
|
---|
754 | else
|
---|
755 | vector=NULL;
|
---|
756 | }
|
---|
757 |
|
---|
758 | else if (PyTuple_Check(py_vector)) {
|
---|
759 | /*retrieve dimensions: */
|
---|
760 | M=(int)PyTuple_Size(py_vector);
|
---|
761 |
|
---|
762 | if (M) {
|
---|
763 | vector=xNew<float>(M);
|
---|
764 | for (int index = 0; index < M; index++) {
|
---|
765 | PyObject *item;
|
---|
766 | item = PyTuple_GetItem(py_vector, index);
|
---|
767 | FetchData(&(vector[index]),item);
|
---|
768 | }
|
---|
769 | }
|
---|
770 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
771 | else
|
---|
772 | vector=NULL;
|
---|
773 | }
|
---|
774 |
|
---|
775 | else{
|
---|
776 | M=1;
|
---|
777 | vector=xNew<float>(M);
|
---|
778 | FetchData(&(vector[0]),py_vector);
|
---|
779 | }
|
---|
780 |
|
---|
781 | /*output: */
|
---|
782 | if(pM)*pM=M;
|
---|
783 | if(pvector)*pvector=vector;
|
---|
784 | }
|
---|
785 | /*}}}*/
|
---|
786 | /*FUNCTION FetchData(int** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
787 | void FetchData(int** pvector,int* pM,PyObject* py_vector){
|
---|
788 |
|
---|
789 | /*output: */
|
---|
790 | int* vector=NULL;
|
---|
791 | int M=0;
|
---|
792 | int ndim;
|
---|
793 | npy_intp* dims=NULL;
|
---|
794 |
|
---|
795 | /*intermediary:*/
|
---|
796 | long* lvector=NULL;
|
---|
797 | bool* bvector=NULL;
|
---|
798 | double* dvector=NULL;
|
---|
799 | int i;
|
---|
800 | PyObject* py_vector2=NULL;
|
---|
801 |
|
---|
802 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
803 | /*retrieve dimensions: */
|
---|
804 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
805 | if (ndim==1) {
|
---|
806 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
807 | M=dims[0];
|
---|
808 | }
|
---|
809 | else if (ndim==2) {
|
---|
810 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
811 | if (dims[1]==1)
|
---|
812 | M=dims[0];
|
---|
813 | else
|
---|
814 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
815 | }
|
---|
816 | else
|
---|
817 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
818 |
|
---|
819 | if (M) {
|
---|
820 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
821 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_LONG,ndim,ndim);
|
---|
822 | py_vector=py_vector2;
|
---|
823 | }
|
---|
824 |
|
---|
825 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
826 | /*retrieve internal value: */
|
---|
827 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
828 |
|
---|
829 | /*transform into int vector: */
|
---|
830 | vector=xNew<int>(M);
|
---|
831 | for(i=0;i<M;i++)vector[i]=(int)dvector[i];
|
---|
832 | }
|
---|
833 |
|
---|
834 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
835 | /*retrieve internal value: */
|
---|
836 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
837 |
|
---|
838 | /*transform into int vector: */
|
---|
839 | vector=xNew<int>(M);
|
---|
840 | for(i=0;i<M;i++)vector[i]=(int)lvector[i];
|
---|
841 | }
|
---|
842 |
|
---|
843 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
844 | /*retrieve internal value: */
|
---|
845 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
846 |
|
---|
847 | /*transform into int vector: */
|
---|
848 | vector=xNew<int>(M);
|
---|
849 | for(i=0;i<M;i++)vector[i]=(int)bvector[i];
|
---|
850 | }
|
---|
851 |
|
---|
852 | else
|
---|
853 | _error_("unrecognized int pyarray type in input!");
|
---|
854 |
|
---|
855 | if (py_vector2)
|
---|
856 | delete(py_vector2);
|
---|
857 | }
|
---|
858 | else
|
---|
859 | vector=NULL;
|
---|
860 | }
|
---|
861 |
|
---|
862 | else if (PyList_Check(py_vector)) {
|
---|
863 | /*retrieve dimensions: */
|
---|
864 | M=(int)PyList_Size(py_vector);
|
---|
865 |
|
---|
866 | if (M) {
|
---|
867 | vector=xNew<int>(M);
|
---|
868 | for (int index = 0; index < M; index++) {
|
---|
869 | PyObject *item;
|
---|
870 | item = PyList_GetItem(py_vector, index);
|
---|
871 | FetchData(&(vector[index]),item);
|
---|
872 | }
|
---|
873 | }
|
---|
874 | else
|
---|
875 | vector=NULL;
|
---|
876 | }
|
---|
877 |
|
---|
878 | else if (PyTuple_Check(py_vector)) {
|
---|
879 | /*retrieve dimensions: */
|
---|
880 | M=(int)PyTuple_Size(py_vector);
|
---|
881 |
|
---|
882 | if (M) {
|
---|
883 | vector=xNew<int>(M);
|
---|
884 | for (int index = 0; index < M; index++) {
|
---|
885 | PyObject *item;
|
---|
886 | item = PyTuple_GetItem(py_vector, index);
|
---|
887 | FetchData(&(vector[index]),item);
|
---|
888 | }
|
---|
889 | }
|
---|
890 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
891 | else
|
---|
892 | vector=NULL;
|
---|
893 | }
|
---|
894 |
|
---|
895 | else {
|
---|
896 | M=1;
|
---|
897 | vector=xNew<int>(M);
|
---|
898 | FetchData(&(vector[0]),py_vector);
|
---|
899 | }
|
---|
900 |
|
---|
901 | /*output: */
|
---|
902 | if(pM)*pM=M;
|
---|
903 | if(pvector)*pvector=vector;
|
---|
904 | }
|
---|
905 | /*}}}*/
|
---|
906 | /*FUNCTION FetchData(bool** pvector,int* pM, PyObject* py_vector){{{*/
|
---|
907 | void FetchData(bool** pvector,int* pM,PyObject* py_vector){
|
---|
908 |
|
---|
909 | /*output: */
|
---|
910 | bool* bvector=NULL;
|
---|
911 | bool* vector=NULL;
|
---|
912 | int M=0;
|
---|
913 | int ndim;
|
---|
914 | npy_intp* dims=NULL;
|
---|
915 |
|
---|
916 | /*intermediary:*/
|
---|
917 | double* dvector=NULL;
|
---|
918 | long* lvector=NULL;
|
---|
919 | int i;
|
---|
920 | PyObject* py_vector2=NULL;
|
---|
921 |
|
---|
922 | if (PyArray_Check((PyArrayObject*)py_vector)) {
|
---|
923 | /*retrieve dimensions: */
|
---|
924 | ndim=PyArray_NDIM((const PyArrayObject*)py_vector);
|
---|
925 | if (ndim==1) {
|
---|
926 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
927 | M=dims[0];
|
---|
928 | }
|
---|
929 | else if (ndim==2) {
|
---|
930 | dims=PyArray_DIMS((PyArrayObject*)py_vector);
|
---|
931 | if (dims[1]==1)
|
---|
932 | M=dims[0];
|
---|
933 | else
|
---|
934 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
935 | }
|
---|
936 | else
|
---|
937 | _error_("expecting an Mx1 matrix or M vector in input!");
|
---|
938 |
|
---|
939 | if (M) {
|
---|
940 | if (!PyArray_ISCONTIGUOUS((PyArrayObject*)py_vector)) {
|
---|
941 | py_vector2=PyArray_ContiguousFromAny(py_vector,NPY_BOOL,ndim,ndim);
|
---|
942 | py_vector=py_vector2;
|
---|
943 | }
|
---|
944 |
|
---|
945 | if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_DOUBLE) {
|
---|
946 | /*retrieve internal value: */
|
---|
947 | dvector=(double*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
948 |
|
---|
949 | /*transform into bool vector: */
|
---|
950 | vector=xNew<bool>(M);
|
---|
951 | for(i=0;i<M;i++)vector[i]=(bool)dvector[i];
|
---|
952 | }
|
---|
953 |
|
---|
954 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_LONG) {
|
---|
955 | /*retrieve internal value: */
|
---|
956 | lvector=(long*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
957 |
|
---|
958 | /*transform into bool vector: */
|
---|
959 | vector=xNew<bool>(M);
|
---|
960 | for(i=0;i<M;i++)vector[i]=(bool)lvector[i];
|
---|
961 | }
|
---|
962 |
|
---|
963 | else if (PyArray_TYPE((PyArrayObject*)py_vector) == NPY_BOOL) {
|
---|
964 | /*retrieve internal value: */
|
---|
965 | bvector=(bool*)PyArray_DATA((PyArrayObject*)py_vector);
|
---|
966 |
|
---|
967 | /*copy vector: */
|
---|
968 | vector=xNew<bool>(M);
|
---|
969 | memcpy(vector,bvector,(M)*sizeof(bool));
|
---|
970 | }
|
---|
971 |
|
---|
972 | else
|
---|
973 | _error_("unrecognized bool pyarray type in input!");
|
---|
974 |
|
---|
975 | if (py_vector2)
|
---|
976 | delete(py_vector2);
|
---|
977 | }
|
---|
978 | else
|
---|
979 | vector=NULL;
|
---|
980 | }
|
---|
981 | else if (PyList_Check(py_vector)) {
|
---|
982 | /*retrieve dimensions: */
|
---|
983 | M=(int)PyList_Size(py_vector);
|
---|
984 | if (M) {
|
---|
985 | vector=xNew<bool>(M);
|
---|
986 | for (int index = 0; index < M; index++) {
|
---|
987 | PyObject *item;
|
---|
988 | item = PyList_GetItem(py_vector, index);
|
---|
989 | FetchData(&(vector[index]),item);
|
---|
990 | }
|
---|
991 | }
|
---|
992 | else
|
---|
993 | vector=NULL;
|
---|
994 | }
|
---|
995 |
|
---|
996 | else if (PyTuple_Check(py_vector)) {
|
---|
997 | /*retrieve dimensions: */
|
---|
998 | M=(int)PyTuple_Size(py_vector);
|
---|
999 |
|
---|
1000 | if (M) {
|
---|
1001 | vector=xNew<bool>(M);
|
---|
1002 | for (int index = 0; index < M; index++) {
|
---|
1003 | PyObject *item;
|
---|
1004 | item = PyTuple_GetItem(py_vector, index);
|
---|
1005 | FetchData(&(vector[index]),item);
|
---|
1006 | }
|
---|
1007 | }
|
---|
1008 | //if (py_matrix2) delete(py_matrix2); Not doing this for now, seems to be creating a segfault!
|
---|
1009 | else
|
---|
1010 | vector=NULL;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | else {
|
---|
1014 | M=1;
|
---|
1015 | vector=xNew<bool>(M);
|
---|
1016 | FetchData(&(vector[0]),py_vector);
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | /*output: */
|
---|
1020 | if(pM)*pM=M;
|
---|
1021 | if(pvector)*pvector=vector;
|
---|
1022 | }
|
---|
1023 | /*}}}*/
|
---|
1024 |
|
---|
1025 | /*ISSM objects*/
|
---|
1026 | /*FUNCTION FetchData(BamgGeom** pbamggeom,PyObject* py_dict){{{*/
|
---|
1027 | void FetchData(BamgGeom** pbamggeom,PyObject* py_dict){
|
---|
1028 |
|
---|
1029 | /*Initialize output*/
|
---|
1030 | BamgGeom* bamggeom=new BamgGeom();
|
---|
1031 |
|
---|
1032 | /*Fetch all fields*/
|
---|
1033 | FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
1034 | FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
1035 | FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], PyDict_GetItemString(py_dict,"Corners"));
|
---|
1036 | FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],PyDict_GetItemString(py_dict,"RequiredVertices"));
|
---|
1037 | FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], PyDict_GetItemString(py_dict,"RequiredEdges"));
|
---|
1038 | FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
1039 | FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],PyDict_GetItemString(py_dict,"SubDomains"));
|
---|
1040 |
|
---|
1041 | /*Assign output pointers:*/
|
---|
1042 | *pbamggeom=bamggeom;
|
---|
1043 | }
|
---|
1044 | /*}}}*/
|
---|
1045 | /*FUNCTION FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){{{*/
|
---|
1046 | void FetchData(BamgMesh** pbamgmesh,PyObject* py_dict){
|
---|
1047 |
|
---|
1048 | /*Initialize output*/
|
---|
1049 | BamgMesh* bamgmesh=new BamgMesh();
|
---|
1050 |
|
---|
1051 | /*Fetch all fields*/
|
---|
1052 | FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],PyDict_GetItemString(py_dict,"Vertices"));
|
---|
1053 | FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], PyDict_GetItemString(py_dict,"Edges"));
|
---|
1054 | FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], PyDict_GetItemString(py_dict,"Triangles"));
|
---|
1055 | FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],PyDict_GetItemString(py_dict,"CrackedEdges"));
|
---|
1056 | FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomEdge"));
|
---|
1057 | FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],PyDict_GetItemString(py_dict,"VerticesOnGeomVertex"));
|
---|
1058 | FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], PyDict_GetItemString(py_dict,"EdgesOnGeomEdge"));
|
---|
1059 | FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],PyDict_GetItemString(py_dict,"IssmSegments"));
|
---|
1060 |
|
---|
1061 | /*Assign output pointers:*/
|
---|
1062 | *pbamgmesh=bamgmesh;
|
---|
1063 | }
|
---|
1064 | /*}}}*/
|
---|
1065 | /*FUNCTION FetchData(BamgOpts** pbamgopts,PyObject* py_dict){{{*/
|
---|
1066 | void FetchData(BamgOpts** pbamgopts,PyObject* py_dict){
|
---|
1067 |
|
---|
1068 | /*Initialize output*/
|
---|
1069 | BamgOpts* bamgopts=new BamgOpts();
|
---|
1070 |
|
---|
1071 | /*Fetch all fields*/
|
---|
1072 | FetchData(&bamgopts->anisomax,PyDict_GetItemString(py_dict,"anisomax"));
|
---|
1073 | FetchData(&bamgopts->cutoff,PyDict_GetItemString(py_dict,"cutoff"));
|
---|
1074 | FetchData(&bamgopts->coeff,PyDict_GetItemString(py_dict,"coeff"));
|
---|
1075 | FetchData(&bamgopts->errg,PyDict_GetItemString(py_dict,"errg"));
|
---|
1076 | FetchData(&bamgopts->gradation,PyDict_GetItemString(py_dict,"gradation"));
|
---|
1077 | FetchData(&bamgopts->Hessiantype,PyDict_GetItemString(py_dict,"Hessiantype"));
|
---|
1078 | FetchData(&bamgopts->maxnbv,PyDict_GetItemString(py_dict,"maxnbv"));
|
---|
1079 | FetchData(&bamgopts->maxsubdiv,PyDict_GetItemString(py_dict,"maxsubdiv"));
|
---|
1080 | FetchData(&bamgopts->Metrictype,PyDict_GetItemString(py_dict,"Metrictype"));
|
---|
1081 | FetchData(&bamgopts->nbjacobi,PyDict_GetItemString(py_dict,"nbjacobi"));
|
---|
1082 | FetchData(&bamgopts->nbsmooth,PyDict_GetItemString(py_dict,"nbsmooth"));
|
---|
1083 | FetchData(&bamgopts->omega,PyDict_GetItemString(py_dict,"omega"));
|
---|
1084 | FetchData(&bamgopts->power,PyDict_GetItemString(py_dict,"power"));
|
---|
1085 | FetchData(&bamgopts->verbose,PyDict_GetItemString(py_dict,"verbose"));
|
---|
1086 |
|
---|
1087 | FetchData(&bamgopts->Crack,PyDict_GetItemString(py_dict,"Crack"));
|
---|
1088 | FetchData(&bamgopts->KeepVertices,PyDict_GetItemString(py_dict,"KeepVertices"));
|
---|
1089 | FetchData(&bamgopts->splitcorners,PyDict_GetItemString(py_dict,"splitcorners"));
|
---|
1090 |
|
---|
1091 | FetchData(&bamgopts->hmin,PyDict_GetItemString(py_dict,"hmin"));
|
---|
1092 | FetchData(&bamgopts->hmax,PyDict_GetItemString(py_dict,"hmax"));
|
---|
1093 | FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],PyDict_GetItemString(py_dict,"hminVertices"));
|
---|
1094 | FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],PyDict_GetItemString(py_dict,"hmaxVertices"));
|
---|
1095 | FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,PyDict_GetItemString(py_dict,"hVertices"));
|
---|
1096 | FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],PyDict_GetItemString(py_dict,"metric"));
|
---|
1097 | FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],PyDict_GetItemString(py_dict,"field"));
|
---|
1098 | FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],PyDict_GetItemString(py_dict,"err"));
|
---|
1099 |
|
---|
1100 | /*Additional checks*/
|
---|
1101 | bamgopts->Check();
|
---|
1102 |
|
---|
1103 | /*Assign output pointers:*/
|
---|
1104 | *pbamgopts=bamgopts;
|
---|
1105 | }
|
---|
1106 | /*}}}*/
|
---|
1107 | /*FUNCTION FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){{{*/
|
---|
1108 | void FetchData(Options** poptions,int istart, int nrhs,PyObject* py_tuple){
|
---|
1109 |
|
---|
1110 | char *name = NULL;
|
---|
1111 | Option *option = NULL;
|
---|
1112 |
|
---|
1113 | /*Initialize output*/
|
---|
1114 | Options* options=new Options();
|
---|
1115 |
|
---|
1116 | /*Fetch all options*/
|
---|
1117 | for (int i=istart; i<nrhs; i=i+2){
|
---|
1118 | #if _PYTHON_MAJOR_ >= 3
|
---|
1119 | if (!PyUnicode_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
1120 | #else
|
---|
1121 | if (!PyString_Check(PyTuple_GetItem(py_tuple,(Py_ssize_t)i))) _error_("Argument " << i+1 << " must be name of option");
|
---|
1122 | #endif
|
---|
1123 |
|
---|
1124 | FetchData(&name,PyTuple_GetItem(py_tuple,(Py_ssize_t)i));
|
---|
1125 | if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
|
---|
1126 |
|
---|
1127 | _printf0_("FetchData for Options not implemented yet, ignoring option \"" << name << "\"!\n");
|
---|
1128 |
|
---|
1129 | // option=(Option*)OptionParse(name,&PyTuple_GetItem(py_tuple,(Py_ssize_t)(i+1)));
|
---|
1130 | // options->AddOption(option);
|
---|
1131 | // option=NULL;
|
---|
1132 | }
|
---|
1133 |
|
---|
1134 | /*Assign output pointers:*/
|
---|
1135 | *poptions=options;
|
---|
1136 | }
|
---|
1137 | /*}}}*/
|
---|
1138 | /*FUNCTION FetchData(Contours** pcontours,PyObject* py_list){{{*/
|
---|
1139 | void FetchData(Contours** pcontours,PyObject* py_list){
|
---|
1140 |
|
---|
1141 | int numcontours,test1,test2;
|
---|
1142 | char *contourname = NULL;
|
---|
1143 | Contours *contours = NULL;
|
---|
1144 | Contour<double> *contouri = NULL;
|
---|
1145 | PyObject *py_dicti = NULL;
|
---|
1146 | PyObject *py_item = NULL;
|
---|
1147 |
|
---|
1148 | #if _PYTHON_MAJOR_ >= 3
|
---|
1149 | if (PyUnicode_Check(py_list)){
|
---|
1150 | #else
|
---|
1151 | if (PyString_Check(py_list)){
|
---|
1152 | #endif
|
---|
1153 | FetchData(&contourname,py_list);
|
---|
1154 | contours=ExpRead<double>(contourname);
|
---|
1155 | }
|
---|
1156 | else if(PyList_Check(py_list)){
|
---|
1157 |
|
---|
1158 | contours=new Contours();
|
---|
1159 | numcontours=(int)PyList_Size(py_list);
|
---|
1160 |
|
---|
1161 | for(int i=0;i<numcontours;i++){
|
---|
1162 |
|
---|
1163 | contouri=new Contour<double>();
|
---|
1164 | py_dicti=PyList_GetItem(py_list,(Py_ssize_t)i);
|
---|
1165 |
|
---|
1166 | py_item = PyDict_GetItemString(py_dicti,"nods");
|
---|
1167 | if(!py_item) _error_("input structure does not have a 'nods' field");
|
---|
1168 | FetchData(&contouri->nods,py_item);
|
---|
1169 |
|
---|
1170 | py_item = PyDict_GetItemString(py_dicti,"x");
|
---|
1171 | if(!py_item) _error_("input structure does not have a 'x' field");
|
---|
1172 | FetchData(&contouri->x,&test1,&test2,py_item);
|
---|
1173 | if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
|
---|
1174 |
|
---|
1175 | py_item = PyDict_GetItemString(py_dicti,"y");
|
---|
1176 | if(!py_item) _error_("input structure does not have a 'y' field");
|
---|
1177 | FetchData(&contouri->y,&test1,&test2,py_item);
|
---|
1178 | if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
|
---|
1179 |
|
---|
1180 | contours->AddObject(contouri);
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | else{
|
---|
1184 | _error_("Contour is neither a string nor a structure and cannot be loaded");
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | /*clean-up and assign output pointer*/
|
---|
1188 | xDelete<char>(contourname);
|
---|
1189 | *pcontours=contours;
|
---|
1190 | }
|
---|
1191 |
|
---|
1192 | void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,PyObject* A_IN, PyObject* EWGTS_IN){/*{{{*/
|
---|
1193 |
|
---|
1194 | double* a = ((double*)PyArray_DATA((PyArrayObject*)A_IN));
|
---|
1195 | int ndim = PyArray_NDIM((PyArrayObject*)A_IN);
|
---|
1196 | int nzmax = PyArray_CountNonzero((PyArrayObject*)A_IN);
|
---|
1197 |
|
---|
1198 | /*Fetch adjacency matrix:*/
|
---|
1199 | int nvtxs = PyArray_DIMS((PyArrayObject*)A_IN)[1];
|
---|
1200 |
|
---|
1201 | int* mwstart = xNewZeroInit<int>(nvtxs+1);
|
---|
1202 | pyGetJc(a,nvtxs,mwstart);
|
---|
1203 |
|
---|
1204 | int* mwadjacency = xNewZeroInit<int>(nzmax);
|
---|
1205 | pyGetIr(a,nvtxs,nzmax,mwadjacency);
|
---|
1206 |
|
---|
1207 | int* start = xNew<int>(nvtxs+1);
|
---|
1208 | for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
|
---|
1209 | int* adjacency = xNew<int>(nzmax);
|
---|
1210 | for(int i=0; i<nzmax; i++) adjacency[i]=(int)mwadjacency[i];
|
---|
1211 |
|
---|
1212 | /*Get edges weights*/
|
---|
1213 | int nedges = start[nvtxs];
|
---|
1214 | float* ewgts = NULL;
|
---|
1215 | int size = PyArray_SIZE((PyArrayObject*)EWGTS_IN);
|
---|
1216 | //size may be 1 and still be empty;
|
---|
1217 | //presumably size of edge_weights input will never be exactly 1
|
---|
1218 | if(size != 1 && size != 0){
|
---|
1219 | ewgts = xNewZeroInit<float>(nedges);
|
---|
1220 | for(int i = 0; i<nedges;i++){
|
---|
1221 | ewgts[i] = (float)a[i];
|
---|
1222 | }
|
---|
1223 | }
|
---|
1224 |
|
---|
1225 | /*Assign output pointers*/
|
---|
1226 | *pnvtxs = nvtxs;
|
---|
1227 | *padjacency = adjacency;
|
---|
1228 | *pstart = start;
|
---|
1229 | *pewgts = ewgts;
|
---|
1230 | }
|
---|
1231 | /*}}}*/
|
---|
1232 |
|
---|
1233 | void pyGetJc(double* a, int nvtxs, int* Jc){
|
---|
1234 | /*{{{*/
|
---|
1235 | //get the number of non-zero elements in each row, adding to the prior number;
|
---|
1236 | //always starts with 0 and has length: number_of_rows_in(a)+1 == nvtxs+1
|
---|
1237 | // eg: 0, 1, 3, 6, 8, 13, ...
|
---|
1238 | // for: 1 in the first row, 2 in the second, 3 in the third, etc.
|
---|
1239 | int c = 0;
|
---|
1240 | Jc[c++] = 0;
|
---|
1241 |
|
---|
1242 | for(int i=0;i<nvtxs;i++){
|
---|
1243 | for(int j=0;j<nvtxs;j++){
|
---|
1244 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
1245 | Jc[c] += 1;
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 | c++;
|
---|
1249 | Jc[c] = Jc[c-1];
|
---|
1250 | }
|
---|
1251 | return;
|
---|
1252 | }
|
---|
1253 | /*}}}*/
|
---|
1254 |
|
---|
1255 | void pyGetIr(double* a, int nvtxs, int nzmax, int* Ir){
|
---|
1256 | /*{{{*/
|
---|
1257 | //get the row-wise position of each non-zero element;
|
---|
1258 | //has length: number_of_non_zero_elements_in(a) == nzmax
|
---|
1259 | // eg: 10, 24, 25, 4, ...
|
---|
1260 | // the 10th, 24th, and 25th elements in the first row, the 4th in the second row
|
---|
1261 | // using pyGetJc to determine which indices correspond to which row
|
---|
1262 | int r = 0;
|
---|
1263 |
|
---|
1264 | for(int i=0;i<nvtxs;i++){
|
---|
1265 | for(int j=0;j<nvtxs;j++){
|
---|
1266 | if ((int)a[i+(j*nvtxs)] != 0){
|
---|
1267 | Ir[r] = j;
|
---|
1268 | r++;
|
---|
1269 | }
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 | return;
|
---|
1273 | }
|
---|
1274 | /*}}}*/
|
---|
1275 |
|
---|
1276 | /*Python version dependent: */
|
---|
1277 | /* #if _PYTHON_MAJOR_ >= 3 */
|
---|
1278 | /* /\*FUNCTION FetchData(char** pstring,PyObject* py_unicode){{{*\/ */
|
---|
1279 | /* void FetchData(char** pstring,PyObject* py_unicode){ */
|
---|
1280 |
|
---|
1281 | /* PyObject* py_bytes; */
|
---|
1282 | /* char* string=NULL; */
|
---|
1283 |
|
---|
1284 | /* /\*convert to bytes format: *\/ */
|
---|
1285 | /* PyUnicode_FSConverter(py_unicode,&py_bytes); */
|
---|
1286 |
|
---|
1287 | /* /\*convert from bytes to string: *\/ */
|
---|
1288 | /* string=PyBytes_AsUTF8(py_bytes); */
|
---|
1289 |
|
---|
1290 | /* /\*copy string (note strlen does not include trailing NULL): *\/ */
|
---|
1291 | /* *pstring=xNew<char>(strlen(string)+1); */
|
---|
1292 | /* memcpy(*pstring,string,(strlen(string)+1)*sizeof(char)); */
|
---|
1293 | /* } */
|
---|
1294 | /* /\*}}}*\/ */
|
---|
1295 | /* #else */
|
---|
1296 | /*FUNCTION FetchData(char** pstring,PyObject* py_string){{{*/
|
---|
1297 | void FetchData(char** pstring,PyObject* py_string){
|
---|
1298 |
|
---|
1299 | char* string=NULL;
|
---|
1300 |
|
---|
1301 | /*extract internal string: */
|
---|
1302 | #if _PYTHON_MAJOR_ == 3
|
---|
1303 | string=PyUnicode_AsUTF8(py_string);
|
---|
1304 | #else
|
---|
1305 | string=PyString_AsString(py_string);
|
---|
1306 | #endif
|
---|
1307 | /*copy string (note strlen does not include trailing NULL): */
|
---|
1308 | *pstring=xNew<char>(strlen(string)+1);
|
---|
1309 | memcpy(*pstring,string,(strlen(string)+1)*sizeof(char));
|
---|
1310 | }
|
---|
1311 | /*}}}*/
|
---|
1312 | //#endif
|
---|