From 1ec91c8d1d871a8aa3243efef1d329d95e447293 Mon Sep 17 00:00:00 2001 From: "Charles L. Dorian" Date: Fri, 26 Mar 2010 14:09:39 -0700 Subject: [PATCH] math: add J1, Y1, Jn and Yn (Bessel functions) Also amend j0.go (variable name conflict, small corrections). R=rsc CC=golang-dev https://golang.org/cl/769041 --- src/pkg/math/Makefile | 2 + src/pkg/math/all_test.go | 202 ++++++++++++++++++- src/pkg/math/j0.go | 72 ++++--- src/pkg/math/j1.go | 426 +++++++++++++++++++++++++++++++++++++++ src/pkg/math/jn.go | 310 ++++++++++++++++++++++++++++ 5 files changed, 970 insertions(+), 42 deletions(-) create mode 100644 src/pkg/math/j1.go create mode 100644 src/pkg/math/jn.go diff --git a/src/pkg/math/Makefile b/src/pkg/math/Makefile index e8c4a22802b..a29245fc33f 100644 --- a/src/pkg/math/Makefile +++ b/src/pkg/math/Makefile @@ -57,6 +57,8 @@ ALLGOFILES=\ hypot.go\ hypot_port.go\ j0.go\ + j1.go\ + jn.go\ logb.go\ lgamma.go\ ldexp.go\ diff --git a/src/pkg/math/all_test.go b/src/pkg/math/all_test.go index 4b0aec6a83f..2f133f143f6 100644 --- a/src/pkg/math/all_test.go +++ b/src/pkg/math/all_test.go @@ -310,6 +310,42 @@ var j0 = []float64{ 3.252650187653420388714693e-01, -8.72218484409407250005360235e-03, } +var j1 = []float64{ + -3.251526395295203422162967e-01, + 1.893581711430515718062564e-01, + -1.3711761352467242914491514e-01, + 3.287486536269617297529617e-01, + 1.3133899188830978473849215e-01, + 3.660243417832986825301766e-01, + -3.4436769271848174665420672e-01, + 4.329481396640773768835036e-01, + 5.8181350531954794639333955e-01, + -2.7030574577733036112996607e-01, +} +var j2 = []float64{ + 5.3837518920137802565192769e-02, + -1.7841678003393207281244667e-01, + 9.521746934916464142495821e-03, + 4.28958355470987397983072e-02, + 2.4115371837854494725492872e-01, + 4.842458532394520316844449e-01, + -3.142145220618633390125946e-02, + 4.720849184745124761189957e-01, + 3.122312022520957042957497e-01, + 7.096213118930231185707277e-02, +} +var jM3 = []float64{ + -3.684042080996403091021151e-01, + 2.8157665936340887268092661e-01, + 4.401005480841948348343589e-04, + 3.629926999056814081597135e-01, + 3.123672198825455192489266e-02, + -2.958805510589623607540455e-01, + -3.2033177696533233403289416e-01, + -2.592737332129663376736604e-01, + -1.0241334641061485092351251e-01, + -2.3762660886100206491674503e-01, +} var lgamma = []fi{ fi{3.146492141244545774319734e+00, 1}, fi{8.003414490659126375852113e+00, 1}, @@ -514,6 +550,42 @@ var y0 = []float64{ 4.8290004112497761007536522e-01, 2.7036697826604756229601611e-01, } +var y1 = []float64{ + 0.15494213737457922210218611, + -0.2165955142081145245075746, + -2.4644949631241895201032829, + 0.1442740489541836405154505, + 0.2215379960518984777080163, + 0.3038800915160754150565448, + 0.0691107642452362383808547, + 0.2380116417809914424860165, + -0.20849492979459761009678934, + 0.0242503179793232308250804, +} +var y2 = []float64{ + 0.3675780219390303613394936, + -0.23034826393250119879267257, + -16.939677983817727205631397, + 0.367653980523052152867791, + -0.0962401471767804440353136, + -0.1923169356184851105200523, + 0.35984072054267882391843766, + -0.2794987252299739821654982, + -0.7113490692587462579757954, + -0.2647831587821263302087457, +} +var yM3 = []float64{ + -0.14035984421094849100895341, + -0.097535139617792072703973, + 242.25775994555580176377379, + -0.1492267014802818619511046, + 0.26148702629155918694500469, + 0.56675383593895176530394248, + -0.206150264009006981070575, + 0.64784284687568332737963658, + 1.3503631555901938037008443, + 0.1461869756579956803341844, +} // arguments and expected results for special cases var vfacoshSC = []float64{ @@ -847,6 +919,24 @@ var j0SC = []float64{ 0, NaN(), } +var j1SC = []float64{ + 0, + 0, + 0, + NaN(), +} +var j2SC = []float64{ + 0, + 0, + 0, + NaN(), +} +var jM3SC = []float64{ + 0, + 0, + 0, + NaN(), +} var vflgammaSC = []float64{ Inf(-1), @@ -1042,6 +1132,24 @@ var y0SC = []float64{ 0, NaN(), } +var y1SC = []float64{ + NaN(), + Inf(-1), + 0, + NaN(), +} +var y2SC = []float64{ + NaN(), + Inf(-1), + 0, + NaN(), +} +var yM3SC = []float64{ + NaN(), + Inf(1), + 0, + NaN(), +} func tolerance(a, b, e float64) bool { d := a - b @@ -1065,10 +1173,6 @@ func alike(a, b float64) bool { switch { case IsNaN(a) && IsNaN(b): return true - case IsInf(a, 1) && IsInf(b, 1): - return true - case IsInf(a, -1) && IsInf(b, -1): - return true case a == b: return true } @@ -1409,6 +1513,38 @@ func TestJ0(t *testing.T) { } } +func TestJ1(t *testing.T) { + for i := 0; i < len(vf); i++ { + if f := J1(vf[i]); !close(j1[i], f) { + t.Errorf("J1(%g) = %g, want %g\n", vf[i], f, j1[i]) + } + } + for i := 0; i < len(vfj0SC); i++ { + if f := J1(vfj0SC[i]); !alike(j1SC[i], f) { + t.Errorf("J1(%g) = %g, want %g\n", vfj0SC[i], f, j1SC[i]) + } + } +} + +func TestJn(t *testing.T) { + for i := 0; i < len(vf); i++ { + if f := Jn(2, vf[i]); !close(j2[i], f) { + t.Errorf("Jn(2, %g) = %g, want %g\n", vf[i], f, j2[i]) + } + if f := Jn(-3, vf[i]); !close(jM3[i], f) { + t.Errorf("Jn(-3, %g) = %g, want %g\n", vf[i], f, jM3[i]) + } + } + for i := 0; i < len(vfj0SC); i++ { + if f := Jn(2, vfj0SC[i]); !alike(j2SC[i], f) { + t.Errorf("Jn(2, %g) = %g, want %g\n", vfj0SC[i], f, j2SC[i]) + } + if f := Jn(-3, vfj0SC[i]); !alike(jM3SC[i], f) { + t.Errorf("Jn(-3, %g) = %g, want %g\n", vfj0SC[i], f, jM3SC[i]) + } + } +} + func TestLdexp(t *testing.T) { for i := 0; i < len(vf); i++ { if f := Ldexp(frexp[i].f, frexp[i].i); !veryclose(vf[i], f) { @@ -1654,6 +1790,40 @@ func TestY0(t *testing.T) { } } +func TestY1(t *testing.T) { + for i := 0; i < len(vf); i++ { + a := Fabs(vf[i]) + if f := Y1(a); !soclose(y1[i], f, 2e-14) { + t.Errorf("Y1(%g) = %g, want %g\n", a, f, y1[i]) + } + } + for i := 0; i < len(vfy0SC); i++ { + if f := Y1(vfy0SC[i]); !alike(y1SC[i], f) { + t.Errorf("Y1(%g) = %g, want %g\n", vfy0SC[i], f, y1SC[i]) + } + } +} + +func TestYn(t *testing.T) { + for i := 0; i < len(vf); i++ { + a := Fabs(vf[i]) + if f := Yn(2, a); !close(y2[i], f) { + t.Errorf("Yn(2, %g) = %g, want %g\n", a, f, y2[i]) + } + if f := Yn(-3, a); !close(yM3[i], f) { + t.Errorf("Yn(-3, %g) = %g, want %g\n", a, f, yM3[i]) + } + } + for i := 0; i < len(vfy0SC); i++ { + if f := Yn(2, vfy0SC[i]); !alike(y2SC[i], f) { + t.Errorf("Yn(2, %g) = %g, want %g\n", vfy0SC[i], f, y2SC[i]) + } + if f := Yn(-3, vfy0SC[i]); !alike(yM3SC[i], f) { + t.Errorf("Yn(-3, %g) = %g, want %g\n", vfy0SC[i], f, yM3SC[i]) + } + } +} + // Check that math functions of high angle values // return similar results to low angle values func TestLargeCos(t *testing.T) { @@ -1896,6 +2066,18 @@ func BenchmarkJ0(b *testing.B) { } } +func BenchmarkJ1(b *testing.B) { + for i := 0; i < b.N; i++ { + J1(2.5) + } +} + +func BenchmarkJn(b *testing.B) { + for i := 0; i < b.N; i++ { + Jn(2, 2.5) + } +} + func BenchmarkLdexp(b *testing.B) { for i := 0; i < b.N; i++ { Ldexp(.5, 2) @@ -2020,3 +2202,15 @@ func BenchmarkY0(b *testing.B) { Y0(2.5) } } + +func BenchmarkY1(b *testing.B) { + for i := 0; i < b.N; i++ { + Y1(2.5) + } +} + +func BenchmarkYn(b *testing.B) { + for i := 0; i < b.N; i++ { + Yn(2, 2.5) + } +} diff --git a/src/pkg/math/j0.go b/src/pkg/math/j0.go index 8f0b7fdb1e0..8a6db3bf8b8 100644 --- a/src/pkg/math/j0.go +++ b/src/pkg/math/j0.go @@ -70,9 +70,8 @@ package math // J0 returns the order-zero Bessel function of the first kind. // // Special cases are: -// J0(Inf) = 0 +// J0(±Inf) = 0 // J0(0) = 1 -// J0(-Inf) = 0 // J0(NaN) = NaN func J0(x float64) float64 { const ( @@ -178,15 +177,12 @@ func Y0(x float64) float64 { switch { case x < 0 || x != x: // x < 0 || IsNaN(x): return NaN() - case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0): + case x > MaxFloat64: // IsInf(x, 1): return 0 case x == 0: return Inf(-1) } - if x < 0 { - x = -x - } if x >= 2 { // |x| >= 2.0 // y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0)) @@ -245,7 +241,7 @@ func Y0(x float64) float64 { // | pzero(x)-1-R/S | <= 2 ** ( -60.26) // for x in [inf, 8]=1/[0,0.125] -var pR8 = [6]float64{ +var p0R8 = [6]float64{ 0.00000000000000000000e+00, // 0x0000000000000000 -7.03124999999900357484e-02, // 0xBFB1FFFFFFFFFD32 -8.08167041275349795626e+00, // 0xC02029D0B44FA779 @@ -253,7 +249,7 @@ var pR8 = [6]float64{ -2.48521641009428822144e+03, // 0xC0A36A6ECD4DCAFC -5.25304380490729545272e+03, // 0xC0B4850B36CC643D } -var pS8 = [5]float64{ +var p0S8 = [5]float64{ 1.16534364619668181717e+02, // 0x405D223307A96751 3.83374475364121826715e+03, // 0x40ADF37D50596938 4.05978572648472545552e+04, // 0x40E3D2BB6EB6B05F @@ -262,7 +258,7 @@ var pS8 = [5]float64{ } // for x in [8,4.5454]=1/[0.125,0.22001] -var pR5 = [6]float64{ +var p0R5 = [6]float64{ -1.14125464691894502584e-11, // 0xBDA918B147E495CC -7.03124940873599280078e-02, // 0xBFB1FFFFE69AFBC6 -4.15961064470587782438e+00, // 0xC010A370F90C6BBF @@ -270,7 +266,7 @@ var pR5 = [6]float64{ -3.31231299649172967747e+02, // 0xC074B3B36742CC63 -3.46433388365604912451e+02, // 0xC075A6EF28A38BD7 } -var pS5 = [5]float64{ +var p0S5 = [5]float64{ 6.07539382692300335975e+01, // 0x404E60810C98C5DE 1.05125230595704579173e+03, // 0x40906D025C7E2864 5.97897094333855784498e+03, // 0x40B75AF88FBE1D60 @@ -279,7 +275,7 @@ var pS5 = [5]float64{ } // for x in [4.547,2.8571]=1/[0.2199,0.35001] -var pR3 = [6]float64{ +var p0R3 = [6]float64{ -2.54704601771951915620e-09, // 0xBE25E1036FE1AA86 -7.03119616381481654654e-02, // 0xBFB1FFF6F7C0E24B -2.40903221549529611423e+00, // 0xC00345B2AEA48074 @@ -287,7 +283,7 @@ var pR3 = [6]float64{ -5.80791704701737572236e+01, // 0xC04D0A22420A1A45 -3.14479470594888503854e+01, // 0xC03F72ACA892D80F } -var pS3 = [5]float64{ +var p0S3 = [5]float64{ 3.58560338055209726349e+01, // 0x4041ED9284077DD3 3.61513983050303863820e+02, // 0x40769839464A7C0E 1.19360783792111533330e+03, // 0x4092A66E6D1061D6 @@ -296,7 +292,7 @@ var pS3 = [5]float64{ } // for x in [2.8570,2]=1/[0.3499,0.5] -var pR2 = [6]float64{ +var p0R2 = [6]float64{ -8.87534333032526411254e-08, // 0xBE77D316E927026D -7.03030995483624743247e-02, // 0xBFB1FF62495E1E42 -1.45073846780952986357e+00, // 0xBFF736398A24A843 @@ -304,7 +300,7 @@ var pR2 = [6]float64{ -1.11931668860356747786e+01, // 0xC02662E6C5246303 -3.23364579351335335033e+00, // 0xC009DE81AF8FE70F } -var pS2 = [5]float64{ +var p0S2 = [5]float64{ 2.22202997532088808441e+01, // 0x40363865908B5959 1.36206794218215208048e+02, // 0x4061069E0EE8878F 2.70470278658083486789e+02, // 0x4070E78642EA079B @@ -316,17 +312,17 @@ func pzero(x float64) float64 { var p [6]float64 var q [5]float64 if x >= 8 { - p = pR8 - q = pS8 + p = p0R8 + q = p0S8 } else if x >= 4.5454 { - p = pR5 - q = pS5 + p = p0R5 + q = p0S5 } else if x >= 2.8571 { - p = pR3 - q = pS3 + p = p0R3 + q = p0S3 } else if x >= 2 { - p = pR2 - q = pS2 + p = p0R2 + q = p0S2 } z := 1 / (x * x) r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))) @@ -344,7 +340,7 @@ func pzero(x float64) float64 { // | qzero(x)/s +1.25-R/S | <= 2 ** ( -61.22) // for x in [inf, 8]=1/[0,0.125] -var qR8 = [6]float64{ +var q0R8 = [6]float64{ 0.00000000000000000000e+00, // 0x0000000000000000 7.32421874999935051953e-02, // 0x3FB2BFFFFFFFFE2C 1.17682064682252693899e+01, // 0x402789525BB334D6 @@ -352,7 +348,7 @@ var qR8 = [6]float64{ 8.85919720756468632317e+03, // 0x40C14D993E18F46D 3.70146267776887834771e+04, // 0x40E212D40E901566 } -var qS8 = [6]float64{ +var q0S8 = [6]float64{ 1.63776026895689824414e+02, // 0x406478D5365B39BC 8.09834494656449805916e+03, // 0x40BFA2584E6B0563 1.42538291419120476348e+05, // 0x4101665254D38C3F @@ -362,7 +358,7 @@ var qS8 = [6]float64{ } // for x in [8,4.5454]=1/[0.125,0.22001] -var qR5 = [6]float64{ +var q0R5 = [6]float64{ 1.84085963594515531381e-11, // 0x3DB43D8F29CC8CD9 7.32421766612684765896e-02, // 0x3FB2BFFFD172B04C 5.83563508962056953777e+00, // 0x401757B0B9953DD3 @@ -370,7 +366,7 @@ var qR5 = [6]float64{ 1.02724376596164097464e+03, // 0x40900CF99DC8C481 1.98997785864605384631e+03, // 0x409F17E953C6E3A6 } -var qS5 = [6]float64{ +var q0S5 = [6]float64{ 8.27766102236537761883e+01, // 0x4054B1B3FB5E1543 2.07781416421392987104e+03, // 0x40A03BA0DA21C0CE 1.88472887785718085070e+04, // 0x40D267D27B591E6D @@ -380,7 +376,7 @@ var qS5 = [6]float64{ } // for x in [4.547,2.8571]=1/[0.2199,0.35001] -var qR3 = [6]float64{ +var q0R3 = [6]float64{ 4.37741014089738620906e-09, // 0x3E32CD036ADECB82 7.32411180042911447163e-02, // 0x3FB2BFEE0E8D0842 3.34423137516170720929e+00, // 0x400AC0FC61149CF5 @@ -388,7 +384,7 @@ var qR3 = [6]float64{ 1.70808091340565596283e+02, // 0x406559DBE25EFD1F 1.66733948696651168575e+02, // 0x4064D77C81FA21E0 } -var qS3 = [6]float64{ +var q0S3 = [6]float64{ 4.87588729724587182091e+01, // 0x40486122BFE343A6 7.09689221056606015736e+02, // 0x40862D8386544EB3 3.70414822620111362994e+03, // 0x40ACF04BE44DFC63 @@ -398,7 +394,7 @@ var qS3 = [6]float64{ } // for x in [2.8570,2]=1/[0.3499,0.5] -var qR2 = [6]float64{ +var q0R2 = [6]float64{ 1.50444444886983272379e-07, // 0x3E84313B54F76BDB 7.32234265963079278272e-02, // 0x3FB2BEC53E883E34 1.99819174093815998816e+00, // 0x3FFFF897E727779C @@ -406,7 +402,7 @@ var qR2 = [6]float64{ 3.16662317504781540833e+01, // 0x403FAA8E29FBDC4A 1.62527075710929267416e+01, // 0x403040B171814BB4 } -var qS2 = [6]float64{ +var q0S2 = [6]float64{ 3.03655848355219184498e+01, // 0x403E5D96F7C07AED 2.69348118608049844624e+02, // 0x4070D591E4D14B40 8.44783757595320139444e+02, // 0x408A664522B3BF22 @@ -418,17 +414,17 @@ var qS2 = [6]float64{ func qzero(x float64) float64 { var p, q [6]float64 if x >= 8 { - p = qR8 - q = qS8 + p = q0R8 + q = q0S8 } else if x >= 4.5454 { - p = qR5 - q = qS5 + p = q0R5 + q = q0S5 } else if x >= 2.8571 { - p = qR3 - q = qS3 + p = q0R3 + q = q0S3 } else if x >= 2 { - p = qR2 - q = qS2 + p = q0R2 + q = q0S2 } z := 1 / (x * x) r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))) diff --git a/src/pkg/math/j1.go b/src/pkg/math/j1.go new file mode 100644 index 00000000000..5c7b79914df --- /dev/null +++ b/src/pkg/math/j1.go @@ -0,0 +1,426 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package math + +/* + Bessel function of the first and second kinds of order one. +*/ + +// The original C code and the long comment below are +// from FreeBSD's /usr/src/lib/msun/src/e_j1.c and +// came with this notice. The go code is a simplified +// version of the original 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. +// ==================================================== +// +// __ieee754_j1(x), __ieee754_y1(x) +// Bessel function of the first and second kinds of order one. +// Method -- j1(x): +// 1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ... +// 2. Reduce x to |x| since j1(x)=-j1(-x), and +// for x in (0,2) +// j1(x) = x/2 + x*z*R0/S0, where z = x*x; +// (precision: |j1/x - 1/2 - R0/S0 |<2**-61.51 ) +// for x in (2,inf) +// j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1)) +// y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) +// where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) +// as follow: +// cos(x1) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) +// = 1/sqrt(2) * (sin(x) - cos(x)) +// sin(x1) = 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.) +// +// 3 Special cases +// j1(nan)= nan +// j1(0) = 0 +// j1(inf) = 0 +// +// Method -- y1(x): +// 1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN +// 2. For x<2. +// Since +// y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...) +// therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function. +// We use the following function to approximate y1, +// y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2 +// where for x in [0,2] (abs err less than 2**-65.89) +// U(z) = U0[0] + U0[1]*z + ... + U0[4]*z^4 +// V(z) = 1 + v0[0]*z + ... + v0[4]*z^5 +// Note: For tiny x, 1/x dominate y1 and hence +// y1(tiny) = -2/pi/tiny, (choose tiny<2**-54) +// 3. For x>=2. +// y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) +// where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) +// by method mentioned above. + +// J1 returns the order-one Bessel function of the first kind. +// +// Special cases are: +// J1(±Inf) = 0 +// J1(NaN) = NaN +func J1(x float64) float64 { + const ( + TwoM27 = 1.0 / (1 << 27) // 2**-27 0x3e40000000000000 + Two129 = 1 << 129 // 2**129 0x4800000000000000 + // R0/S0 on [0, 2] + R00 = -6.25000000000000000000e-02 // 0xBFB0000000000000 + R01 = 1.40705666955189706048e-03 // 0x3F570D9F98472C61 + R02 = -1.59955631084035597520e-05 // 0xBEF0C5C6BA169668 + R03 = 4.96727999609584448412e-08 // 0x3E6AAAFA46CA0BD9 + S01 = 1.91537599538363460805e-02 // 0x3F939D0B12637E53 + S02 = 1.85946785588630915560e-04 // 0x3F285F56B9CDF664 + S03 = 1.17718464042623683263e-06 // 0x3EB3BFF8333F8498 + S04 = 5.04636257076217042715e-09 // 0x3E35AC88C97DFF2C + S05 = 1.23542274426137913908e-11 // 0x3DAB2ACFCFB97ED8 + ) + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x != x: // IsNaN(x) + return x + case x < -MaxFloat64 || x > MaxFloat64 || x == 0: // IsInf(x, 0) || x == 0: + return 0 + } + + sign := false + if x < 0 { + x = -x + sign = true + } + if x >= 2 { + s, c := Sincos(x) + ss := -s - c + cc := s - c + + // make sure x+x does not overflow + if x < MaxFloat64/2 { + z := Cos(x + x) + if s*c > 0 { + cc = z / ss + } else { + ss = z / cc + } + } + + // j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x) + // y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x) + + var z float64 + if x > Two129 { + z = (1 / SqrtPi) * cc / Sqrt(x) + } else { + u := pone(x) + v := qone(x) + z = (1 / SqrtPi) * (u*cc - v*ss) / Sqrt(x) + } + if sign { + return -z + } + return z + } + if x < TwoM27 { // |x|<2**-27 + return 0.5 * x // inexact if x!=0 necessary + } + z := x * x + r := z * (R00 + z*(R01+z*(R02+z*R03))) + s := 1.0 + z*(S01+z*(S02+z*(S03+z*(S04+z*S05)))) + r *= x + z = 0.5*x + r/s + if sign { + return -z + } + return z +} + +// Y1 returns the order-one Bessel function of the second kind. +// +// Special cases are: +// Y1(+Inf) = 0 +// Y1(0) = -Inf +// Y1(x < 0) = NaN +// Y1(NaN) = NaN +func Y1(x float64) float64 { + const ( + TwoM54 = 1.0 / (1 << 54) // 2**-54 0x3c90000000000000 + Two129 = 1 << 129 // 2**129 0x4800000000000000 + U00 = -1.96057090646238940668e-01 // 0xBFC91866143CBC8A + U01 = 5.04438716639811282616e-02 // 0x3FA9D3C776292CD1 + U02 = -1.91256895875763547298e-03 // 0xBF5F55E54844F50F + U03 = 2.35252600561610495928e-05 // 0x3EF8AB038FA6B88E + U04 = -9.19099158039878874504e-08 // 0xBE78AC00569105B8 + V00 = 1.99167318236649903973e-02 // 0x3F94650D3F4DA9F0 + V01 = 2.02552581025135171496e-04 // 0x3F2A8C896C257764 + V02 = 1.35608801097516229404e-06 // 0x3EB6C05A894E8CA6 + V03 = 6.22741452364621501295e-09 // 0x3E3ABF1D5BA69A86 + V04 = 1.66559246207992079114e-11 // 0x3DB25039DACA772A + ) + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x < 0 || x != x: // x < 0 || IsNaN(x): + return NaN() + case x > MaxFloat64: // IsInf(x, 1): + return 0 + case x == 0: + return Inf(-1) + } + + if x >= 2 { + s, c := Sincos(x) + ss := -s - c + cc := s - c + + // make sure x+x does not overflow + if x < MaxFloat64/2 { + z := Cos(x + x) + if s*c > 0 { + cc = z / ss + } else { + ss = z / cc + } + } + // y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0)) + // where x0 = x-3pi/4 + // Better formula: + // cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) + // = 1/sqrt(2) * (sin(x) - cos(x)) + // sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + // = -1/sqrt(2) * (cos(x) + sin(x)) + // To avoid cancellation, use + // sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + // to compute the worse one. + + var z float64 + if x > Two129 { + z = (1 / SqrtPi) * ss / Sqrt(x) + } else { + u := pone(x) + v := qone(x) + z = (1 / SqrtPi) * (u*ss + v*cc) / Sqrt(x) + } + return z + } + if x <= TwoM54 { // x < 2**-54 + return -(2 / Pi) / x + } + z := x * x + u := U00 + z*(U01+z*(U02+z*(U03+z*U04))) + v := 1 + z*(V00+z*(V01+z*(V02+z*(V03+z*V04)))) + return x*(u/v) + (2/Pi)*(J1(x)*Log(x)-1/x) +} + +// For x >= 8, the asymptotic expansions of pone is +// 1 + 15/128 s^2 - 4725/2^15 s^4 - ..., where s = 1/x. +// We approximate pone by +// pone(x) = 1 + (R/S) +// where R = pr0 + pr1*s^2 + pr2*s^4 + ... + pr5*s^10 +// S = 1 + ps0*s^2 + ... + ps4*s^10 +// and +// | pone(x)-1-R/S | <= 2**(-60.06) + +// for x in [inf, 8]=1/[0,0.125] +var p1R8 = [6]float64{ + 0.00000000000000000000e+00, // 0x0000000000000000 + 1.17187499999988647970e-01, // 0x3FBDFFFFFFFFFCCE + 1.32394806593073575129e+01, // 0x402A7A9D357F7FCE + 4.12051854307378562225e+02, // 0x4079C0D4652EA590 + 3.87474538913960532227e+03, // 0x40AE457DA3A532CC + 7.91447954031891731574e+03, // 0x40BEEA7AC32782DD +} +var p1S8 = [5]float64{ + 1.14207370375678408436e+02, // 0x405C8D458E656CAC + 3.65093083420853463394e+03, // 0x40AC85DC964D274F + 3.69562060269033463555e+04, // 0x40E20B8697C5BB7F + 9.76027935934950801311e+04, // 0x40F7D42CB28F17BB + 3.08042720627888811578e+04, // 0x40DE1511697A0B2D +} + +// for x in [8,4.5454] = 1/[0.125,0.22001] +var p1R5 = [6]float64{ + 1.31990519556243522749e-11, // 0x3DAD0667DAE1CA7D + 1.17187493190614097638e-01, // 0x3FBDFFFFE2C10043 + 6.80275127868432871736e+00, // 0x401B36046E6315E3 + 1.08308182990189109773e+02, // 0x405B13B9452602ED + 5.17636139533199752805e+02, // 0x40802D16D052D649 + 5.28715201363337541807e+02, // 0x408085B8BB7E0CB7 +} +var p1S5 = [5]float64{ + 5.92805987221131331921e+01, // 0x404DA3EAA8AF633D + 9.91401418733614377743e+02, // 0x408EFB361B066701 + 5.35326695291487976647e+03, // 0x40B4E9445706B6FB + 7.84469031749551231769e+03, // 0x40BEA4B0B8A5BB15 + 1.50404688810361062679e+03, // 0x40978030036F5E51 +} + +// for x in[4.5453,2.8571] = 1/[0.2199,0.35001] +var p1R3 = [6]float64{ + 3.02503916137373618024e-09, // 0x3E29FC21A7AD9EDD + 1.17186865567253592491e-01, // 0x3FBDFFF55B21D17B + 3.93297750033315640650e+00, // 0x400F76BCE85EAD8A + 3.51194035591636932736e+01, // 0x40418F489DA6D129 + 9.10550110750781271918e+01, // 0x4056C3854D2C1837 + 4.85590685197364919645e+01, // 0x4048478F8EA83EE5 +} +var p1S3 = [5]float64{ + 3.47913095001251519989e+01, // 0x40416549A134069C + 3.36762458747825746741e+02, // 0x40750C3307F1A75F + 1.04687139975775130551e+03, // 0x40905B7C5037D523 + 8.90811346398256432622e+02, // 0x408BD67DA32E31E9 + 1.03787932439639277504e+02, // 0x4059F26D7C2EED53 +} + +// for x in [2.8570,2] = 1/[0.3499,0.5] +var p1R2 = [6]float64{ + 1.07710830106873743082e-07, // 0x3E7CE9D4F65544F4 + 1.17176219462683348094e-01, // 0x3FBDFF42BE760D83 + 2.36851496667608785174e+00, // 0x4002F2B7F98FAEC0 + 1.22426109148261232917e+01, // 0x40287C377F71A964 + 1.76939711271687727390e+01, // 0x4031B1A8177F8EE2 + 5.07352312588818499250e+00, // 0x40144B49A574C1FE +} +var p1S2 = [5]float64{ + 2.14364859363821409488e+01, // 0x40356FBD8AD5ECDC + 1.25290227168402751090e+02, // 0x405F529314F92CD5 + 2.32276469057162813669e+02, // 0x406D08D8D5A2DBD9 + 1.17679373287147100768e+02, // 0x405D6B7ADA1884A9 + 8.36463893371618283368e+00, // 0x4020BAB1F44E5192 +} + +func pone(x float64) float64 { + var p [6]float64 + var q [5]float64 + if x >= 8 { + p = p1R8 + q = p1S8 + } else if x >= 4.5454 { + p = p1R5 + q = p1S5 + } else if x >= 2.8571 { + p = p1R3 + q = p1S3 + } else if x >= 2 { + p = p1R2 + q = p1S2 + } + z := 1 / (x * x) + r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))) + s := 1.0 + z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*q[4])))) + return 1 + r/s +} + +// For x >= 8, the asymptotic expansions of qone is +// 3/8 s - 105/1024 s^3 - ..., where s = 1/x. +// We approximate qone by +// qone(x) = s*(0.375 + (R/S)) +// where R = qr1*s^2 + qr2*s^4 + ... + qr5*s^10 +// S = 1 + qs1*s^2 + ... + qs6*s^12 +// and +// | qone(x)/s -0.375-R/S | <= 2**(-61.13) + +// for x in [inf, 8] = 1/[0,0.125] +var q1R8 = [6]float64{ + 0.00000000000000000000e+00, // 0x0000000000000000 + -1.02539062499992714161e-01, // 0xBFBA3FFFFFFFFDF3 + -1.62717534544589987888e+01, // 0xC0304591A26779F7 + -7.59601722513950107896e+02, // 0xC087BCD053E4B576 + -1.18498066702429587167e+04, // 0xC0C724E740F87415 + -4.84385124285750353010e+04, // 0xC0E7A6D065D09C6A +} +var q1S8 = [6]float64{ + 1.61395369700722909556e+02, // 0x40642CA6DE5BCDE5 + 7.82538599923348465381e+03, // 0x40BE9162D0D88419 + 1.33875336287249578163e+05, // 0x4100579AB0B75E98 + 7.19657723683240939863e+05, // 0x4125F65372869C19 + 6.66601232617776375264e+05, // 0x412457D27719AD5C + -2.94490264303834643215e+05, // 0xC111F9690EA5AA18 +} + +// for x in [8,4.5454] = 1/[0.125,0.22001] +var q1R5 = [6]float64{ + -2.08979931141764104297e-11, // 0xBDB6FA431AA1A098 + -1.02539050241375426231e-01, // 0xBFBA3FFFCB597FEF + -8.05644828123936029840e+00, // 0xC0201CE6CA03AD4B + -1.83669607474888380239e+02, // 0xC066F56D6CA7B9B0 + -1.37319376065508163265e+03, // 0xC09574C66931734F + -2.61244440453215656817e+03, // 0xC0A468E388FDA79D +} +var q1S5 = [6]float64{ + 8.12765501384335777857e+01, // 0x405451B2FF5A11B2 + 1.99179873460485964642e+03, // 0x409F1F31E77BF839 + 1.74684851924908907677e+04, // 0x40D10F1F0D64CE29 + 4.98514270910352279316e+04, // 0x40E8576DAABAD197 + 2.79480751638918118260e+04, // 0x40DB4B04CF7C364B + -4.71918354795128470869e+03, // 0xC0B26F2EFCFFA004 +} + +// for x in [4.5454,2.8571] = 1/[0.2199,0.35001] ??? +var q1R3 = [6]float64{ + -5.07831226461766561369e-09, // 0xBE35CFA9D38FC84F + -1.02537829820837089745e-01, // 0xBFBA3FEB51AEED54 + -4.61011581139473403113e+00, // 0xC01270C23302D9FF + -5.78472216562783643212e+01, // 0xC04CEC71C25D16DA + -2.28244540737631695038e+02, // 0xC06C87D34718D55F + -2.19210128478909325622e+02, // 0xC06B66B95F5C1BF6 +} +var q1S3 = [6]float64{ + 4.76651550323729509273e+01, // 0x4047D523CCD367E4 + 6.73865112676699709482e+02, // 0x40850EEBC031EE3E + 3.38015286679526343505e+03, // 0x40AA684E448E7C9A + 5.54772909720722782367e+03, // 0x40B5ABBAA61D54A6 + 1.90311919338810798763e+03, // 0x409DBC7A0DD4DF4B + -1.35201191444307340817e+02, // 0xC060E670290A311F +} + +// for x in [2.8570,2] = 1/[0.3499,0.5] +var q1R2 = [6]float64{ + -1.78381727510958865572e-07, // 0xBE87F12644C626D2 + -1.02517042607985553460e-01, // 0xBFBA3E8E9148B010 + -2.75220568278187460720e+00, // 0xC006048469BB4EDA + -1.96636162643703720221e+01, // 0xC033A9E2C168907F + -4.23253133372830490089e+01, // 0xC04529A3DE104AAA + -2.13719211703704061733e+01, // 0xC0355F3639CF6E52 +} +var q1S2 = [6]float64{ + 2.95333629060523854548e+01, // 0x403D888A78AE64FF + 2.52981549982190529136e+02, // 0x406F9F68DB821CBA + 7.57502834868645436472e+02, // 0x4087AC05CE49A0F7 + 7.39393205320467245656e+02, // 0x40871B2548D4C029 + 1.55949003336666123687e+02, // 0x40637E5E3C3ED8D4 + -4.95949898822628210127e+00, // 0xC013D686E71BE86B +} + +func qone(x float64) float64 { + var p, q [6]float64 + if x >= 8 { + p = q1R8 + q = q1S8 + } else if x >= 4.5454 { + p = q1R5 + q = q1S5 + } else if x >= 2.8571 { + p = q1R3 + q = q1S3 + } else if x >= 2 { + p = q1R2 + q = q1S2 + } + z := 1 / (x * x) + r := p[0] + z*(p[1]+z*(p[2]+z*(p[3]+z*(p[4]+z*p[5])))) + s := 1 + z*(q[0]+z*(q[1]+z*(q[2]+z*(q[3]+z*(q[4]+z*q[5]))))) + return (0.375 + r/s) / x +} diff --git a/src/pkg/math/jn.go b/src/pkg/math/jn.go new file mode 100644 index 00000000000..ecd7ab68dfa --- /dev/null +++ b/src/pkg/math/jn.go @@ -0,0 +1,310 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package math + +/* + Bessel function of the first and second kinds of order n. +*/ + +// The original C code and the long comment below are +// from FreeBSD's /usr/src/lib/msun/src/e_jn.c and +// came with this notice. The go code is a simplified +// version of the original 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. +// ==================================================== +// +// __ieee754_jn(n, x), __ieee754_yn(n, x) +// floating point Bessel's function of the 1st and 2nd kind +// of order n +// +// Special cases: +// y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal; +// y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. +// Note 2. About jn(n,x), yn(n,x) +// For n=0, j0(x) is called, +// for n=1, j1(x) is called, +// for nx, a continued fraction approximation to +// j(n,x)/j(n-1,x) is evaluated and then backward +// recursion is used starting from a supposed value +// for j(n,x). The resulting value of j(0,x) is +// compared with the actual value to correct the +// supposed value of j(n,x). +// +// yn(n,x) is similar in all respects, except +// that forward recursion is used for all +// values of n>1. + +// Jn returns the order-n Bessel function of the first kind. +// +// Special cases are: +// Jn(n, ±Inf) = 0 +// Jn(n, NaN) = NaN +func Jn(n int, x float64) float64 { + const ( + TwoM29 = 1.0 / (1 << 29) // 2**-29 0x3e10000000000000 + Two302 = 1 << 302 // 2**302 0x52D0000000000000 + ) + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x != x: // IsNaN(x) + return x + case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0): + return 0 + } + // J(-n, x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) + // Thus, J(-n, x) = J(n, -x) + + if n == 0 { + return J0(x) + } + if x == 0 { + return 0 + } + if n < 0 { + n, x = -n, -x + } + if n == 1 { + return J1(x) + } + sign := false + if x < 0 { + x = -x + if n&1 == 1 { + sign = true // odd n and negative x + } + } + var b float64 + if float64(n) <= x { + // Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) + if x >= Two302 { // x > 2**302 + + // (x >> n**2) + // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) + // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) + // Let s=sin(x), c=cos(x), + // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then + // + // n sin(xn)*sqt2 cos(xn)*sqt2 + // ---------------------------------- + // 0 s-c c+s + // 1 -s-c -c+s + // 2 -s+c -c-s + // 3 s+c c-s + + var temp float64 + switch n & 3 { + case 0: + temp = Cos(x) + Sin(x) + case 1: + temp = -Cos(x) + Sin(x) + case 2: + temp = -Cos(x) - Sin(x) + case 3: + temp = Cos(x) - Sin(x) + } + b = (1 / SqrtPi) * temp / Sqrt(x) + } else { + b = J1(x) + for i, a := 1, J0(x); i < n; i++ { + a, b = b, b*(float64(i+i)/x)-a // avoid underflow + } + } + } else { + if x < TwoM29 { // x < 2**-29 + // x is tiny, return the first Taylor expansion of J(n,x) + // J(n,x) = 1/n!*(x/2)^n - ... + + if n > 33 { // underflow + b = 0 + } else { + temp := x * 0.5 + b = temp + a := float64(1) + for i := 2; i <= n; i++ { + a *= float64(i) // a = n! + b *= temp // b = (x/2)^n + } + b /= a + } + } else { + // use backward recurrence + // x x^2 x^2 + // J(n,x)/J(n-1,x) = ---- ------ ------ ..... + // 2n - 2(n+1) - 2(n+2) + // + // 1 1 1 + // (for large x) = ---- ------ ------ ..... + // 2n 2(n+1) 2(n+2) + // -- - ------ - ------ - + // x x x + // + // Let w = 2n/x and h=2/x, then the above quotient + // is equal to the continued fraction: + // 1 + // = ----------------------- + // 1 + // w - ----------------- + // 1 + // w+h - --------- + // w+2h - ... + // + // To determine how many terms needed, let + // Q(0) = w, Q(1) = w(w+h) - 1, + // Q(k) = (w+k*h)*Q(k-1) - Q(k-2), + // When Q(k) > 1e4 good for single + // When Q(k) > 1e9 good for double + // When Q(k) > 1e17 good for quadruple + + // determine k + w := float64(n+n) / x + h := 2 / x + q0 := w + z := w + h + q1 := w*z - 1 + k := 1 + for q1 < 1e9 { + k += 1 + z += h + q0, q1 = q1, z*q1-q0 + } + m := n + n + t := float64(0) + for i := 2 * (n + k); i >= m; i -= 2 { + t = 1 / (float64(i)/x - t) + } + a := t + b = 1 + // estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) + // Hence, if n*(log(2n/x)) > ... + // single 8.8722839355e+01 + // double 7.09782712893383973096e+02 + // long double 1.1356523406294143949491931077970765006170e+04 + // then recurrent value may overflow and the result is + // likely underflow to zero + + tmp := float64(n) + v := 2 / x + tmp = tmp * Log(Fabs(v*tmp)) + if tmp < 7.09782712893383973096e+02 { + for i := n - 1; i > 0; i-- { + di := float64(i + i) + a, b = b, b*di/x-a + di -= 2 + } + } else { + for i := n - 1; i > 0; i-- { + di := float64(i + i) + a, b = b, b*di/x-a + di -= 2 + // scale b to avoid spurious overflow + if b > 1e100 { + a /= b + t /= b + b = 1 + } + } + } + b = t * J0(x) / b + } + } + if sign { + return -b + } + return b +} + +// Yn returns the order-n Bessel function of the second kind. +// +// Special cases are: +// Yn(n, +Inf) = 0 +// Yn(n > 0, 0) = -Inf +// Yn(n < 0, 0) = +Inf if n is odd, -Inf if n is even +// Y1(n, x < 0) = NaN +// Y1(n, NaN) = NaN +func Yn(n int, x float64) float64 { + const Two302 = 1 << 302 // 2**302 0x52D0000000000000 + // TODO(rsc): Remove manual inlining of IsNaN, IsInf + // when compiler does it for us + // special cases + switch { + case x < 0 || x != x: // x < 0 || IsNaN(x): + return NaN() + case x > MaxFloat64: // IsInf(x, 1) + return 0 + } + + if n == 0 { + return Y0(x) + } + if x == 0 { + if n < 0 && n&1 == 1 { + return Inf(1) + } + return Inf(-1) + } + sign := false + if n < 0 { + n = -n + if n&1 == 1 { + sign = true // sign true if n < 0 && |n| odd + } + } + if n == 1 { + if sign { + return -Y1(x) + } + return Y1(x) + } + var b float64 + if x >= Two302 { // x > 2**302 + // (x >> n**2) + // Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) + // Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) + // Let s=sin(x), c=cos(x), + // xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then + // + // n sin(xn)*sqt2 cos(xn)*sqt2 + // ---------------------------------- + // 0 s-c c+s + // 1 -s-c -c+s + // 2 -s+c -c-s + // 3 s+c c-s + + var temp float64 + switch n & 3 { + case 0: + temp = Sin(x) - Cos(x) + case 1: + temp = -Sin(x) - Cos(x) + case 2: + temp = -Sin(x) + Cos(x) + case 3: + temp = Sin(x) + Cos(x) + } + b = (1 / SqrtPi) * temp / Sqrt(x) + } else { + a := Y0(x) + b = Y1(x) + // quit if b is -inf + for i := 1; i < n && b >= -MaxFloat64; i++ { // for i := 1; i < n && !IsInf(b, -1); i++ { + a, b = b, (float64(i+i)/x)*b-a + } + } + if sign { + return -b + } + return b +}