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