numpy  2.0.0
include/numpy/npy_math.h
Go to the documentation of this file.
00001 #ifndef __NPY_MATH_C99_H_
00002 #define __NPY_MATH_C99_H_
00003 
00004 #ifdef __cplusplus
00005 extern "C" {
00006 #endif
00007 
00008 #include <math.h>
00009 #ifdef __SUNPRO_CC
00010 #include <sunmath.h>
00011 #endif
00012 #ifdef HAVE_NPY_CONFIG_H
00013 #include <npy_config.h>
00014 #endif
00015 #include <numpy/npy_common.h>
00016 
00017 
00018 /*
00019  * NAN and INFINITY like macros (same behavior as glibc for NAN, same as C99
00020  * for INFINITY)
00021  *
00022  * XXX: I should test whether INFINITY and NAN are available on the platform
00023  */
00024 NPY_INLINE static float __npy_inff(void)
00025 {
00026     const union { npy_uint32 __i; float __f;} __bint = {0x7f800000UL};
00027     return __bint.__f;
00028 }
00029 
00030 NPY_INLINE static float __npy_nanf(void)
00031 {
00032     const union { npy_uint32 __i; float __f;} __bint = {0x7fc00000UL};
00033     return __bint.__f;
00034 }
00035 
00036 NPY_INLINE static float __npy_pzerof(void)
00037 {
00038     const union { npy_uint32 __i; float __f;} __bint = {0x00000000UL};
00039     return __bint.__f;
00040 }
00041 
00042 NPY_INLINE static float __npy_nzerof(void)
00043 {
00044     const union { npy_uint32 __i; float __f;} __bint = {0x80000000UL};
00045     return __bint.__f;
00046 }
00047 
00048 #define NPY_INFINITYF __npy_inff()
00049 #define NPY_NANF __npy_nanf()
00050 #define NPY_PZEROF __npy_pzerof()
00051 #define NPY_NZEROF __npy_nzerof()
00052 
00053 #define NPY_INFINITY ((npy_double)NPY_INFINITYF)
00054 #define NPY_NAN ((npy_double)NPY_NANF)
00055 #define NPY_PZERO ((npy_double)NPY_PZEROF)
00056 #define NPY_NZERO ((npy_double)NPY_NZEROF)
00057 
00058 #define NPY_INFINITYL ((npy_longdouble)NPY_INFINITYF)
00059 #define NPY_NANL ((npy_longdouble)NPY_NANF)
00060 #define NPY_PZEROL ((npy_longdouble)NPY_PZEROF)
00061 #define NPY_NZEROL ((npy_longdouble)NPY_NZEROF)
00062 
00063 /*
00064  * Useful constants
00065  */
00066 #define NPY_E         2.718281828459045235360287471352662498  /* e */
00067 #define NPY_LOG2E     1.442695040888963407359924681001892137  /* log_2 e */
00068 #define NPY_LOG10E    0.434294481903251827651128918916605082  /* log_10 e */
00069 #define NPY_LOGE2     0.693147180559945309417232121458176568  /* log_e 2 */
00070 #define NPY_LOGE10    2.302585092994045684017991454684364208  /* log_e 10 */
00071 #define NPY_PI        3.141592653589793238462643383279502884  /* pi */
00072 #define NPY_PI_2      1.570796326794896619231321691639751442  /* pi/2 */
00073 #define NPY_PI_4      0.785398163397448309615660845819875721  /* pi/4 */
00074 #define NPY_1_PI      0.318309886183790671537767526745028724  /* 1/pi */
00075 #define NPY_2_PI      0.636619772367581343075535053490057448  /* 2/pi */
00076 #define NPY_EULER     0.577215664901532860606512090082402431  /* Euler constant */
00077 #define NPY_SQRT2     1.414213562373095048801688724209698079  /* sqrt(2) */
00078 #define NPY_SQRT1_2   0.707106781186547524400844362104849039  /* 1/sqrt(2) */
00079 
00080 #define NPY_Ef        2.718281828459045235360287471352662498F /* e */
00081 #define NPY_LOG2Ef    1.442695040888963407359924681001892137F /* log_2 e */
00082 #define NPY_LOG10Ef   0.434294481903251827651128918916605082F /* log_10 e */
00083 #define NPY_LOGE2f    0.693147180559945309417232121458176568F /* log_e 2 */
00084 #define NPY_LOGE10f   2.302585092994045684017991454684364208F /* log_e 10 */
00085 #define NPY_PIf       3.141592653589793238462643383279502884F /* pi */
00086 #define NPY_PI_2f     1.570796326794896619231321691639751442F /* pi/2 */
00087 #define NPY_PI_4f     0.785398163397448309615660845819875721F /* pi/4 */
00088 #define NPY_1_PIf     0.318309886183790671537767526745028724F /* 1/pi */
00089 #define NPY_2_PIf     0.636619772367581343075535053490057448F /* 2/pi */
00090 #define NPY_EULERf    0.577215664901532860606512090082402431F /* Euler constant */
00091 #define NPY_SQRT2f    1.414213562373095048801688724209698079F /* sqrt(2) */
00092 #define NPY_SQRT1_2f  0.707106781186547524400844362104849039F /* 1/sqrt(2) */
00093 
00094 #define NPY_El        2.718281828459045235360287471352662498L /* e */
00095 #define NPY_LOG2El    1.442695040888963407359924681001892137L /* log_2 e */
00096 #define NPY_LOG10El   0.434294481903251827651128918916605082L /* log_10 e */
00097 #define NPY_LOGE2l    0.693147180559945309417232121458176568L /* log_e 2 */
00098 #define NPY_LOGE10l   2.302585092994045684017991454684364208L /* log_e 10 */
00099 #define NPY_PIl       3.141592653589793238462643383279502884L /* pi */
00100 #define NPY_PI_2l     1.570796326794896619231321691639751442L /* pi/2 */
00101 #define NPY_PI_4l     0.785398163397448309615660845819875721L /* pi/4 */
00102 #define NPY_1_PIl     0.318309886183790671537767526745028724L /* 1/pi */
00103 #define NPY_2_PIl     0.636619772367581343075535053490057448L /* 2/pi */
00104 #define NPY_EULERl    0.577215664901532860606512090082402431L /* Euler constant */
00105 #define NPY_SQRT2l    1.414213562373095048801688724209698079L /* sqrt(2) */
00106 #define NPY_SQRT1_2l  0.707106781186547524400844362104849039L /* 1/sqrt(2) */
00107 
00108 /*
00109  * C99 double math funcs
00110  */
00111 double npy_sin(double x);
00112 double npy_cos(double x);
00113 double npy_tan(double x);
00114 double npy_sinh(double x);
00115 double npy_cosh(double x);
00116 double npy_tanh(double x);
00117 
00118 double npy_asin(double x);
00119 double npy_acos(double x);
00120 double npy_atan(double x);
00121 
00122 double npy_log(double x);
00123 double npy_log10(double x);
00124 double npy_exp(double x);
00125 double npy_sqrt(double x);
00126 double npy_cbrt(double x);
00127 
00128 double npy_fabs(double x);
00129 double npy_ceil(double x);
00130 double npy_fmod(double x, double y);
00131 double npy_floor(double x);
00132 
00133 double npy_expm1(double x);
00134 double npy_log1p(double x);
00135 double npy_hypot(double x, double y);
00136 double npy_acosh(double x);
00137 double npy_asinh(double xx);
00138 double npy_atanh(double x);
00139 double npy_rint(double x);
00140 double npy_trunc(double x);
00141 double npy_exp2(double x);
00142 double npy_log2(double x);
00143 
00144 double npy_atan2(double x, double y);
00145 double npy_pow(double x, double y);
00146 double npy_modf(double x, double* y);
00147 double npy_frexp(double x, int* y);
00148 double npy_ldexp(double n, int y);
00149 
00150 double npy_copysign(double x, double y);
00151 double npy_nextafter(double x, double y);
00152 double npy_spacing(double x);
00153 
00154 /*
00155  * IEEE 754 fpu handling. Those are guaranteed to be macros
00156  */
00157 
00158 /* use builtins to avoid function calls in tight loops
00159  * only available if npy_config.h is available (= numpys own build) */
00160 #if HAVE___BUILTIN_ISNAN
00161     #define npy_isnan(x) __builtin_isnan(x)
00162 #else
00163     #ifndef NPY_HAVE_DECL_ISNAN
00164         #define npy_isnan(x) ((x) != (x))
00165     #else
00166         #if defined(_MSC_VER) && (_MSC_VER < 1900)
00167             #define npy_isnan(x) _isnan((x))
00168         #else
00169             #define npy_isnan(x) isnan(x)
00170         #endif
00171     #endif
00172 #endif
00173 
00174 
00175 /* only available if npy_config.h is available (= numpys own build) */
00176 #if HAVE___BUILTIN_ISFINITE
00177     #define npy_isfinite(x) __builtin_isfinite(x)
00178 #else
00179     #ifndef NPY_HAVE_DECL_ISFINITE
00180         #ifdef _MSC_VER
00181             #define npy_isfinite(x) _finite((x))
00182         #else
00183             #define npy_isfinite(x) !npy_isnan((x) + (-x))
00184         #endif
00185     #else
00186         #define npy_isfinite(x) isfinite((x))
00187     #endif
00188 #endif
00189 
00190 /* only available if npy_config.h is available (= numpys own build) */
00191 #if HAVE___BUILTIN_ISINF
00192     #define npy_isinf(x) __builtin_isinf(x)
00193 #else
00194     #ifndef NPY_HAVE_DECL_ISINF
00195         #define npy_isinf(x) (!npy_isfinite(x) && !npy_isnan(x))
00196     #else
00197         #if defined(_MSC_VER) && (_MSC_VER < 1900)
00198             #define npy_isinf(x) (!_finite((x)) && !_isnan((x)))
00199         #else
00200             #define npy_isinf(x) isinf((x))
00201         #endif
00202     #endif
00203 #endif
00204 
00205 #ifndef NPY_HAVE_DECL_SIGNBIT
00206     int _npy_signbit_f(float x);
00207     int _npy_signbit_d(double x);
00208     int _npy_signbit_ld(long double x);
00209     #define npy_signbit(x) \
00210         (sizeof (x) == sizeof (long double) ? _npy_signbit_ld (x) \
00211          : sizeof (x) == sizeof (double) ? _npy_signbit_d (x) \
00212          : _npy_signbit_f (x))
00213 #else
00214     #define npy_signbit(x) signbit((x))
00215 #endif
00216 
00217 /*
00218  * float C99 math functions
00219  */
00220 
00221 float npy_sinf(float x);
00222 float npy_cosf(float x);
00223 float npy_tanf(float x);
00224 float npy_sinhf(float x);
00225 float npy_coshf(float x);
00226 float npy_tanhf(float x);
00227 float npy_fabsf(float x);
00228 float npy_floorf(float x);
00229 float npy_ceilf(float x);
00230 float npy_rintf(float x);
00231 float npy_truncf(float x);
00232 float npy_sqrtf(float x);
00233 float npy_cbrtf(float x);
00234 float npy_log10f(float x);
00235 float npy_logf(float x);
00236 float npy_expf(float x);
00237 float npy_expm1f(float x);
00238 float npy_asinf(float x);
00239 float npy_acosf(float x);
00240 float npy_atanf(float x);
00241 float npy_asinhf(float x);
00242 float npy_acoshf(float x);
00243 float npy_atanhf(float x);
00244 float npy_log1pf(float x);
00245 float npy_exp2f(float x);
00246 float npy_log2f(float x);
00247 
00248 float npy_atan2f(float x, float y);
00249 float npy_hypotf(float x, float y);
00250 float npy_powf(float x, float y);
00251 float npy_fmodf(float x, float y);
00252 
00253 float npy_modff(float x, float* y);
00254 float npy_frexpf(float x, int* y);
00255 float npy_ldexpf(float x, int y);
00256 
00257 float npy_copysignf(float x, float y);
00258 float npy_nextafterf(float x, float y);
00259 float npy_spacingf(float x);
00260 
00261 /*
00262  * long double C99 math functions
00263  */
00264 
00265 npy_longdouble npy_sinl(npy_longdouble x);
00266 npy_longdouble npy_cosl(npy_longdouble x);
00267 npy_longdouble npy_tanl(npy_longdouble x);
00268 npy_longdouble npy_sinhl(npy_longdouble x);
00269 npy_longdouble npy_coshl(npy_longdouble x);
00270 npy_longdouble npy_tanhl(npy_longdouble x);
00271 npy_longdouble npy_fabsl(npy_longdouble x);
00272 npy_longdouble npy_floorl(npy_longdouble x);
00273 npy_longdouble npy_ceill(npy_longdouble x);
00274 npy_longdouble npy_rintl(npy_longdouble x);
00275 npy_longdouble npy_truncl(npy_longdouble x);
00276 npy_longdouble npy_sqrtl(npy_longdouble x);
00277 npy_longdouble npy_cbrtl(npy_longdouble x);
00278 npy_longdouble npy_log10l(npy_longdouble x);
00279 npy_longdouble npy_logl(npy_longdouble x);
00280 npy_longdouble npy_expl(npy_longdouble x);
00281 npy_longdouble npy_expm1l(npy_longdouble x);
00282 npy_longdouble npy_asinl(npy_longdouble x);
00283 npy_longdouble npy_acosl(npy_longdouble x);
00284 npy_longdouble npy_atanl(npy_longdouble x);
00285 npy_longdouble npy_asinhl(npy_longdouble x);
00286 npy_longdouble npy_acoshl(npy_longdouble x);
00287 npy_longdouble npy_atanhl(npy_longdouble x);
00288 npy_longdouble npy_log1pl(npy_longdouble x);
00289 npy_longdouble npy_exp2l(npy_longdouble x);
00290 npy_longdouble npy_log2l(npy_longdouble x);
00291 
00292 npy_longdouble npy_atan2l(npy_longdouble x, npy_longdouble y);
00293 npy_longdouble npy_hypotl(npy_longdouble x, npy_longdouble y);
00294 npy_longdouble npy_powl(npy_longdouble x, npy_longdouble y);
00295 npy_longdouble npy_fmodl(npy_longdouble x, npy_longdouble y);
00296 
00297 npy_longdouble npy_modfl(npy_longdouble x, npy_longdouble* y);
00298 npy_longdouble npy_frexpl(npy_longdouble x, int* y);
00299 npy_longdouble npy_ldexpl(npy_longdouble x, int y);
00300 
00301 npy_longdouble npy_copysignl(npy_longdouble x, npy_longdouble y);
00302 npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y);
00303 npy_longdouble npy_spacingl(npy_longdouble x);
00304 
00305 /*
00306  * Non standard functions
00307  */
00308 double npy_deg2rad(double x);
00309 double npy_rad2deg(double x);
00310 double npy_logaddexp(double x, double y);
00311 double npy_logaddexp2(double x, double y);
00312 double npy_divmod(double x, double y, double *modulus);
00313 
00314 float npy_deg2radf(float x);
00315 float npy_rad2degf(float x);
00316 float npy_logaddexpf(float x, float y);
00317 float npy_logaddexp2f(float x, float y);
00318 float npy_divmodf(float x, float y, float *modulus);
00319 
00320 npy_longdouble npy_deg2radl(npy_longdouble x);
00321 npy_longdouble npy_rad2degl(npy_longdouble x);
00322 npy_longdouble npy_logaddexpl(npy_longdouble x, npy_longdouble y);
00323 npy_longdouble npy_logaddexp2l(npy_longdouble x, npy_longdouble y);
00324 npy_longdouble npy_divmodl(npy_longdouble x, npy_longdouble y,
00325                            npy_longdouble *modulus);
00326 
00327 #define npy_degrees npy_rad2deg
00328 #define npy_degreesf npy_rad2degf
00329 #define npy_degreesl npy_rad2degl
00330 
00331 #define npy_radians npy_deg2rad
00332 #define npy_radiansf npy_deg2radf
00333 #define npy_radiansl npy_deg2radl
00334 
00335 /*
00336  * Complex declarations
00337  */
00338 
00339 /*
00340  * C99 specifies that complex numbers have the same representation as
00341  * an array of two elements, where the first element is the real part
00342  * and the second element is the imaginary part.
00343  */
00344 #define __NPY_CPACK_IMP(x, y, type, ctype)   \
00345     union {                                  \
00346         ctype z;                             \
00347         type a[2];                           \
00348     } z1;;                                   \
00349                                              \
00350     z1.a[0] = (x);                           \
00351     z1.a[1] = (y);                           \
00352                                              \
00353     return z1.z;
00354 
00355 static NPY_INLINE npy_cdouble npy_cpack(double x, double y)
00356 {
00357     __NPY_CPACK_IMP(x, y, double, npy_cdouble);
00358 }
00359 
00360 static NPY_INLINE npy_cfloat npy_cpackf(float x, float y)
00361 {
00362     __NPY_CPACK_IMP(x, y, float, npy_cfloat);
00363 }
00364 
00365 static NPY_INLINE npy_clongdouble npy_cpackl(npy_longdouble x, npy_longdouble y)
00366 {
00367     __NPY_CPACK_IMP(x, y, npy_longdouble, npy_clongdouble);
00368 }
00369 #undef __NPY_CPACK_IMP
00370 
00371 /*
00372  * Same remark as above, but in the other direction: extract first/second
00373  * member of complex number, assuming a C99-compatible representation
00374  *
00375  * Those are defineds as static inline, and such as a reasonable compiler would
00376  * most likely compile this to one or two instructions (on CISC at least)
00377  */
00378 #define __NPY_CEXTRACT_IMP(z, index, type, ctype)   \
00379     union {                                         \
00380         ctype z;                                    \
00381         type a[2];                                  \
00382     } __z_repr;                                     \
00383     __z_repr.z = z;                                 \
00384                                                     \
00385     return __z_repr.a[index];
00386 
00387 static NPY_INLINE double npy_creal(npy_cdouble z)
00388 {
00389     __NPY_CEXTRACT_IMP(z, 0, double, npy_cdouble);
00390 }
00391 
00392 static NPY_INLINE double npy_cimag(npy_cdouble z)
00393 {
00394     __NPY_CEXTRACT_IMP(z, 1, double, npy_cdouble);
00395 }
00396 
00397 static NPY_INLINE float npy_crealf(npy_cfloat z)
00398 {
00399     __NPY_CEXTRACT_IMP(z, 0, float, npy_cfloat);
00400 }
00401 
00402 static NPY_INLINE float npy_cimagf(npy_cfloat z)
00403 {
00404     __NPY_CEXTRACT_IMP(z, 1, float, npy_cfloat);
00405 }
00406 
00407 static NPY_INLINE npy_longdouble npy_creall(npy_clongdouble z)
00408 {
00409     __NPY_CEXTRACT_IMP(z, 0, npy_longdouble, npy_clongdouble);
00410 }
00411 
00412 static NPY_INLINE npy_longdouble npy_cimagl(npy_clongdouble z)
00413 {
00414     __NPY_CEXTRACT_IMP(z, 1, npy_longdouble, npy_clongdouble);
00415 }
00416 #undef __NPY_CEXTRACT_IMP
00417 
00418 /*
00419  * Double precision complex functions
00420  */
00421 double npy_cabs(npy_cdouble z);
00422 double npy_carg(npy_cdouble z);
00423 
00424 npy_cdouble npy_cexp(npy_cdouble z);
00425 npy_cdouble npy_clog(npy_cdouble z);
00426 npy_cdouble npy_cpow(npy_cdouble x, npy_cdouble y);
00427 
00428 npy_cdouble npy_csqrt(npy_cdouble z);
00429 
00430 npy_cdouble npy_ccos(npy_cdouble z);
00431 npy_cdouble npy_csin(npy_cdouble z);
00432 npy_cdouble npy_ctan(npy_cdouble z);
00433 
00434 npy_cdouble npy_ccosh(npy_cdouble z);
00435 npy_cdouble npy_csinh(npy_cdouble z);
00436 npy_cdouble npy_ctanh(npy_cdouble z);
00437 
00438 npy_cdouble npy_cacos(npy_cdouble z);
00439 npy_cdouble npy_casin(npy_cdouble z);
00440 npy_cdouble npy_catan(npy_cdouble z);
00441 
00442 npy_cdouble npy_cacosh(npy_cdouble z);
00443 npy_cdouble npy_casinh(npy_cdouble z);
00444 npy_cdouble npy_catanh(npy_cdouble z);
00445 
00446 /*
00447  * Single precision complex functions
00448  */
00449 float npy_cabsf(npy_cfloat z);
00450 float npy_cargf(npy_cfloat z);
00451 
00452 npy_cfloat npy_cexpf(npy_cfloat z);
00453 npy_cfloat npy_clogf(npy_cfloat z);
00454 npy_cfloat npy_cpowf(npy_cfloat x, npy_cfloat y);
00455 
00456 npy_cfloat npy_csqrtf(npy_cfloat z);
00457 
00458 npy_cfloat npy_ccosf(npy_cfloat z);
00459 npy_cfloat npy_csinf(npy_cfloat z);
00460 npy_cfloat npy_ctanf(npy_cfloat z);
00461 
00462 npy_cfloat npy_ccoshf(npy_cfloat z);
00463 npy_cfloat npy_csinhf(npy_cfloat z);
00464 npy_cfloat npy_ctanhf(npy_cfloat z);
00465 
00466 npy_cfloat npy_cacosf(npy_cfloat z);
00467 npy_cfloat npy_casinf(npy_cfloat z);
00468 npy_cfloat npy_catanf(npy_cfloat z);
00469 
00470 npy_cfloat npy_cacoshf(npy_cfloat z);
00471 npy_cfloat npy_casinhf(npy_cfloat z);
00472 npy_cfloat npy_catanhf(npy_cfloat z);
00473 
00474 
00475 /*
00476  * Extended precision complex functions
00477  */
00478 npy_longdouble npy_cabsl(npy_clongdouble z);
00479 npy_longdouble npy_cargl(npy_clongdouble z);
00480 
00481 npy_clongdouble npy_cexpl(npy_clongdouble z);
00482 npy_clongdouble npy_clogl(npy_clongdouble z);
00483 npy_clongdouble npy_cpowl(npy_clongdouble x, npy_clongdouble y);
00484 
00485 npy_clongdouble npy_csqrtl(npy_clongdouble z);
00486 
00487 npy_clongdouble npy_ccosl(npy_clongdouble z);
00488 npy_clongdouble npy_csinl(npy_clongdouble z);
00489 npy_clongdouble npy_ctanl(npy_clongdouble z);
00490 
00491 npy_clongdouble npy_ccoshl(npy_clongdouble z);
00492 npy_clongdouble npy_csinhl(npy_clongdouble z);
00493 npy_clongdouble npy_ctanhl(npy_clongdouble z);
00494 
00495 npy_clongdouble npy_cacosl(npy_clongdouble z);
00496 npy_clongdouble npy_casinl(npy_clongdouble z);
00497 npy_clongdouble npy_catanl(npy_clongdouble z);
00498 
00499 npy_clongdouble npy_cacoshl(npy_clongdouble z);
00500 npy_clongdouble npy_casinhl(npy_clongdouble z);
00501 npy_clongdouble npy_catanhl(npy_clongdouble z);
00502 
00503 
00504 /*
00505  * Functions that set the floating point error
00506  * status word.
00507  */
00508 
00509 /*
00510  * platform-dependent code translates floating point
00511  * status to an integer sum of these values
00512  */
00513 #define NPY_FPE_DIVIDEBYZERO  1
00514 #define NPY_FPE_OVERFLOW      2
00515 #define NPY_FPE_UNDERFLOW     4
00516 #define NPY_FPE_INVALID       8
00517 
00518 int npy_get_floatstatus(void);
00519 int npy_clear_floatstatus(void);
00520 void npy_set_floatstatus_divbyzero(void);
00521 void npy_set_floatstatus_overflow(void);
00522 void npy_set_floatstatus_underflow(void);
00523 void npy_set_floatstatus_invalid(void);
00524 
00525 #ifdef __cplusplus
00526 }
00527 #endif
00528 
00529 #endif