gsl_math.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* gsl_math.h
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2007 Gerard Jungman, Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #ifndef __GSL_MATH_H__
  20. #define __GSL_MATH_H__
  21. #include <math.h>
  22. #include "gsl_sys.h"
  23. #include "gsl_machine.h"
  24. #include "gsl_precision.h"
  25. #include "gsl_nan.h"
  26. #include "gsl_pow_int.h"
  27. #ifndef M_E
  28. #define M_E 2.71828182845904523536028747135 /* e */
  29. #endif
  30. #ifndef M_LOG2E
  31. #define M_LOG2E 1.44269504088896340735992468100 /* log_2 (e) */
  32. #endif
  33. #ifndef M_LOG10E
  34. #define M_LOG10E 0.43429448190325182765112891892 /* log_10 (e) */
  35. #endif
  36. #ifndef M_SQRT2
  37. #define M_SQRT2 1.41421356237309504880168872421 /* sqrt(2) */
  38. #endif
  39. #ifndef M_SQRT1_2
  40. #define M_SQRT1_2 0.70710678118654752440084436210 /* sqrt(1/2) */
  41. #endif
  42. #ifndef M_SQRT3
  43. #define M_SQRT3 1.73205080756887729352744634151 /* sqrt(3) */
  44. #endif
  45. #ifndef M_PI
  46. #define M_PI 3.14159265358979323846264338328 /* pi */
  47. #endif
  48. #ifndef M_PI_2
  49. #define M_PI_2 1.57079632679489661923132169164 /* pi/2 */
  50. #endif
  51. #ifndef M_PI_4
  52. #define M_PI_4 0.78539816339744830961566084582 /* pi/4 */
  53. #endif
  54. #ifndef M_SQRTPI
  55. #define M_SQRTPI 1.77245385090551602729816748334 /* sqrt(pi) */
  56. #endif
  57. #ifndef M_2_SQRTPI
  58. #define M_2_SQRTPI 1.12837916709551257389615890312 /* 2/sqrt(pi) */
  59. #endif
  60. #ifndef M_1_PI
  61. #define M_1_PI 0.31830988618379067153776752675 /* 1/pi */
  62. #endif
  63. #ifndef M_2_PI
  64. #define M_2_PI 0.63661977236758134307553505349 /* 2/pi */
  65. #endif
  66. #ifndef M_LN10
  67. #define M_LN10 2.30258509299404568401799145468 /* ln(10) */
  68. #endif
  69. #ifndef M_LN2
  70. #define M_LN2 0.69314718055994530941723212146 /* ln(2) */
  71. #endif
  72. #ifndef M_LNPI
  73. #define M_LNPI 1.14472988584940017414342735135 /* ln(pi) */
  74. #endif
  75. #ifndef M_EULER
  76. #define M_EULER 0.57721566490153286060651209008 /* Euler constant */
  77. #endif
  78. #undef __BEGIN_DECLS
  79. #undef __END_DECLS
  80. #ifdef __cplusplus
  81. # define __BEGIN_DECLS extern "C" {
  82. # define __END_DECLS }
  83. #else
  84. # define __BEGIN_DECLS /* empty */
  85. # define __END_DECLS /* empty */
  86. #endif
  87. __BEGIN_DECLS
  88. /* other needlessly compulsive abstractions */
  89. #define GSL_IS_ODD(n) ((n) & 1)
  90. #define GSL_IS_EVEN(n) (!(GSL_IS_ODD(n)))
  91. #define GSL_SIGN(x) ((x) >= 0.0 ? 1 : -1)
  92. /* Return nonzero if x is a real number, i.e. non NaN or infinite. */
  93. #define GSL_IS_REAL(x) (gsl_finite(x))
  94. /* Define MAX and MIN macros/functions if they don't exist. */
  95. /* plain old macros for general use */
  96. #define GSL_MAX(a,b) ((a) > (b) ? (a) : (b))
  97. #define GSL_MIN(a,b) ((a) < (b) ? (a) : (b))
  98. /* function versions of the above, in case they are needed */
  99. double gsl_max (double a, double b);
  100. double gsl_min (double a, double b);
  101. /* inline-friendly strongly typed versions */
  102. #ifdef HAVE_INLINE
  103. extern inline int GSL_MAX_INT (int a, int b);
  104. extern inline int GSL_MIN_INT (int a, int b);
  105. extern inline double GSL_MAX_DBL (double a, double b);
  106. extern inline double GSL_MIN_DBL (double a, double b);
  107. extern inline long double GSL_MAX_LDBL (long double a, long double b);
  108. extern inline long double GSL_MIN_LDBL (long double a, long double b);
  109. extern inline int
  110. GSL_MAX_INT (int a, int b)
  111. {
  112. return GSL_MAX (a, b);
  113. }
  114. extern inline int
  115. GSL_MIN_INT (int a, int b)
  116. {
  117. return GSL_MIN (a, b);
  118. }
  119. extern inline double
  120. GSL_MAX_DBL (double a, double b)
  121. {
  122. return GSL_MAX (a, b);
  123. }
  124. extern inline double
  125. GSL_MIN_DBL (double a, double b)
  126. {
  127. return GSL_MIN (a, b);
  128. }
  129. extern inline long double
  130. GSL_MAX_LDBL (long double a, long double b)
  131. {
  132. return GSL_MAX (a, b);
  133. }
  134. extern inline long double
  135. GSL_MIN_LDBL (long double a, long double b)
  136. {
  137. return GSL_MIN (a, b);
  138. }
  139. #else
  140. #define GSL_MAX_INT(a,b) GSL_MAX(a,b)
  141. #define GSL_MIN_INT(a,b) GSL_MIN(a,b)
  142. #define GSL_MAX_DBL(a,b) GSL_MAX(a,b)
  143. #define GSL_MIN_DBL(a,b) GSL_MIN(a,b)
  144. #define GSL_MAX_LDBL(a,b) GSL_MAX(a,b)
  145. #define GSL_MIN_LDBL(a,b) GSL_MIN(a,b)
  146. #endif /* HAVE_INLINE */
  147. /* Definition of an arbitrary function with parameters */
  148. struct gsl_function_struct
  149. {
  150. double (* function) (double x, void * params);
  151. void * params;
  152. };
  153. typedef struct gsl_function_struct gsl_function ;
  154. #define GSL_FN_EVAL(F,x) (*((F)->function))(x,(F)->params)
  155. /* Definition of an arbitrary function returning two values, r1, r2 */
  156. struct gsl_function_fdf_struct
  157. {
  158. double (* f) (double x, void * params);
  159. double (* df) (double x, void * params);
  160. void (* fdf) (double x, void * params, double * f, double * df);
  161. void * params;
  162. };
  163. typedef struct gsl_function_fdf_struct gsl_function_fdf ;
  164. #define GSL_FN_FDF_EVAL_F(FDF,x) (*((FDF)->f))(x,(FDF)->params)
  165. #define GSL_FN_FDF_EVAL_DF(FDF,x) (*((FDF)->df))(x,(FDF)->params)
  166. #define GSL_FN_FDF_EVAL_F_DF(FDF,x,y,dy) (*((FDF)->fdf))(x,(FDF)->params,(y),(dy))
  167. /* Definition of an arbitrary vector-valued function with parameters */
  168. struct gsl_function_vec_struct
  169. {
  170. int (* function) (double x, double y[], void * params);
  171. void * params;
  172. };
  173. typedef struct gsl_function_vec_struct gsl_function_vec ;
  174. #define GSL_FN_VEC_EVAL(F,x,y) (*((F)->function))(x,y,(F)->params)
  175. __END_DECLS
  176. #endif /* __GSL_MATH_H__ */