/// <summary> /// Run example /// </summary> /// <seealso cref="http://en.wikipedia.org/wiki/Digamma_function">Digamma function</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number">Harmonic number</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number#Generalized_harmonic_numbers">Generalized harmonic numbers</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Logistic_function">Logistic function</seealso> /// <seealso cref="http://en.wikipedia.org/wiki/Logit">Logit function</seealso> public void Run() { // 1. Calculate the Digamma function at point 5.0 Console.WriteLine(@"1. Calculate the Digamma function at point 5.0"); Console.WriteLine(SpecialFunctions.DiGamma(5.0)); Console.WriteLine(); // 2. Calculate the inverse Digamma function at point 1.5 Console.WriteLine(@"2. Calculate the inverse Digamma function at point 1.5"); Console.WriteLine(SpecialFunctions.DiGammaInv(1.5)); Console.WriteLine(); // 3. Calculate the 10'th Harmonic number Console.WriteLine(@"3. Calculate the 10'th Harmonic number"); Console.WriteLine(SpecialFunctions.Harmonic(10)); Console.WriteLine(); // 4. Calculate the generalized harmonic number of order 10 of 3.0. Console.WriteLine(@"4. Calculate the generalized harmonic number of order 10 of 3.0"); Console.WriteLine(SpecialFunctions.GeneralHarmonic(10, 3.0)); Console.WriteLine(); // 5. Calculate the logistic function of 3.0 Console.WriteLine(@"5. Calculate the logistic function of 3.0"); Console.WriteLine(SpecialFunctions.Logistic(3.0)); Console.WriteLine(); // 6. Calculate the logit function of 0.3 Console.WriteLine(@"6. Calculate the logit function of 0.3"); Console.WriteLine(SpecialFunctions.Logit(0.3)); Console.WriteLine(); }
private double DGamma(double x) { return(SpecialFunctions.DiGamma(Sqrt(x * x + 1)) * SpecialFunctions.Gamma(Sqrt(x * x + 1)) * ((2 * x) / Sqrt(x * x + 1) / 2.0) - 4 * x); }
public void ValidateEntropy(double[] x) { var d = new Dirichlet(x); var sum = x.Sum(t => (t - 1) * SpecialFunctions.DiGamma(t)); var res = SpecialFunctions.GammaLn(x.Sum()) + ((x.Sum() - x.Length) * SpecialFunctions.DiGamma(x.Sum())) - sum; AssertHelpers.AlmostEqualRelative(res, d.Entropy, 12); }
public void ValidateEntropy([Values(new[] { 0.1, 0.3, 0.5, 0.8 }, new[] { 0.1, 0.2, 0.3, 0.4 })] double[] x) { var d = new Dirichlet(x); var sum = x.Sum(t => (t - 1) * SpecialFunctions.DiGamma(t)); var res = SpecialFunctions.GammaLn(x.Sum()) + ((x.Sum() - x.Length) * SpecialFunctions.DiGamma(x.Sum())) - sum; AssertHelpers.AlmostEqual(res, d.Entropy, 12); }
public void DiGamma( [Values(Double.NaN, -1.5, -0.5, 0.1, 1.0, 1.5, Constants.Pi / 2, 2.0, 2.5, 3.0, Constants.Pi, 3.5, 4.0, 4.5, 5.0, 5.5, 10.1)] double x, [Values(Double.NaN, 0.70315664064524318722569033366791109947350706200623256, 0.036489973978576520559023667001244432806840395339565891, -10.423754940411076232100295314502760886768558023951363, -0.57721566490153286060651209008240243104215933593992359, 0.036489973978576520559023667001244432806840395339565888, 0.10067337642740238636795561404029690452798358068944001, 0.42278433509846713939348790991759756895784066406007641, 0.70315664064524318722569033366791109947350706200623255, 0.92278433509846713939348790991759756895784066406007641, 0.97721330794200673329206948640618234364083460999432603, 1.1031566406452431872256903336679110994735070620062326, 1.2561176684318004727268212432509309022911739973934097, 1.3888709263595289015114046193821968137592213477205183, 1.5061176684318004727268212432509309022911739973934097, 1.6110931485817511237336268416044190359814435699427405, 2.2622143570941481235561593642219403924532310597356171)] double f) { AssertHelpers.AlmostEqual(f, SpecialFunctions.DiGamma(x), 13); }
public void DiGamma(double x, double f) { AssertHelpers.AlmostEqualRelative(f, SpecialFunctions.DiGamma(x), 12); }
private double NewtonApproxGammaShape(double shape, double s) { double digamma = SpecialFunctions.DiGamma(shape); return(shape - (Math.Log(shape) - digamma - s) / (1 / shape - this.Trigamma(shape))); }
/// <summary> /// Executes the example. /// </summary> public override void ExecuteExample() { // <seealso cref="http://en.wikipedia.org/wiki/Beta_function">Beta function</seealso> MathDisplay.WriteLine("<b>Beta fuction</b>"); // 1. Compute the Beta function at z = 1.0, w = 3.0 MathDisplay.WriteLine(@"1. Compute the Beta function at z = 1.0, w = 3.0"); MathDisplay.WriteLine(SpecialFunctions.Beta(1.0, 3.0).ToString()); MathDisplay.WriteLine(); // 2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0 MathDisplay.WriteLine(@"2. Compute the logarithm of the Beta function at z = 1.0, w = 3.0"); MathDisplay.WriteLine(SpecialFunctions.BetaLn(1.0, 3.0).ToString()); MathDisplay.WriteLine(); // 3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7 MathDisplay.WriteLine(@"3. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 0.7"); MathDisplay.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 0.7).ToString()); MathDisplay.WriteLine(); // 4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0 MathDisplay.WriteLine(@"4. Compute the Beta incomplete function at z = 1.0, w = 3.0, x = 1.0"); MathDisplay.WriteLine(SpecialFunctions.BetaIncomplete(1.0, 3.0, 1.0).ToString()); MathDisplay.WriteLine(); // 5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7 MathDisplay.WriteLine(@"5. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 0.7"); MathDisplay.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 0.7).ToString()); MathDisplay.WriteLine(); // 6. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 1.0 MathDisplay.WriteLine(@"6. Compute the Beta regularized function at z = 1.0, w = 3.0, x = 1.0"); MathDisplay.WriteLine(SpecialFunctions.BetaRegularized(1.0, 3.0, 1.0).ToString()); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Common functions</b>"); // 1. Calculate the Digamma function at point 5.0 // <seealso cref="http://en.wikipedia.org/wiki/Digamma_function">Digamma function</seealso> MathDisplay.WriteLine(@"1. Calculate the Digamma function at point 5.0"); MathDisplay.WriteLine(SpecialFunctions.DiGamma(5.0).ToString()); MathDisplay.WriteLine(); // 2. Calculate the inverse Digamma function at point 1.5 MathDisplay.WriteLine(@"2. Calculate the inverse Digamma function at point 1.5"); MathDisplay.WriteLine(SpecialFunctions.DiGammaInv(1.5).ToString()); MathDisplay.WriteLine(); // 3. Calculate the 10'th Harmonic number // <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number">Harmonic number</seealso> MathDisplay.WriteLine(@"3. Calculate the 10'th Harmonic number"); MathDisplay.WriteLine(SpecialFunctions.Harmonic(10).ToString()); MathDisplay.WriteLine(); // 4. Calculate the generalized harmonic number of order 10 of 3.0. // <seealso cref="http://en.wikipedia.org/wiki/Harmonic_number#Generalized_harmonic_numbers">Generalized harmonic numbers</seealso> MathDisplay.WriteLine(@"4. Calculate the generalized harmonic number of order 10 of 3.0"); MathDisplay.WriteLine(SpecialFunctions.GeneralHarmonic(10, 3.0).ToString()); MathDisplay.WriteLine(); // 5. Calculate the logistic function of 3.0 // <seealso cref="http://en.wikipedia.org/wiki/Logistic_function">Logistic function</seealso> MathDisplay.WriteLine(@"5. Calculate the logistic function of 3.0"); MathDisplay.WriteLine(SpecialFunctions.Logistic(3.0).ToString()); MathDisplay.WriteLine(); // 6. Calculate the logit function of 0.3 // <seealso cref="http://en.wikipedia.org/wiki/Logit">Logit function</seealso> MathDisplay.WriteLine(@"6. Calculate the logit function of 0.3"); MathDisplay.WriteLine(SpecialFunctions.Logit(0.3).ToString()); MathDisplay.WriteLine(); // <seealso cref="http://en.wikipedia.org/wiki/Error_function">Error function</seealso> MathDisplay.WriteLine("<b>Error function</b>"); // 1. Calculate the error function at point 2 MathDisplay.WriteLine(@"1. Calculate the error function at point 2"); MathDisplay.WriteLine(SpecialFunctions.Erf(2).ToString()); MathDisplay.WriteLine(); // 2. Sample 10 values of the error function in [-1.0; 1.0] MathDisplay.WriteLine(@"2. Sample 10 values of the error function in [-1.0; 1.0]"); var data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.Erf); for (var i = 0; i < data.Length; i++) { MathDisplay.Write(data[i].ToString("N") + @" "); } MathDisplay.WriteLine(); MathDisplay.WriteLine(); // 3. Calculate the complementary error function at point 2 MathDisplay.WriteLine(@"3. Calculate the complementary error function at point 2"); MathDisplay.WriteLine(SpecialFunctions.Erfc(2).ToString()); MathDisplay.WriteLine(); // 4. Sample 10 values of the complementary error function in [-1.0; 1.0] MathDisplay.WriteLine(@"4. Sample 10 values of the complementary error function in [-1.0; 1.0]"); data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.Erfc); for (var i = 0; i < data.Length; i++) { MathDisplay.Write(data[i].ToString("N") + @" "); } MathDisplay.WriteLine(); MathDisplay.WriteLine(); // 5. Calculate the inverse error function at point z=0.5 MathDisplay.WriteLine(@"5. Calculate the inverse error function at point z=0.5"); MathDisplay.WriteLine(SpecialFunctions.ErfInv(0.5).ToString()); MathDisplay.WriteLine(); // 6. Sample 10 values of the inverse error function in [-1.0; 1.0] MathDisplay.WriteLine(@"6. Sample 10 values of the inverse error function in [-1.0; 1.0]"); data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.ErfInv); for (var i = 0; i < data.Length; i++) { MathDisplay.Write(data[i].ToString("N") + @" "); } MathDisplay.WriteLine(); MathDisplay.WriteLine(); // 7. Calculate the complementary inverse error function at point z=0.5 MathDisplay.WriteLine(@"7. Calculate the complementary inverse error function at point z=0.5"); MathDisplay.WriteLine(SpecialFunctions.ErfcInv(0.5).ToString()); MathDisplay.WriteLine(); // 8. Sample 10 values of the complementary inverse error function in [-1.0; 1.0] MathDisplay.WriteLine(@"8. Sample 10 values of the complementary inverse error function in [-1.0; 1.0]"); data = Generate.LinearSpacedMap <double>(10, -1.0, 1.0, SpecialFunctions.ErfcInv); for (var i = 0; i < data.Length; i++) { MathDisplay.Write(data[i].ToString("N") + @" "); } MathDisplay.WriteLine(); // <seealso cref="http://en.wikipedia.org/wiki/Factorial">Factorial</seealso> MathDisplay.WriteLine("<b>Factorial</b>"); // 1. Compute the factorial of 5 MathDisplay.WriteLine(@"1. Compute the factorial of 5"); MathDisplay.WriteLine(SpecialFunctions.Factorial(5).ToString("N")); MathDisplay.WriteLine(); // 2. Compute the logarithm of the factorial of 5 MathDisplay.WriteLine(@"2. Compute the logarithm of the factorial of 5"); MathDisplay.WriteLine(SpecialFunctions.FactorialLn(5).ToString("N")); MathDisplay.WriteLine(); // <seealso cref="http://en.wikipedia.org/wiki/Binomial_coefficient">Binomial coefficient</seealso> MathDisplay.WriteLine("<b>Binomial coefficient</b>"); // 3. Compute the binomial coefficient: 10 choose 8 MathDisplay.WriteLine(@"3. Compute the binomial coefficient: 10 choose 8"); MathDisplay.WriteLine(SpecialFunctions.Binomial(10, 8).ToString("N")); MathDisplay.WriteLine(); // 4. Compute the logarithm of the binomial coefficient: 10 choose 8 MathDisplay.WriteLine(@"4. Compute the logarithm of the binomial coefficient: 10 choose 8"); MathDisplay.WriteLine(SpecialFunctions.BinomialLn(10, 8).ToString("N")); MathDisplay.WriteLine(); // <seealso cref="http://en.wikipedia.org/wiki/Multinomial_theorem#Multinomial_coefficients">Multinomial coefficients</seealso> MathDisplay.WriteLine("<b>Multinomial coefficient</b>"); // 5. Compute the multinomial coefficient: 10 choose 2, 3, 5 MathDisplay.WriteLine(@"5. Compute the multinomial coefficient: 10 choose 2, 3, 5"); MathDisplay.WriteLine(SpecialFunctions.Multinomial(10, new[] { 2, 3, 5 }).ToString("N")); MathDisplay.WriteLine(); // <seealso cref="http://en.wikipedia.org/wiki/Gamma_function">Gamma function</seealso> MathDisplay.WriteLine("<b>Gamma function</b>"); // 1. Compute the Gamma function of 10 MathDisplay.WriteLine(@"1. Compute the Gamma function of 10"); MathDisplay.WriteLine(SpecialFunctions.Gamma(10).ToString("N")); MathDisplay.WriteLine(); // 2. Compute the logarithm of the Gamma function of 10 MathDisplay.WriteLine(@"2. Compute the logarithm of the Gamma function of 10"); MathDisplay.WriteLine(SpecialFunctions.GammaLn(10).ToString("N")); MathDisplay.WriteLine(); // 3. Compute the lower incomplete gamma(a, x) function at a = 10, x = 14 MathDisplay.WriteLine(@"3. Compute the lower incomplete gamma(a, x) function at a = 10, x = 14"); MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 14).ToString("N")); MathDisplay.WriteLine(); // 4. Compute the lower incomplete gamma(a, x) function at a = 10, x = 100 MathDisplay.WriteLine(@"4. Compute the lower incomplete gamma(a, x) function at a = 10, x = 100"); MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 100).ToString("N")); MathDisplay.WriteLine(); // 5. Compute the upper incomplete gamma(a, x) function at a = 10, x = 0 MathDisplay.WriteLine(@"5. Compute the upper incomplete gamma(a, x) function at a = 10, x = 0"); MathDisplay.WriteLine(SpecialFunctions.GammaUpperIncomplete(10, 0).ToString("N")); MathDisplay.WriteLine(); // 6. Compute the upper incomplete gamma(a, x) function at a = 10, x = 10 MathDisplay.WriteLine(@"6. Compute the upper incomplete gamma(a, x) function at a = 10, x = 100"); MathDisplay.WriteLine(SpecialFunctions.GammaLowerIncomplete(10, 10).ToString("N")); MathDisplay.WriteLine(); // 7. Compute the lower regularized gamma(a, x) function at a = 10, x = 14 MathDisplay.WriteLine(@"7. Compute the lower regularized gamma(a, x) function at a = 10, x = 14"); MathDisplay.WriteLine(SpecialFunctions.GammaLowerRegularized(10, 14).ToString("N")); MathDisplay.WriteLine(); // 8. Compute the lower regularized gamma(a, x) function at a = 10, x = 100 MathDisplay.WriteLine(@"8. Compute the lower regularized gamma(a, x) function at a = 10, x = 100"); MathDisplay.WriteLine(SpecialFunctions.GammaLowerRegularized(10, 100).ToString("N")); MathDisplay.WriteLine(); // 9. Compute the upper regularized gamma(a, x) function at a = 10, x = 0 MathDisplay.WriteLine(@"9. Compute the upper regularized gamma(a, x) function at a = 10, x = 0"); MathDisplay.WriteLine(SpecialFunctions.GammaUpperRegularized(10, 0).ToString("N")); MathDisplay.WriteLine(); // 10. Compute the upper regularized gamma(a, x) function at a = 10, x = 10 MathDisplay.WriteLine(@"10. Compute the upper regularized gamma(a, x) function at a = 10, x = 100"); MathDisplay.WriteLine(SpecialFunctions.GammaUpperRegularized(10, 10).ToString("N")); MathDisplay.WriteLine(); MathDisplay.WriteLine("<b>Numerical stability</b>"); // 1. Compute numerically stable exponential of 10 minus one MathDisplay.WriteLine(@"1. Compute numerically stable exponential of 4.2876 minus one"); MathDisplay.WriteLine(SpecialFunctions.ExponentialMinusOne(4.2876).ToString()); MathDisplay.WriteLine(); // 2. Compute regular System.Math exponential of 15.28 minus one MathDisplay.WriteLine(@"2. Compute regular System.Math exponential of 4.2876 minus one "); MathDisplay.WriteLine((Math.Exp(4.2876) - 1).ToString()); MathDisplay.WriteLine(); // 3. Compute numerically stable hypotenuse of a right angle triangle with a = 5, b = 3 MathDisplay.WriteLine(@"3. Compute numerically stable hypotenuse of a right angle triangle with a = 5, b = 3"); MathDisplay.WriteLine(SpecialFunctions.Hypotenuse(5, 3).ToString()); MathDisplay.WriteLine(); }