github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mathext/ell_carlson.go (about) 1 // Copyright ©2017 The Gonum 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 mathext 6 7 import ( 8 "math" 9 ) 10 11 // EllipticRF computes the symmetric elliptic integral R_F(x,y,z): 12 // R_F(x,y,z) = (1/2)\int_{0}^{\infty}{1/s(t)} dt, 13 // s(t) = \sqrt{(t+x)(t+y)(t+z)}. 14 // 15 // The arguments x, y, z must satisfy the following conditions, otherwise the function returns math.NaN(): 16 // 0 ≤ x,y,z ≤ upper, 17 // lower ≤ x+y,y+z,z+x, 18 // where: 19 // lower = 5/(2^1022) = 1.112536929253601e-307, 20 // upper = (2^1022)/5 = 8.988465674311580e+306. 21 // 22 // The definition of the symmetric elliptic integral R_F can be found in NIST 23 // Digital Library of Mathematical Functions (http://dlmf.nist.gov/19.16.E1). 24 func EllipticRF(x, y, z float64) float64 { 25 // The original Fortran code was published as Algorithm 577 in ACM TOMS (http://doi.org/10.1145/355958.355970). 26 // This code is also available as a part of SLATEC Common Mathematical Library (http://netlib.org/slatec/index.html). Later, Carlson described 27 // an improved version in http://dx.doi.org/10.1007/BF02198293 (also available at https://arxiv.org/abs/math/9409227). 28 const ( 29 lower = 5.0 / (1 << 256) / (1 << 256) / (1 << 256) / (1 << 254) // 5*2^-1022 30 upper = 1 / lower 31 tol = 1.2674918778210762260320167734407048051023273568443e-02 // (3ε)^(1/8) 32 ) 33 if x < 0 || y < 0 || z < 0 || math.IsNaN(x) || math.IsNaN(y) || math.IsNaN(z) { 34 return math.NaN() 35 } 36 if upper < x || upper < y || upper < z { 37 return math.NaN() 38 } 39 if x+y < lower || y+z < lower || z+x < lower { 40 return math.NaN() 41 } 42 43 A0 := (x + y + z) / 3 44 An := A0 45 Q := math.Max(math.Max(math.Abs(A0-x), math.Abs(A0-y)), math.Abs(A0-z)) / tol 46 xn, yn, zn := x, y, z 47 mul := 1.0 48 49 for Q >= mul*math.Abs(An) { 50 xnsqrt, ynsqrt, znsqrt := math.Sqrt(xn), math.Sqrt(yn), math.Sqrt(zn) 51 lambda := xnsqrt*ynsqrt + ynsqrt*znsqrt + znsqrt*xnsqrt 52 An = (An + lambda) * 0.25 53 xn = (xn + lambda) * 0.25 54 yn = (yn + lambda) * 0.25 55 zn = (zn + lambda) * 0.25 56 mul *= 4 57 } 58 59 X := (A0 - x) / (mul * An) 60 Y := (A0 - y) / (mul * An) 61 Z := -(X + Y) 62 E2 := X*Y - Z*Z 63 E3 := X * Y * Z 64 65 // http://dlmf.nist.gov/19.36.E1 66 return (1 - 1/10.0*E2 + 1/14.0*E3 + 1/24.0*E2*E2 - 3/44.0*E2*E3 - 5/208.0*E2*E2*E2 + 3/104.0*E3*E3 + 1/16.0*E2*E2*E3) / math.Sqrt(An) 67 } 68 69 // EllipticRD computes the symmetric elliptic integral R_D(x,y,z): 70 // R_D(x,y,z) = (1/2)\int_{0}^{\infty}{1/(s(t)(t+z))} dt, 71 // s(t) = \sqrt{(t+x)(t+y)(t+z)}. 72 // 73 // The arguments x, y, z must satisfy the following conditions, otherwise the function returns math.NaN(): 74 // 0 ≤ x,y ≤ upper, 75 // lower ≤ z ≤ upper, 76 // lower ≤ x+y, 77 // where: 78 // lower = (5/(2^1022))^(1/3) = 4.809554074311679e-103, 79 // upper = ((2^1022)/5)^(1/3) = 2.079194837087086e+102. 80 // 81 // The definition of the symmetric elliptic integral R_D can be found in NIST 82 // Digital Library of Mathematical Functions (http://dlmf.nist.gov/19.16.E5). 83 func EllipticRD(x, y, z float64) float64 { 84 // The original Fortran code was published as Algorithm 577 in ACM TOMS (http://doi.org/10.1145/355958.355970). 85 // This code is also available as a part of SLATEC Common Mathematical Library (http://netlib.org/slatec/index.html). Later, Carlson described 86 // an improved version in http://dx.doi.org/10.1007/BF02198293 (also available at https://arxiv.org/abs/math/9409227). 87 const ( 88 lower = 4.8095540743116787026618007863123676393525016818363e-103 // (5*2^-1022)^(1/3) 89 upper = 1 / lower 90 tol = 9.0351169339315770474760122547068324993857488849382e-03 // (ε/5)^(1/8) 91 ) 92 if x < 0 || y < 0 || math.IsNaN(x) || math.IsNaN(y) || math.IsNaN(z) { 93 return math.NaN() 94 } 95 if upper < x || upper < y || upper < z { 96 return math.NaN() 97 } 98 if x+y < lower || z < lower { 99 return math.NaN() 100 } 101 102 A0 := (x + y + 3*z) / 5 103 An := A0 104 Q := math.Max(math.Max(math.Abs(A0-x), math.Abs(A0-y)), math.Abs(A0-z)) / tol 105 xn, yn, zn := x, y, z 106 mul, s := 1.0, 0.0 107 108 for Q >= mul*math.Abs(An) { 109 xnsqrt, ynsqrt, znsqrt := math.Sqrt(xn), math.Sqrt(yn), math.Sqrt(zn) 110 lambda := xnsqrt*ynsqrt + ynsqrt*znsqrt + znsqrt*xnsqrt 111 s += 1 / (mul * znsqrt * (zn + lambda)) 112 An = (An + lambda) * 0.25 113 xn = (xn + lambda) * 0.25 114 yn = (yn + lambda) * 0.25 115 zn = (zn + lambda) * 0.25 116 mul *= 4 117 } 118 119 X := (A0 - x) / (mul * An) 120 Y := (A0 - y) / (mul * An) 121 Z := -(X + Y) / 3 122 E2 := X*Y - 6*Z*Z 123 E3 := (3*X*Y - 8*Z*Z) * Z 124 E4 := 3 * (X*Y - Z*Z) * Z * Z 125 E5 := X * Y * Z * Z * Z 126 127 // http://dlmf.nist.gov/19.36.E2 128 return (1-3/14.0*E2+1/6.0*E3+9/88.0*E2*E2-3/22.0*E4-9/52.0*E2*E3+3/26.0*E5-1/16.0*E2*E2*E2+3/40.0*E3*E3+3/20.0*E2*E4+45/272.0*E2*E2*E3-9/68.0*(E3*E4+E2*E5))/(mul*An*math.Sqrt(An)) + 3*s 129 } 130 131 // EllipticF computes the Legendre's elliptic integral of the 1st kind F(phi,m), 0≤m<1: 132 // F(\phi,m) = \int_{0}^{\phi} 1 / \sqrt{1-m\sin^2(\theta)} d\theta 133 // 134 // Legendre's elliptic integrals can be expressed as symmetric elliptic integrals, in this case: 135 // F(\phi,m) = \sin\phi R_F(\cos^2\phi,1-m\sin^2\phi,1) 136 // 137 // The definition of F(phi,k) where k=sqrt(m) can be found in NIST Digital Library of Mathematical 138 // Functions (http://dlmf.nist.gov/19.2.E4). 139 func EllipticF(phi, m float64) float64 { 140 s, c := math.Sincos(phi) 141 return s * EllipticRF(c*c, 1-m*s*s, 1) 142 } 143 144 // EllipticE computes the Legendre's elliptic integral of the 2nd kind E(phi,m), 0≤m<1: 145 // E(\phi,m) = \int_{0}^{\phi} \sqrt{1-m\sin^2(\theta)} d\theta 146 // 147 // Legendre's elliptic integrals can be expressed as symmetric elliptic integrals, in this case: 148 // E(\phi,m) = \sin\phi R_F(\cos^2\phi,1-m\sin^2\phi,1)-(m/3)\sin^3\phi R_D(\cos^2\phi,1-m\sin^2\phi,1) 149 // 150 // The definition of E(phi,k) where k=sqrt(m) can be found in NIST Digital Library of Mathematical 151 // Functions (http://dlmf.nist.gov/19.2.E5). 152 func EllipticE(phi, m float64) float64 { 153 s, c := math.Sincos(phi) 154 x, y := c*c, 1-m*s*s 155 return s * (EllipticRF(x, y, 1) - (m/3)*s*s*EllipticRD(x, y, 1)) 156 }