Browse Source

Use existing constants where appropriate

optimization-1
Christoph Ruegg 13 years ago
parent
commit
09cc6f7a13
  1. 5
      src/FSharpExamples/MCMC.fsx
  2. 2
      src/Numerics/Distributions/Chi.cs
  3. 2
      src/Numerics/Distributions/Laplace.cs
  4. 2
      src/Numerics/Distributions/Normal.cs
  5. 2
      src/Numerics/Distributions/Rayleigh.cs
  6. 4
      src/Numerics/Distributions/Stable.cs
  7. 2
      src/Numerics/LinearAlgebra/Complex/Factorization/UserQR.cs
  8. 2
      src/Numerics/LinearAlgebra/Complex32/Factorization/UserQR.cs
  9. 2
      src/Numerics/LinearAlgebra/Double/Factorization/UserQR.cs
  10. 2
      src/Numerics/LinearAlgebra/Single/Factorization/UserQR.cs
  11. 2
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs
  12. 2
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs
  13. 2
      src/Numerics/Trigonometry.cs
  14. 2
      src/UnitTests/DistributionTests/Continuous/ChiTests.cs
  15. 2
      src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs
  16. 2
      src/UnitTests/DistributionTests/Continuous/RayleighTests.cs
  17. 2
      src/UnitTests/StatisticsTests/MCMCTests/UnivariateHybridMCTest.cs

5
src/FSharpExamples/MCMC.fsx

@ -31,6 +31,7 @@
#r "../../out/lib/Net40/MathNet.Numerics.dll" #r "../../out/lib/Net40/MathNet.Numerics.dll"
#r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll" #r "../../out/lib/Net40/MathNet.Numerics.FSharp.dll"
open MathNet.Numerics
open MathNet.Numerics.Random open MathNet.Numerics.Random
open MathNet.Numerics.Statistics open MathNet.Numerics.Statistics
open MathNet.Numerics.Distributions open MathNet.Numerics.Distributions
@ -113,7 +114,7 @@ do
let normal = new Normal(mean, stddev) let normal = new Normal(mean, stddev)
/// Evaluates the log normal distribution. /// Evaluates the log normal distribution.
let npdf x m s = -0.5*(x-m)*(x-m)/(s*s) - 0.5 * log(2.0 * System.Math.PI * s * s) let npdf x m s = -0.5*(x-m)*(x-m)/(s*s) - 0.5 * log(Constants.Pi2 * s * s)
/// Implements the rejection sampling procedure. /// Implements the rejection sampling procedure.
let ms = new MetropolisHastingsSampler<float>( 0.1, (fun x -> log(normal.Density(x))), let ms = new MetropolisHastingsSampler<float>( 0.1, (fun x -> log(normal.Density(x))),
@ -144,7 +145,7 @@ do
let normal = new Normal(mean, stddev) let normal = new Normal(mean, stddev)
/// Evaluates the logarithm of the normal distribution function. /// Evaluates the logarithm of the normal distribution function.
let npdf x m s = -0.5*(x-m)*(x-m)/(s*s) - 0.5 * log(2.0 * System.Math.PI * s * s) let npdf x m s = -0.5*(x-m)*(x-m)/(s*s) - 0.5 * log(Constants.Pi2 * s * s)
/// Samples from a mixture that is biased towards samples larger than x. /// Samples from a mixture that is biased towards samples larger than x.
let mixSample x = let mixSample x =

2
src/Numerics/Distributions/Chi.cs

@ -144,7 +144,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Mean public double Mean
{ {
get { return Math.Sqrt(2)*(SpecialFunctions.Gamma((_dof + 1.0)/2.0)/SpecialFunctions.Gamma(_dof/2.0)); } get { return Constants.Sqrt2*(SpecialFunctions.Gamma((_dof + 1.0)/2.0)/SpecialFunctions.Gamma(_dof/2.0)); }
} }
/// <summary> /// <summary>

2
src/Numerics/Distributions/Laplace.cs

@ -181,7 +181,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double StdDev public double StdDev
{ {
get { return Math.Sqrt(2.0)*_scale; } get { return Constants.Sqrt2*_scale; }
} }
/// <summary> /// <summary>

2
src/Numerics/Distributions/Normal.cs

@ -378,7 +378,7 @@ namespace MathNet.Numerics.Distributions
/// <returns>the inverse cumulative density at <paramref name="p"/>.</returns> /// <returns>the inverse cumulative density at <paramref name="p"/>.</returns>
public double InverseCumulativeDistribution(double p) public double InverseCumulativeDistribution(double p)
{ {
return _mean - (_stdDev*Math.Sqrt(2.0)*SpecialFunctions.ErfcInv(2.0*p)); return _mean - (_stdDev*Constants.Sqrt2*SpecialFunctions.ErfcInv(2.0*p));
} }
/// <summary> /// <summary>

2
src/Numerics/Distributions/Rayleigh.cs

@ -171,7 +171,7 @@ namespace MathNet.Numerics.Distributions
/// </summary> /// </summary>
public double Entropy public double Entropy
{ {
get { return 1.0 + Math.Log(_scale/Math.Sqrt(2)) + (Constants.EulerMascheroni/2.0); } get { return 1.0 + Math.Log(_scale/Constants.Sqrt2) + (Constants.EulerMascheroni/2.0); }
} }
/// <summary> /// <summary>

4
src/Numerics/Distributions/Stable.cs

@ -237,7 +237,7 @@ namespace MathNet.Numerics.Distributions
{ {
if (_alpha == 2) if (_alpha == 2)
{ {
return Math.Sqrt(2.0)*_scale; return Constants.Sqrt2*_scale;
} }
return Double.PositiveInfinity; return Double.PositiveInfinity;
@ -452,7 +452,7 @@ namespace MathNet.Numerics.Distributions
var summand = part1*Math.Tan(randTheta); var summand = part1*Math.Tan(randTheta);
var subtrahend = beta*Math.Log(Constants.PiOver2*randW*Math.Cos(randTheta)/part1); var subtrahend = beta*Math.Log(Constants.PiOver2*randW*Math.Cos(randTheta)/part1);
return location + scale*((2.0/Math.PI)*(summand - subtrahend)); return location + scale*Constants.TwoInvPi*(summand - subtrahend);
} }
} }

2
src/Numerics/LinearAlgebra/Complex/Factorization/UserQR.cs

@ -152,7 +152,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
if (row == a.RowCount - 1 || norm.Magnitude == 0) if (row == a.RowCount - 1 || norm.Magnitude == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = Math.Sqrt(2.0); u[0] = Constants.Sqrt2;
return u; return u;
} }

2
src/Numerics/LinearAlgebra/Complex32/Factorization/UserQR.cs

@ -147,7 +147,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
if (row == a.RowCount - 1 || norm.Magnitude == 0) if (row == a.RowCount - 1 || norm.Magnitude == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = (float)Math.Sqrt(2.0); u[0] = (float)Constants.Sqrt2;
return u; return u;
} }

2
src/Numerics/LinearAlgebra/Double/Factorization/UserQR.cs

@ -146,7 +146,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
if (row == a.RowCount - 1 || norm == 0) if (row == a.RowCount - 1 || norm == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = Math.Sqrt(2.0); u[0] = Constants.Sqrt2;
return u; return u;
} }

2
src/Numerics/LinearAlgebra/Single/Factorization/UserQR.cs

@ -145,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
if (row == a.RowCount - 1 || norm == 0) if (row == a.RowCount - 1 || norm == 0)
{ {
a.At(row, column, -u[0]); a.At(row, column, -u[0]);
u[0] = (float)Math.Sqrt(2.0); u[0] = (float)Constants.Sqrt2;
return u; return u;
} }

2
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs

@ -1720,7 +1720,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
if (row == rowCount - 1 || norm == 0) if (row == rowCount - 1 || norm == 0)
{ {
a[index] = -work[tmp]; a[index] = -work[tmp];
work[tmp] = Math.Sqrt(2.0); work[tmp] = Constants.Sqrt2;
return; return;
} }

2
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs

@ -1721,7 +1721,7 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
if (row == rowCount - 1 || norm == 0) if (row == rowCount - 1 || norm == 0)
{ {
a[index] = -work[tmp]; a[index] = -work[tmp];
work[tmp] = (float) Math.Sqrt(2.0); work[tmp] = (float) Constants.Sqrt2;
return; return;
} }

2
src/Numerics/Trigonometry.cs

@ -473,7 +473,7 @@ namespace MathNet.Numerics
{ {
if (value.IsZero()) if (value.IsZero())
{ {
return Math.PI / 2.0; return Constants.PiOver2;
} }
var inv = Complex.ImaginaryOne / value; var inv = Complex.ImaginaryOne / value;

2
src/UnitTests/DistributionTests/Continuous/ChiTests.cs

@ -124,7 +124,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateMean(double dof) public void ValidateMean(double dof)
{ {
var n = new Chi(dof); var n = new Chi(dof);
Assert.AreEqual(Math.Sqrt(2) * (SpecialFunctions.Gamma((dof + 1.0) / 2.0) / SpecialFunctions.Gamma(dof / 2.0)), n.Mean); Assert.AreEqual(Constants.Sqrt2 * (SpecialFunctions.Gamma((dof + 1.0) / 2.0) / SpecialFunctions.Gamma(dof / 2.0)), n.Mean);
} }
/// <summary> /// <summary>

2
src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs

@ -192,7 +192,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateStdDev(double location, double scale) public void ValidateStdDev(double location, double scale)
{ {
var n = new Laplace(location, scale); var n = new Laplace(location, scale);
Assert.AreEqual(Math.Sqrt(2.0) * scale, n.StdDev); Assert.AreEqual(Constants.Sqrt2 * scale, n.StdDev);
} }
/// <summary> /// <summary>

2
src/UnitTests/DistributionTests/Continuous/RayleighTests.cs

@ -167,7 +167,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous
public void ValidateEntropy(double scale) public void ValidateEntropy(double scale)
{ {
var n = new Rayleigh(scale); var n = new Rayleigh(scale);
Assert.AreEqual(1.0 + Math.Log(scale / Math.Sqrt(2)) + (Constants.EulerMascheroni / 2.0), n.Entropy); Assert.AreEqual(1.0 + Math.Log(scale / Constants.Sqrt2) + (Constants.EulerMascheroni / 2.0), n.Entropy);
} }
/// <summary> /// <summary>

2
src/UnitTests/StatisticsTests/MCMCTests/UnivariateHybridMCTest.cs

@ -141,7 +141,7 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
double deviationRation = Math.Pow(stats.StandardDeviation/sdv, 2); double deviationRation = Math.Pow(stats.StandardDeviation/sdv, 2);
//Approximating chi-square with normal distribution. (Degree of freedom is large) //Approximating chi-square with normal distribution. (Degree of freedom is large)
double deviationConvergence = 3*Math.Sqrt(2)/Math.Sqrt(effective); double deviationConvergence = 3*Constants.Sqrt2/Math.Sqrt(effective);
Assert.AreEqual(deviationRation, 1, deviationConvergence, "Standard Deivation"); Assert.AreEqual(deviationRation, 1, deviationConvergence, "Standard Deivation");
} }
} }

Loading…
Cancel
Save