1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __jnl = jnl
  32 #pragma weak __ynl = ynl
  33 
  34 /*
  35  * floating point Bessel's function of the 1st and 2nd kind
  36  * of order n: jn(n,x),yn(n,x);
  37  *
  38  * Special cases:
  39  *      y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
  40  *      y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
  41  * Note 2. About jn(n,x), yn(n,x)
  42  *      For n=0, j0(x) is called,
  43  *      for n=1, j1(x) is called,
  44  *      for n<x, forward recursion us used starting
  45  *      from values of j0(x) and j1(x).
  46  *      for n>x, a continued fraction approximation to
  47  *      j(n,x)/j(n-1,x) is evaluated and then backward
  48  *      recursion is used starting from a supposed value
  49  *      for j(n,x). The resulting value of j(0,x) is
  50  *      compared with the actual value to correct the
  51  *      supposed value of j(n,x).
  52  *
  53  *      yn(n,x) is similar in all respects, except
  54  *      that forward recursion is used for all
  55  *      values of n>1.
  56  *
  57  */
  58 
  59 #include "libm.h"
  60 #include "longdouble.h"
  61 #include <float.h>                        /* LDBL_MAX */
  62 
  63 #define GENERIC long double
  64 
  65 static const GENERIC
  66 invsqrtpi = 5.641895835477562869480794515607725858441e-0001L,
  67         two = 2.0L,
  68         zero = 0.0L,
  69         one = 1.0L;
  70 
  71 GENERIC
  72 jnl(int n, GENERIC x)
  73 {
  74         int i, sgn;
  75         GENERIC a, b, temp = 0, z, w;
  76 
  77         /*
  78          * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
  79          * Thus, J(-n,x) = J(n,-x)
  80          */
  81         if (n < 0) {
  82                 n = -n;
  83                 x = -x;
  84         }
  85 
  86         if (n == 0)
  87                 return (j0l(x));
  88 
  89         if (n == 1)
  90                 return (j1l(x));
  91 
  92         if (x != x)
  93                 return (x + x);
  94 
  95         if ((n & 1) == 0)
  96                 sgn = 0;                /* even n */
  97         else
  98                 sgn = signbitl(x);      /* old n  */
  99 
 100         x = fabsl(x);
 101 
 102         if (x == zero || !finitel(x)) {
 103                 b = zero;
 104         } else if ((GENERIC)n <= x) {
 105                 /*
 106                  * Safe to use
 107                  * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
 108                  */
 109                 if (x > 1.0e91L) {
 110                         /*
 111                          * x >> n**2
 112                          *  Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 113                          *  Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 114                          *  Let s=sin(x), c=cos(x),
 115                          *  xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
 116                          *
 117                          *         n    sin(xn)*sqt2    cos(xn)*sqt2
 118                          *      ----------------------------------
 119                          *         0     s-c             c+s
 120                          *         1    -s-c            -c+s
 121                          *         2    -s+c            -c-s
 122                          *         3     s+c             c-s
 123                          */
 124                         switch (n & 3) {
 125                         case 0:
 126                                 temp = cosl(x) + sinl(x);
 127                                 break;
 128                         case 1:
 129                                 temp = -cosl(x) + sinl(x);
 130                                 break;
 131                         case 2:
 132                                 temp = -cosl(x) - sinl(x);
 133                                 break;
 134                         case 3:
 135                                 temp = cosl(x) - sinl(x);
 136                                 break;
 137                         }
 138 
 139                         b = invsqrtpi * temp / sqrtl(x);
 140                 } else {
 141                         a = j0l(x);
 142                         b = j1l(x);
 143 
 144                         for (i = 1; i < n; i++) {
 145                                 temp = b;
 146                                 /* avoid underflow */
 147                                 b = b * ((GENERIC)(i + i) / x) - a;
 148                                 a = temp;
 149                         }
 150                 }
 151         } else {
 152                 if (x < 1e-17L) {    /* use J(n,x) = 1/n!*(x/2)^n */
 153                         b = powl(0.5L * x, (GENERIC)n);
 154 
 155                         if (b != zero) {
 156                                 for (a = one, i = 1; i <= n; i++)
 157                                         a *= (GENERIC)i;
 158 
 159                                 b = b / a;
 160                         }
 161                 } else {
 162                         /* BEGIN CSTYLED */
 163 
 164                         /*
 165                          * use backward recurrence
 166                          *                      x      x^2      x^2
 167                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
 168                          *                      2n  - 2(n+1) - 2(n+2)
 169                          *
 170                          *                      1      1        1
 171                          *  (for large x)   =  ----  ------   ------   .....
 172                          *                      2n   2(n+1)   2(n+2)
 173                          *                      -- - ------ - ------ -
 174                          *                       x     x         x
 175                          *
 176                          * Let w = 2n/x and h=2/x, then the above quotient
 177                          * is equal to the continued fraction:
 178                          *                  1
 179                          *      = -----------------------
 180                          *                     1
 181                          *         w - -----------------
 182                          *                        1
 183                          *              w+h - ---------
 184                          *                     w+2h - ...
 185                          *
 186                          * To determine how many terms needed, let
 187                          * Q(0) = w, Q(1) = w(w+h) - 1,
 188                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
 189                          * When Q(k) > 1e4   good for single
 190                          * When Q(k) > 1e9   good for double
 191                          * When Q(k) > 1e17  good for quaduple
 192                          */
 193 
 194                         /*
 195                          * END CSTYLED
 196                          * determine k
 197                          */
 198                         GENERIC t, v;
 199                         double q0, q1, h, tmp;
 200                         int k, m;
 201 
 202                         w = (n + n) / (double)x;
 203                         h = 2.0 / (double)x;
 204                         q0 = w;
 205                         z = w + h;
 206                         q1 = w * z - 1.0;
 207                         k = 1;
 208 
 209                         while (q1 < 1.0e17) {
 210                                 k += 1;
 211                                 z += h;
 212                                 tmp = z * q1 - q0;
 213                                 q0 = q1;
 214                                 q1 = tmp;
 215                         }
 216 
 217                         m = n + n;
 218 
 219                         for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
 220                                 t = one / (i / x - t);
 221 
 222                         a = t;
 223                         b = one;
 224 
 225                         /*
 226                          * Estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
 227                          * hence, if n*(log(2n/x)) > ...
 228                          *  single:
 229                          *    8.8722839355e+01
 230                          *  double:
 231                          *    7.09782712893383973096e+02
 232                          *  long double:
 233                          *    1.1356523406294143949491931077970765006170e+04
 234                          * then recurrent value may overflow and the result is
 235                          * likely underflow to zero.
 236                          */
 237                         tmp = n;
 238                         v = two / x;
 239                         tmp = tmp * logl(fabsl(v * tmp));
 240 
 241                         if (tmp < 1.1356523406294143949491931077970765e+04L) {
 242                                 for (i = n - 1; i > 0; i--) {
 243                                         temp = b;
 244                                         b = ((i + i) / x) * b - a;
 245                                         a = temp;
 246                                 }
 247                         } else {
 248                                 for (i = n - 1; i > 0; i--) {
 249                                         temp = b;
 250                                         b = ((i + i) / x) * b - a;
 251                                         a = temp;
 252 
 253                                         if (b > 1e1000L) {
 254                                                 a /= b;
 255                                                 t /= b;
 256                                                 b = 1.0;
 257                                         }
 258                                 }
 259                         }
 260 
 261                         b = (t * j0l(x) / b);
 262                 }
 263         }
 264 
 265         if (sgn != 0)
 266                 return (-b);
 267         else
 268                 return (b);
 269 }
 270 
 271 GENERIC
 272 ynl(int n, GENERIC x)
 273 {
 274         int i;
 275         int sign;
 276         GENERIC a, b, temp = 0;
 277 
 278         if (x != x)
 279                 return (x + x);
 280 
 281         if (x <= zero) {
 282                 if (x == zero)
 283                         return (-one / zero);
 284                 else
 285                         return (zero / zero);
 286         }
 287 
 288         sign = 1;
 289 
 290         if (n < 0) {
 291                 n = -n;
 292 
 293                 if ((n & 1) == 1)
 294                         sign = -1;
 295         }
 296 
 297         if (n == 0)
 298                 return (y0l(x));
 299 
 300         if (n == 1)
 301                 return (sign * y1l(x));
 302 
 303         if (!finitel(x))
 304                 return (zero);
 305 
 306         if (x > 1.0e91L) {
 307                 /*
 308                  * x >> n**2
 309                  * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 310                  *   Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
 311                  *   Let s=sin(x), c=cos(x),
 312                  * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
 313                  *
 314                  *         n    sin(xn)*sqt2    cos(xn)*sqt2
 315                  *      ----------------------------------
 316                  *         0     s-c             c+s
 317                  *         1    -s-c            -c+s
 318                  *         2    -s+c            -c-s
 319                  *         3     s+c             c-s
 320                  */
 321                 switch (n & 3) {
 322                 case 0:
 323                         temp = sinl(x) - cosl(x);
 324                         break;
 325                 case 1:
 326                         temp = -sinl(x) - cosl(x);
 327                         break;
 328                 case 2:
 329                         temp = -sinl(x) + cosl(x);
 330                         break;
 331                 case 3:
 332                         temp = sinl(x) + cosl(x);
 333                         break;
 334                 }
 335 
 336                 b = invsqrtpi * temp / sqrtl(x);
 337         } else {
 338                 a = y0l(x);
 339                 b = y1l(x);
 340 
 341                 /*
 342                  * fix 1262058 and take care of non-default rounding
 343                  */
 344                 for (i = 1; i < n; i++) {
 345                         temp = b;
 346                         b *= (GENERIC)(i + i) / x;
 347 
 348                         if (b <= -LDBL_MAX)
 349                                 break;
 350 
 351                         b -= a;
 352                         a = temp;
 353                 }
 354         }
 355 
 356         if (sign > 0)
 357                 return (b);
 358         else
 359                 return (-b);
 360 }