Skip to content
Snippets Groups Projects
Commit 376848c7 authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

gmshpy : convert fullMatrix to numpy array on return

parent d512d596
No related branches found
No related tags found
No related merge requests found
...@@ -15,12 +15,17 @@ ...@@ -15,12 +15,17 @@
%#endif %#endif
%#endif %#endif
fullMatrix<double> *pySequenceToFullMatrixDouble(PyObject *o) { fullMatrix<double> *pySequenceToFullMatrixDouble(PyObject *o) {
fullMatrix<double> *fm; fullMatrix<double> *fm = NULL;
if (!PySequence_Check(o)) if (!PySequence_Check(o))
return NULL; return NULL;
long int nRow = PySequence_Length(o); long int nRow = PySequence_Length(o);
for (int i = 0; i < PySequence_Length(o); ++i) { for (int i = 0; i < PySequence_Length(o); ++i) {
PyObject *l = PySequence_GetItem(o, i); PyObject *l = PySequence_GetItem(o, i);
if (!PySequence_Check(l)){
if (fm != NULL)
delete fm;
return NULL;
}
long int nCol = PySequence_Length(l); long int nCol = PySequence_Length(l);
if (i == 0) if (i == 0)
fm = new fullMatrix<double>(nRow, nCol); fm = new fullMatrix<double>(nRow, nCol);
...@@ -80,6 +85,30 @@ ...@@ -80,6 +85,30 @@
%#endif %#endif
return NULL; return NULL;
} }
%#ifdef HAVE_NUMPY
static void deleteCapsuleArray(PyObject *capsule)
{
delete [](double*)PyCapsule_GetPointer(capsule, NULL);
}
PyObject *fullMatrix2PyArray(fullMatrix<double> &fm)
{
npy_intp dims[2] = {fm.size1(), fm.size2()};
double *data = &fm.operator()(0, 0);
/*PyObject *array = PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, NULL, NULL, 0, NPY_ARRAY_F_CONTIGUOUS, NULL);
// copy data
memcpy((void*)PyArray_DATA(array), data, dims[0] * dims[1] * sizeof(double));*/
// do not copy data
PyObject *array = PyArray_New(&PyArray_Type, 2, dims, NPY_DOUBLE, NULL, (void*)data, 0, NPY_ARRAY_F_CONTIGUOUS, NULL);
PyArray_UpdateFlags((PyArrayObject*)array, NPY_ARRAY_ALIGNED);
if (fm.getOwnData()) {
fm.setOwnData(false);
PyObject *capsule = PyCapsule_New((void*) data, NULL, deleteCapsuleArray);
PyArray_SetBaseObject((PyArrayObject*)array, capsule);
}
return array;
}
%#endif
} }
%typemap(in, fragment="fullMatrixConversion") const fullMatrix<double> &(PyObject *tmpObject = NULL, bool newMatrix = false){ %typemap(in, fragment="fullMatrixConversion") const fullMatrix<double> &(PyObject *tmpObject = NULL, bool newMatrix = false){
...@@ -127,3 +156,12 @@ ...@@ -127,3 +156,12 @@
%typemap(freearg) fullMatrix<double> *{ %typemap(freearg) fullMatrix<double> *{
if (newMatrix$argnum && $1) delete $1; if (newMatrix$argnum && $1) delete $1;
} }
%#include "GmshConfig.h"
%#ifdef HAVE_NUMPY
%typemap(out, fragment="fullMatrixConversion") fullMatrix<double> {
$result = fullMatrix2PyArray($1);
}
%#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment