move stirling methods to public package
This commit is contained in:
@@ -1,82 +0,0 @@
|
||||
/*
|
||||
Copyright © 2025 filifa
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package lib
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// given a slice where vals[i] = Stirling1(i+k-1, k-1) for a given k, nextStirling1 updates the slice so vals[i] = Stirling1(i+k, k) using the property that Stirling1(n, k) = -(n-1)*Stirling1(n-1, k) + Stirling1(n-1, k-1)
|
||||
func nextStirling1(k int, vals []*big.Int) {
|
||||
for i := 1; i < len(vals); i++ {
|
||||
n := int64(k + i - 1)
|
||||
v := big.NewInt(-n)
|
||||
v.Mul(v, vals[i-1])
|
||||
vals[i].Add(vals[i], v)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Stirling1 computes Stirling numbers of the first kind.
|
||||
*/
|
||||
func Stirling1(n, k int) *big.Int {
|
||||
if k > n {
|
||||
return big.NewInt(0)
|
||||
}
|
||||
|
||||
vals := make([]*big.Int, n-k+1)
|
||||
for i := range vals {
|
||||
vals[i] = big.NewInt(0)
|
||||
}
|
||||
vals[0] = big.NewInt(1)
|
||||
|
||||
for i := 1; i <= k; i++ {
|
||||
nextStirling1(i, vals)
|
||||
}
|
||||
|
||||
return vals[n-k]
|
||||
}
|
||||
|
||||
// given a slice where vals[i] = Stirling2(i+k-1, k-1) for a given k, nextStirling2 updates the slice so vals[i] = Stirling2(i+k, k) using the property that Stirling2(n, k) = k*Stirling2(n-1, k) + Stirling2(n-1, k-1)
|
||||
func nextStirling2(k int64, vals []*big.Int) {
|
||||
for i := 1; i < len(vals); i++ {
|
||||
v := big.NewInt(k)
|
||||
v.Mul(v, vals[i-1])
|
||||
vals[i].Add(vals[i], v)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Stirling2 computes Stirling numbers of the second kind.
|
||||
*/
|
||||
func Stirling2(n, k int) *big.Int {
|
||||
if k > n {
|
||||
return big.NewInt(0)
|
||||
}
|
||||
|
||||
vals := make([]*big.Int, n-k+1)
|
||||
for i := range vals {
|
||||
vals[i] = big.NewInt(0)
|
||||
}
|
||||
vals[0] = big.NewInt(1)
|
||||
|
||||
for i := 1; i <= k; i++ {
|
||||
nextStirling2(int64(i), vals)
|
||||
}
|
||||
|
||||
return vals[n-k]
|
||||
}
|
||||
Reference in New Issue
Block a user