// ascee_python.h // // Author: J.A. de Jong - ASCEE - Redu-Sone // // Description: // Some routines to generate numpy arrays from matrices and vectors. ////////////////////////////////////////////////////////////////////// #pragma once #ifndef LASP_PYTHON_H #define LASP_PYTHON_H #ifdef __cplusplus #error "Cannot compile this file with C++" #endif #include "lasp_types.h" #include "lasp_mat.h" #include "lasp_pyarray.h" /** * @brief Create a Numpy array from a dmat structure * * @param mat Pointer to the dmat * @param transfer_ownership If set to true, the created Numpy array will * obtain ownership of the data and calls free() on destruction. The dmat will * have the foreign_data flag set, such that it won't free the data. * * @return */ static inline PyObject* dmat_to_ndarray(dmat* mat,bool transfer_ownership) { dbgassert(mat,NULLPTRDEREF); dbgassert(mat->_data,NULLPTRDEREF); /* fprintf(stderr, "Enter dmat_to_ndarray\n"); */ // Dimensions given in wrong order, as mat is // Fortran-contiguous. Later on we transpose the result. This is // more easy than using the PyArray_New syntax. PyObject* arr = data_to_ndarray(mat->_data, mat->n_rows, mat->n_cols, LASP_NUMPY_FLOAT_TYPE, transfer_ownership, true); // Fortran-contiguous if(transfer_ownership) { mat->_foreign_data = true; } if(!arr) { WARN("Array creation failure"); feTRACE(15); return NULL; } /* fprintf(stderr, "Exit dmat_to_ndarray\n"); */ return arr; } #endif // LASP_PYTHON_H //////////////////////////////////////////////////////////////////////