PageRenderTime 43ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/mpfr/src/digamma.c

https://gitlab.com/HowTheStoryEnds/freebsd11-psm-port
C | 378 lines | 269 code | 30 blank | 79 comment | 57 complexity | faf7c3dce86bae663a5f9c59b9698c9f MD5 | raw file
  1. /* mpfr_digamma -- digamma function of a floating-point number
  2. Copyright 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  3. Contributed by the AriC and Caramel projects, INRIA.
  4. This file is part of the GNU MPFR Library.
  5. The GNU MPFR Library is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or (at your
  8. option) any later version.
  9. The GNU MPFR Library is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
  15. http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
  16. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #include "mpfr-impl.h"
  18. /* Put in s an approximation of digamma(x).
  19. Assumes x >= 2.
  20. Assumes s does not overlap with x.
  21. Returns an integer e such that the error is bounded by 2^e ulps
  22. of the result s.
  23. */
  24. static mpfr_exp_t
  25. mpfr_digamma_approx (mpfr_ptr s, mpfr_srcptr x)
  26. {
  27. mpfr_prec_t p = MPFR_PREC (s);
  28. mpfr_t t, u, invxx;
  29. mpfr_exp_t e, exps, f, expu;
  30. mpz_t *INITIALIZED(B); /* variable B declared as initialized */
  31. unsigned long n0, n; /* number of allocated B[] */
  32. MPFR_ASSERTN(MPFR_IS_POS(x) && (MPFR_EXP(x) >= 2));
  33. mpfr_init2 (t, p);
  34. mpfr_init2 (u, p);
  35. mpfr_init2 (invxx, p);
  36. mpfr_log (s, x, MPFR_RNDN); /* error <= 1/2 ulp */
  37. mpfr_ui_div (t, 1, x, MPFR_RNDN); /* error <= 1/2 ulp */
  38. mpfr_div_2exp (t, t, 1, MPFR_RNDN); /* exact */
  39. mpfr_sub (s, s, t, MPFR_RNDN);
  40. /* error <= 1/2 + 1/2*2^(EXP(olds)-EXP(s)) + 1/2*2^(EXP(t)-EXP(s)).
  41. For x >= 2, log(x) >= 2*(1/(2x)), thus olds >= 2t, and olds - t >= olds/2,
  42. thus 0 <= EXP(olds)-EXP(s) <= 1, and EXP(t)-EXP(s) <= 0, thus
  43. error <= 1/2 + 1/2*2 + 1/2 <= 2 ulps. */
  44. e = 2; /* initial error */
  45. mpfr_mul (invxx, x, x, MPFR_RNDZ); /* invxx = x^2 * (1 + theta)
  46. for |theta| <= 2^(-p) */
  47. mpfr_ui_div (invxx, 1, invxx, MPFR_RNDU); /* invxx = 1/x^2 * (1 + theta)^2 */
  48. /* in the following we note err=xxx when the ratio between the approximation
  49. and the exact result can be written (1 + theta)^xxx for |theta| <= 2^(-p),
  50. following Higham's method */
  51. B = mpfr_bernoulli_internal ((mpz_t *) 0, 0);
  52. mpfr_set_ui (t, 1, MPFR_RNDN); /* err = 0 */
  53. for (n = 1;; n++)
  54. {
  55. /* compute next Bernoulli number */
  56. B = mpfr_bernoulli_internal (B, n);
  57. /* The main term is Bernoulli[2n]/(2n)/x^(2n) = B[n]/(2n+1)!(2n)/x^(2n)
  58. = B[n]*t[n]/(2n) where t[n]/t[n-1] = 1/(2n)/(2n+1)/x^2. */
  59. mpfr_mul (t, t, invxx, MPFR_RNDU); /* err = err + 3 */
  60. mpfr_div_ui (t, t, 2 * n, MPFR_RNDU); /* err = err + 1 */
  61. mpfr_div_ui (t, t, 2 * n + 1, MPFR_RNDU); /* err = err + 1 */
  62. /* we thus have err = 5n here */
  63. mpfr_div_ui (u, t, 2 * n, MPFR_RNDU); /* err = 5n+1 */
  64. mpfr_mul_z (u, u, B[n], MPFR_RNDU); /* err = 5n+2, and the
  65. absolute error is bounded
  66. by 10n+4 ulp(u) [Rule 11] */
  67. /* if the terms 'u' are decreasing by a factor two at least,
  68. then the error coming from those is bounded by
  69. sum((10n+4)/2^n, n=1..infinity) = 24 */
  70. exps = mpfr_get_exp (s);
  71. expu = mpfr_get_exp (u);
  72. if (expu < exps - (mpfr_exp_t) p)
  73. break;
  74. mpfr_sub (s, s, u, MPFR_RNDN); /* error <= 24 + n/2 */
  75. if (mpfr_get_exp (s) < exps)
  76. e <<= exps - mpfr_get_exp (s);
  77. e ++; /* error in mpfr_sub */
  78. f = 10 * n + 4;
  79. while (expu < exps)
  80. {
  81. f = (1 + f) / 2;
  82. expu ++;
  83. }
  84. e += f; /* total rouding error coming from 'u' term */
  85. }
  86. n0 = ++n;
  87. while (n--)
  88. mpz_clear (B[n]);
  89. (*__gmp_free_func) (B, n0 * sizeof (mpz_t));
  90. mpfr_clear (t);
  91. mpfr_clear (u);
  92. mpfr_clear (invxx);
  93. f = 0;
  94. while (e > 1)
  95. {
  96. f++;
  97. e = (e + 1) / 2;
  98. /* Invariant: 2^f * e does not decrease */
  99. }
  100. return f;
  101. }
  102. /* Use the reflection formula Digamma(1-x) = Digamma(x) + Pi * cot(Pi*x),
  103. i.e., Digamma(x) = Digamma(1-x) - Pi * cot(Pi*x).
  104. Assume x < 1/2. */
  105. static int
  106. mpfr_digamma_reflection (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
  107. {
  108. mpfr_prec_t p = MPFR_PREC(y) + 10, q;
  109. mpfr_t t, u, v;
  110. mpfr_exp_t e1, expv;
  111. int inex;
  112. MPFR_ZIV_DECL (loop);
  113. /* we want that 1-x is exact with precision q: if 0 < x < 1/2, then
  114. q = PREC(x)-EXP(x) is ok, otherwise if -1 <= x < 0, q = PREC(x)-EXP(x)
  115. is ok, otherwise for x < -1, PREC(x) is ok if EXP(x) <= PREC(x),
  116. otherwise we need EXP(x) */
  117. if (MPFR_EXP(x) < 0)
  118. q = MPFR_PREC(x) + 1 - MPFR_EXP(x);
  119. else if (MPFR_EXP(x) <= MPFR_PREC(x))
  120. q = MPFR_PREC(x) + 1;
  121. else
  122. q = MPFR_EXP(x);
  123. mpfr_init2 (u, q);
  124. MPFR_ASSERTN(mpfr_ui_sub (u, 1, x, MPFR_RNDN) == 0);
  125. /* if x is half an integer, cot(Pi*x) = 0, thus Digamma(x) = Digamma(1-x) */
  126. mpfr_mul_2exp (u, u, 1, MPFR_RNDN);
  127. inex = mpfr_integer_p (u);
  128. mpfr_div_2exp (u, u, 1, MPFR_RNDN);
  129. if (inex)
  130. {
  131. inex = mpfr_digamma (y, u, rnd_mode);
  132. goto end;
  133. }
  134. mpfr_init2 (t, p);
  135. mpfr_init2 (v, p);
  136. MPFR_ZIV_INIT (loop, p);
  137. for (;;)
  138. {
  139. mpfr_const_pi (v, MPFR_RNDN); /* v = Pi*(1+theta) for |theta|<=2^(-p) */
  140. mpfr_mul (t, v, x, MPFR_RNDN); /* (1+theta)^2 */
  141. e1 = MPFR_EXP(t) - (mpfr_exp_t) p + 1; /* bound for t: err(t) <= 2^e1 */
  142. mpfr_cot (t, t, MPFR_RNDN);
  143. /* cot(t * (1+h)) = cot(t) - theta * (1 + cot(t)^2) with |theta|<=t*h */
  144. if (MPFR_EXP(t) > 0)
  145. e1 = e1 + 2 * MPFR_EXP(t) + 1;
  146. else
  147. e1 = e1 + 1;
  148. /* now theta * (1 + cot(t)^2) <= 2^e1 */
  149. e1 += (mpfr_exp_t) p - MPFR_EXP(t); /* error is now 2^e1 ulps */
  150. mpfr_mul (t, t, v, MPFR_RNDN);
  151. e1 ++;
  152. mpfr_digamma (v, u, MPFR_RNDN); /* error <= 1/2 ulp */
  153. expv = MPFR_EXP(v);
  154. mpfr_sub (v, v, t, MPFR_RNDN);
  155. if (MPFR_EXP(v) < MPFR_EXP(t))
  156. e1 += MPFR_EXP(t) - MPFR_EXP(v); /* scale error for t wrt new v */
  157. /* now take into account the 1/2 ulp error for v */
  158. if (expv - MPFR_EXP(v) - 1 > e1)
  159. e1 = expv - MPFR_EXP(v) - 1;
  160. else
  161. e1 ++;
  162. e1 ++; /* rounding error for mpfr_sub */
  163. if (MPFR_CAN_ROUND (v, p - e1, MPFR_PREC(y), rnd_mode))
  164. break;
  165. MPFR_ZIV_NEXT (loop, p);
  166. mpfr_set_prec (t, p);
  167. mpfr_set_prec (v, p);
  168. }
  169. MPFR_ZIV_FREE (loop);
  170. inex = mpfr_set (y, v, rnd_mode);
  171. mpfr_clear (t);
  172. mpfr_clear (v);
  173. end:
  174. mpfr_clear (u);
  175. return inex;
  176. }
  177. /* we have x >= 1/2 here */
  178. static int
  179. mpfr_digamma_positive (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
  180. {
  181. mpfr_prec_t p = MPFR_PREC(y) + 10, q;
  182. mpfr_t t, u, x_plus_j;
  183. int inex;
  184. mpfr_exp_t errt, erru, expt;
  185. unsigned long j = 0, min;
  186. MPFR_ZIV_DECL (loop);
  187. /* compute a precision q such that x+1 is exact */
  188. if (MPFR_PREC(x) < MPFR_EXP(x))
  189. q = MPFR_EXP(x);
  190. else
  191. q = MPFR_PREC(x) + 1;
  192. mpfr_init2 (x_plus_j, q);
  193. mpfr_init2 (t, p);
  194. mpfr_init2 (u, p);
  195. MPFR_ZIV_INIT (loop, p);
  196. for(;;)
  197. {
  198. /* Lower bound for x+j in mpfr_digamma_approx call: since the smallest
  199. term of the divergent series for Digamma(x) is about exp(-2*Pi*x), and
  200. we want it to be less than 2^(-p), this gives x > p*log(2)/(2*Pi)
  201. i.e., x >= 0.1103 p.
  202. To be safe, we ensure x >= 0.25 * p.
  203. */
  204. min = (p + 3) / 4;
  205. if (min < 2)
  206. min = 2;
  207. mpfr_set (x_plus_j, x, MPFR_RNDN);
  208. mpfr_set_ui (u, 0, MPFR_RNDN);
  209. j = 0;
  210. while (mpfr_cmp_ui (x_plus_j, min) < 0)
  211. {
  212. j ++;
  213. mpfr_ui_div (t, 1, x_plus_j, MPFR_RNDN); /* err <= 1/2 ulp */
  214. mpfr_add (u, u, t, MPFR_RNDN);
  215. inex = mpfr_add_ui (x_plus_j, x_plus_j, 1, MPFR_RNDZ);
  216. if (inex != 0) /* we lost one bit */
  217. {
  218. q ++;
  219. mpfr_prec_round (x_plus_j, q, MPFR_RNDZ);
  220. mpfr_nextabove (x_plus_j);
  221. }
  222. /* since all terms are positive, the error is bounded by j ulps */
  223. }
  224. for (erru = 0; j > 1; erru++, j = (j + 1) / 2);
  225. errt = mpfr_digamma_approx (t, x_plus_j);
  226. expt = MPFR_EXP(t);
  227. mpfr_sub (t, t, u, MPFR_RNDN);
  228. if (MPFR_EXP(t) < expt)
  229. errt += expt - MPFR_EXP(t);
  230. if (MPFR_EXP(t) < MPFR_EXP(u))
  231. erru += MPFR_EXP(u) - MPFR_EXP(t);
  232. if (errt > erru)
  233. errt = errt + 1;
  234. else if (errt == erru)
  235. errt = errt + 2;
  236. else
  237. errt = erru + 1;
  238. if (MPFR_CAN_ROUND (t, p - errt, MPFR_PREC(y), rnd_mode))
  239. break;
  240. MPFR_ZIV_NEXT (loop, p);
  241. mpfr_set_prec (t, p);
  242. mpfr_set_prec (u, p);
  243. }
  244. MPFR_ZIV_FREE (loop);
  245. inex = mpfr_set (y, t, rnd_mode);
  246. mpfr_clear (t);
  247. mpfr_clear (u);
  248. mpfr_clear (x_plus_j);
  249. return inex;
  250. }
  251. int
  252. mpfr_digamma (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
  253. {
  254. int inex;
  255. MPFR_SAVE_EXPO_DECL (expo);
  256. MPFR_LOG_FUNC
  257. (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode),
  258. ("y[%Pu]=%.*Rg inexact=%d", mpfr_get_prec(y), mpfr_log_prec, y, inex));
  259. if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
  260. {
  261. if (MPFR_IS_NAN(x))
  262. {
  263. MPFR_SET_NAN(y);
  264. MPFR_RET_NAN;
  265. }
  266. else if (MPFR_IS_INF(x))
  267. {
  268. if (MPFR_IS_POS(x)) /* Digamma(+Inf) = +Inf */
  269. {
  270. MPFR_SET_SAME_SIGN(y, x);
  271. MPFR_SET_INF(y);
  272. MPFR_RET(0);
  273. }
  274. else /* Digamma(-Inf) = NaN */
  275. {
  276. MPFR_SET_NAN(y);
  277. MPFR_RET_NAN;
  278. }
  279. }
  280. else /* Zero case */
  281. {
  282. /* the following works also in case of overlap */
  283. MPFR_SET_INF(y);
  284. MPFR_SET_OPPOSITE_SIGN(y, x);
  285. mpfr_set_divby0 ();
  286. MPFR_RET(0);
  287. }
  288. }
  289. /* Digamma is undefined for negative integers */
  290. if (MPFR_IS_NEG(x) && mpfr_integer_p (x))
  291. {
  292. MPFR_SET_NAN(y);
  293. MPFR_RET_NAN;
  294. }
  295. /* now x is a normal number */
  296. MPFR_SAVE_EXPO_MARK (expo);
  297. /* for x very small, we have Digamma(x) = -1/x - gamma + O(x), more precisely
  298. -1 < Digamma(x) + 1/x < 0 for -0.2 < x < 0.2, thus:
  299. (i) either x is a power of two, then 1/x is exactly representable, and
  300. as long as 1/2*ulp(1/x) > 1, we can conclude;
  301. (ii) otherwise assume x has <= n bits, and y has <= n+1 bits, then
  302. |y + 1/x| >= 2^(-2n) ufp(y), where ufp means unit in first place.
  303. Since |Digamma(x) + 1/x| <= 1, if 2^(-2n) ufp(y) >= 2, then
  304. |y - Digamma(x)| >= 2^(-2n-1)ufp(y), and rounding -1/x gives the correct result.
  305. If x < 2^E, then y > 2^(-E), thus ufp(y) > 2^(-E-1).
  306. A sufficient condition is thus EXP(x) <= -2 MAX(PREC(x),PREC(Y)). */
  307. if (MPFR_EXP(x) < -2)
  308. {
  309. if (MPFR_EXP(x) <= -2 * (mpfr_exp_t) MAX(MPFR_PREC(x), MPFR_PREC(y)))
  310. {
  311. int signx = MPFR_SIGN(x);
  312. inex = mpfr_si_div (y, -1, x, rnd_mode);
  313. if (inex == 0) /* x is a power of two */
  314. { /* result always -1/x, except when rounding down */
  315. if (rnd_mode == MPFR_RNDA)
  316. rnd_mode = (signx > 0) ? MPFR_RNDD : MPFR_RNDU;
  317. if (rnd_mode == MPFR_RNDZ)
  318. rnd_mode = (signx > 0) ? MPFR_RNDU : MPFR_RNDD;
  319. if (rnd_mode == MPFR_RNDU)
  320. inex = 1;
  321. else if (rnd_mode == MPFR_RNDD)
  322. {
  323. mpfr_nextbelow (y);
  324. inex = -1;
  325. }
  326. else /* nearest */
  327. inex = 1;
  328. }
  329. MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
  330. goto end;
  331. }
  332. }
  333. if (MPFR_IS_NEG(x))
  334. inex = mpfr_digamma_reflection (y, x, rnd_mode);
  335. /* if x < 1/2 we use the reflection formula */
  336. else if (MPFR_EXP(x) < 0)
  337. inex = mpfr_digamma_reflection (y, x, rnd_mode);
  338. else
  339. inex = mpfr_digamma_positive (y, x, rnd_mode);
  340. end:
  341. MPFR_SAVE_EXPO_FREE (expo);
  342. return mpfr_check_range (y, inex, rnd_mode);
  343. }