GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: Replace npy complex structs with native complex types by lysnikolaou · Pull Request #24085 · numpy/numpy

ENH: Replace npy complex structs with native complex types #24085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
mattip merged 46 commits into numpy:main from lysnikolaou:real-imag-remove
Jul 31, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

ENH: Replace npy complex structs with native complex types #24085

Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
fd455d2
Replace npy_c* structs with native types
lysnikolaou May 16, 2023
9eb7053
Format
lysnikolaou Jun 21, 2023
d2b5512
Fix f2py
lysnikolaou Jun 22, 2023
4a246bb
Rename complex to singlecomplex in all f2c'd files
lysnikolaou Jun 29, 2023
5c38f35
Fix complex init
lysnikolaou Jun 29, 2023
b240b10
Fix uninitialized values in arraytypes
lysnikolaou Jun 29, 2023
725a8d6
Make GET_REAL and GET_IMAG into inline functions that use unions
lysnikolaou Jun 30, 2023
6050aad
Fix call to memset with std::complex in umath_linalg
lysnikolaou Jun 30, 2023
a7fea9b
Fix call to SET_REAL/SET_IMAG in umath matmul
lysnikolaou Jun 30, 2023
8749082
Remove c99 compat layer from npymath
lysnikolaou Jun 30, 2023
ea282c7
Fix log10 and log2
lysnikolaou Jun 30, 2023
a926e01
Fix initialization of complex in npy_math_complex
lysnikolaou Jun 30, 2023
3d29fb1
Merge branch 'main' into real-imag-remove
lysnikolaou Jun 30, 2023
2b2c597
First attempt to fix CI // Add static inline
lysnikolaou Jul 4, 2023
ad60798
Fix inline setters
lysnikolaou Jul 5, 2023
f65c643
Initialize complex numbers
lysnikolaou Jul 5, 2023
bf3fa21
Fix implicit declaration of CMPLX on musllinux
lysnikolaou Jul 5, 2023
57e204b
Hopefully fix Windows stuff
lysnikolaou Jul 6, 2023
c24f998
Remove cast from INIT macros
lysnikolaou Jul 7, 2023
c38cab6
Remove direct complex arithmetic due to Windows
lysnikolaou Jul 7, 2023
5f4ea8a
Fix complex init in more places
lysnikolaou Jul 7, 2023
d2e8b41
Fix complex init in scalarmath
lysnikolaou Jul 7, 2023
0ef48b3
Use latest Cython in CI
lysnikolaou Jul 10, 2023
c4e9b34
Change pyproject.toml to build with latest Cython
lysnikolaou Jul 10, 2023
e972bc8
Demote linkage errors to warnings in pyodide build
lysnikolaou Jul 12, 2023
45b15a1
Revert changes to lapack_lite
lysnikolaou Jul 12, 2023
2118738
Necessary changes for complex not to be a reserved keyword
lysnikolaou Jul 12, 2023
8c77d69
Move complex utilities to npy_math.h [wheel build]
lysnikolaou Jul 12, 2023
7377f24
Remove necessity for INIT macro [wheel build]
lysnikolaou Jul 14, 2023
55a903b
Merge branch 'main' into real-imag-remove
lysnikolaou Jul 14, 2023
045a8cd
Fix Windows-CL workflow; update to latest cython 0.29.x
lysnikolaou Jul 14, 2023
ddcdf86
Necessary changes to only use C99 complex types (not std::complex)
lysnikolaou Jul 17, 2023
6ce134b
Use structs for C++ on MSVC
lysnikolaou Jul 21, 2023
1a89522
Fix when _NPY_MAX/MIN is defined
lysnikolaou Jul 21, 2023
f66f6e4
Revert changes to CFLAGS in pyodide build; improve formatting
lysnikolaou Jul 21, 2023
f6a516f
Merge branch 'main' into real-imag-remove
lysnikolaou Jul 25, 2023
013a8d4
Revert changes to Cython version now that cython3 is the default
lysnikolaou Jul 25, 2023
5757057
Merge branch 'main' into real-imag-remove
lysnikolaou Jul 25, 2023
7508afd
Remove unnecessary inline functions, use npy_c* inline functions instead
lysnikolaou Jul 25, 2023
19bdf90
Merge branch 'main' into real-imag-remove
lysnikolaou Jul 26, 2023
04f4ce1
Add const wherever possible
lysnikolaou Jul 26, 2023
991c4b1
Add complex compat header
lysnikolaou Jul 27, 2023
e2ad3c8
Change compat header to not use versions
lysnikolaou Jul 27, 2023
2d25d2a
Improve comment in compat header
lysnikolaou Jul 31, 2023
530cbc5
Address feedback
lysnikolaou Jul 31, 2023
2665d56
Merge branch 'main' into real-imag-remove
lysnikolaou Jul 31, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

42 changes: 32 additions & 10 deletions 42 numpy/core/include/numpy/ndarraytypes.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -962,16 +962,38 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);

#define PyArray_MAX(a,b) (((a)>(b))?(a):(b))
#define PyArray_MIN(a,b) (((a)<(b))?(a):(b))
#define PyArray_CLT(p,q) ((((p).real==(q).real) ? ((p).imag < (q).imag) : \
((p).real < (q).real)))
#define PyArray_CGT(p,q) ((((p).real==(q).real) ? ((p).imag > (q).imag) : \
((p).real > (q).real)))
#define PyArray_CLE(p,q) ((((p).real==(q).real) ? ((p).imag <= (q).imag) : \
((p).real <= (q).real)))
#define PyArray_CGE(p,q) ((((p).real==(q).real) ? ((p).imag >= (q).imag) : \
((p).real >= (q).real)))
#define PyArray_CEQ(p,q) (((p).real==(q).real) && ((p).imag == (q).imag))
#define PyArray_CNE(p,q) (((p).real!=(q).real) || ((p).imag != (q).imag))

#ifdef __cplusplus
#define PyArray_CLT(p,q, type) (p).real() == (q).real() \
? (p).imag() < (q).imag() \
: (p).real() < (q).real()
#define PyArray_CGT(p,q, type) (p).real() == (q).real() \
? (p).imag() > (q).imag() \
: (p).real() > (q).real()
#define PyArray_CLE(p,q, type) (p).real() == (q).real() \
? (p).imag() <= (q).imag() \
: (p).real() <= (q).real()
#define PyArray_CGE(p,q, type) (p).real() == (q).real() \
? (p).imag() >= (q).imag() \
: (p).real() >= (q).real()
#define PyArray_CEQ(p,q, type) (p) == (q)
#define PyArray_CNE(p,q, type) (p) != (q)
#else
#define PyArray_CLT(p,q, type) NPY_##type##_GET_REAL(&(p)) == NPY_##type##_GET_REAL(&(q)) \
? NPY_##type##_GET_IMAG(&(p)) < NPY_##type##_GET_IMAG(&(q)) \
: NPY_##type##_GET_REAL(&(p)) < NPY_##type##_GET_REAL(&(q))
#define PyArray_CGT(p,q, type) NPY_##type##_GET_REAL(&(p)) == NPY_##type##_GET_REAL(&(q)) \
? NPY_##type##_GET_IMAG(&(p)) > NPY_##type##_GET_IMAG(&(q)) \
: NPY_##type##_GET_REAL(&(p)) > NPY_##type##_GET_REAL(&(q))
#define PyArray_CLE(p,q, type) NPY_##type##_GET_REAL(&(p)) == NPY_##type##_GET_REAL(&(q)) \
? NPY_##type##_GET_IMAG(&(p)) <= NPY_##type##_GET_IMAG(&(q)) \
: NPY_##type##_GET_REAL(&(p)) <= NPY_##type##_GET_REAL(&(q))
#define PyArray_CGE(p,q, type) NPY_##type##_GET_REAL(&(p)) == NPY_##type##_GET_REAL(&(q)) \
? NPY_##type##_GET_IMAG(&(p)) >= NPY_##type##_GET_IMAG(&(q)) \
: NPY_##type##_GET_REAL(&(p)) >= NPY_##type##_GET_REAL(&(q))
#define PyArray_CEQ(p,q, type) (p) == (q)
#define PyArray_CNE(p,q, type) (p) != (q)
#endif

/*
* C API: consists of Macros and functions. The MACROS are defined
Expand Down
205 changes: 180 additions & 25 deletions 205 numpy/core/include/numpy/npy_common.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,11 @@ typedef unsigned char npy_bool;
*/
#if NPY_SIZEOF_LONGDOUBLE == NPY_SIZEOF_DOUBLE
#define NPY_LONGDOUBLE_FMT "g"
#define longdouble_t double
typedef double npy_longdouble;
#else
#define NPY_LONGDOUBLE_FMT "Lg"
#define longdouble_t long double
typedef long double npy_longdouble;
#endif

Expand All @@ -361,49 +363,202 @@ typedef double npy_double;
typedef Py_hash_t npy_hash_t;
#define NPY_SIZEOF_HASH_T NPY_SIZEOF_INTP

/*
* Disabling C99 complex usage: a lot of C code in numpy/scipy rely on being
* able to do .real/.imag. Will have to convert code first.
*/
#if 0
#if defined(NPY_USE_C99_COMPLEX) && defined(NPY_HAVE_COMPLEX_DOUBLE)
typedef complex npy_cdouble;
#ifdef __cplusplus
extern "C++" {
#include <complex>
}
#else
typedef struct { double real, imag; } npy_cdouble;
#include <complex.h>
#endif

#if defined(NPY_USE_C99_COMPLEX) && defined(NPY_HAVE_COMPLEX_FLOAT)
typedef complex float npy_cfloat;
#else
typedef struct { float real, imag; } npy_cfloat;
#endif

#if defined(NPY_USE_C99_COMPLEX) && defined(NPY_HAVE_COMPLEX_LONG_DOUBLE)
typedef complex long double npy_clongdouble;
#else
typedef struct {npy_longdouble real, imag;} npy_clongdouble;
#endif
#endif
#if NPY_SIZEOF_COMPLEX_DOUBLE != 2 * NPY_SIZEOF_DOUBLE
#error npy_cdouble definition is not compatible with C99 complex definition ! \
Please contact NumPy maintainers and give detailed information about your \
compiler and platform
#endif
typedef struct { double real, imag; } npy_cdouble;

#if NPY_SIZEOF_COMPLEX_FLOAT != 2 * NPY_SIZEOF_FLOAT
#error npy_cfloat definition is not compatible with C99 complex definition ! \
Please contact NumPy maintainers and give detailed information about your \
compiler and platform
#endif
typedef struct { float real, imag; } npy_cfloat;

#if NPY_SIZEOF_COMPLEX_LONGDOUBLE != 2 * NPY_SIZEOF_LONGDOUBLE
#error npy_clongdouble definition is not compatible with C99 complex definition ! \
Please contact NumPy maintainers and give detailed information about your \
compiler and platform
#endif
typedef struct { npy_longdouble real, imag; } npy_clongdouble;

Comment thread
lysnikolaou marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


#if !defined(__cplusplus) && defined(_MSC_VER) && !defined(__INTEL_COMPILER)
typedef _Dcomplex npy_cdouble;
typedef _Fcomplex npy_cfloat;
typedef _Lcomplex npy_clongdouble;
#define NPY_CDOUBLE_INIT(real, imag) _CBuild(real, imag)
#define NPY_CFLOAT_INIT(real, imag) _FCBuild(real, imag)
#define NPY_CLONGDOUBLE_INIT(real, imag) _LCBuild(real, imag)
#elif defined(__cplusplus) /* && (!defined(_MSC_VER) || defined(__INTEL_COMPILER)) */
typedef std::complex<double> npy_cdouble;
typedef std::complex<float> npy_cfloat;
typedef std::complex<longdouble_t> npy_clongdouble;
Comment thread
lysnikolaou marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

#define NPY_CDOUBLE_INIT(real, imag) std::complex<double>(real, imag)
#define NPY_CFLOAT_INIT(real, imag) std::complex<float>(real, imag)
#define NPY_CLONGDOUBLE_INIT(real, imag) std::complex<longdouble_t>(real, imag)
#else /* !defined(__cplusplus) && (!defined(_MSC_VER) || defined(__INTEL_COMPILER)) */
typedef complex double npy_cdouble;
typedef complex float npy_cfloat;
typedef complex longdouble_t npy_clongdouble;
#define NPY_CDOUBLE_INIT(real, imag) CMPLX(real, imag)
#define NPY_CFLOAT_INIT(real, imag) CMPLXF(real, imag)
#define NPY_CLONGDOUBLE_INIT(real, imag) CMPLXL(real, imag)
#endif

#ifndef __cplusplus
typedef union {
double arr[2];
npy_cdouble comp;
} _npy_cdouble_to_arr;

typedef union {
float arr[2];
npy_cfloat comp;
} _npy_cfloat_to_arr;

typedef union {
longdouble_t arr[2];
npy_clongdouble comp;
} _npy_clongdouble_to_arr;
#endif

static inline double NPY_CDOUBLE_GET_REAL(const npy_cdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const double *>(c)[0];
#else
_npy_cdouble_to_arr tmp;
tmp.comp = *c;
return tmp.arr[0];
#endif
}

static inline double NPY_CDOUBLE_GET_IMAG(const npy_cdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const double *>(c)[1];
#else
_npy_cdouble_to_arr tmp;
tmp.comp = *c;
return tmp.arr[1];
#endif
}

static inline void NPY_CDOUBLE_SET_REAL(npy_cdouble *c, double real) {
#ifdef __cplusplus
double *tmp = reinterpret_cast<double *>(c);
tmp[0] = real;
#else
_npy_cdouble_to_arr tmp;
tmp.comp = *c;
tmp.arr[0] = real;
*c = tmp.comp;
#endif
Comment thread
lysnikolaou marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

}

static inline void NPY_CDOUBLE_SET_IMAG(npy_cdouble *c, double imag) {
#ifdef __cplusplus
double *tmp = reinterpret_cast<double *>(c);
tmp[1] = imag;
#else
_npy_cdouble_to_arr tmp;
tmp.comp = *c;
tmp.arr[1] = imag;
*c = tmp.comp;
#endif
}

static inline float NPY_CFLOAT_GET_REAL(const npy_cfloat *c) {
#ifdef __cplusplus
return reinterpret_cast<const float *>(c)[0];
#else
_npy_cfloat_to_arr tmp;
tmp.comp = *c;
return tmp.arr[0];
#endif
}

static inline float NPY_CFLOAT_GET_IMAG(const npy_cfloat *c) {
#ifdef __cplusplus
return reinterpret_cast<const float *>(c)[1];
#else
_npy_cdouble_to_arr tmp;
tmp.comp = *c;
return tmp.arr[1];
#endif
}

static inline void NPY_CFLOAT_SET_REAL(npy_cfloat *c, float real) {
#ifdef __cplusplus
float *tmp = reinterpret_cast<float *>(c);
tmp[0] = real;
#else
_npy_cfloat_to_arr tmp;
tmp.comp = *c;
tmp.arr[0] = real;
*c = tmp.comp;
#endif
}

static inline void NPY_CFLOAT_SET_IMAG(npy_cfloat *c, float imag) {
#ifdef __cplusplus
float *tmp = reinterpret_cast<float *>(c);
tmp[1] = imag;
#else
_npy_cfloat_to_arr tmp;
tmp.comp = *c;
tmp.arr[1] = imag;
*c = tmp.comp;
#endif
}

static inline longdouble_t NPY_CLONGDOUBLE_GET_REAL(const npy_clongdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const longdouble_t *>(c)[0];
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = *c;
return tmp.arr[0];
#endif
}

static inline longdouble_t NPY_CLONGDOUBLE_GET_IMAG(const npy_clongdouble *c) {
#ifdef __cplusplus
return reinterpret_cast<const longdouble_t *>(c)[1];
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = *c;
return tmp.arr[1];
#endif
}

static inline void NPY_CLONGDOUBLE_SET_REAL(npy_clongdouble *c, longdouble_t real) {
#ifdef __cplusplus
longdouble_t *tmp = reinterpret_cast<longdouble_t *>(c);
tmp[0] = real;
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = *c;
tmp.arr[0] = real;
*c = tmp.comp;
#endif
}

static inline void NPY_CLONGDOUBLE_SET_IMAG(npy_clongdouble *c, longdouble_t imag) {
#ifdef __cplusplus
longdouble_t *tmp = reinterpret_cast<longdouble_t *>(c);
tmp[1] = imag;
#else
_npy_clongdouble_to_arr tmp;
tmp.comp = *c;
tmp.arr[1] = imag;
*c = tmp.comp;
#endif
}

/*
* numarray-style bit-width typedefs
Expand Down
61 changes: 18 additions & 43 deletions 61 numpy/core/include/numpy/npy_math.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -357,84 +357,59 @@ NPY_INPLACE npy_longdouble npy_heavisidel(npy_longdouble x, npy_longdouble h0);
* Complex declarations
*/

/*
* C99 specifies that complex numbers have the same representation as
* an array of two elements, where the first element is the real part
* and the second element is the imaginary part.
*/
#define __NPY_CPACK_IMP(x, y, type, ctype) \
union { \
ctype z; \
type a[2]; \
} z1; \
\
z1.a[0] = (x); \
z1.a[1] = (y); \
\
return z1.z;

static inline npy_cdouble npy_cpack(double x, double y)
{
__NPY_CPACK_IMP(x, y, double, npy_cdouble);
npy_cdouble z = NPY_CDOUBLE_INIT(0.0, 0.0);
NPY_CDOUBLE_SET_REAL(&z, x);
NPY_CDOUBLE_SET_IMAG(&z, y);
return z;
}

static inline npy_cfloat npy_cpackf(float x, float y)
{
__NPY_CPACK_IMP(x, y, float, npy_cfloat);
npy_cfloat z = NPY_CFLOAT_INIT(0.0f, 0.0f);
NPY_CFLOAT_SET_REAL(&z, x);
NPY_CFLOAT_SET_IMAG(&z, y);
return z;
}

static inline npy_clongdouble npy_cpackl(npy_longdouble x, npy_longdouble y)
{
__NPY_CPACK_IMP(x, y, npy_longdouble, npy_clongdouble);
npy_clongdouble z = NPY_CLONGDOUBLE_INIT(0.0l, 0.0l);
NPY_CLONGDOUBLE_SET_REAL(&z, x);
NPY_CLONGDOUBLE_SET_IMAG(&z, y);
return z;
}
#undef __NPY_CPACK_IMP

/*
* Same remark as above, but in the other direction: extract first/second
* member of complex number, assuming a C99-compatible representation
*
* Those are defineds as static inline, and such as a reasonable compiler would
* most likely compile this to one or two instructions (on CISC at least)
*/
#define __NPY_CEXTRACT_IMP(z, index, type, ctype) \
union { \
ctype z; \
type a[2]; \
} __z_repr; \
__z_repr.z = z; \
\
return __z_repr.a[index];

static inline double npy_creal(npy_cdouble z)
{
__NPY_CEXTRACT_IMP(z, 0, double, npy_cdouble);
return NPY_CDOUBLE_GET_REAL(&z);
}

static inline double npy_cimag(npy_cdouble z)
{
__NPY_CEXTRACT_IMP(z, 1, double, npy_cdouble);
return NPY_CDOUBLE_GET_IMAG(&z);
}

static inline float npy_crealf(npy_cfloat z)
{
__NPY_CEXTRACT_IMP(z, 0, float, npy_cfloat);
return NPY_CFLOAT_GET_REAL(&z);
}

static inline float npy_cimagf(npy_cfloat z)
{
__NPY_CEXTRACT_IMP(z, 1, float, npy_cfloat);
return NPY_CFLOAT_GET_IMAG(&z);
}

static inline npy_longdouble npy_creall(npy_clongdouble z)
{
__NPY_CEXTRACT_IMP(z, 0, npy_longdouble, npy_clongdouble);
return NPY_CLONGDOUBLE_GET_REAL(&z);
}

static inline npy_longdouble npy_cimagl(npy_clongdouble z)
{
__NPY_CEXTRACT_IMP(z, 1, npy_longdouble, npy_clongdouble);
return NPY_CLONGDOUBLE_GET_IMAG(&z);
}
#undef __NPY_CEXTRACT_IMP
Comment thread
lysnikolaou marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.


/*
* Double precision complex functions
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.