Skip to content

Commit

Permalink
Merge pull request #22161 from mattip/simplify-npy_math2
Browse files Browse the repository at this point in the history
MAINT: simplify complex math function handling in npymath
  • Loading branch information
mattip committed Oct 6, 2022
2 parents 5ecaf36 + 05e213e commit 562c80a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 451 deletions.
41 changes: 41 additions & 0 deletions numpy/core/feature_detection_cmath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <complex.h>

#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#define cfloat _Fcomplex
#define cdouble _Dcomplex
#define cldouble _Lcomplex
#else
#define cfloat complex float
#define cdouble complex double
#define cldouble complex long double
#endif

cfloat csinf(cfloat);
cfloat ccosf(cfloat);
cfloat ctanf(cfloat);
cfloat csinhf(cfloat);
cfloat ccoshf(cfloat);
cfloat ctanhf(cfloat);
float crealf(cfloat);
float cimagf(cfloat);
cfloat conjf(cfloat);

cdouble csin(cdouble);
cdouble ccos(cdouble);
cdouble ctan(cdouble);
cdouble csinh(cdouble);
cdouble ccosh(cdouble);
cdouble ctanh(cdouble);
double creal(cdouble);
double cimag(cdouble);
cdouble conj(cdouble);

cldouble csinl(cldouble);
cldouble ccosl(cldouble);
cldouble ctanl(cldouble);
cldouble csinhl(cldouble);
cldouble ccoshl(cldouble);
cldouble ctanhl(cldouble);
long double creall(cldouble);
long double cimagl(cldouble);
cldouble conjl(cldouble);
12 changes: 8 additions & 4 deletions numpy/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def check_math_capabilities(config, ext, moredefs, mathlibs):
def check_func(
func_name,
decl=False,
headers=["feature_detection_math.h"],
headers=["feature_detection_math.h", "feature_detection_cmath.h"],
):
return config.check_func(
func_name,
Expand All @@ -145,8 +145,10 @@ def check_func(
headers=headers,
)

def check_funcs_once(funcs_name, headers=["feature_detection_math.h"],
add_to_moredefs=True):
def check_funcs_once(
funcs_name,
headers=["feature_detection_math.h", "feature_detection_cmath.h"],
add_to_moredefs=True):
call = dict([(f, True) for f in funcs_name])
call_args = dict([(f, FUNC_CALL_ARGS[f]) for f in funcs_name])
st = config.check_funcs_once(
Expand All @@ -161,7 +163,9 @@ def check_funcs_once(funcs_name, headers=["feature_detection_math.h"],
moredefs.extend([(fname2def(f), 1) for f in funcs_name])
return st

def check_funcs(funcs_name, headers=["feature_detection_math.h"]):
def check_funcs(
funcs_name,
headers=["feature_detection_math.h", "feature_detection_cmath.h"]):
# Use check_funcs_once first, and if it does not work, test func per
# func. Return success only if all the functions are available
if not check_funcs_once(funcs_name, headers=headers):
Expand Down
9 changes: 6 additions & 3 deletions numpy/core/setup_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ def set_sig(sig):
args = args.rpartition(")")[0]
funcname = prefix.rpartition(" ")[-1]
args = [arg.strip() for arg in args.split(",")]
FUNC_CALL_ARGS[funcname] = ", ".join("(%s) 0" % arg for arg in args)
# We use {0} because 0 alone cannot be cast to complex on MSVC in C:
FUNC_CALL_ARGS[funcname] = ", ".join("(%s){0}" % arg for arg in args)


for file in [
"feature_detection_locale.h",
"feature_detection_math.h",
"feature_detection_cmath.h",
"feature_detection_misc.h",
"feature_detection_stdio.h",
]:
Expand All @@ -128,6 +130,8 @@ def set_sig(sig):
"rint", "trunc", "exp2",
"copysign", "nextafter", "strtoll", "strtoull", "cbrt",
"log2", "pow", "hypot", "atan2",
"csin", "csinh", "ccos", "ccosh", "ctan", "ctanh",
"creal", "cimag", "conj"
]

OPTIONAL_LOCALE_FUNCS = ["strtold_l"]
Expand All @@ -147,8 +151,7 @@ def set_sig(sig):
]
C99_COMPLEX_FUNCS = [
"cabs", "cacos", "cacosh", "carg", "casin", "casinh", "catan",
"catanh", "ccos", "ccosh", "cexp", "cimag", "clog", "conj", "cpow",
"cproj", "creal", "csin", "csinh", "csqrt", "ctan", "ctanh"
"catanh", "cexp", "clog", "cpow", "csqrt",
]

OPTIONAL_HEADERS = [
Expand Down

0 comments on commit 562c80a

Please sign in to comment.