github.com/gopherd/gonum@v0.0.4/mathext/mvgamma.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 mathext
     6  
     7  import "math"
     8  
     9  const (
    10  	logPi = 1.14472988584940017414342735135305871164729481 // http://oeis.org/A053510
    11  )
    12  
    13  // MvLgamma returns the log of the multivariate Gamma function. Dim
    14  // must be greater than zero, and MvLgamma will return NaN if v < (dim-1)/2.
    15  //
    16  // See https://en.wikipedia.org/wiki/Multivariate_gamma_function for more
    17  // information.
    18  func MvLgamma(v float64, dim int) float64 {
    19  	if dim < 1 {
    20  		panic("mathext: negative dimension")
    21  	}
    22  	df := float64(dim)
    23  	if v < (df-1)*0.5 {
    24  		return math.NaN()
    25  	}
    26  	ans := df * (df - 1) * 0.25 * logPi
    27  	for i := 1; i <= dim; i++ {
    28  		lg, _ := math.Lgamma(v + float64(1-i)*0.5)
    29  		ans += lg
    30  	}
    31  	return ans
    32  }