github.com/gopherd/gonum@v0.0.4/mathext/internal/gonum/beta.go (about) 1 // Copyright ©2016 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 gonum 6 7 import ( 8 "math" 9 ) 10 11 // Beta returns the value of the complete beta function B(a, b). It is defined as 12 // Γ(a)Γ(b) / Γ(a+b) 13 // Special cases are: 14 // B(a,b) returns NaN if a or b is Inf 15 // B(a,b) returns NaN if a and b are 0 16 // B(a,b) returns NaN if a or b is NaN 17 // B(a,b) returns NaN if a or b is < 0 18 // B(a,b) returns +Inf if a xor b is 0. 19 // 20 // See http://mathworld.wolfram.com/BetaFunction.html for more detailed information. 21 func Beta(a, b float64) float64 { 22 return math.Exp(Lbeta(a, b)) 23 } 24 25 // Lbeta returns the natural logarithm of the complete beta function B(a,b). 26 // Lbeta is defined as: 27 // Ln(Γ(a)Γ(b)/Γ(a+b)) 28 // Special cases are: 29 // Lbeta(a,b) returns NaN if a or b is Inf 30 // Lbeta(a,b) returns NaN if a and b are 0 31 // Lbeta(a,b) returns NaN if a or b is NaN 32 // Lbeta(a,b) returns NaN if a or b is < 0 33 // Lbeta(a,b) returns +Inf if a xor b is 0. 34 func Lbeta(a, b float64) float64 { 35 switch { 36 case math.IsInf(a, +1) || math.IsInf(b, +1): 37 return math.NaN() 38 case a == 0 && b == 0: 39 return math.NaN() 40 case a < 0 || b < 0: 41 return math.NaN() 42 case math.IsNaN(a) || math.IsNaN(b): 43 return math.NaN() 44 case a == 0 || b == 0: 45 return math.Inf(+1) 46 } 47 48 la, _ := math.Lgamma(a) 49 lb, _ := math.Lgamma(b) 50 lab, _ := math.Lgamma(a + b) 51 return la + lb - lab 52 }