14a238c70SJohn Marino /* mpfr_jn_asympt, mpfr_yn_asympt -- shared code for mpfr_jn and mpfr_yn
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino
234a238c70SJohn Marino #ifdef MPFR_JN
244a238c70SJohn Marino # define FUNCTION mpfr_jn_asympt
254a238c70SJohn Marino #else
264a238c70SJohn Marino # ifdef MPFR_YN
274a238c70SJohn Marino # define FUNCTION mpfr_yn_asympt
284a238c70SJohn Marino # else
294a238c70SJohn Marino # error "neither MPFR_JN nor MPFR_YN is defined"
304a238c70SJohn Marino # endif
314a238c70SJohn Marino #endif
324a238c70SJohn Marino
334a238c70SJohn Marino /* Implements asymptotic expansion for jn or yn (formulae 9.2.5 and 9.2.6
344a238c70SJohn Marino from Abramowitz & Stegun).
354a238c70SJohn Marino Assumes |z| > p log(2)/2, where p is the target precision
364a238c70SJohn Marino (z can be negative only for jn).
374a238c70SJohn Marino Return 0 if the expansion does not converge enough (the value 0 as inexact
384a238c70SJohn Marino flag should not happen for normal input).
394a238c70SJohn Marino */
404a238c70SJohn Marino static int
FUNCTION(mpfr_ptr res,long n,mpfr_srcptr z,mpfr_rnd_t r)414a238c70SJohn Marino FUNCTION (mpfr_ptr res, long n, mpfr_srcptr z, mpfr_rnd_t r)
424a238c70SJohn Marino {
434a238c70SJohn Marino mpfr_t s, c, P, Q, t, iz, err_t, err_s, err_u;
444a238c70SJohn Marino mpfr_prec_t w;
454a238c70SJohn Marino long k;
464a238c70SJohn Marino int inex, stop, diverge = 0;
474a238c70SJohn Marino mpfr_exp_t err2, err;
484a238c70SJohn Marino MPFR_ZIV_DECL (loop);
494a238c70SJohn Marino
504a238c70SJohn Marino mpfr_init (c);
514a238c70SJohn Marino
524a238c70SJohn Marino w = MPFR_PREC(res) + MPFR_INT_CEIL_LOG2(MPFR_PREC(res)) + 4;
534a238c70SJohn Marino
544a238c70SJohn Marino MPFR_ZIV_INIT (loop, w);
554a238c70SJohn Marino for (;;)
564a238c70SJohn Marino {
574a238c70SJohn Marino mpfr_set_prec (c, w);
584a238c70SJohn Marino mpfr_init2 (s, w);
594a238c70SJohn Marino mpfr_init2 (P, w);
604a238c70SJohn Marino mpfr_init2 (Q, w);
614a238c70SJohn Marino mpfr_init2 (t, w);
624a238c70SJohn Marino mpfr_init2 (iz, w);
634a238c70SJohn Marino mpfr_init2 (err_t, 31);
644a238c70SJohn Marino mpfr_init2 (err_s, 31);
654a238c70SJohn Marino mpfr_init2 (err_u, 31);
664a238c70SJohn Marino
674a238c70SJohn Marino /* Approximate sin(z) and cos(z). In the following, err <= k means that
684a238c70SJohn Marino the approximate value y and the true value x are related by
694a238c70SJohn Marino y = x * (1 + u)^k with |u| <= 2^(-w), following Higham's method. */
704a238c70SJohn Marino mpfr_sin_cos (s, c, z, MPFR_RNDN);
714a238c70SJohn Marino if (MPFR_IS_NEG(z))
724a238c70SJohn Marino mpfr_neg (s, s, MPFR_RNDN); /* compute jn/yn(|z|), fix sign later */
734a238c70SJohn Marino /* The absolute error on s/c is bounded by 1/2 ulp(1/2) <= 2^(-w-1). */
744a238c70SJohn Marino mpfr_add (t, s, c, MPFR_RNDN);
754a238c70SJohn Marino mpfr_sub (c, s, c, MPFR_RNDN);
764a238c70SJohn Marino mpfr_swap (s, t);
774a238c70SJohn Marino /* now s approximates sin(z)+cos(z), and c approximates sin(z)-cos(z),
784a238c70SJohn Marino with total absolute error bounded by 2^(1-w). */
794a238c70SJohn Marino
804a238c70SJohn Marino /* precompute 1/(8|z|) */
814a238c70SJohn Marino mpfr_si_div (iz, MPFR_IS_POS(z) ? 1 : -1, z, MPFR_RNDN); /* err <= 1 */
824a238c70SJohn Marino mpfr_div_2ui (iz, iz, 3, MPFR_RNDN);
834a238c70SJohn Marino
844a238c70SJohn Marino /* compute P and Q */
854a238c70SJohn Marino mpfr_set_ui (P, 1, MPFR_RNDN);
864a238c70SJohn Marino mpfr_set_ui (Q, 0, MPFR_RNDN);
874a238c70SJohn Marino mpfr_set_ui (t, 1, MPFR_RNDN); /* current term */
884a238c70SJohn Marino mpfr_set_ui (err_t, 0, MPFR_RNDN); /* error on t */
894a238c70SJohn Marino mpfr_set_ui (err_s, 0, MPFR_RNDN); /* error on P and Q (sum of errors) */
904a238c70SJohn Marino for (k = 1, stop = 0; stop < 4; k++)
914a238c70SJohn Marino {
924a238c70SJohn Marino /* compute next term: t(k)/t(k-1) = (2n+2k-1)(2n-2k+1)/(8kz) */
934a238c70SJohn Marino mpfr_mul_si (t, t, 2 * (n + k) - 1, MPFR_RNDN); /* err <= err_k + 1 */
944a238c70SJohn Marino mpfr_mul_si (t, t, 2 * (n - k) + 1, MPFR_RNDN); /* err <= err_k + 2 */
954a238c70SJohn Marino mpfr_div_ui (t, t, k, MPFR_RNDN); /* err <= err_k + 3 */
964a238c70SJohn Marino mpfr_mul (t, t, iz, MPFR_RNDN); /* err <= err_k + 5 */
974a238c70SJohn Marino /* the relative error on t is bounded by (1+u)^(5k)-1, which is
984a238c70SJohn Marino bounded by 6ku for 6ku <= 0.02: first |5 log(1+u)| <= |5.5u|
994a238c70SJohn Marino for |u| <= 0.15, then |exp(5.5u)-1| <= 6u for |u| <= 0.02. */
1004a238c70SJohn Marino mpfr_mul_ui (err_t, t, 6 * k, MPFR_IS_POS(t) ? MPFR_RNDU : MPFR_RNDD);
1014a238c70SJohn Marino mpfr_abs (err_t, err_t, MPFR_RNDN); /* exact */
1024a238c70SJohn Marino /* the absolute error on t is bounded by err_t * 2^(-w) */
1034a238c70SJohn Marino mpfr_abs (err_u, t, MPFR_RNDU);
1044a238c70SJohn Marino mpfr_mul_2ui (err_u, err_u, w, MPFR_RNDU); /* t * 2^w */
1054a238c70SJohn Marino mpfr_add (err_u, err_u, err_t, MPFR_RNDU); /* max|t| * 2^w */
1064a238c70SJohn Marino if (stop >= 2)
1074a238c70SJohn Marino {
1084a238c70SJohn Marino /* take into account the neglected terms: t * 2^w */
1094a238c70SJohn Marino mpfr_div_2ui (err_s, err_s, w, MPFR_RNDU);
1104a238c70SJohn Marino if (MPFR_IS_POS(t))
1114a238c70SJohn Marino mpfr_add (err_s, err_s, t, MPFR_RNDU);
1124a238c70SJohn Marino else
1134a238c70SJohn Marino mpfr_sub (err_s, err_s, t, MPFR_RNDU);
1144a238c70SJohn Marino mpfr_mul_2ui (err_s, err_s, w, MPFR_RNDU);
1154a238c70SJohn Marino stop ++;
1164a238c70SJohn Marino }
1174a238c70SJohn Marino /* if k is odd, add to Q, otherwise to P */
1184a238c70SJohn Marino else if (k & 1)
1194a238c70SJohn Marino {
1204a238c70SJohn Marino /* if k = 1 mod 4, add, otherwise subtract */
1214a238c70SJohn Marino if ((k & 2) == 0)
1224a238c70SJohn Marino mpfr_add (Q, Q, t, MPFR_RNDN);
1234a238c70SJohn Marino else
1244a238c70SJohn Marino mpfr_sub (Q, Q, t, MPFR_RNDN);
1254a238c70SJohn Marino /* check if the next term is smaller than ulp(Q): if EXP(err_u)
1264a238c70SJohn Marino <= EXP(Q), since the current term is bounded by
1274a238c70SJohn Marino err_u * 2^(-w), it is bounded by ulp(Q) */
1284a238c70SJohn Marino if (MPFR_EXP(err_u) <= MPFR_EXP(Q))
1294a238c70SJohn Marino stop ++;
1304a238c70SJohn Marino else
1314a238c70SJohn Marino stop = 0;
1324a238c70SJohn Marino }
1334a238c70SJohn Marino else
1344a238c70SJohn Marino {
1354a238c70SJohn Marino /* if k = 0 mod 4, add, otherwise subtract */
1364a238c70SJohn Marino if ((k & 2) == 0)
1374a238c70SJohn Marino mpfr_add (P, P, t, MPFR_RNDN);
1384a238c70SJohn Marino else
1394a238c70SJohn Marino mpfr_sub (P, P, t, MPFR_RNDN);
1404a238c70SJohn Marino /* check if the next term is smaller than ulp(P) */
1414a238c70SJohn Marino if (MPFR_EXP(err_u) <= MPFR_EXP(P))
1424a238c70SJohn Marino stop ++;
1434a238c70SJohn Marino else
1444a238c70SJohn Marino stop = 0;
1454a238c70SJohn Marino }
1464a238c70SJohn Marino mpfr_add (err_s, err_s, err_t, MPFR_RNDU);
1474a238c70SJohn Marino /* the sum of the rounding errors on P and Q is bounded by
1484a238c70SJohn Marino err_s * 2^(-w) */
1494a238c70SJohn Marino
1504a238c70SJohn Marino /* stop when start to diverge */
1514a238c70SJohn Marino if (stop < 2 &&
1524a238c70SJohn Marino ((MPFR_IS_POS(z) && mpfr_cmp_ui (z, (k + 1) / 2) < 0) ||
1534a238c70SJohn Marino (MPFR_IS_NEG(z) && mpfr_cmp_si (z, - ((k + 1) / 2)) > 0)))
1544a238c70SJohn Marino {
1554a238c70SJohn Marino /* if we have to stop the series because it diverges, then
1564a238c70SJohn Marino increasing the precision will most probably fail, since
1574a238c70SJohn Marino we will stop to the same point, and thus compute a very
1584a238c70SJohn Marino similar approximation */
1594a238c70SJohn Marino diverge = 1;
1604a238c70SJohn Marino stop = 2; /* force stop */
1614a238c70SJohn Marino }
1624a238c70SJohn Marino }
1634a238c70SJohn Marino /* the sum of the total errors on P and Q is bounded by err_s * 2^(-w) */
1644a238c70SJohn Marino
1654a238c70SJohn Marino /* Now combine: the sum of the rounding errors on P and Q is bounded by
1664a238c70SJohn Marino err_s * 2^(-w), and the absolute error on s/c is bounded by 2^(1-w) */
1674a238c70SJohn Marino if ((n & 1) == 0) /* n even: P * (sin + cos) + Q (cos - sin) for jn
1684a238c70SJohn Marino Q * (sin + cos) + P (sin - cos) for yn */
1694a238c70SJohn Marino {
1704a238c70SJohn Marino #ifdef MPFR_JN
1714a238c70SJohn Marino mpfr_mul (c, c, Q, MPFR_RNDN); /* Q * (sin - cos) */
1724a238c70SJohn Marino mpfr_mul (s, s, P, MPFR_RNDN); /* P * (sin + cos) */
1734a238c70SJohn Marino #else
1744a238c70SJohn Marino mpfr_mul (c, c, P, MPFR_RNDN); /* P * (sin - cos) */
1754a238c70SJohn Marino mpfr_mul (s, s, Q, MPFR_RNDN); /* Q * (sin + cos) */
1764a238c70SJohn Marino #endif
1774a238c70SJohn Marino err = MPFR_EXP(c);
1784a238c70SJohn Marino if (MPFR_EXP(s) > err)
1794a238c70SJohn Marino err = MPFR_EXP(s);
1804a238c70SJohn Marino #ifdef MPFR_JN
1814a238c70SJohn Marino mpfr_sub (s, s, c, MPFR_RNDN);
1824a238c70SJohn Marino #else
1834a238c70SJohn Marino mpfr_add (s, s, c, MPFR_RNDN);
1844a238c70SJohn Marino #endif
1854a238c70SJohn Marino }
1864a238c70SJohn Marino else /* n odd: P * (sin - cos) + Q (cos + sin) for jn,
1874a238c70SJohn Marino Q * (sin - cos) - P (cos + sin) for yn */
1884a238c70SJohn Marino {
1894a238c70SJohn Marino #ifdef MPFR_JN
1904a238c70SJohn Marino mpfr_mul (c, c, P, MPFR_RNDN); /* P * (sin - cos) */
1914a238c70SJohn Marino mpfr_mul (s, s, Q, MPFR_RNDN); /* Q * (sin + cos) */
1924a238c70SJohn Marino #else
1934a238c70SJohn Marino mpfr_mul (c, c, Q, MPFR_RNDN); /* Q * (sin - cos) */
1944a238c70SJohn Marino mpfr_mul (s, s, P, MPFR_RNDN); /* P * (sin + cos) */
1954a238c70SJohn Marino #endif
1964a238c70SJohn Marino err = MPFR_EXP(c);
1974a238c70SJohn Marino if (MPFR_EXP(s) > err)
1984a238c70SJohn Marino err = MPFR_EXP(s);
1994a238c70SJohn Marino #ifdef MPFR_JN
2004a238c70SJohn Marino mpfr_add (s, s, c, MPFR_RNDN);
2014a238c70SJohn Marino #else
2024a238c70SJohn Marino mpfr_sub (s, c, s, MPFR_RNDN);
2034a238c70SJohn Marino #endif
2044a238c70SJohn Marino }
2054a238c70SJohn Marino if ((n & 2) != 0)
2064a238c70SJohn Marino mpfr_neg (s, s, MPFR_RNDN);
2074a238c70SJohn Marino if (MPFR_EXP(s) > err)
2084a238c70SJohn Marino err = MPFR_EXP(s);
2094a238c70SJohn Marino /* the absolute error on s is bounded by P*err(s/c) + Q*err(s/c)
2104a238c70SJohn Marino + err(P)*(s/c) + err(Q)*(s/c) + 3 * 2^(err - w - 1)
2114a238c70SJohn Marino <= (|P|+|Q|) * 2^(1-w) + err_s * 2^(1-w) + 2^err * 2^(1-w),
2124a238c70SJohn Marino since |c|, |old_s| <= 2. */
2134a238c70SJohn Marino err2 = (MPFR_EXP(P) >= MPFR_EXP(Q)) ? MPFR_EXP(P) + 2 : MPFR_EXP(Q) + 2;
2144a238c70SJohn Marino /* (|P| + |Q|) * 2^(1 - w) <= 2^(err2 - w) */
2154a238c70SJohn Marino err = MPFR_EXP(err_s) >= err ? MPFR_EXP(err_s) + 2 : err + 2;
2164a238c70SJohn Marino /* err_s * 2^(1-w) + 2^old_err * 2^(1-w) <= 2^err * 2^(-w) */
2174a238c70SJohn Marino err2 = (err >= err2) ? err + 1 : err2 + 1;
2184a238c70SJohn Marino /* now the absolute error on s is bounded by 2^(err2 - w) */
2194a238c70SJohn Marino
2204a238c70SJohn Marino /* multiply by sqrt(1/(Pi*z)) */
2214a238c70SJohn Marino mpfr_const_pi (c, MPFR_RNDN); /* Pi, err <= 1 */
2224a238c70SJohn Marino mpfr_mul (c, c, z, MPFR_RNDN); /* err <= 2 */
2234a238c70SJohn Marino mpfr_si_div (c, MPFR_IS_POS(z) ? 1 : -1, c, MPFR_RNDN); /* err <= 3 */
2244a238c70SJohn Marino mpfr_sqrt (c, c, MPFR_RNDN); /* err<=5/2, thus the absolute error is
2254a238c70SJohn Marino bounded by 3*u*|c| for |u| <= 0.25 */
2264a238c70SJohn Marino mpfr_mul (err_t, c, s, MPFR_SIGN(c)==MPFR_SIGN(s) ? MPFR_RNDU : MPFR_RNDD);
2274a238c70SJohn Marino mpfr_abs (err_t, err_t, MPFR_RNDU);
2284a238c70SJohn Marino mpfr_mul_ui (err_t, err_t, 3, MPFR_RNDU);
2294a238c70SJohn Marino /* 3*2^(-w)*|old_c|*|s| [see below] is bounded by err_t * 2^(-w) */
2304a238c70SJohn Marino err2 += MPFR_EXP(c);
2314a238c70SJohn Marino /* |old_c| * 2^(err2 - w) [see below] is bounded by 2^(err2-w) */
2324a238c70SJohn Marino mpfr_mul (c, c, s, MPFR_RNDN); /* the absolute error on c is bounded by
2334a238c70SJohn Marino 1/2 ulp(c) + 3*2^(-w)*|old_c|*|s|
2344a238c70SJohn Marino + |old_c| * 2^(err2 - w) */
2354a238c70SJohn Marino /* compute err_t * 2^(-w) + 1/2 ulp(c) = (err_t + 2^EXP(c)) * 2^(-w) */
2364a238c70SJohn Marino err = (MPFR_EXP(err_t) > MPFR_EXP(c)) ? MPFR_EXP(err_t) + 1 : MPFR_EXP(c) + 1;
2374a238c70SJohn Marino /* err_t * 2^(-w) + 1/2 ulp(c) <= 2^(err - w) */
2384a238c70SJohn Marino /* now err_t * 2^(-w) bounds 1/2 ulp(c) + 3*2^(-w)*|old_c|*|s| */
2394a238c70SJohn Marino err = (err >= err2) ? err + 1 : err2 + 1;
2404a238c70SJohn Marino /* the absolute error on c is bounded by 2^(err - w) */
2414a238c70SJohn Marino
2424a238c70SJohn Marino mpfr_clear (s);
2434a238c70SJohn Marino mpfr_clear (P);
2444a238c70SJohn Marino mpfr_clear (Q);
2454a238c70SJohn Marino mpfr_clear (t);
2464a238c70SJohn Marino mpfr_clear (iz);
2474a238c70SJohn Marino mpfr_clear (err_t);
2484a238c70SJohn Marino mpfr_clear (err_s);
2494a238c70SJohn Marino mpfr_clear (err_u);
2504a238c70SJohn Marino
2514a238c70SJohn Marino err -= MPFR_EXP(c);
2524a238c70SJohn Marino if (MPFR_LIKELY (MPFR_CAN_ROUND (c, w - err, MPFR_PREC(res), r)))
2534a238c70SJohn Marino break;
2544a238c70SJohn Marino if (diverge != 0)
2554a238c70SJohn Marino {
2564a238c70SJohn Marino mpfr_set (c, z, r); /* will force inex=0 below, which means the
2574a238c70SJohn Marino asymptotic expansion failed */
2584a238c70SJohn Marino break;
2594a238c70SJohn Marino }
2604a238c70SJohn Marino MPFR_ZIV_NEXT (loop, w);
2614a238c70SJohn Marino }
2624a238c70SJohn Marino MPFR_ZIV_FREE (loop);
2634a238c70SJohn Marino
2644a238c70SJohn Marino inex = (MPFR_IS_POS(z) || ((n & 1) == 0)) ? mpfr_set (res, c, r)
2654a238c70SJohn Marino : mpfr_neg (res, c, r);
2664a238c70SJohn Marino mpfr_clear (c);
2674a238c70SJohn Marino
2684a238c70SJohn Marino return inex;
2694a238c70SJohn Marino }
270