maths includes mathematical functions not defined in the standard Go math package. Most functions support any primitive integer or float type through generics.
go get github.com/theriault/maths
import "github.com/theriault/maths/combinatorics"
Source | Tests | Wikipedia | OEIS
combinatorics.Factorial(10) // will return uint64(3628800)
combinatorics.FallingFactorial(8, 3) // will return uint64(336)
combinatorics.PartialPermutations(8, 3) // will return uint64(336)
combinatorics.RisingFactorial(2, 3) // will return uint64(24)
import "github.com/theriault/maths/numbertheory"
Source | Tests | Wikipedia | OEIS
numbertheory.AliquotSum(60) // will return uint64(108)
$\displaystyle f(a,b) = \begin{cases}\text{true} &\text{if}\ \gcd(a,b) = 1 \\ \text{false} &\text{else} \end{cases}$
numbertheory.Coprime(3*5*7, 11*13*17) // will return true
numbertheory.DigitSum(9045, 10) // will return int(18)
$\displaystyle f_{b}(n)={\begin{cases} 0 &\text{if}\ n=0\\ n\ \bmod (b-1)&{\text{if}}\ n\not \equiv 0{\pmod {b-1}} \\ b-1 &\text{else} \end{cases}}$
numbertheory.DigitalRoot(9045, 10) // will return int(9)
Source | Tests | Wikipedia | OEIS
numbertheory.NumberOfDivisors(48) // will return uint64(10)
Source | Tests | Wikipedia | OEIS
numbertheory.SumOfDivisors(48) // will return uint64(124)
numbertheory.GCD(48,18) // will return int(6)
numbertheory.LCM(48,18) // will return int(144)
Source | Tests | Wikipedia | OEIS
$\displaystyle \mu(n) = \begin{cases} +1 & n \text{ is square-free with even number of prime factors} \\ -1 & n \text{ is square-free with odd number of prime factors} \\ 0 & n \text{ is not square-free} \end{cases}$
numbertheory.Mobius(70) // will return int8(-1)
Source | Tests | Wikipedia | OEIS
where
numbertheory.Politeness(32) // will return uint64(0)
numbertheory.PolygonalNumber(3, 4) // will return uint64(10)
Finding
numbertheory.PolygonalRoot(3, 10) // will return float64(4)
Finding
numbertheory.PolygonalSides(4, 10) // will return float64(3)
numbertheory.PrimeFactorization(184756) // will return []uint64{2, 2, 11, 13, 17, 19}
Source | Tests | Wikipedia | OEIS
numbertheory.Primorial(30) // will return uint64(6469693230)
Source | Tests | Wikipedia | OEIS
numbertheory.Radical(60) // will return uint64(30)
Source | Tests | Wikipedia | OEIS
numbertheory.Totient(68) // will return uint64(32)
numbertheory.TotientK(60, 2) // will return uint64(2304)
import "github.com/theriault/maths/statistics"
statistics.GeneralizedMean([]float64{1, 1000}, 2) // will return float64(707.1071347398497)
statistics.RootMeanSquare(1, 1000) // will return float64(707.1071347398497)
statistics.Mean(1, 1000) // will return float64(500.5)
statistics.GeometricMean(1, 1000) // will return float64(31.62...)
statistics.HarmonicMean(1, 1000) // will return float64(1.99...)
statistics.CentralMoment([]uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2) // returns float64(8.25)
statistics.InterquartileRange(3, 6, 7, 8, 8, 10, 13, 15, 16, 20) // returns float64(7.25)
statistics.Kurtosis(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // returns float64(1.5133)
statistics.SampleKurtosis(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // returns float64(2.522167)
statistics.ExcessSampleKurtosis([]uint8{8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8}) // returns float64(-1.6445)
-
$L$ - curve's max value -
$x_0$ - sigmoid's midpoint -
$k$ - logistic growth rate
maxValue := 1.0
midpoint := 0.0
growthRate := 1.0
fx := statistics.LogisticFunction(maxValue, midpoint, growthRate) // will return func (x float64) float64
statistics.Mode(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
$\displaystyle {\begin{aligned}{\textit {SMA}}{k}&={\frac{p{n-k+1}+p_{n-k+2}\cdots +p_{n}}{k}}\&={\frac{1}{k}}\sum_{i=n-k+1}^np_i\end{aligned}}$
statistics.SimpleMovingAverage(3, 1, 2, 3, 4, 5, 6, 7, 8, 9) // will return []float64{2, 3, 4, 5, 6, 7, 8}
statistics.PowerSum([]float64{2, 3, 4}, 2) // will return float64(29)
statistics.PowerSumAround([]float64{2, 3, 4}, 3, 2) // will return float64(29)
n := []float64{3, 6, 7, 8, 8, 10, 13, 15, 16, 20}
statistics.Quantile(n, 2) // median: will return []float64{9}
statistics.Quantile(n, 3) // tertiles: will return []float64{8, 13}
statistics.Quantile(n, 4) // quartiles: will return []float64{7.25, 9, 14.5}
statistics.Quantile(n, 100) // percentile: will return []float64{3.27, 3.54, 3.81, 4.08, ...95 other values...}
// aliases
statistics.Tertile(n) // will return []float64{8, 13}
statistics.Quartile(n) // will return []float64{7.25, 9, 14.5}
statistics.Percentile(n) // will return []float64{3.27, 3.54, 3.81, 4.08, ...95 other values...}
Median (Source | Tests | Wikipedia)
n := []float64{3, 6, 7, 8, 8, 10, 13, 15, 16, 20}
statistics.Median(n) // will return float64(9)
n := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
statistics.Max(n...) // will return uint8(10)
n := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
statistics.Min(n...) // will return uint8(1)
n := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
statistics.Range(n...) // will return uint8(9)
statistics.Skewness(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // returns float64(-0.274241)
statistics.SampleSkewness(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // returns float64(-0.319584)
statistics.StandardDeviation(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
statistics.SampleStandardDeviation(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
statistics.StandardError(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
statistics.SampleStandardError(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
X := []float64{1, 2, 3, 4, 1, 2, 3}
statistics.Softmax(X) // will return []float64{0.0236405, 0.0642617, 0.1746813, 0.4748330, 0.0236405, 0.0642617, 0.1746813}
statistics.MutableSoftmax(X) // same as above, but will modify X and return X
X = []float64{1, 2, 3, 4, 1, 2, 3}
statistics.GeneralizedSoftmax(X, 0.5) // will return []float64{0.0018889, 0.0139573, 0.1031315, 0.7620445, 0.0018889, 0.0139573, 0.1031315}
statistics.Sum(1.1, 1.2, 1.3) // will return float64(3.6)
statistics.Variance(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
statistics.SampleVariance(8, 3, 6, 2, 7, 1, 8, 3, 7, 4, 8) // will return []float64{8}
X := []uint8{8, 7, 3, 2, 6, 11, 6, 7, 2, 1, 7}
W := []uint8{1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2}
mean, err := statistics.WeightedGeneralizedMean(X, Y, 1) // will return float64(5.5)
X := []uint8{8, 7, 3, 2, 6, 11, 6, 7, 2, 1, 7}
W := []uint8{1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2}
mean, err := statistics.WeightedMean(X, Y) // will return float64(5.5)
X := []uint8{8, 7, 3, 2, 6, 11, 6, 7, 2, 1, 7}
W := []uint8{1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2}
mean, err := statistics.WeightedGeometricMean(X, Y) // will return float64(4.6)
X := []uint8{8, 7, 3, 2, 6, 11, 6, 7, 2, 1, 7}
W := []uint8{1, 2, 1, 1, 2, 1, 2, 1, 2, 1, 2}
mean, err := statistics.WeightedHarmonicMean(X, Y) // will return float64(5.8)
See docs/complexity.md for information on time complexity and space complexity.