npy_math.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #ifndef __NPY_MATH_C99_H_
  2. #define __NPY_MATH_C99_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <math.h>
  7. #ifdef __SUNPRO_CC
  8. #include <sunmath.h>
  9. #endif
  10. #ifdef HAVE_NPY_CONFIG_H
  11. #include <npy_config.h>
  12. #endif
  13. #include <numpy/npy_common.h>
  14. /*
  15. * NAN and INFINITY like macros (same behavior as glibc for NAN, same as C99
  16. * for INFINITY)
  17. *
  18. * XXX: I should test whether INFINITY and NAN are available on the platform
  19. */
  20. NPY_INLINE static float __npy_inff(void)
  21. {
  22. const union { npy_uint32 __i; float __f;} __bint = {0x7f800000UL};
  23. return __bint.__f;
  24. }
  25. NPY_INLINE static float __npy_nanf(void)
  26. {
  27. const union { npy_uint32 __i; float __f;} __bint = {0x7fc00000UL};
  28. return __bint.__f;
  29. }
  30. NPY_INLINE static float __npy_pzerof(void)
  31. {
  32. const union { npy_uint32 __i; float __f;} __bint = {0x00000000UL};
  33. return __bint.__f;
  34. }
  35. NPY_INLINE static float __npy_nzerof(void)
  36. {
  37. const union { npy_uint32 __i; float __f;} __bint = {0x80000000UL};
  38. return __bint.__f;
  39. }
  40. #define NPY_INFINITYF __npy_inff()
  41. #define NPY_NANF __npy_nanf()
  42. #define NPY_PZEROF __npy_pzerof()
  43. #define NPY_NZEROF __npy_nzerof()
  44. #define NPY_INFINITY ((npy_double)NPY_INFINITYF)
  45. #define NPY_NAN ((npy_double)NPY_NANF)
  46. #define NPY_PZERO ((npy_double)NPY_PZEROF)
  47. #define NPY_NZERO ((npy_double)NPY_NZEROF)
  48. #define NPY_INFINITYL ((npy_longdouble)NPY_INFINITYF)
  49. #define NPY_NANL ((npy_longdouble)NPY_NANF)
  50. #define NPY_PZEROL ((npy_longdouble)NPY_PZEROF)
  51. #define NPY_NZEROL ((npy_longdouble)NPY_NZEROF)
  52. /*
  53. * Useful constants
  54. */
  55. #define NPY_E 2.718281828459045235360287471352662498 /* e */
  56. #define NPY_LOG2E 1.442695040888963407359924681001892137 /* log_2 e */
  57. #define NPY_LOG10E 0.434294481903251827651128918916605082 /* log_10 e */
  58. #define NPY_LOGE2 0.693147180559945309417232121458176568 /* log_e 2 */
  59. #define NPY_LOGE10 2.302585092994045684017991454684364208 /* log_e 10 */
  60. #define NPY_PI 3.141592653589793238462643383279502884 /* pi */
  61. #define NPY_PI_2 1.570796326794896619231321691639751442 /* pi/2 */
  62. #define NPY_PI_4 0.785398163397448309615660845819875721 /* pi/4 */
  63. #define NPY_1_PI 0.318309886183790671537767526745028724 /* 1/pi */
  64. #define NPY_2_PI 0.636619772367581343075535053490057448 /* 2/pi */
  65. #define NPY_EULER 0.577215664901532860606512090082402431 /* Euler constant */
  66. #define NPY_SQRT2 1.414213562373095048801688724209698079 /* sqrt(2) */
  67. #define NPY_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */
  68. #define NPY_Ef 2.718281828459045235360287471352662498F /* e */
  69. #define NPY_LOG2Ef 1.442695040888963407359924681001892137F /* log_2 e */
  70. #define NPY_LOG10Ef 0.434294481903251827651128918916605082F /* log_10 e */
  71. #define NPY_LOGE2f 0.693147180559945309417232121458176568F /* log_e 2 */
  72. #define NPY_LOGE10f 2.302585092994045684017991454684364208F /* log_e 10 */
  73. #define NPY_PIf 3.141592653589793238462643383279502884F /* pi */
  74. #define NPY_PI_2f 1.570796326794896619231321691639751442F /* pi/2 */
  75. #define NPY_PI_4f 0.785398163397448309615660845819875721F /* pi/4 */
  76. #define NPY_1_PIf 0.318309886183790671537767526745028724F /* 1/pi */
  77. #define NPY_2_PIf 0.636619772367581343075535053490057448F /* 2/pi */
  78. #define NPY_EULERf 0.577215664901532860606512090082402431F /* Euler constant */
  79. #define NPY_SQRT2f 1.414213562373095048801688724209698079F /* sqrt(2) */
  80. #define NPY_SQRT1_2f 0.707106781186547524400844362104849039F /* 1/sqrt(2) */
  81. #define NPY_El 2.718281828459045235360287471352662498L /* e */
  82. #define NPY_LOG2El 1.442695040888963407359924681001892137L /* log_2 e */
  83. #define NPY_LOG10El 0.434294481903251827651128918916605082L /* log_10 e */
  84. #define NPY_LOGE2l 0.693147180559945309417232121458176568L /* log_e 2 */
  85. #define NPY_LOGE10l 2.302585092994045684017991454684364208L /* log_e 10 */
  86. #define NPY_PIl 3.141592653589793238462643383279502884L /* pi */
  87. #define NPY_PI_2l 1.570796326794896619231321691639751442L /* pi/2 */
  88. #define NPY_PI_4l 0.785398163397448309615660845819875721L /* pi/4 */
  89. #define NPY_1_PIl 0.318309886183790671537767526745028724L /* 1/pi */
  90. #define NPY_2_PIl 0.636619772367581343075535053490057448L /* 2/pi */
  91. #define NPY_EULERl 0.577215664901532860606512090082402431L /* Euler constant */
  92. #define NPY_SQRT2l 1.414213562373095048801688724209698079L /* sqrt(2) */
  93. #define NPY_SQRT1_2l 0.707106781186547524400844362104849039L /* 1/sqrt(2) */
  94. /*
  95. * C99 double math funcs
  96. */
  97. double npy_sin(double x);
  98. double npy_cos(double x);
  99. double npy_tan(double x);
  100. double npy_sinh(double x);
  101. double npy_cosh(double x);
  102. double npy_tanh(double x);
  103. double npy_asin(double x);
  104. double npy_acos(double x);
  105. double npy_atan(double x);
  106. double npy_log(double x);
  107. double npy_log10(double x);
  108. double npy_exp(double x);
  109. double npy_sqrt(double x);
  110. double npy_cbrt(double x);
  111. double npy_fabs(double x);
  112. double npy_ceil(double x);
  113. double npy_fmod(double x, double y);
  114. double npy_floor(double x);
  115. double npy_expm1(double x);
  116. double npy_log1p(double x);
  117. double npy_hypot(double x, double y);
  118. double npy_acosh(double x);
  119. double npy_asinh(double xx);
  120. double npy_atanh(double x);
  121. double npy_rint(double x);
  122. double npy_trunc(double x);
  123. double npy_exp2(double x);
  124. double npy_log2(double x);
  125. double npy_atan2(double x, double y);
  126. double npy_pow(double x, double y);
  127. double npy_modf(double x, double* y);
  128. double npy_frexp(double x, int* y);
  129. double npy_ldexp(double n, int y);
  130. double npy_copysign(double x, double y);
  131. double npy_nextafter(double x, double y);
  132. double npy_spacing(double x);
  133. /*
  134. * IEEE 754 fpu handling. Those are guaranteed to be macros
  135. */
  136. /* use builtins to avoid function calls in tight loops
  137. * only available if npy_config.h is available (= numpys own build) */
  138. #if HAVE___BUILTIN_ISNAN
  139. #define npy_isnan(x) __builtin_isnan(x)
  140. #else
  141. #ifndef NPY_HAVE_DECL_ISNAN
  142. #define npy_isnan(x) ((x) != (x))
  143. #else
  144. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  145. #define npy_isnan(x) _isnan((x))
  146. #else
  147. #define npy_isnan(x) isnan(x)
  148. #endif
  149. #endif
  150. #endif
  151. /* only available if npy_config.h is available (= numpys own build) */
  152. #if HAVE___BUILTIN_ISFINITE
  153. #define npy_isfinite(x) __builtin_isfinite(x)
  154. #else
  155. #ifndef NPY_HAVE_DECL_ISFINITE
  156. #ifdef _MSC_VER
  157. #define npy_isfinite(x) _finite((x))
  158. #else
  159. #define npy_isfinite(x) !npy_isnan((x) + (-x))
  160. #endif
  161. #else
  162. #define npy_isfinite(x) isfinite((x))
  163. #endif
  164. #endif
  165. /* only available if npy_config.h is available (= numpys own build) */
  166. #if HAVE___BUILTIN_ISINF
  167. #define npy_isinf(x) __builtin_isinf(x)
  168. #else
  169. #ifndef NPY_HAVE_DECL_ISINF
  170. #define npy_isinf(x) (!npy_isfinite(x) && !npy_isnan(x))
  171. #else
  172. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  173. #define npy_isinf(x) (!_finite((x)) && !_isnan((x)))
  174. #else
  175. #define npy_isinf(x) isinf((x))
  176. #endif
  177. #endif
  178. #endif
  179. #ifndef NPY_HAVE_DECL_SIGNBIT
  180. int _npy_signbit_f(float x);
  181. int _npy_signbit_d(double x);
  182. int _npy_signbit_ld(long double x);
  183. #define npy_signbit(x) \
  184. (sizeof (x) == sizeof (long double) ? _npy_signbit_ld (x) \
  185. : sizeof (x) == sizeof (double) ? _npy_signbit_d (x) \
  186. : _npy_signbit_f (x))
  187. #else
  188. #define npy_signbit(x) signbit((x))
  189. #endif
  190. /*
  191. * float C99 math functions
  192. */
  193. float npy_sinf(float x);
  194. float npy_cosf(float x);
  195. float npy_tanf(float x);
  196. float npy_sinhf(float x);
  197. float npy_coshf(float x);
  198. float npy_tanhf(float x);
  199. float npy_fabsf(float x);
  200. float npy_floorf(float x);
  201. float npy_ceilf(float x);
  202. float npy_rintf(float x);
  203. float npy_truncf(float x);
  204. float npy_sqrtf(float x);
  205. float npy_cbrtf(float x);
  206. float npy_log10f(float x);
  207. float npy_logf(float x);
  208. float npy_expf(float x);
  209. float npy_expm1f(float x);
  210. float npy_asinf(float x);
  211. float npy_acosf(float x);
  212. float npy_atanf(float x);
  213. float npy_asinhf(float x);
  214. float npy_acoshf(float x);
  215. float npy_atanhf(float x);
  216. float npy_log1pf(float x);
  217. float npy_exp2f(float x);
  218. float npy_log2f(float x);
  219. float npy_atan2f(float x, float y);
  220. float npy_hypotf(float x, float y);
  221. float npy_powf(float x, float y);
  222. float npy_fmodf(float x, float y);
  223. float npy_modff(float x, float* y);
  224. float npy_frexpf(float x, int* y);
  225. float npy_ldexpf(float x, int y);
  226. float npy_copysignf(float x, float y);
  227. float npy_nextafterf(float x, float y);
  228. float npy_spacingf(float x);
  229. /*
  230. * long double C99 math functions
  231. */
  232. npy_longdouble npy_sinl(npy_longdouble x);
  233. npy_longdouble npy_cosl(npy_longdouble x);
  234. npy_longdouble npy_tanl(npy_longdouble x);
  235. npy_longdouble npy_sinhl(npy_longdouble x);
  236. npy_longdouble npy_coshl(npy_longdouble x);
  237. npy_longdouble npy_tanhl(npy_longdouble x);
  238. npy_longdouble npy_fabsl(npy_longdouble x);
  239. npy_longdouble npy_floorl(npy_longdouble x);
  240. npy_longdouble npy_ceill(npy_longdouble x);
  241. npy_longdouble npy_rintl(npy_longdouble x);
  242. npy_longdouble npy_truncl(npy_longdouble x);
  243. npy_longdouble npy_sqrtl(npy_longdouble x);
  244. npy_longdouble npy_cbrtl(npy_longdouble x);
  245. npy_longdouble npy_log10l(npy_longdouble x);
  246. npy_longdouble npy_logl(npy_longdouble x);
  247. npy_longdouble npy_expl(npy_longdouble x);
  248. npy_longdouble npy_expm1l(npy_longdouble x);
  249. npy_longdouble npy_asinl(npy_longdouble x);
  250. npy_longdouble npy_acosl(npy_longdouble x);
  251. npy_longdouble npy_atanl(npy_longdouble x);
  252. npy_longdouble npy_asinhl(npy_longdouble x);
  253. npy_longdouble npy_acoshl(npy_longdouble x);
  254. npy_longdouble npy_atanhl(npy_longdouble x);
  255. npy_longdouble npy_log1pl(npy_longdouble x);
  256. npy_longdouble npy_exp2l(npy_longdouble x);
  257. npy_longdouble npy_log2l(npy_longdouble x);
  258. npy_longdouble npy_atan2l(npy_longdouble x, npy_longdouble y);
  259. npy_longdouble npy_hypotl(npy_longdouble x, npy_longdouble y);
  260. npy_longdouble npy_powl(npy_longdouble x, npy_longdouble y);
  261. npy_longdouble npy_fmodl(npy_longdouble x, npy_longdouble y);
  262. npy_longdouble npy_modfl(npy_longdouble x, npy_longdouble* y);
  263. npy_longdouble npy_frexpl(npy_longdouble x, int* y);
  264. npy_longdouble npy_ldexpl(npy_longdouble x, int y);
  265. npy_longdouble npy_copysignl(npy_longdouble x, npy_longdouble y);
  266. npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y);
  267. npy_longdouble npy_spacingl(npy_longdouble x);
  268. /*
  269. * Non standard functions
  270. */
  271. double npy_deg2rad(double x);
  272. double npy_rad2deg(double x);
  273. double npy_logaddexp(double x, double y);
  274. double npy_logaddexp2(double x, double y);
  275. double npy_divmod(double x, double y, double *modulus);
  276. float npy_deg2radf(float x);
  277. float npy_rad2degf(float x);
  278. float npy_logaddexpf(float x, float y);
  279. float npy_logaddexp2f(float x, float y);
  280. float npy_divmodf(float x, float y, float *modulus);
  281. npy_longdouble npy_deg2radl(npy_longdouble x);
  282. npy_longdouble npy_rad2degl(npy_longdouble x);
  283. npy_longdouble npy_logaddexpl(npy_longdouble x, npy_longdouble y);
  284. npy_longdouble npy_logaddexp2l(npy_longdouble x, npy_longdouble y);
  285. npy_longdouble npy_divmodl(npy_longdouble x, npy_longdouble y,
  286. npy_longdouble *modulus);
  287. #define npy_degrees npy_rad2deg
  288. #define npy_degreesf npy_rad2degf
  289. #define npy_degreesl npy_rad2degl
  290. #define npy_radians npy_deg2rad
  291. #define npy_radiansf npy_deg2radf
  292. #define npy_radiansl npy_deg2radl
  293. /*
  294. * Complex declarations
  295. */
  296. /*
  297. * C99 specifies that complex numbers have the same representation as
  298. * an array of two elements, where the first element is the real part
  299. * and the second element is the imaginary part.
  300. */
  301. #define __NPY_CPACK_IMP(x, y, type, ctype) \
  302. union { \
  303. ctype z; \
  304. type a[2]; \
  305. } z1;; \
  306. \
  307. z1.a[0] = (x); \
  308. z1.a[1] = (y); \
  309. \
  310. return z1.z;
  311. static NPY_INLINE npy_cdouble npy_cpack(double x, double y)
  312. {
  313. __NPY_CPACK_IMP(x, y, double, npy_cdouble);
  314. }
  315. static NPY_INLINE npy_cfloat npy_cpackf(float x, float y)
  316. {
  317. __NPY_CPACK_IMP(x, y, float, npy_cfloat);
  318. }
  319. static NPY_INLINE npy_clongdouble npy_cpackl(npy_longdouble x, npy_longdouble y)
  320. {
  321. __NPY_CPACK_IMP(x, y, npy_longdouble, npy_clongdouble);
  322. }
  323. #undef __NPY_CPACK_IMP
  324. /*
  325. * Same remark as above, but in the other direction: extract first/second
  326. * member of complex number, assuming a C99-compatible representation
  327. *
  328. * Those are defineds as static inline, and such as a reasonable compiler would
  329. * most likely compile this to one or two instructions (on CISC at least)
  330. */
  331. #define __NPY_CEXTRACT_IMP(z, index, type, ctype) \
  332. union { \
  333. ctype z; \
  334. type a[2]; \
  335. } __z_repr; \
  336. __z_repr.z = z; \
  337. \
  338. return __z_repr.a[index];
  339. static NPY_INLINE double npy_creal(npy_cdouble z)
  340. {
  341. __NPY_CEXTRACT_IMP(z, 0, double, npy_cdouble);
  342. }
  343. static NPY_INLINE double npy_cimag(npy_cdouble z)
  344. {
  345. __NPY_CEXTRACT_IMP(z, 1, double, npy_cdouble);
  346. }
  347. static NPY_INLINE float npy_crealf(npy_cfloat z)
  348. {
  349. __NPY_CEXTRACT_IMP(z, 0, float, npy_cfloat);
  350. }
  351. static NPY_INLINE float npy_cimagf(npy_cfloat z)
  352. {
  353. __NPY_CEXTRACT_IMP(z, 1, float, npy_cfloat);
  354. }
  355. static NPY_INLINE npy_longdouble npy_creall(npy_clongdouble z)
  356. {
  357. __NPY_CEXTRACT_IMP(z, 0, npy_longdouble, npy_clongdouble);
  358. }
  359. static NPY_INLINE npy_longdouble npy_cimagl(npy_clongdouble z)
  360. {
  361. __NPY_CEXTRACT_IMP(z, 1, npy_longdouble, npy_clongdouble);
  362. }
  363. #undef __NPY_CEXTRACT_IMP
  364. /*
  365. * Double precision complex functions
  366. */
  367. double npy_cabs(npy_cdouble z);
  368. double npy_carg(npy_cdouble z);
  369. npy_cdouble npy_cexp(npy_cdouble z);
  370. npy_cdouble npy_clog(npy_cdouble z);
  371. npy_cdouble npy_cpow(npy_cdouble x, npy_cdouble y);
  372. npy_cdouble npy_csqrt(npy_cdouble z);
  373. npy_cdouble npy_ccos(npy_cdouble z);
  374. npy_cdouble npy_csin(npy_cdouble z);
  375. npy_cdouble npy_ctan(npy_cdouble z);
  376. npy_cdouble npy_ccosh(npy_cdouble z);
  377. npy_cdouble npy_csinh(npy_cdouble z);
  378. npy_cdouble npy_ctanh(npy_cdouble z);
  379. npy_cdouble npy_cacos(npy_cdouble z);
  380. npy_cdouble npy_casin(npy_cdouble z);
  381. npy_cdouble npy_catan(npy_cdouble z);
  382. npy_cdouble npy_cacosh(npy_cdouble z);
  383. npy_cdouble npy_casinh(npy_cdouble z);
  384. npy_cdouble npy_catanh(npy_cdouble z);
  385. /*
  386. * Single precision complex functions
  387. */
  388. float npy_cabsf(npy_cfloat z);
  389. float npy_cargf(npy_cfloat z);
  390. npy_cfloat npy_cexpf(npy_cfloat z);
  391. npy_cfloat npy_clogf(npy_cfloat z);
  392. npy_cfloat npy_cpowf(npy_cfloat x, npy_cfloat y);
  393. npy_cfloat npy_csqrtf(npy_cfloat z);
  394. npy_cfloat npy_ccosf(npy_cfloat z);
  395. npy_cfloat npy_csinf(npy_cfloat z);
  396. npy_cfloat npy_ctanf(npy_cfloat z);
  397. npy_cfloat npy_ccoshf(npy_cfloat z);
  398. npy_cfloat npy_csinhf(npy_cfloat z);
  399. npy_cfloat npy_ctanhf(npy_cfloat z);
  400. npy_cfloat npy_cacosf(npy_cfloat z);
  401. npy_cfloat npy_casinf(npy_cfloat z);
  402. npy_cfloat npy_catanf(npy_cfloat z);
  403. npy_cfloat npy_cacoshf(npy_cfloat z);
  404. npy_cfloat npy_casinhf(npy_cfloat z);
  405. npy_cfloat npy_catanhf(npy_cfloat z);
  406. /*
  407. * Extended precision complex functions
  408. */
  409. npy_longdouble npy_cabsl(npy_clongdouble z);
  410. npy_longdouble npy_cargl(npy_clongdouble z);
  411. npy_clongdouble npy_cexpl(npy_clongdouble z);
  412. npy_clongdouble npy_clogl(npy_clongdouble z);
  413. npy_clongdouble npy_cpowl(npy_clongdouble x, npy_clongdouble y);
  414. npy_clongdouble npy_csqrtl(npy_clongdouble z);
  415. npy_clongdouble npy_ccosl(npy_clongdouble z);
  416. npy_clongdouble npy_csinl(npy_clongdouble z);
  417. npy_clongdouble npy_ctanl(npy_clongdouble z);
  418. npy_clongdouble npy_ccoshl(npy_clongdouble z);
  419. npy_clongdouble npy_csinhl(npy_clongdouble z);
  420. npy_clongdouble npy_ctanhl(npy_clongdouble z);
  421. npy_clongdouble npy_cacosl(npy_clongdouble z);
  422. npy_clongdouble npy_casinl(npy_clongdouble z);
  423. npy_clongdouble npy_catanl(npy_clongdouble z);
  424. npy_clongdouble npy_cacoshl(npy_clongdouble z);
  425. npy_clongdouble npy_casinhl(npy_clongdouble z);
  426. npy_clongdouble npy_catanhl(npy_clongdouble z);
  427. /*
  428. * Functions that set the floating point error
  429. * status word.
  430. */
  431. /*
  432. * platform-dependent code translates floating point
  433. * status to an integer sum of these values
  434. */
  435. #define NPY_FPE_DIVIDEBYZERO 1
  436. #define NPY_FPE_OVERFLOW 2
  437. #define NPY_FPE_UNDERFLOW 4
  438. #define NPY_FPE_INVALID 8
  439. int npy_get_floatstatus(void);
  440. int npy_clear_floatstatus(void);
  441. void npy_set_floatstatus_divbyzero(void);
  442. void npy_set_floatstatus_overflow(void);
  443. void npy_set_floatstatus_underflow(void);
  444. void npy_set_floatstatus_invalid(void);
  445. #ifdef __cplusplus
  446. }
  447. #endif
  448. #endif