Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logmultinomial implements log(Combinatorics.multinomial) #231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/functions_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ SpecialFunctions.beta
SpecialFunctions.logbeta
SpecialFunctions.logabsbeta
SpecialFunctions.logabsbinomial
SpecialFunctions.logmultinomial
```
1 change: 1 addition & 0 deletions docs/src/functions_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Here the *Special Functions* are listed according to the structure of [NIST Digi
| [`logabsgamma(x)`](@ref SpecialFunctions.logabsgamma) | accurate `log(abs(gamma(x)))` for large `x` |
| [`lgamma(x)`](@ref SpecialFunctions.lgamma) | accurate `log(gamma(x))` for large `x` |
| [`logfactorial(x)`](@ref SpecialFunctions.logfactorial) | accurate `log(factorial(x))` for large `x`; same as `lgamma(x+1)` for `x > 1`, zero otherwise |
| [`logmultinomial(k...)`](@ref SpecialFunctions.logmultinomial) | accurate `log(multinomial(k...))` for large multisets |
| [`beta(x,y)`](@ref SpecialFunctions.beta) | [beta function](https://en.wikipedia.org/wiki/Beta_function) at `x,y` |
| [`logbeta(x,y)`](@ref SpecialFunctions.logbeta) | accurate `log(beta(x,y))` for large `x` or `y` |
| [`logabsbeta(x,y)`](@ref SpecialFunctions.logabsbeta) | accurate `log(abs(beta(x,y)))` for large `x` or `y` |
Expand Down
3 changes: 2 additions & 1 deletion src/SpecialFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export
zeta,
sinint,
cosint,
lbinomial
lbinomial,
logmultinomial

include("bessel.jl")
include("erf.jl")
Expand Down
24 changes: 23 additions & 1 deletion src/gamma.jl
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ function polygamma(m::Integer, z::Number)
polygamma(m, x)
end

export gamma, loggamma, logabsgamma, beta, logbeta, logabsbeta, logfactorial, logabsbinomial
export gamma, loggamma, logabsgamma, beta, logbeta, logabsbeta, logfactorial, logabsbinomial, logmultinomial

## from base/special/gamma.jl

Expand Down Expand Up @@ -922,3 +922,25 @@ function logabsbinomial(n::T, k::T) where {T<:Integer}
end
end
logabsbinomial(n::Integer, k::Integer) = logabsbinomial(promote(n, k)...)

#=
logmultinomial computes the log of the multinomial coefficient.
From Wolfram Mathworld (https://mathworld.wolfram.com/MultinomialCoefficient.html):

The multinomial coefficients

$$(n_1, n_2, \ldots, n_k)! = \frac{(n_1+n_2+...+n_k)!}{n_1! n_2! \ldots n_k!}$$

are the terms in the multinomial series expansion. In other words, the number
of distinct permutations in a multiset of $k$ distinct elements of multiplicity
$n_i$ $(1 \le i \le k)$ is $(n_1, \ldots, n_k)!$.
=#
function logmultinomial(multiset...)
numerator = 0
denominator = 0.0
@inbounds for multiplicity in multiset
numerator += multiplicity
denominator += loggamma(multiplicity + 1)
end
return loggamma(numerator + 1) - denominator
end