private static MultiPrecision <N> BesselJLimit(MultiPrecision <N> nu, MultiPrecision <N> z) { Consts.BesselLimitCoef table = Consts.Bessel.LimitCoef(nu); MultiPrecision <Plus4 <N> > z_ex = z.Convert <Plus4 <N> >(); MultiPrecision <Plus4 <N> > v = 1 / z_ex; MultiPrecision <Plus4 <N> > w = v * v; MultiPrecision <Plus4 <N> > x = 0, y = 0, p = 1, q = v; Sign sign = Sign.Plus; for (int k = 0; k <= Consts.BesselJY.LimitApproxTerms; k++, p *= w, q *= w) { MultiPrecision <Plus4 <N> > c = p * table.Value(k * 2); MultiPrecision <Plus4 <N> > s = q * table.Value(k * 2 + 1); if (sign == Sign.Plus) { x += c; y += s; sign = Sign.Minus; } else { x -= c; y -= s; sign = Sign.Plus; } if (!c.IsZero && x.Exponent - c.Exponent <= MultiPrecision <Plus2 <N> > .Bits) { continue; } if (!s.IsZero && y.Exponent - s.Exponent <= MultiPrecision <Plus2 <N> > .Bits) { continue; } break; } MultiPrecision <Plus4 <N> > omega = z_ex - (2 * nu.Convert <Plus4 <N> >() + 1) * MultiPrecision <Plus4 <N> > .PI / 4; MultiPrecision <Plus4 <N> > m = x * MultiPrecision <Plus4 <N> > .Cos(omega) - y * MultiPrecision <Plus4 <N> > .Sin(omega); MultiPrecision <Plus4 <N> > t = m * MultiPrecision <Plus4 <N> > .Sqrt(2 / (MultiPrecision <Plus4 <N> > .PI *z_ex)); return(t.Convert <N>()); }
public static MultiPrecision <N> BesselY(MultiPrecision <N> nu, MultiPrecision <N> x) { if (Abs(nu) > 64) { throw new ArgumentOutOfRangeException( nameof(nu), "In the calculation of the Bessel function, nu with an absolute value greater than 64 is not supported." ); } if (nu.IsNaN || x.IsNaN) { return(NaN); } if (x.Sign == Sign.Minus) { if (nu != Truncate(nu)) { return(NaN); } long n = (long)nu; return(((n & 1L) == 0) ? BesselY(nu, Abs(x)) : -BesselY(nu, Abs(x))); } if (!x.IsFinite) { return(0); } if (x.Exponent >= Bits) { return(NaN); } if (nu.Sign == Sign.Minus && nu == Truncate(nu)) { long n = (long)nu; return(((n & 1L) == 0) ? BesselY(Abs(nu), x) : -BesselY(Abs(nu), x)); } if (nu - Point5 == Floor(nu)) { long n = (long)Floor(nu); if (n >= -2 && n < 2) { MultiPrecision <Plus1 <N> > x_ex = x.Convert <Plus1 <N> >(); MultiPrecision <Plus1 <N> > envelope = MultiPrecision <Plus1 <N> > .Sqrt(2 / (MultiPrecision <Plus1 <N> > .PI *x_ex)); if (n == -2) { MultiPrecision <N> y = -(envelope * (MultiPrecision <Plus1 <N> > .Sin(x_ex) / x_ex - MultiPrecision <Plus1 <N> > .Cos(x_ex))).Convert <N>(); return(y.IsNaN ? 0 : y); } if (n == -1) { MultiPrecision <N> y = (envelope * MultiPrecision <Plus1 <N> > .Sin(x_ex)).Convert <N>(); return(y.IsNaN ? 0 : y); } if (n == 0) { return(-(envelope * MultiPrecision <Plus1 <N> > .Cos(x_ex)).Convert <N>()); } if (n == 1) { return(-(envelope * (MultiPrecision <Plus1 <N> > .Cos(x_ex) / x_ex + MultiPrecision <Plus1 <N> > .Sin(x_ex))).Convert <N>()); } } } if (x.Exponent <= -0x1000000) { return(nu.IsZero ? NegativeInfinity : NaN); } if (x < Consts.BesselJY.ApproxThreshold) { return(BesselYNearZero(nu, x).Convert <N>()); } else { return(BesselYLimit(nu, x)); } }