source: issm/trunk-jpl/src/wrappers/python/io/FetchPythonData.cpp@ 24502

Last change on this file since 24502 was 24502, checked in by jdquinn, 5 years ago

CHG: One more adjustment based on return type of functions.

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