Skip to content

Commit

Permalink
complex array tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kalinowski committed Jun 18, 2024
1 parent 3283fa1 commit b87fb5f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/libpython.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ typedef struct tagPyArrayObject {
typedef unsigned char npy_bool;
typedef long npy_long;
typedef double npy_double;
// with numpy 2.0, direct field access of complex numbers is no longer valid.
// accessors like npy_creal() and npy_cimag() are the recomended way.
// However, the memory layout is unchanged, and we define the struct here, so access is still valid.
typedef struct { double real, imag; } npy_cdouble;
typedef npy_cdouble npy_complex128;

Expand Down
3 changes: 2 additions & 1 deletion src/python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1541,12 +1541,13 @@ SEXP py_to_r_cpp(PyObject* x, bool convert, bool simple) {
case NPY_CDOUBLE: {
npy_complex128* pData = (npy_complex128*)PyArray_DATA(array);
rArray = Rf_allocArray(CPLXSXP, dimsVector);
Rcomplex* rArray_ptr = COMPLEX(rArray);
for (int i=0; i<len; i++) {
npy_complex128 data = pData[i];
Rcomplex cpx;
cpx.r = data.real;
cpx.i = data.imag;
COMPLEX(rArray)[i] = cpx;
rArray_ptr[i] = cpx;
}
break;
}
Expand Down

0 comments on commit b87fb5f

Please sign in to comment.