Source file src/math/j1.go

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package math
     6  
     7  /*
     8  	Bessel function of the first and second kinds of order one.
     9  */
    10  
    11  // The original C code and the long comment below are
    12  // from FreeBSD's /usr/src/lib/msun/src/e_j1.c and
    13  // came with this notice. The go code is a simplified
    14  // version of the original C.
    15  //
    16  // ====================================================
    17  // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
    18  //
    19  // Developed at SunPro, a Sun Microsystems, Inc. business.
    20  // Permission to use, copy, modify, and distribute this
    21  // software is freely granted, provided that this notice
    22  // is preserved.
    23  // ====================================================
    24  //
    25  // __ieee754_j1(x), __ieee754_y1(x)
    26  // Bessel function of the first and second kinds of order one.
    27  // Method -- j1(x):
    28  //      1. For tiny x, we use j1(x) = x/2 - x**3/16 + x**5/384 - ...
    29  //      2. Reduce x to |x| since j1(x)=-j1(-x),  and
    30  //         for x in (0,2)
    31  //              j1(x) = x/2 + x*z*R0/S0,  where z = x*x;
    32  //         (precision:  |j1/x - 1/2 - R0/S0 |<2**-61.51 )
    33  //         for x in (2,inf)
    34  //              j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1))
    35  //              y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
    36  //         where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
    37  //         as follow:
    38  //              cos(x1) =  cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
    39  //                      =  1/sqrt(2) * (sin(x) - cos(x))
    40  //              sin(x1) =  sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
    41  //                      = -1/sqrt(2) * (sin(x) + cos(x))
    42  //         (To avoid cancellation, use
    43  //              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
    44  //         to compute the worse one.)
    45  //
    46  //      3 Special cases
    47  //              j1(nan)= nan
    48  //              j1(0) = 0
    49  //              j1(inf) = 0
    50  //
    51  // Method -- y1(x):
    52  //      1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN
    53  //      2. For x<2.
    54  //         Since
    55  //              y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x**3-...)
    56  //         therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function.
    57  //         We use the following function to approximate y1,
    58  //              y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x**2
    59  //         where for x in [0,2] (abs err less than 2**-65.89)
    60  //              U(z) = U0[0] + U0[1]*z + ... + U0[4]*z**4
    61  //              V(z) = 1  + v0[0]*z + ... + v0[4]*z**5
    62  //         Note: For tiny x, 1/x dominate y1 and hence
    63  //              y1(tiny) = -2/pi/tiny, (choose tiny<2**-54)
    64  //      3. For x>=2.
    65  //               y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1))
    66  //         where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1)
    67  //         by method mentioned above.
    68  
    69  // J1 returns the order-one Bessel function of the first kind.
    70  //
    71  // Special cases are:
    72  //
    73  //	J1(±Inf) = 0
    74  //	J1(NaN) = NaN
    75  func J1(x float64) float64 {
    76  	const (
    77  		TwoM27 = 1.0 / (1 << 27) // 2**-27 0x3e40000000000000
    78  		Two129 = 1 << 129        // 2**129 0x4800000000000000
    79  		// R0/S0 on [0, 2]
    80  		R00 = -6.25000000000000000000e-02 // 0xBFB0000000000000
    81  		R01 = 1.40705666955189706048e-03  // 0x3F570D9F98472C61
    82  		R02 = -1.59955631084035597520e-05 // 0xBEF0C5C6BA169668
    83  		R03 = 4.96727999609584448412e-08  // 0x3E6AAAFA46CA0BD9
    84  		S01 = 1.91537599538363460805e-02  // 0x3F939D0B12637E53
    85  		S02 = 1.85946785588630915560e-04  // 0x3F285F56B9CDF664
    86  		S03 = 1.17718464042623683263e-06  // 0x3EB3BFF8333F8498
    87  		S04 = 5.04636257076217042715e-09  // 0x3E35AC88C97DFF2C
    88  		S05 = 1.23542274426137913908e-11  // 0x3DAB2ACFCFB97ED8
    89  	)
    90  	// special cases
    91  	switch {
    92  	case IsNaN(x):
    93  		return x
    94  	case IsInf(x, 0) || x == 0:
    95  		return 0
    96  	}
    97  
    98  	sign := false
    99  	if x < 0 {
   100  		x = -x
   101  		sign = true
   102  	}
   103  	if x >= 2 {
   104  		s, c := Sincos(x)
   105  		ss := -s - c
   106  		cc := s - c
   107  
   108  		// make sure x+x does not overflow
   109  		if x < MaxFloat64/2 {
   110  			z := Cos(x + x)
   111  			if s*c > 0 {
   112  				cc = z / ss
   113  			} else {
   114  				ss = z / cc
   115  			}
   116  		}
   117  
   118  		// j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x)
   119  		// y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x)
   120  
   121  		var z float64
   122  		if x > Two129 {
   123  			z = (1 / SqrtPi) * cc / Sqrt(x)
   124  		} else {
   125  			u := pone(x)
   126  			v := qone(x)
   127  			z = (1 / SqrtPi) * (u*cc - v*ss) / Sqrt(x)
   128  		}
   129  		if sign {
   130  			return -z
   131  		}
   132  		return z
   133  	}
   134  	if x < TwoM27 { // |x|<2**-27
   135  		return 0.5 * x // inexact if x!=0 necessary
   136  	}
   137  	z := x * x
   138  	r := z * (R00 + z*(R01+z*(R02+z*R03)))
   139  	s := 1.0 + z*(S01+z*(S02+z*(S03+z*(S04+z*S05))))
   140  	r *= x
   141  	z = 0.5*x + r/s
   142  	if sign {
   143  		return -z
   144  	}
   145  	return z
   146  }
   147  
   148  // Y1 returns the order-one Bessel function of the second kind.
   149  //
   150  // Special cases are:
   151  //
   152  //	Y1(+Inf) = 0
   153  //	Y1(0) = -Inf
   154  //	Y1(x < 0) = NaN
   155  //	Y1(NaN) = NaN
   156  func Y1(x float64) float64 {
   157  	const (
   158  		TwoM54 = 1.0 / (1 << 54)             // 2**-54 0x3c90000000000000
   159  		Two129 = 1 << 129                    // 2**129 0x4800000000000000
   160  		U00    = -1.96057090646238940668e-01 // 0xBFC91866143CBC8A
   161  		U01    = 5.04438716639811282616e-02  // 0x3FA9D3C776292CD1
   162  		U02    = -1.91256895875763547298e-03 // 0xBF5F55E54844F50F
   163  		U03    = 2.35252600561610495928e-05  // 0x3EF8AB038FA6B88E
   164  		U04    = -9.19099158039878874504e-08 // 0xBE78AC00569105B8
   165  		V00    = 1.99167318236649903973e-02  // 0x3F94650D3F4DA9F0
   166  		V01    = 2.02552581025135171496e-04  // 0x3F2A8C896C257764
   167  		V02    = 1.35608801097516229404e-06  // 0x3EB6C05A894E8CA6
   168  		V03    = 6.22741452364621501295e-09  // 0x3E3ABF1D5BA69A86
   169  		V04    = 1.66559246207992079114e-11  // 0x3DB25039DACA772A
   170  	)
   171  	// special cases
   172  	switch {
   173  	case x < 0 || IsNaN(x):
   174  		return NaN()
   175  	case IsInf(x, 1):
   176  		return 0
   177  	case x == 0:
   178  		return Inf(-1)
   179  	}
   180  
   181  	if x >= 2 {
   182  		s, c := Sincos(x)
   183  		ss := -s - c
   184  		cc := s - c
   185  
   186  		// make sure x+x does not overflow
   187  		if x < MaxFloat64/2 {
   188  			z := Cos(x + x)
   189  			if s*c > 0 {
   190  				cc = z / ss
   191  			} else {
   192  				ss = z / cc
   193  			}
   194  		}
   195  		// y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0))
   196  		// where x0 = x-3pi/4
   197  		//     Better formula:
   198  		//         cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4)
   199  		//                 =  1/sqrt(2) * (sin(x) - cos(x))
   200  		//         sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4)
   201  		//                 = -1/sqrt(2) * (cos(x) + sin(x))
   202  		// To avoid cancellation, use
   203  		//     sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
   204  		// to compute the worse one.
   205  
   206  		var z float64
   207  		if x > Two129 {
   208  			z = (1 / SqrtPi) * ss / Sqrt(x)
   209  		} else {
   210  			u := pone(x)
   211  			v := qone(x)
   212  			z = (1 / SqrtPi) * (u*ss + v*cc) / Sqrt(x)
   213  		}
   214  		return z
   215  	}
   216  	if x <= TwoM54 { // x < 2**-54
   217  		return -(2 / Pi) / x
   218  	}
   219  	z := x * x
   220  	u := U00 + z*(U01+z*(U02+z*(U03+z*U04)))
   221  	v := 1 + z*(V00+z*(V01+z*(V02+z*(V03+z*V04))))
   222  	return x*(u/v) + (2/Pi)*(J1(x)*Log(x)-1/x)
   223  }
   224  
   225  // For x >= 8, the asymptotic expansions of pone is
   226  //      1 + 15/128 s**2 - 4725/2**15 s**4 - ..., where s = 1/x.
   227  // We approximate pone by
   228  //      pone(x) = 1 + (R/S)
   229  // where R = pr0 + pr1*s**2 + pr2*s**4 + ... + pr5*s**10
   230  //       S = 1 + ps0*s**2 + ... + ps4*s**10
   231  // and
   232  //      | pone(x)-1-R/S | <= 2**(-60.06)
   233  
   234  // for x in [inf, 8]=1/[0,0.125]
   235  var p1R8 = [6]float64{
   236  	0.00000000000000000000e+00, // 0x0000000000000000
   237  	1.17187499999988647970e-01, // 0x3FBDFFFFFFFFFCCE
   238  	1.32394806593073575129e+01, // 0x402A7A9D357F7FCE
   239  	4.12051854307378562225e+02, // 0x4079C0D4652EA590
   240  	3.87474538913960532227e+03, // 0x40AE457DA3A532CC
   241  	7.91447954031891731574e+03, // 0x40BEEA7AC32782DD
   242  }
   243  var p1S8 = [5]float64{
   244  	1.14207370375678408436e+02, // 0x405C8D458E656CAC
   245  	3.65093083420853463394e+03, // 0x40AC85DC964D274F
   246  	3.69562060269033463555e+04, // 0x40E20B8697C5BB7F
   247  	9.76027935934950801311e+04, // 0x40F7D42CB28F17BB
   248  	3.08042720627888811578e+04, // 0x40DE1511697A0B2D
   249  }
   250  
   251  // for x in [8,4.5454] = 1/[0.125,0.22001]
   252  var p1R5 = [6]float64{
   253  	1.31990519556243522749e-11, // 0x3DAD0667DAE1CA7D
   254  	1.17187493190614097638e-01, // 0x3FBDFFFFE2C10043
   255  	6.80275127868432871736e+00, // 0x401B36046E6315E3
   256  	1.08308182990189109773e+02, // 0x405B13B9452602ED
   257  	5.17636139533199752805e+02, // 0x40802D16D052D649
   258  	5.28715201363337541807e+02, // 0x408085B8BB7E0CB7
   259  }
   260  var p1S5 = [5]float64{
   261  	5.92805987221131331921e+01, // 0x404DA3EAA8AF633D
   262  	9.91401418733614377743e+02, // 0x408EFB361B066701
   263  	5.35326695291487976647e+03, // 0x40B4E9445706B6FB
   264  	7.84469031749551231769e+03, // 0x40BEA4B0B8A5BB15
   265  	1.50404688810361062679e+03, // 0x40978030036F5E51
   266  }
   267  
   268  // for x in[4.5453,2.8571] = 1/[0.2199,0.35001]
   269  var p1R3 = [6]float64{
   270  	3.02503916137373618024e-09, // 0x3E29FC21A7AD9EDD
   271  	1.17186865567253592491e-01, // 0x3FBDFFF55B21D17B
   272  	3.93297750033315640650e+00, // 0x400F76BCE85EAD8A
   273  	3.51194035591636932736e+01, // 0x40418F489DA6D129
   274  	9.10550110750781271918e+01, // 0x4056C3854D2C1837
   275  	4.85590685197364919645e+01, // 0x4048478F8EA83EE5
   276  }
   277  var p1S3 = [5]float64{
   278  	3.47913095001251519989e+01, // 0x40416549A134069C
   279  	3.36762458747825746741e+02, // 0x40750C3307F1A75F
   280  	1.04687139975775130551e+03, // 0x40905B7C5037D523
   281  	8.90811346398256432622e+02, // 0x408BD67DA32E31E9
   282  	1.03787932439639277504e+02, // 0x4059F26D7C2EED53
   283  }
   284  
   285  // for x in [2.8570,2] = 1/[0.3499,0.5]
   286  var p1R2 = [6]float64{
   287  	1.07710830106873743082e-07, // 0x3E7CE9D4F65544F4
   288  	1.17176219462683348094e-01, // 0x3FBDFF42BE760D83
   289  	2.36851496667608785174e+00, // 0x4002F2B7F98FAEC0
   290  	1.22426109148261232917e+01, // 0x40287C377F71A964
   291  	1.76939711271687727390e+01, // 0x4031B1A8177F8EE2
   292  	5.07352312588818499250e+00, // 0x40144B49A574C1FE
   293  }
   294  var p1S2 = [5]float64{
   295  	2.14364859363821409488e+01, // 0x40356FBD8AD5ECDC
   296  	1.25290227168402751090e+02, // 0x405F529314F92CD5
   297  	2.32276469057162813669e+02, // 0x406D08D8D5A2DBD9
   298  	1.17679373287147100768e+02, // 0x405D6B7ADA1884A9
   299  	8.36463893371618283368e+00, // 0x4020BAB1F44E5192
   300  }
   301  
   302  func pone(x float64) float64 {
   303  	var p *[6]float64
   304  	var q *[5]float64
   305  	if x >= 8 {
   306  		p = &p1R8
   307  		q = &p1S8
   308  	} else if x >= 4.5454 {
   309  		p = &p1R5
   310  		q = &p1S5
   311  	} else if x >= 2.8571 {
   312  		p = &p1R3
   313  		q = &p1S3
   314  	} else if x >= 2 {
   315  		p = &p1R2
   316  		q = &p1S2
   317  	}
   318  	z := 1 / (x * x)
   319  	r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))))
   320  	s := 1.0 + z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4]))))
   321  	return 1 + r/s
   322  }
   323  
   324  // For x >= 8, the asymptotic expansions of qone is
   325  //      3/8 s - 105/1024 s**3 - ..., where s = 1/x.
   326  // We approximate qone by
   327  //      qone(x) = s*(0.375 + (R/S))
   328  // where R = qr1*s**2 + qr2*s**4 + ... + qr5*s**10
   329  //       S = 1 + qs1*s**2 + ... + qs6*s**12
   330  // and
   331  //      | qone(x)/s -0.375-R/S | <= 2**(-61.13)
   332  
   333  // for x in [inf, 8] = 1/[0,0.125]
   334  var q1R8 = [6]float64{
   335  	0.00000000000000000000e+00,  // 0x0000000000000000
   336  	-1.02539062499992714161e-01, // 0xBFBA3FFFFFFFFDF3
   337  	-1.62717534544589987888e+01, // 0xC0304591A26779F7
   338  	-7.59601722513950107896e+02, // 0xC087BCD053E4B576
   339  	-1.18498066702429587167e+04, // 0xC0C724E740F87415
   340  	-4.84385124285750353010e+04, // 0xC0E7A6D065D09C6A
   341  }
   342  var q1S8 = [6]float64{
   343  	1.61395369700722909556e+02,  // 0x40642CA6DE5BCDE5
   344  	7.82538599923348465381e+03,  // 0x40BE9162D0D88419
   345  	1.33875336287249578163e+05,  // 0x4100579AB0B75E98
   346  	7.19657723683240939863e+05,  // 0x4125F65372869C19
   347  	6.66601232617776375264e+05,  // 0x412457D27719AD5C
   348  	-2.94490264303834643215e+05, // 0xC111F9690EA5AA18
   349  }
   350  
   351  // for x in [8,4.5454] = 1/[0.125,0.22001]
   352  var q1R5 = [6]float64{
   353  	-2.08979931141764104297e-11, // 0xBDB6FA431AA1A098
   354  	-1.02539050241375426231e-01, // 0xBFBA3FFFCB597FEF
   355  	-8.05644828123936029840e+00, // 0xC0201CE6CA03AD4B
   356  	-1.83669607474888380239e+02, // 0xC066F56D6CA7B9B0
   357  	-1.37319376065508163265e+03, // 0xC09574C66931734F
   358  	-2.61244440453215656817e+03, // 0xC0A468E388FDA79D
   359  }
   360  var q1S5 = [6]float64{
   361  	8.12765501384335777857e+01,  // 0x405451B2FF5A11B2
   362  	1.99179873460485964642e+03,  // 0x409F1F31E77BF839
   363  	1.74684851924908907677e+04,  // 0x40D10F1F0D64CE29
   364  	4.98514270910352279316e+04,  // 0x40E8576DAABAD197
   365  	2.79480751638918118260e+04,  // 0x40DB4B04CF7C364B
   366  	-4.71918354795128470869e+03, // 0xC0B26F2EFCFFA004
   367  }
   368  
   369  // for x in [4.5454,2.8571] = 1/[0.2199,0.35001] ???
   370  var q1R3 = [6]float64{
   371  	-5.07831226461766561369e-09, // 0xBE35CFA9D38FC84F
   372  	-1.02537829820837089745e-01, // 0xBFBA3FEB51AEED54
   373  	-4.61011581139473403113e+00, // 0xC01270C23302D9FF
   374  	-5.78472216562783643212e+01, // 0xC04CEC71C25D16DA
   375  	-2.28244540737631695038e+02, // 0xC06C87D34718D55F
   376  	-2.19210128478909325622e+02, // 0xC06B66B95F5C1BF6
   377  }
   378  var q1S3 = [6]float64{
   379  	4.76651550323729509273e+01,  // 0x4047D523CCD367E4
   380  	6.73865112676699709482e+02,  // 0x40850EEBC031EE3E
   381  	3.38015286679526343505e+03,  // 0x40AA684E448E7C9A
   382  	5.54772909720722782367e+03,  // 0x40B5ABBAA61D54A6
   383  	1.90311919338810798763e+03,  // 0x409DBC7A0DD4DF4B
   384  	-1.35201191444307340817e+02, // 0xC060E670290A311F
   385  }
   386  
   387  // for x in [2.8570,2] = 1/[0.3499,0.5]
   388  var q1R2 = [6]float64{
   389  	-1.78381727510958865572e-07, // 0xBE87F12644C626D2
   390  	-1.02517042607985553460e-01, // 0xBFBA3E8E9148B010
   391  	-2.75220568278187460720e+00, // 0xC006048469BB4EDA
   392  	-1.96636162643703720221e+01, // 0xC033A9E2C168907F
   393  	-4.23253133372830490089e+01, // 0xC04529A3DE104AAA
   394  	-2.13719211703704061733e+01, // 0xC0355F3639CF6E52
   395  }
   396  var q1S2 = [6]float64{
   397  	2.95333629060523854548e+01,  // 0x403D888A78AE64FF
   398  	2.52981549982190529136e+02,  // 0x406F9F68DB821CBA
   399  	7.57502834868645436472e+02,  // 0x4087AC05CE49A0F7
   400  	7.39393205320467245656e+02,  // 0x40871B2548D4C029
   401  	1.55949003336666123687e+02,  // 0x40637E5E3C3ED8D4
   402  	-4.95949898822628210127e+00, // 0xC013D686E71BE86B
   403  }
   404  
   405  func qone(x float64) float64 {
   406  	var p, q *[6]float64
   407  	if x >= 8 {
   408  		p = &q1R8
   409  		q = &q1S8
   410  	} else if x >= 4.5454 {
   411  		p = &q1R5
   412  		q = &q1S5
   413  	} else if x >= 2.8571 {
   414  		p = &q1R3
   415  		q = &q1S3
   416  	} else if x >= 2 {
   417  		p = &q1R2
   418  		q = &q1S2
   419  	}
   420  	z := 1 / (x * x)
   421  	r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5]))))
   422  	s := 1 + z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5])))))
   423  	return (0.375 + r/s) / x
   424  }
   425  

View as plain text