📄 e_j0l.c
字号:
/* * ==================================================== * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * * Developed at SunPro, a Sun Microsystems, Inc. business. * Permission to use, copy, modify, and distribute this * software is freely granted, provided that this notice * is preserved. * ==================================================== *//* Long double expansions are Copyright (C) 2001 Stephen L. Moshier <moshier@na-net.ornl.gov> and are incorporated herein by permission of the author. The author reserves the right to distribute this material elsewhere under different copying permissions. These modifications are distributed here under the following terms: This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* __ieee754_j0(x), __ieee754_y0(x) * Bessel function of the first and second kinds of order zero. * Method -- j0(x): * 1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ... * 2. Reduce x to |x| since j0(x)=j0(-x), and * for x in (0,2) * j0(x) = 1 - z/4 + z^2*R0/S0, where z = x*x; * for x in (2,inf) * j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0)) * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) * as follow: * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) * = 1/sqrt(2) * (cos(x) + sin(x)) * sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4) * = 1/sqrt(2) * (sin(x) - cos(x)) * (To avoid cancellation, use * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) * to compute the worse one.) * * 3 Special cases * j0(nan)= nan * j0(0) = 1 * j0(inf) = 0 * * Method -- y0(x): * 1. For x<2. * Since * y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...) * therefore y0(x)-2/pi*j0(x)*ln(x) is an even function. * We use the following function to approximate y0, * y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2 * * Note: For tiny x, U/V = u0 and j0(x)~1, hence * y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27) * 2. For x>=2. * y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0)) * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) * by the method mentioned above. * 3. Special cases: y0(0)=-inf, y0(x<0)=NaN, y0(inf)=0. */#include "math.h"#include "math_private.h"#ifdef __STDC__static long double pzero (long double), qzero (long double);#elsestatic long double pzero (), qzero ();#endif#ifdef __STDC__static const long double#elsestatic long double#endif huge = 1e4930L, one = 1.0L, invsqrtpi = 5.6418958354775628694807945156077258584405e-1L, tpi = 6.3661977236758134307553505349005744813784e-1L, /* J0(x) = 1 - x^2 / 4 + x^4 R0(x^2) / S0(x^2) 0 <= x <= 2 peak relative error 1.41e-22 */ R[5] = { 4.287176872744686992880841716723478740566E7L, -6.652058897474241627570911531740907185772E5L, 7.011848381719789863458364584613651091175E3L, -3.168040850193372408702135490809516253693E1L, 6.030778552661102450545394348845599300939E-2L,}, S[4] = { 2.743793198556599677955266341699130654342E9L, 3.364330079384816249840086842058954076201E7L, 1.924119649412510777584684927494642526573E5L, 6.239282256012734914211715620088714856494E2L, /* 1.000000000000000000000000000000000000000E0L,*/};#ifdef __STDC__static const long double zero = 0.0;#elsestatic long double zero = 0.0;#endif#ifdef __STDC__long double__ieee754_j0l (long double x)#elselong double__ieee754_j0l (x) long double x;#endif{ long double z, s, c, ss, cc, r, u, v; int32_t ix; u_int32_t se, i0, i1; GET_LDOUBLE_WORDS (se, i0, i1, x); ix = se & 0x7fff; if (ix >= 0x7fff) return one / (x * x); x = fabsl (x); if (ix >= 0x4000) /* |x| >= 2.0 */ { __sincosl (x, &s, &c); ss = s - c; cc = s + c; if (ix < 0x7ffe) { /* make sure x+x not overflow */ z = -__cosl (x + x); if ((s * c) < zero) cc = z / ss; else ss = z / cc; } /* * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) */ if (ix > 0x4080) /* 2^129 */ z = (invsqrtpi * cc) / __ieee754_sqrtl (x); else { u = pzero (x); v = qzero (x); z = invsqrtpi * (u * cc - v * ss) / __ieee754_sqrtl (x); } return z; } if (ix < 0x3fef) /* |x| < 2**-16 */ { if (huge + x > one) { /* raise inexact if x != 0 */ if (ix < 0x3fde) /* |x| < 2^-33 */ return one; else return one - 0.25 * x * x; } } z = x * x; r = z * (R[0] + z * (R[1] + z * (R[2] + z * (R[3] + z * R[4])))); s = S[0] + z * (S[1] + z * (S[2] + z * (S[3] + z))); if (ix < 0x3fff) { /* |x| < 1.00 */ return (one - 0.25 * z + z * (r / s)); } else { u = 0.5 * x; return ((one + u) * (one - u) + z * (r / s)); }}/* y0(x) = 2/pi ln(x) J0(x) + U(x^2)/V(x^2) 0 < x <= 2 peak relative error 1.7e-21 */#ifdef __STDC__static const long double#elsestatic long double#endifU[6] = { -1.054912306975785573710813351985351350861E10L, 2.520192609749295139432773849576523636127E10L, -1.856426071075602001239955451329519093395E9L, 4.079209129698891442683267466276785956784E7L, -3.440684087134286610316661166492641011539E5L, 1.005524356159130626192144663414848383774E3L,};#ifdef __STDC__static const long double#elsestatic long double#endifV[5] = { 1.429337283720789610137291929228082613676E11L, 2.492593075325119157558811370165695013002E9L, 2.186077620785925464237324417623665138376E7L, 1.238407896366385175196515057064384929222E5L, 4.693924035211032457494368947123233101664E2L, /* 1.000000000000000000000000000000000000000E0L */};#ifdef __STDC__long double__ieee754_y0l (long double x)#elselong double__ieee754_y0l (x) long double x;#endif{ long double z, s, c, ss, cc, u, v; int32_t ix; u_int32_t se, i0, i1; GET_LDOUBLE_WORDS (se, i0, i1, x); ix = se & 0x7fff; /* Y0(NaN) is NaN, y0(-inf) is Nan, y0(inf) is 0 */ if (se & 0x8000) return zero / zero; if (ix >= 0x7fff) return one / (x + x * x); if ((i0 | i1) == 0) return -one / zero; if (ix >= 0x4000) { /* |x| >= 2.0 */ /* y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0)) * where x0 = x-pi/4 * Better formula: * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) * = 1/sqrt(2) * (sin(x) + cos(x)) * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) * = 1/sqrt(2) * (sin(x) - cos(x)) * To avoid cancellation, use * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) * to compute the worse one. */ __sincosl (x, &s, &c); ss = s - c; cc = s + c; /* * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) */ if (ix < 0x7ffe) { /* make sure x+x not overflow */ z = -__cosl (x + x); if ((s * c) < zero) cc = z / ss; else ss = z / cc; } if (ix > 0x4080) /* 1e39 */ z = (invsqrtpi * ss) / __ieee754_sqrtl (x); else { u = pzero (x); v = qzero (x); z = invsqrtpi * (u * ss + v * cc) / __ieee754_sqrtl (x); } return z; } if (ix <= 0x3fde) /* x < 2^-33 */ { z = -7.380429510868722527629822444004602747322E-2L + tpi * __ieee754_logl (x); return z; } z = x * x; u = U[0] + z * (U[1] + z * (U[2] + z * (U[3] + z * (U[4] + z * U[5])))); v = V[0] + z * (V[1] + z * (V[2] + z * (V[3] + z * (V[4] + z)))); return (u / v + tpi * (__ieee754_j0l (x) * __ieee754_logl (x)));}/* The asymptotic expansions of pzero is * 1 - 9/128 s^2 + 11025/98304 s^4 - ..., where s = 1/x. * For x >= 2, We approximate pzero by * pzero(x) = 1 + s^2 R(s^2) / S(s^2) */#ifdef __STDC__static const long double pR8[7] = {#elsestatic long double pR8[7] = {#endif /* 8 <= x <= inf Peak relative error 4.62 */ -4.094398895124198016684337960227780260127E-9L, -8.929643669432412640061946338524096893089E-7L, -6.281267456906136703868258380673108109256E-5L, -1.736902783620362966354814353559382399665E-3L, -1.831506216290984960532230842266070146847E-2L, -5.827178869301452892963280214772398135283E-2L, -2.087563267939546435460286895807046616992E-2L,};#ifdef __STDC__static const long double pS8[6] = {#elsestatic long double pS8[6] = {#endif 5.823145095287749230197031108839653988393E-8L, 1.279281986035060320477759999428992730280E-5L, 9.132668954726626677174825517150228961304E-4L, 2.606019379433060585351880541545146252534E-2L, 2.956262215119520464228467583516287175244E-1L, 1.149498145388256448535563278632697465675E0L, /* 1.000000000000000000000000000000000000000E0L, */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -