diff --git a/MathNet.Numerics.All.sln.DotSettings b/MathNet.Numerics.All.sln.DotSettings index cd49821b..29ce644d 100644 --- a/MathNet.Numerics.All.sln.DotSettings +++ b/MathNet.Numerics.All.sln.DotSettings @@ -10,6 +10,10 @@ False False False + ALWAYS_ADD + ALWAYS_ADD + ALWAYS_ADD + ALWAYS_ADD False False True diff --git a/src/Numerics/Distributions/Bernoulli.cs b/src/Numerics/Distributions/Bernoulli.cs index 3f454bcd..62ece6c0 100644 --- a/src/Numerics/Distributions/Bernoulli.cs +++ b/src/Numerics/Distributions/Bernoulli.cs @@ -204,8 +204,16 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public double Probability(int k) { - if (k == 0) return 1.0 - _p; - if (k == 1) return _p; + if (k == 0) + { + return 1.0 - _p; + } + + if (k == 1) + { + return _p; + } + return 0.0; } @@ -216,8 +224,12 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public double ProbabilityLn(int k) { - if (k == 0) return Math.Log(1.0 - _p); - return k == 1 ? Math.Log(_p) : Double.NegativeInfinity; + if (k == 0) + { + return Math.Log(1.0 - _p); + } + + return k == 1 ? Math.Log(_p) : double.NegativeInfinity; } /// @@ -227,8 +239,16 @@ namespace MathNet.Numerics.Distributions /// the cumulative distribution at location . public double CumulativeDistribution(double x) { - if (x < 0.0) return 0.0; - if (x >= 1.0) return 1.0; + if (x < 0.0) + { + return 0.0; + } + + if (x >= 1.0) + { + return 1.0; + } + return 1.0 - _p; } @@ -240,9 +260,21 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double p, int k) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k == 0) return 1.0 - p; - if (k == 1) return p; + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k == 0) + { + return 1.0 - p; + } + + if (k == 1) + { + return p; + } + return 0.0; } @@ -254,9 +286,17 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double p, int k) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k == 0) return Math.Log(1.0 - p); - return k == 1 ? Math.Log(p) : Double.NegativeInfinity; + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k == 0) + { + return Math.Log(1.0 - p); + } + + return k == 1 ? Math.Log(p) : double.NegativeInfinity; } /// @@ -268,9 +308,21 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double p, double x) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (x < 0.0) return 0.0; - if (x >= 1.0) return 1.0; + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (x < 0.0) + { + return 0.0; + } + + if (x >= 1.0) + { + return 1.0; + } + return 1.0 - p; } @@ -344,7 +396,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the Bernoulli distribution. public static int Sample(System.Random rnd, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, p); } @@ -357,7 +412,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, p); } @@ -371,7 +429,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, int[] values, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, p); } @@ -383,7 +444,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the Bernoulli distribution. public static int Sample(double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, p); } @@ -395,7 +459,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, p); } @@ -408,7 +475,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(int[] values, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, p); } diff --git a/src/Numerics/Distributions/Beta.cs b/src/Numerics/Distributions/Beta.cs index b4523205..f570825a 100644 --- a/src/Numerics/Distributions/Beta.cs +++ b/src/Numerics/Distributions/Beta.cs @@ -143,21 +143,32 @@ namespace MathNet.Numerics.Distributions { get { - if (_shapeA == 0.0 && _shapeB == 0.0) return 0.5; - if (_shapeA == 0.0) return 0.0; - if (_shapeB == 0.0) return 1.0; + if (_shapeA == 0.0 && _shapeB == 0.0) + { + return 0.5; + } + + if (_shapeA == 0.0) + { + return 0.0; + } - if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB)) + if (_shapeB == 0.0) + { + return 1.0; + } + + if (double.IsPositiveInfinity(_shapeA) && double.IsPositiveInfinity(_shapeB)) { return 0.5; } - if (Double.IsPositiveInfinity(_shapeA)) + if (double.IsPositiveInfinity(_shapeA)) { return 1.0; } - if (Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeB)) { return 0.0; } @@ -189,7 +200,7 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_shapeA) || Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeA) || double.IsPositiveInfinity(_shapeB)) { return 0.0; } @@ -218,24 +229,35 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeA) && double.IsPositiveInfinity(_shapeB)) { return 0.0; } - if (Double.IsPositiveInfinity(_shapeA)) + if (double.IsPositiveInfinity(_shapeA)) { return -2.0; } - if (Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeB)) { return 2.0; } - if (_shapeA == 0.0 && _shapeB == 0.0) return 0.0; - if (_shapeA == 0.0) return 2.0; - if (_shapeB == 0.0) return -2.0; + if (_shapeA == 0.0 && _shapeB == 0.0) + { + return 0.0; + } + + if (_shapeA == 0.0) + { + return 2.0; + } + + if (_shapeB == 0.0) + { + return -2.0; + } return 2.0*(_shapeB - _shapeA)*Math.Sqrt(_shapeA + _shapeB + 1.0) /((_shapeA + _shapeB + 2.0)*Math.Sqrt(_shapeA*_shapeB)); @@ -249,26 +271,40 @@ namespace MathNet.Numerics.Distributions { get { - if (_shapeA == 0.0 && _shapeB == 0.0) return 0.5; - if (_shapeA == 0.0) return 0.0; - if (_shapeB == 0.0) return 1.0; + if (_shapeA == 0.0 && _shapeB == 0.0) + { + return 0.5; + } + + if (_shapeA == 0.0) + { + return 0.0; + } + + if (_shapeB == 0.0) + { + return 1.0; + } - if (Double.IsPositiveInfinity(_shapeA) && Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeA) && double.IsPositiveInfinity(_shapeB)) { return 0.5; } - if (Double.IsPositiveInfinity(_shapeA)) + if (double.IsPositiveInfinity(_shapeA)) { return 1.0; } - if (Double.IsPositiveInfinity(_shapeB)) + if (double.IsPositiveInfinity(_shapeB)) { return 0.0; } - if (_shapeA == 1.0 && _shapeB == 1.0) return 0.5; + if (_shapeA == 1.0 && _shapeB == 1.0) + { + return 0.5; + } return (_shapeA - 1)/(_shapeA + _shapeB - 2); } @@ -416,38 +452,55 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double a, double b, double x) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (x < 0.0 || x > 1.0) return 0.0; + if (x < 0.0 || x > 1.0) + { + return 0.0; + } - if (Double.IsPositiveInfinity(a) && Double.IsPositiveInfinity(b)) + if (double.IsPositiveInfinity(a) && double.IsPositiveInfinity(b)) { - return x == 0.5 ? Double.PositiveInfinity : 0.0; + return x == 0.5 ? double.PositiveInfinity : 0.0; } - if (Double.IsPositiveInfinity(a)) + if (double.IsPositiveInfinity(a)) { - return x == 1.0 ? Double.PositiveInfinity : 0.0; + return x == 1.0 ? double.PositiveInfinity : 0.0; } - if (Double.IsPositiveInfinity(b)) + if (double.IsPositiveInfinity(b)) { - return x == 0.0 ? Double.PositiveInfinity : 0.0; + return x == 0.0 ? double.PositiveInfinity : 0.0; } if (a == 0.0 && b == 0.0) { if (x == 0.0 || x == 1.0) { - return Double.PositiveInfinity; + return double.PositiveInfinity; } return 0.0; } - if (a == 0.0) return x == 0.0 ? Double.PositiveInfinity : 0.0; - if (b == 0.0) return x == 1.0 ? Double.PositiveInfinity : 0.0; - if (a == 1.0 && b == 1.0) return 1.0; + if (a == 0.0) + { + return x == 0.0 ? double.PositiveInfinity : 0.0; + } + + if (b == 0.0) + { + return x == 1.0 ? double.PositiveInfinity : 0.0; + } + + if (a == 1.0 && b == 1.0) + { + return 1.0; + } var bb = SpecialFunctions.Gamma(a + b)/(SpecialFunctions.Gamma(a)*SpecialFunctions.Gamma(b)); return bb*Math.Pow(x, a - 1.0)*Math.Pow(1.0 - x, b - 1.0); @@ -463,37 +516,54 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double a, double b, double x) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (x < 0.0 || x > 1.0) return Double.NegativeInfinity; + if (x < 0.0 || x > 1.0) + { + return double.NegativeInfinity; + } - if (Double.IsPositiveInfinity(a) && Double.IsPositiveInfinity(b)) + if (double.IsPositiveInfinity(a) && double.IsPositiveInfinity(b)) { - return x == 0.5 ? Double.PositiveInfinity : Double.NegativeInfinity; + return x == 0.5 ? double.PositiveInfinity : double.NegativeInfinity; } - if (Double.IsPositiveInfinity(a)) + if (double.IsPositiveInfinity(a)) { - return x == 1.0 ? Double.PositiveInfinity : Double.NegativeInfinity; + return x == 1.0 ? double.PositiveInfinity : double.NegativeInfinity; } - if (Double.IsPositiveInfinity(b)) + if (double.IsPositiveInfinity(b)) { - return x == 0.0 ? Double.PositiveInfinity : Double.NegativeInfinity; + return x == 0.0 ? double.PositiveInfinity : double.NegativeInfinity; } if (a == 0.0 && b == 0.0) { - return x == 0.0 || x == 1.0 ? Double.PositiveInfinity : Double.NegativeInfinity; + return x == 0.0 || x == 1.0 ? double.PositiveInfinity : double.NegativeInfinity; + } + + if (a == 0.0) + { + return x == 0.0 ? double.PositiveInfinity : double.NegativeInfinity; + } + + if (b == 0.0) + { + return x == 1.0 ? double.PositiveInfinity : double.NegativeInfinity; } - if (a == 0.0) return x == 0.0 ? Double.PositiveInfinity : Double.NegativeInfinity; - if (b == 0.0) return x == 1.0 ? Double.PositiveInfinity : Double.NegativeInfinity; - if (a == 1.0 && b == 1.0) return 0.0; + if (a == 1.0 && b == 1.0) + { + return 0.0; + } var aa = SpecialFunctions.GammaLn(a + b) - SpecialFunctions.GammaLn(a) - SpecialFunctions.GammaLn(b); - var bb = x == 0.0 ? (a == 1.0 ? 0.0 : Double.NegativeInfinity) : (a - 1.0)*Math.Log(x); - var cc = x == 1.0 ? (b == 1.0 ? 0.0 : Double.NegativeInfinity) : (b - 1.0)*Math.Log(1.0 - x); + var bb = x == 0.0 ? (a == 1.0 ? 0.0 : double.NegativeInfinity) : (a - 1.0)*Math.Log(x); + var cc = x == 1.0 ? (b == 1.0 ? 0.0 : double.NegativeInfinity) : (b - 1.0)*Math.Log(1.0 - x); return aa + bb + cc; } @@ -508,22 +578,32 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double a, double b, double x) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (x < 0.0) return 0.0; - if (x >= 1.0) return 1.0; + if (x < 0.0) + { + return 0.0; + } - if (Double.IsPositiveInfinity(a) && Double.IsPositiveInfinity(b)) + if (x >= 1.0) + { + return 1.0; + } + + if (double.IsPositiveInfinity(a) && double.IsPositiveInfinity(b)) { return x < 0.5 ? 0.0 : 1.0; } - if (Double.IsPositiveInfinity(a)) + if (double.IsPositiveInfinity(a)) { return x < 1.0 ? 0.0 : 1.0; } - if (Double.IsPositiveInfinity(b)) + if (double.IsPositiveInfinity(b)) { return x >= 0.0 ? 1.0 : 0.0; } @@ -538,9 +618,20 @@ namespace MathNet.Numerics.Distributions return 1.0; } - if (a == 0.0) return 1.0; - if (b == 0.0) return x >= 1.0 ? 1.0 : 0.0; - if (a == 1.0 && b == 1.0) return x; + if (a == 0.0) + { + return 1.0; + } + + if (b == 0.0) + { + return x >= 1.0 ? 1.0 : 0.0; + } + + if (a == 1.0 && b == 1.0) + { + return x; + } return SpecialFunctions.BetaRegularized(a, b, x); } @@ -557,7 +648,10 @@ namespace MathNet.Numerics.Distributions /// WARNING: currently not an explicit implementation, hence slow and unreliable. public static double InvCDF(double a, double b, double p) { - if (a < 0.0 || b < 0.0 || p < 0.0 || p > 1.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0 || p < 0.0 || p > 1.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Brent.FindRoot(x => SpecialFunctions.BetaRegularized(a, b, x) - p, 0.0, 1.0, accuracy: 1e-8); } @@ -571,7 +665,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, a, b); } @@ -585,7 +682,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, a, b); } @@ -600,7 +700,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, a, b); } @@ -613,7 +716,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, a, b); } @@ -626,7 +732,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, a, b); } @@ -640,7 +749,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double a, double b) { - if (a < 0.0 || b < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (a < 0.0 || b < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, a, b); } diff --git a/src/Numerics/Distributions/Binomial.cs b/src/Numerics/Distributions/Binomial.cs index 9f019613..fd4acad6 100644 --- a/src/Numerics/Distributions/Binomial.cs +++ b/src/Numerics/Distributions/Binomial.cs @@ -165,7 +165,10 @@ namespace MathNet.Numerics.Distributions { get { - if (_p == 0.0 || _p == 1.0) return 0.0; + if (_p == 0.0 || _p == 1.0) + { + return 0.0; + } var e = 0.0; for (var i = 0; i <= _trials; i++) @@ -209,8 +212,15 @@ namespace MathNet.Numerics.Distributions { get { - if (_p == 1.0) return _trials; - if (_p == 0.0) return 0; + if (_p == 1.0) + { + return _trials; + } + + if (_p == 0.0) + { + return 0; + } return (int)Math.Floor((_trials + 1)*_p); } @@ -223,8 +233,15 @@ namespace MathNet.Numerics.Distributions { get { - if (_p == 1.0) return new[] { _trials }; - if (_p == 0.0) return new[] { 0 }; + if (_p == 1.0) + { + return new[] { _trials }; + } + + if (_p == 0.0) + { + return new[] { 0 }; + } double td = (_trials + 1)*_p; int t = (int)Math.Floor(td); @@ -279,10 +296,26 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double p, int n, int k) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k < 0 || k > n) return 0.0; - if (p == 0.0) return k == 0 ? 1.0 : 0.0; - if (p == 1.0) return k == n ? 1.0 : 0.0; + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k < 0 || k > n) + { + return 0.0; + } + + if (p == 0.0) + { + return k == 0 ? 1.0 : 0.0; + } + + if (p == 1.0) + { + return k == n ? 1.0 : 0.0; + } + return Math.Exp(SpecialFunctions.BinomialLn(n, k) + (k*Math.Log(p)) + ((n - k)*Math.Log(1.0 - p))); } @@ -295,10 +328,26 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double p, int n, int k) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k < 0 || k > n) return Double.NegativeInfinity; - if (p == 0.0) return k == 0 ? 0.0 : Double.NegativeInfinity; - if (p == 1.0) return k == n ? 0.0 : Double.NegativeInfinity; + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k < 0 || k > n) + { + return double.NegativeInfinity; + } + + if (p == 0.0) + { + return k == 0 ? 0.0 : double.NegativeInfinity; + } + + if (p == 1.0) + { + return k == n ? 0.0 : double.NegativeInfinity; + } + return SpecialFunctions.BinomialLn(n, k) + (k*Math.Log(p)) + ((n - k)*Math.Log(1.0 - p)); } @@ -312,9 +361,21 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double p, int n, double x) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (x < 0.0) return 0.0; - if (x > n) return 1.0; + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (x < 0.0) + { + return 0.0; + } + + if (x > n) + { + return 1.0; + } + double k = Math.Floor(x); return SpecialFunctions.BetaRegularized(n - k, k + 1, 1 - p); } @@ -350,6 +411,7 @@ namespace MathNet.Numerics.Distributions { sum += uniform[k + j] < p ? 1 : 0; } + values[i] = sum; } }); @@ -398,7 +460,10 @@ namespace MathNet.Numerics.Distributions /// The number of successes in trials. public static int Sample(System.Random rnd, double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, p, n); } @@ -412,7 +477,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of successes in trials. public static IEnumerable Samples(System.Random rnd, double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, p, n); } @@ -427,7 +495,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of successes in trials. public static void Samples(System.Random rnd, int[] values, double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, p, n); } @@ -440,7 +511,10 @@ namespace MathNet.Numerics.Distributions /// The number of successes in trials. public static int Sample(double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, p, n); } @@ -453,7 +527,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of successes in trials. public static IEnumerable Samples(double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, p, n); } @@ -467,7 +544,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of successes in trials. public static void Samples(int[] values, double p, int n) { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0 && n >= 0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, p, n); } diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs index ad9bd6c5..7c6c70c4 100644 --- a/src/Numerics/Distributions/Categorical.cs +++ b/src/Numerics/Distributions/Categorical.cs @@ -169,7 +169,7 @@ namespace MathNet.Numerics.Distributions for (int i = 0; i < p.Length; i++) { double t = p[i]; - if (t < 0.0 || Double.IsNaN(t)) + if (t < 0.0 || double.IsNaN(t)) { return false; } @@ -191,7 +191,7 @@ namespace MathNet.Numerics.Distributions for (int i = 0; i < cdf.Length; i++) { double t = cdf[i]; - if (t < 0.0 || Double.IsNaN(t) || t < last) + if (t < 0.0 || double.IsNaN(t) || t < last) { return false; } @@ -229,12 +229,12 @@ namespace MathNet.Numerics.Distributions { // Mean = E[X] = Sum(x * p(x), x=0..N-1) // where f(x) is the probability mass function, and N is the number of categories. - var sum = 0.0; for (int i = 0; i < _pmfNormalized.Length; i++) { sum += i*_pmfNormalized[i]; } + return sum; } } @@ -262,6 +262,7 @@ namespace MathNet.Numerics.Distributions var r = i - m; sum += r*r*_pmfNormalized[i]; } + return sum; } } @@ -384,7 +385,7 @@ namespace MathNet.Numerics.Distributions /// An integer between 0 and the size of the categorical (exclusive), that corresponds to the inverse CDF for the given probability. public int InverseCumulativeDistribution(double probability) { - if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability)) + if (probability < 0.0 || probability > 1.0 || double.IsNaN(probability)) { throw new ArgumentOutOfRangeException("probability"); } @@ -413,8 +414,15 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - if (k < 0) return 0.0; - if (k >= probabilityMass.Length) return 0.0; + if (k < 0) + { + return 0.0; + } + + if (k >= probabilityMass.Length) + { + return 0.0; + } return probabilityMass[k]/probabilityMass.Sum(); } @@ -446,8 +454,15 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - if (x < 0.0) return 0.0; - if (x >= probabilityMass.Length) return 1.0; + if (x < 0.0) + { + return 0.0; + } + + if (x >= probabilityMass.Length) + { + return 1.0; + } var cdfUnnormalized = ProbabilityMassToCumulativeDistribution(probabilityMass); return cdfUnnormalized[(int)Math.Floor(x)]/cdfUnnormalized[cdfUnnormalized.Length - 1]; @@ -468,7 +483,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability)) + if (probability < 0.0 || probability > 1.0 || double.IsNaN(probability)) { throw new ArgumentOutOfRangeException("probability"); } @@ -498,7 +513,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - if (probability < 0.0 || probability > 1.0 || Double.IsNaN(probability)) + if (probability < 0.0 || probability > 1.0 || double.IsNaN(probability)) { throw new ArgumentOutOfRangeException("probability"); } @@ -567,6 +582,7 @@ namespace MathNet.Numerics.Distributions { idx++; } + values[i] = idx; } }); @@ -615,7 +631,9 @@ namespace MathNet.Numerics.Distributions public static int Sample(System.Random rnd, double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); return SampleUnchecked(rnd, cdf); @@ -630,7 +648,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(System.Random rnd, double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); return SamplesUnchecked(rnd, cdf); @@ -646,7 +666,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(System.Random rnd, int[] values, double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); SamplesUnchecked(rnd, values, cdf); @@ -660,7 +682,9 @@ namespace MathNet.Numerics.Distributions public static int Sample(double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); return SampleUnchecked(SystemRandomSource.Default, cdf); @@ -674,7 +698,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); return SamplesUnchecked(SystemRandomSource.Default, cdf); @@ -689,7 +715,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(int[] values, double[] probabilityMass) { if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } var cdf = ProbabilityMassToCumulativeDistribution(probabilityMass); SamplesUnchecked(SystemRandomSource.Default, values, cdf); @@ -704,7 +732,9 @@ namespace MathNet.Numerics.Distributions public static int SampleWithCumulativeDistribution(System.Random rnd, double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, cdfUnnormalized); } @@ -718,7 +748,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable SamplesWithCumulativeDistribution(System.Random rnd, double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, cdfUnnormalized); } @@ -733,7 +765,9 @@ namespace MathNet.Numerics.Distributions public static void SamplesWithCumulativeDistribution(System.Random rnd, int[] values, double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, cdfUnnormalized); } @@ -746,7 +780,9 @@ namespace MathNet.Numerics.Distributions public static int SampleWithCumulativeDistribution(double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, cdfUnnormalized); } @@ -759,7 +795,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable SamplesWithCumulativeDistribution(double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, cdfUnnormalized); } @@ -773,7 +811,9 @@ namespace MathNet.Numerics.Distributions public static void SamplesWithCumulativeDistribution(int[] values, double[] cdfUnnormalized) { if (Control.CheckDistributionParameters && !IsValidCumulativeDistribution(cdfUnnormalized)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, cdfUnnormalized); } diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs index 9b70b9b9..76aba49b 100644 --- a/src/Numerics/Distributions/Cauchy.cs +++ b/src/Numerics/Distributions/Cauchy.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics.Distributions /// The scale (γ) of the distribution. Range: γ > 0. public static bool IsValidParameterSet(double location, double scale) { - return scale > 0.0 && !Double.IsNaN(location); + return scale > 0.0 && !double.IsNaN(location); } /// @@ -195,7 +195,7 @@ namespace MathNet.Numerics.Distributions /// public double Minimum { - get { return Double.NegativeInfinity; } + get { return double.NegativeInfinity; } } /// @@ -203,7 +203,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -313,7 +313,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 1.0/(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale)))); } @@ -328,7 +331,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return -Math.Log(Constants.Pi*scale*(1.0 + (((x - location)/scale)*((x - location)/scale)))); } @@ -343,7 +349,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Atan((x - location)/scale)/Constants.Pi + 0.5; } @@ -359,7 +368,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double location, double scale, double p) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return p <= 0.0 ? double.NegativeInfinity : p >= 1.0 ? double.PositiveInfinity : location + scale*Math.Tan((p - 0.5)*Constants.Pi); @@ -374,7 +386,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, location, scale); } @@ -388,7 +403,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, location, scale); } @@ -403,7 +421,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, location, scale); } @@ -416,7 +437,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, location, scale); } @@ -429,7 +453,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, location, scale); } @@ -443,7 +470,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, location, scale); } diff --git a/src/Numerics/Distributions/Chi.cs b/src/Numerics/Distributions/Chi.cs index 54c3e511..d813b531 100644 --- a/src/Numerics/Distributions/Chi.cs +++ b/src/Numerics/Distributions/Chi.cs @@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -271,6 +271,7 @@ namespace MathNet.Numerics.Distributions { sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2); } + return Math.Sqrt(sum); } @@ -288,6 +289,7 @@ namespace MathNet.Numerics.Distributions { sum += standard[k + j]*standard[k + j]; } + values[i] = Math.Sqrt(sum); } }); @@ -310,7 +312,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return (Math.Pow(2.0, 1.0 - (freedom/2.0))*Math.Pow(x, freedom - 1.0)*Math.Exp(-x*x/2.0))/SpecialFunctions.Gamma(freedom/2.0); } @@ -324,7 +329,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return ((1.0 - (freedom/2.0))*Math.Log(2.0)) + ((freedom - 1.0)*Math.Log(x)) - (x*x/2.0) - SpecialFunctions.GammaLn(freedom/2.0); } @@ -338,7 +346,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.GammaLowerIncomplete(freedom/2.0, x*x/2.0)/SpecialFunctions.Gamma(freedom/2.0); } @@ -351,7 +362,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, freedom); } @@ -364,7 +378,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, freedom); } @@ -378,7 +395,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, freedom); } @@ -390,7 +410,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, freedom); } @@ -402,7 +425,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, freedom); } @@ -415,7 +441,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, int freedom) { - if (freedom <= 0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, freedom); } diff --git a/src/Numerics/Distributions/ChiSquared.cs b/src/Numerics/Distributions/ChiSquared.cs index ffe7c1e9..53a2a7ba 100644 --- a/src/Numerics/Distributions/ChiSquared.cs +++ b/src/Numerics/Distributions/ChiSquared.cs @@ -253,7 +253,7 @@ namespace MathNet.Numerics.Distributions static double SampleUnchecked(System.Random rnd, double freedom) { // Use the simple method if the degrees of freedom is an integer anyway - if (Math.Floor(freedom) == freedom && freedom < Int32.MaxValue) + if (Math.Floor(freedom) == freedom && freedom < int.MaxValue) { double sum = 0; var n = (int)freedom; @@ -261,6 +261,7 @@ namespace MathNet.Numerics.Distributions { sum += Math.Pow(Normal.Sample(rnd, 0.0, 1.0), 2); } + return sum; } @@ -272,7 +273,7 @@ namespace MathNet.Numerics.Distributions internal static void SamplesUnchecked(System.Random rnd, double[] values, double freedom) { // Use the simple method if the degrees of freedom is an integer anyway - if (Math.Floor(freedom) == freedom && freedom < Int32.MaxValue) + if (Math.Floor(freedom) == freedom && freedom < int.MaxValue) { var n = (int)freedom; var standard = new double[values.Length*n]; @@ -287,6 +288,7 @@ namespace MathNet.Numerics.Distributions { sum += standard[k + j]*standard[k + j]; } + values[i] = Math.Sqrt(sum); } }); @@ -315,7 +317,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return (Math.Pow(x, (freedom/2.0) - 1.0)*Math.Exp(-x/2.0))/(Math.Pow(2.0, freedom/2.0)*SpecialFunctions.Gamma(freedom/2.0)); } @@ -329,7 +334,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return (-x/2.0) + (((freedom/2.0) - 1.0)*Math.Log(x)) - ((freedom/2.0)*Math.Log(2)) - SpecialFunctions.GammaLn(freedom/2.0); } @@ -343,7 +351,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double freedom, double x) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.GammaLowerIncomplete(freedom/2.0, x/2.0)/SpecialFunctions.Gamma(freedom/2.0); } @@ -356,7 +367,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, freedom); } @@ -369,7 +383,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static IEnumerable Samples(System.Random rnd, double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, freedom); } @@ -383,7 +400,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static void Samples(System.Random rnd, double[] values, double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, freedom); } @@ -395,7 +415,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, freedom); } @@ -407,7 +430,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static IEnumerable Samples(double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, freedom); } @@ -420,7 +446,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static void Samples(double[] values, double freedom) { - if (freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, freedom); } diff --git a/src/Numerics/Distributions/ContinuousUniform.cs b/src/Numerics/Distributions/ContinuousUniform.cs index 877e51b0..19764732 100644 --- a/src/Numerics/Distributions/ContinuousUniform.cs +++ b/src/Numerics/Distributions/ContinuousUniform.cs @@ -230,7 +230,7 @@ namespace MathNet.Numerics.Distributions /// public double DensityLn(double x) { - return x < _lower || x > _upper ? Double.NegativeInfinity : -Math.Log(_upper - _lower); + return x < _lower || x > _upper ? double.NegativeInfinity : -Math.Log(_upper - _lower); } /// @@ -319,7 +319,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double lower, double upper, double x) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x < lower || x > upper ? 0.0 : 1.0/(upper - lower); } @@ -334,9 +337,12 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double lower, double upper, double x) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - return x < lower || x > upper ? Double.NegativeInfinity : -Math.Log(upper - lower); + return x < lower || x > upper ? double.NegativeInfinity : -Math.Log(upper - lower); } /// @@ -349,7 +355,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double lower, double upper, double x) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x <= lower ? 0.0 : x >= upper ? 1.0 : (x - lower)/(upper - lower); } @@ -365,7 +374,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double lower, double upper, double p) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return p <= 0.0 ? lower : p >= 1.0 ? upper : lower*(1.0 - p) + upper*p; } @@ -379,7 +391,10 @@ namespace MathNet.Numerics.Distributions /// a uniformly distributed sample. public static double Sample(System.Random rnd, double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, lower, upper); } @@ -393,7 +408,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of uniformly distributed samples. public static IEnumerable Samples(System.Random rnd, double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, lower, upper); } @@ -408,7 +426,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, lower, upper); } @@ -421,7 +442,10 @@ namespace MathNet.Numerics.Distributions /// a uniformly distributed sample. public static double Sample(double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, lower, upper); } @@ -434,7 +458,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of uniformly distributed samples. public static IEnumerable Samples(double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, lower, upper); } @@ -448,7 +475,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double lower, double upper) { - if (upper < lower) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (upper < lower) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, lower, upper); } diff --git a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs index 01284511..fb82cbdb 100644 --- a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs +++ b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs @@ -367,6 +367,7 @@ namespace MathNet.Numerics.Distributions { sum += Math.Pow(_lambda, i)/Math.Pow(SpecialFunctions.Factorial(i), _nu)/z; } + return sum; } @@ -379,7 +380,10 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double lambda, double nu, int k) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return Math.Pow(lambda, k)/Math.Pow(SpecialFunctions.Factorial(k), nu)/z; @@ -394,7 +398,10 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double lambda, double nu, int k) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return k*Math.Log(lambda) - nu*SpecialFunctions.FactorialLn(k) - Math.Log(z); @@ -410,7 +417,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double lambda, double nu, double x) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); double sum = 0; @@ -418,6 +428,7 @@ namespace MathNet.Numerics.Distributions { sum += Math.Pow(lambda, i)/Math.Pow(SpecialFunctions.Factorial(i), nu)/z; } + return sum; } @@ -523,6 +534,7 @@ namespace MathNet.Numerics.Distributions p = p*lambda/Math.Pow(k, nu); cdf += p; } + values[i] = k; } }); @@ -572,7 +584,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static int Sample(System.Random rnd, double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return SampleUnchecked(rnd, lambda, nu, z); @@ -586,7 +601,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static IEnumerable Samples(System.Random rnd, double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return SamplesUnchecked(rnd, lambda, nu, z); @@ -601,7 +619,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static void Samples(System.Random rnd, int[] values, double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); SamplesUnchecked(rnd, values, lambda, nu, z); @@ -614,7 +635,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static int Sample(double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return SampleUnchecked(SystemRandomSource.Default, lambda, nu, z); @@ -627,7 +651,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static IEnumerable Samples(double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); return SamplesUnchecked(SystemRandomSource.Default, lambda, nu, z); @@ -641,7 +668,10 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public static void Samples(int[] values, double lambda, double nu) { - if (!(lambda > 0.0 && nu >= 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0 && nu >= 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var z = Normalization(lambda, nu); SamplesUnchecked(SystemRandomSource.Default, values, lambda, nu, z); diff --git a/src/Numerics/Distributions/Dirichlet.cs b/src/Numerics/Distributions/Dirichlet.cs index 722e6270..8e8cf24c 100644 --- a/src/Numerics/Distributions/Dirichlet.cs +++ b/src/Numerics/Distributions/Dirichlet.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.Distributions } _random = SystemRandomSource.Default; - _alpha = (double[]) alpha.Clone(); + _alpha = (double[])alpha.Clone(); } /// @@ -75,7 +75,7 @@ namespace MathNet.Numerics.Distributions } _random = randomSource ?? SystemRandomSource.Default; - _alpha = (double[]) alpha.Clone(); + _alpha = (double[])alpha.Clone(); } /// @@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - _alpha = (double[]) parm.Clone(); + _alpha = (double[])parm.Clone(); } /// @@ -122,7 +122,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - _alpha = (double[]) parm.Clone(); + _alpha = (double[])parm.Clone(); } /// @@ -148,8 +148,15 @@ namespace MathNet.Numerics.Distributions for (int i = 0; i < alpha.Length; i++) { var t = alpha[i]; - if (t < 0.0) return false; - if (t > 0.0) allzero = false; + if (t < 0.0) + { + return false; + } + + if (t > 0.0) + { + allzero = false; + } } return !allzero; diff --git a/src/Numerics/Distributions/DiscreteUniform.cs b/src/Numerics/Distributions/DiscreteUniform.cs index 8efbbd62..da3ad402 100644 --- a/src/Numerics/Distributions/DiscreteUniform.cs +++ b/src/Numerics/Distributions/DiscreteUniform.cs @@ -241,7 +241,10 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(int lower, int upper, int k) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return k >= lower && k <= upper ? 1.0/(upper - lower + 1) : 0.0; } @@ -255,9 +258,12 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(int lower, int upper, int k) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - return k >= lower && k <= upper ? -Math.Log(upper - lower + 1) : Double.NegativeInfinity; + return k >= lower && k <= upper ? -Math.Log(upper - lower + 1) : double.NegativeInfinity; } /// @@ -270,10 +276,20 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(int lower, int upper, double x) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (x < lower) + { + return 0.0; + } - if (x < lower) return 0.0; - if (x >= upper) return 1.0; + if (x >= upper) + { + return 1.0; + } return Math.Min(1.0, (Math.Floor(x) - lower + 1)/(upper - lower + 1)); } @@ -335,7 +351,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the discrete uniform distribution. public static int Sample(System.Random rnd, int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, lower, upper); } @@ -349,7 +368,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the discrete uniform distribution. public static IEnumerable Samples(System.Random rnd, int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, lower, upper); } @@ -364,7 +386,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the discrete uniform distribution. public static void Samples(System.Random rnd, int[] values, int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, lower, upper); } @@ -377,7 +402,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the discrete uniform distribution. public static int Sample(int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, lower, upper); } @@ -390,7 +418,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the discrete uniform distribution. public static IEnumerable Samples(int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, lower, upper); } @@ -404,7 +435,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the discrete uniform distribution. public static void Samples(int[] values, int lower, int upper) { - if (!(lower <= upper)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lower <= upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, lower, upper); } diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs index b6abee39..f4253cdb 100644 --- a/src/Numerics/Distributions/Erlang.cs +++ b/src/Numerics/Distributions/Erlang.cs @@ -153,7 +153,7 @@ namespace MathNet.Numerics.Distributions /// public double Scale { - get { return 1.0 / _rate; } + get { return 1.0/_rate; } } /// @@ -172,14 +172,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return _shape; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape/_rate; @@ -193,14 +193,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape/(_rate*_rate); @@ -214,14 +214,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return Math.Sqrt(_shape)/_rate; @@ -235,14 +235,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape - Math.Log(_rate) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape)); @@ -256,14 +256,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return 2.0/Math.Sqrt(_shape); @@ -282,14 +282,14 @@ namespace MathNet.Numerics.Distributions throw new NotSupportedException(); } - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return _shape; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return (_shape - 1.0)/_rate; @@ -392,11 +392,25 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(int shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (double.IsPositiveInfinity(rate)) + { + return x == shape ? double.PositiveInfinity : 0.0; + } - if (Double.IsPositiveInfinity(rate)) return x == shape ? Double.PositiveInfinity : 0.0; - if (shape == 0.0 && rate == 0.0) return 0.0; - if (shape == 1.0) return rate*Math.Exp(-rate*x); + if (shape == 0.0 && rate == 0.0) + { + return 0.0; + } + + if (shape == 1.0) + { + return rate*Math.Exp(-rate*x); + } return Math.Pow(rate, shape)*Math.Pow(x, shape - 1.0)*Math.Exp(-rate*x)/SpecialFunctions.Gamma(shape); } @@ -417,11 +431,25 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(int shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (double.IsPositiveInfinity(rate)) + { + return x == shape ? double.PositiveInfinity : double.NegativeInfinity; + } + + if (shape == 0.0 && rate == 0.0) + { + return double.NegativeInfinity; + } - if (Double.IsPositiveInfinity(rate)) return x == shape ? Double.PositiveInfinity : Double.NegativeInfinity; - if (shape == 0.0 && rate == 0.0) return Double.NegativeInfinity; - if (shape == 1.0) return Math.Log(rate) - (rate*x); + if (shape == 1.0) + { + return Math.Log(rate) - (rate*x); + } return (shape*Math.Log(rate)) + ((shape - 1.0)*Math.Log(x)) - (rate*x) - SpecialFunctions.GammaLn(shape); } @@ -442,10 +470,20 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(int shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (Double.IsPositiveInfinity(rate)) return x >= shape ? 1.0 : 0.0; - if (shape == 0.0 && rate == 0.0) return 0.0; + if (double.IsPositiveInfinity(rate)) + { + return x >= shape ? 1.0 : 0.0; + } + + if (shape == 0.0 && rate == 0.0) + { + return 0.0; + } return SpecialFunctions.GammaLowerRegularized(shape, x*rate); } diff --git a/src/Numerics/Distributions/Exponential.cs b/src/Numerics/Distributions/Exponential.cs index 62a58d20..8b06db23 100644 --- a/src/Numerics/Distributions/Exponential.cs +++ b/src/Numerics/Distributions/Exponential.cs @@ -183,7 +183,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -281,6 +281,7 @@ namespace MathNet.Numerics.Distributions { r = rnd.NextDouble(); } + values[i] = -Math.Log(r)/rate; } }); @@ -300,7 +301,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double rate, double x) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x < 0.0 ? 0.0 : rate*Math.Exp(-rate*x); } @@ -314,7 +318,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double rate, double x) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Log(rate) - (rate*x); } @@ -328,7 +335,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double rate, double x) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x < 0.0 ? 0.0 : 1.0 - Math.Exp(-rate*x); } @@ -343,7 +353,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double rate, double p) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return p >= 1.0 ? double.PositiveInfinity : -Math.Log(1 - p)/rate; } @@ -356,7 +369,10 @@ namespace MathNet.Numerics.Distributions /// A random number from this distribution. public static double Sample(System.Random rnd, double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, rate); } @@ -370,7 +386,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, rate); } @@ -383,7 +402,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, rate); } @@ -395,7 +417,10 @@ namespace MathNet.Numerics.Distributions /// A random number from this distribution. public static double Sample(double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, rate); } @@ -408,7 +433,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, rate); } @@ -420,7 +448,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double rate) { - if (rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, rate); } diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs index db074e6f..3e38ffa2 100644 --- a/src/Numerics/Distributions/FisherSnedecor.cs +++ b/src/Numerics/Distributions/FisherSnedecor.cs @@ -348,7 +348,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double d1, double d2, double x) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Sqrt(Math.Pow(d1*x, d1)*Math.Pow(d2, d2)/Math.Pow((d1*x) + d2, d1 + d2))/(x*SpecialFunctions.Beta(d1/2.0, d2/2.0)); } @@ -376,7 +379,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double d1, double d2, double x) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.BetaRegularized(d1/2.0, d2/2.0, d1*x/(d1*x + d2)); } @@ -393,7 +399,10 @@ namespace MathNet.Numerics.Distributions /// WARNING: currently not an explicit implementation, hence slow and unreliable. public static double InvCDF(double d1, double d2, double p) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Brent.FindRoot( x => SpecialFunctions.BetaRegularized(d1/2.0, d2/2.0, d1*x/(d1*x + d2)) - p, @@ -409,7 +418,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, d1, d2); } @@ -423,7 +435,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, d1, d2); } @@ -438,7 +453,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, d1, d2); } @@ -451,7 +469,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, d1, d2); } @@ -464,7 +485,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, d1, d2); } @@ -478,7 +502,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double d1, double d2) { - if (d1 <= 0.0 || d2 <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (d1 <= 0.0 || d2 <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, d1, d2); } diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs index 4a02265c..28eadb5a 100644 --- a/src/Numerics/Distributions/Gamma.cs +++ b/src/Numerics/Distributions/Gamma.cs @@ -156,7 +156,7 @@ namespace MathNet.Numerics.Distributions /// public double Scale { - get { return 1.0 / _rate; } + get { return 1.0/_rate; } } /// @@ -175,14 +175,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return _shape; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape/_rate; @@ -196,14 +196,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape/(_rate*_rate); @@ -217,14 +217,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return Math.Sqrt(_shape/(_rate*_rate)); @@ -238,14 +238,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return _shape - Math.Log(_rate) + SpecialFunctions.GammaLn(_shape) + ((1.0 - _shape)*SpecialFunctions.DiGamma(_shape)); @@ -259,14 +259,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return 0.0; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return 2.0/Math.Sqrt(_shape); @@ -280,14 +280,14 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_rate)) + if (double.IsPositiveInfinity(_rate)) { return _shape; } if (_rate == 0.0 && _shape == 0.0) { - return Double.NaN; + return double.NaN; } return (_shape - 1.0)/_rate; @@ -315,7 +315,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -401,7 +401,7 @@ namespace MathNet.Numerics.Distributions /// A sample from a Gamma distributed random variable. internal static double SampleUnchecked(System.Random rnd, double shape, double rate) { - if (Double.IsPositiveInfinity(rate)) + if (double.IsPositiveInfinity(rate)) { return shape; } @@ -413,32 +413,32 @@ namespace MathNet.Numerics.Distributions if (shape < 1.0) { a = shape + 1.0; - alphafix = Math.Pow(rnd.NextDouble(), 1.0 / shape); + alphafix = Math.Pow(rnd.NextDouble(), 1.0/shape); } - var d = a - (1.0 / 3.0); - var c = 1.0 / Math.Sqrt(9.0 * d); + var d = a - (1.0/3.0); + var c = 1.0/Math.Sqrt(9.0*d); while (true) { var x = Normal.Sample(rnd, 0.0, 1.0); - var v = 1.0 + (c * x); + var v = 1.0 + (c*x); while (v <= 0.0) { x = Normal.Sample(rnd, 0.0, 1.0); - v = 1.0 + (c * x); + v = 1.0 + (c*x); } - v = v * v * v; + v = v*v*v; var u = rnd.NextDouble(); - x = x * x; - if (u < 1.0 - (0.0331 * x * x)) + x = x*x; + if (u < 1.0 - (0.0331*x*x)) { - return alphafix * d * v / rate; + return alphafix*d*v/rate; } - if (Math.Log(u) < (0.5 * x) + (d * (1.0 - v + Math.Log(v)))) + if (Math.Log(u) < (0.5*x) + (d*(1.0 - v + Math.Log(v)))) { - return alphafix * d * v / rate; + return alphafix*d*v/rate; } } } @@ -469,11 +469,25 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (Double.IsPositiveInfinity(rate)) return x == shape ? Double.PositiveInfinity : 0.0; - if (shape == 0.0 && rate == 0.0) return 0.0; - if (shape == 1.0) return rate*Math.Exp(-rate*x); + if (double.IsPositiveInfinity(rate)) + { + return x == shape ? double.PositiveInfinity : 0.0; + } + + if (shape == 0.0 && rate == 0.0) + { + return 0.0; + } + + if (shape == 1.0) + { + return rate*Math.Exp(-rate*x); + } return Math.Pow(rate, shape)*Math.Pow(x, shape - 1.0)*Math.Exp(-rate*x)/SpecialFunctions.Gamma(shape); } @@ -488,11 +502,25 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (Double.IsPositiveInfinity(rate)) return x == shape ? Double.PositiveInfinity : Double.NegativeInfinity; - if (shape == 0.0 && rate == 0.0) return Double.NegativeInfinity; - if (shape == 1.0) return Math.Log(rate) - (rate*x); + if (double.IsPositiveInfinity(rate)) + { + return x == shape ? double.PositiveInfinity : double.NegativeInfinity; + } + + if (shape == 0.0 && rate == 0.0) + { + return double.NegativeInfinity; + } + + if (shape == 1.0) + { + return Math.Log(rate) - (rate*x); + } return (shape*Math.Log(rate)) + ((shape - 1.0)*Math.Log(x)) - (rate*x) - SpecialFunctions.GammaLn(shape); } @@ -507,10 +535,20 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double shape, double rate, double x) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (Double.IsPositiveInfinity(rate)) return x >= shape ? 1.0 : 0.0; - if (shape == 0.0 && rate == 0.0) return 0.0; + if (double.IsPositiveInfinity(rate)) + { + return x >= shape ? 1.0 : 0.0; + } + + if (shape == 0.0 && rate == 0.0) + { + return 0.0; + } return SpecialFunctions.GammaLowerRegularized(shape, x*rate); } @@ -526,7 +564,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double shape, double rate, double p) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.GammaLowerRegularizedInv(shape, p)/rate; } @@ -540,7 +581,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, shape, rate); } @@ -554,7 +598,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, shape, rate); } @@ -569,7 +616,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, shape, rate); } @@ -582,7 +632,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, shape, rate); } @@ -595,7 +648,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, shape, rate); } @@ -609,7 +665,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double shape, double rate) { - if (shape < 0.0 || rate < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape < 0.0 || rate < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, shape, rate); } diff --git a/src/Numerics/Distributions/Geometric.cs b/src/Numerics/Distributions/Geometric.cs index 6a43e236..8d1f80f2 100644 --- a/src/Numerics/Distributions/Geometric.cs +++ b/src/Numerics/Distributions/Geometric.cs @@ -195,7 +195,11 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public double Probability(int k) { - if (k <= 0) return 0.0; + if (k <= 0) + { + return 0.0; + } + return Math.Pow(1.0 - _p, k - 1)*_p; } @@ -206,7 +210,11 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public double ProbabilityLn(int k) { - if (k <= 0) return Double.NegativeInfinity; + if (k <= 0) + { + return double.NegativeInfinity; + } + return ((k - 1)*Math.Log(1.0 - _p)) + Math.Log(_p); } @@ -228,8 +236,16 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double p, int k) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k <= 0) return 0.0; + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k <= 0) + { + return 0.0; + } + return Math.Pow(1.0 - p, k - 1)*p; } @@ -241,8 +257,16 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double p, int k) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (k <= 0) return Double.NegativeInfinity; + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (k <= 0) + { + return double.NegativeInfinity; + } + return ((k - 1)*Math.Log(1.0 - p)) + Math.Log(p); } @@ -255,7 +279,11 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double p, double x) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return 1.0 - Math.Pow(1.0 - p, x); } @@ -339,7 +367,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static int Sample(System.Random rnd, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, p); } @@ -351,7 +382,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static IEnumerable Samples(System.Random rnd, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, p); } @@ -364,7 +398,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static void Samples(System.Random rnd, int[] values, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, p); } @@ -375,7 +412,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static int Sample(double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, p); } @@ -386,7 +426,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static IEnumerable Samples(double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, p); } @@ -398,7 +441,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public static void Samples(int[] values, double p) { - if (!(p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, p); } diff --git a/src/Numerics/Distributions/Hypergeometric.cs b/src/Numerics/Distributions/Hypergeometric.cs index bb37e41a..2eb44e33 100644 --- a/src/Numerics/Distributions/Hypergeometric.cs +++ b/src/Numerics/Distributions/Hypergeometric.cs @@ -298,8 +298,15 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - if (x < Math.Max(0, draws + success - population)) return 0.0; - if (x >= Math.Min(success, draws)) return 1.0; + if (x < Math.Max(0, draws + success - population)) + { + return 0.0; + } + + if (x >= Math.Min(success, draws)) + { + return 1.0; + } var k = (int)Math.Floor(x); var denominatorLn = SpecialFunctions.BinomialLn(population, draws); @@ -308,6 +315,7 @@ namespace MathNet.Numerics.Distributions { sum += Math.Exp(SpecialFunctions.BinomialLn(success, i) + SpecialFunctions.BinomialLn(population - success, draws - i) - denominatorLn); } + return sum; } @@ -335,7 +343,8 @@ namespace MathNet.Numerics.Distributions population--; draws--; - } while (0 < draws); + } + while (0 < draws); return x; } @@ -392,7 +401,9 @@ namespace MathNet.Numerics.Distributions public static int Sample(System.Random rnd, int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, population, success, draws); } @@ -407,7 +418,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(System.Random rnd, int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, population, success, draws); } @@ -423,7 +436,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(System.Random rnd, int[] values, int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, population, success, draws); } @@ -437,7 +452,9 @@ namespace MathNet.Numerics.Distributions public static int Sample(int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, population, success, draws); } @@ -451,7 +468,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, population, success, draws); } @@ -466,7 +485,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(int[] values, int population, int success, int draws) { if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, population, success, draws); } diff --git a/src/Numerics/Distributions/InverseGamma.cs b/src/Numerics/Distributions/InverseGamma.cs index 3f151964..e3392fac 100644 --- a/src/Numerics/Distributions/InverseGamma.cs +++ b/src/Numerics/Distributions/InverseGamma.cs @@ -223,7 +223,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -317,7 +317,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double shape, double scale, double x) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x < 0.0 ? 0.0 : Math.Pow(scale, shape)*Math.Pow(x, -shape - 1.0)*Math.Exp(-scale/x)/SpecialFunctions.Gamma(shape); } @@ -345,7 +348,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double shape, double scale, double x) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.GammaUpperRegularized(shape, scale/x); } @@ -359,7 +365,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, shape, scale); } @@ -373,7 +382,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, shape, scale); } @@ -388,7 +400,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, shape, scale); } @@ -401,7 +416,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, shape, scale); } @@ -414,7 +432,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, shape, scale); } @@ -428,7 +449,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, shape, scale); } diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs index bafa61c1..59f3d77e 100644 --- a/src/Numerics/Distributions/Laplace.cs +++ b/src/Numerics/Distributions/Laplace.cs @@ -111,7 +111,7 @@ namespace MathNet.Numerics.Distributions /// The scale (b) of the distribution. Range: b > 0. public static bool IsValidParameterSet(double location, double scale) { - return scale > 0.0 && !Double.IsNaN(location); + return scale > 0.0 && !double.IsNaN(location); } /// @@ -200,7 +200,7 @@ namespace MathNet.Numerics.Distributions /// public double Minimum { - get { return Double.NegativeInfinity; } + get { return double.NegativeInfinity; } } /// @@ -208,7 +208,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -307,7 +307,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Exp(-Math.Abs(x - location)/scale)/(2.0*scale); } @@ -322,7 +325,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return -Math.Abs(x - location)/scale - Math.Log(2.0*scale); } @@ -337,7 +343,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double location, double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 0.5*(1.0 + (Math.Sign(x - location)*(1.0 - Math.Exp(-Math.Abs(x - location)/scale)))); } @@ -351,7 +360,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, location, scale); } @@ -365,7 +377,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, location, scale); } @@ -380,7 +395,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, location, scale); } @@ -393,7 +411,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, location, scale); } @@ -406,7 +427,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, location, scale); } @@ -420,7 +444,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double location, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, location, scale); } diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs index f10f02b3..b59bb8af 100644 --- a/src/Numerics/Distributions/LogNormal.cs +++ b/src/Numerics/Distributions/LogNormal.cs @@ -143,7 +143,7 @@ namespace MathNet.Numerics.Distributions /// The shape (σ) of the distribution. Range: σ ≥ 0. public static bool IsValidParameterSet(double mu, double sigma) { - return sigma >= 0.0 && !Double.IsNaN(mu); + return sigma >= 0.0 && !double.IsNaN(mu); } /// @@ -252,7 +252,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -282,7 +282,7 @@ namespace MathNet.Numerics.Distributions { if (x < 0.0) { - return Double.NegativeInfinity; + return double.NegativeInfinity; } var a = (Math.Log(x) - _mu)/_sigma; @@ -373,7 +373,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: lognpdf public static double PDF(double mu, double sigma, double x) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } if (x < 0.0) { @@ -394,11 +397,14 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double mu, double sigma, double x) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } if (x < 0.0) { - return Double.NegativeInfinity; + return double.NegativeInfinity; } var a = (Math.Log(x) - mu)/sigma; @@ -416,7 +422,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: logncdf public static double CDF(double mu, double sigma, double x) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return x < 0.0 ? 0.0 : 0.5*(1.0 + SpecialFunctions.Erf((Math.Log(x) - mu)/(sigma*Constants.Sqrt2))); @@ -434,7 +443,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: logninv public static double InvCDF(double mu, double sigma, double p) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return p <= 0.0 ? 0.0 : p >= 1.0 ? double.PositiveInfinity : Math.Exp(mu - sigma*Constants.Sqrt2*SpecialFunctions.ErfcInv(2.0*p)); @@ -449,7 +461,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, mu, sigma); } @@ -463,7 +478,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, mu, sigma); } @@ -478,7 +496,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, mu, sigma); } @@ -491,7 +512,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, mu, sigma); } @@ -504,7 +528,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, mu, sigma); } @@ -518,7 +545,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double mu, double sigma) { - if (sigma < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (sigma < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, mu, sigma); } diff --git a/src/Numerics/Distributions/Multinomial.cs b/src/Numerics/Distributions/Multinomial.cs index 8f78718c..81f4d2c7 100644 --- a/src/Numerics/Distributions/Multinomial.cs +++ b/src/Numerics/Distributions/Multinomial.cs @@ -78,7 +78,7 @@ namespace MathNet.Numerics.Distributions } _random = SystemRandomSource.Default; - _p = (double[]) p.Clone(); + _p = (double[])p.Clone(); _trials = n; } @@ -99,7 +99,7 @@ namespace MathNet.Numerics.Distributions } _random = randomSource ?? SystemRandomSource.Default; - _p = (double[]) p.Clone(); + _p = (double[])p.Clone(); _trials = n; } @@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions throw new ArgumentException(Resources.InvalidDistributionParameters); } - _p = (double[]) p.Clone(); + _p = (double[])p.Clone(); _trials = n; RandomSource = SystemRandomSource.Default; } @@ -159,7 +159,7 @@ namespace MathNet.Numerics.Distributions var sum = 0.0; foreach (var t in p) { - if (t < 0.0 || Double.IsNaN(t)) + if (t < 0.0 || double.IsNaN(t)) { return false; } @@ -205,7 +205,7 @@ namespace MathNet.Numerics.Distributions /// public Vector Mean { - get { return _trials*(DenseVector) P; } + get { return _trials*(DenseVector)P; } } /// @@ -216,7 +216,7 @@ namespace MathNet.Numerics.Distributions get { // Do not use _p, because operations below will modify _p array. Use P or _p.Clone(). - var res = (DenseVector) P; + var res = (DenseVector)P; for (var i = 0; i < res.Count; i++) { res[i] *= _trials*(1 - res[i]); @@ -234,7 +234,7 @@ namespace MathNet.Numerics.Distributions get { // Do not use _p, because operations below will modify _p array. Use P or _p.Clone(). - var res = (DenseVector) P; + var res = (DenseVector)P; for (var i = 0; i < res.Count; i++) { res[i] = (1.0 - (2.0*res[i]))/Math.Sqrt(_trials*(1.0 - res[i])*res[i]); diff --git a/src/Numerics/Distributions/NegativeBinomial.cs b/src/Numerics/Distributions/NegativeBinomial.cs index c7182366..f1ccab53 100644 --- a/src/Numerics/Distributions/NegativeBinomial.cs +++ b/src/Numerics/Distributions/NegativeBinomial.cs @@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public NegativeBinomial(double r, double p) { - if (!IsValidParameterSet(r,p)) + if (!IsValidParameterSet(r, p)) { throw new ArgumentException(Resources.InvalidDistributionParameters); } @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public NegativeBinomial(double r, double p, System.Random randomSource) { - if (!IsValidParameterSet(r,p)) + if (!IsValidParameterSet(r, p)) { throw new ArgumentException(Resources.InvalidDistributionParameters); } @@ -253,7 +253,10 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double r, double p, int k) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SpecialFunctions.GammaLn(r + k) - SpecialFunctions.GammaLn(r) @@ -272,7 +275,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double r, double p, double x) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 1 - SpecialFunctions.BetaRegularized(x + 1, r, 1 - p); } @@ -294,7 +300,8 @@ namespace MathNet.Numerics.Distributions { k = k + 1; p1 = p1*rnd.NextDouble(); - } while (p1 >= c); + } + while (p1 >= c); return k - 1; } @@ -348,7 +355,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static int Sample(System.Random rnd, double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, r, p); } @@ -361,7 +371,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static IEnumerable Samples(System.Random rnd, double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, r, p); } @@ -375,7 +388,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static void Samples(System.Random rnd, int[] values, double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, r, p); } @@ -387,7 +403,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static int Sample(double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, r, p); } @@ -399,7 +418,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static IEnumerable Samples(double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, r, p); } @@ -412,7 +434,10 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public static void Samples(int[] values, double r, double p) { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, r, p); } diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs index f145a72e..ea84578b 100644 --- a/src/Numerics/Distributions/Normal.cs +++ b/src/Numerics/Distributions/Normal.cs @@ -171,7 +171,7 @@ namespace MathNet.Numerics.Distributions /// The standard deviation (σ) of the normal distribution. Range: σ ≥ 0. public static bool IsValidParameterSet(double mean, double stddev) { - return stddev >= 0.0 && !Double.IsNaN(mean); + return stddev >= 0.0 && !double.IsNaN(mean); } /// @@ -252,7 +252,7 @@ namespace MathNet.Numerics.Distributions /// public double Minimum { - get { return Double.NegativeInfinity; } + get { return double.NegativeInfinity; } } /// @@ -260,7 +260,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -342,6 +342,7 @@ namespace MathNet.Numerics.Distributions while (!PolarTransform(rnd.NextDouble(), rnd.NextDouble(), out x, out y)) { } + return mean + (stddev*x); } @@ -354,6 +355,7 @@ namespace MathNet.Numerics.Distributions { continue; } + yield return mean + (stddev*x); yield return mean + (stddev*y); } @@ -373,6 +375,7 @@ namespace MathNet.Numerics.Distributions { n++; } + var uniform = rnd.NextDoubles(n); // Polar transform @@ -384,10 +387,18 @@ namespace MathNet.Numerics.Distributions { continue; } + values[index++] = mean + stddev*x; - if (index == values.Length) return; + if (index == values.Length) + { + return; + } + values[index++] = mean + stddev*y; - if (index == values.Length) return; + if (index == values.Length) + { + return; + } } // remaining, if any @@ -397,10 +408,18 @@ namespace MathNet.Numerics.Distributions { continue; } + values[index++] = mean + stddev*x; - if (index == values.Length) return; + if (index == values.Length) + { + return; + } + values[index++] = mean + stddev*y; - if (index == values.Length) return; + if (index == values.Length) + { + return; + } } } @@ -433,7 +452,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: normpdf public static double PDF(double mean, double stddev, double x) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var d = (x - mean)/stddev; return Math.Exp(-0.5*d*d)/(Constants.Sqrt2Pi*stddev); @@ -449,7 +471,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double mean, double stddev, double x) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var d = (x - mean)/stddev; return (-0.5*d*d) - Math.Log(stddev) - Constants.LogSqrt2Pi; @@ -466,7 +491,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: normcdf public static double CDF(double mean, double stddev, double x) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 0.5*(1.0 + SpecialFunctions.Erf((x - mean)/(stddev*Constants.Sqrt2))); } @@ -483,7 +511,10 @@ namespace MathNet.Numerics.Distributions /// MATLAB: norminv public static double InvCDF(double mean, double stddev, double p) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return mean - (stddev*Constants.Sqrt2*SpecialFunctions.ErfcInv(2.0*p)); } @@ -497,7 +528,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, mean, stddev); } @@ -511,7 +545,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, mean, stddev); } @@ -526,7 +563,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, mean, stddev); } @@ -539,7 +579,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, mean, stddev); } @@ -552,7 +595,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, mean, stddev); } @@ -566,7 +612,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double mean, double stddev) { - if (stddev < 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (stddev < 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, mean, stddev); } diff --git a/src/Numerics/Distributions/NormalGamma.cs b/src/Numerics/Distributions/NormalGamma.cs index 0f7194ba..a0c859c9 100644 --- a/src/Numerics/Distributions/NormalGamma.cs +++ b/src/Numerics/Distributions/NormalGamma.cs @@ -165,7 +165,7 @@ namespace MathNet.Numerics.Distributions /// The inverse scale of the precision. public static bool IsValidParameterSet(double meanLocation, double meanScale, double precShape, double precInvScale) { - return meanScale > 0.0 && precShape > 0.0 && precInvScale > 0.0 && !Double.IsNaN(meanLocation); + return meanScale > 0.0 && precShape > 0.0 && precInvScale > 0.0 && !double.IsNaN(meanLocation); } /// @@ -215,9 +215,9 @@ namespace MathNet.Numerics.Distributions /// the marginal distribution for the mean of the NormalGamma distribution. public StudentT MeanMarginal() { - if (Double.IsPositiveInfinity(_precisionInvScale)) + if (double.IsPositiveInfinity(_precisionInvScale)) { - return new StudentT(_meanLocation, 1.0/(_meanScale*_precisionShape), Double.PositiveInfinity); + return new StudentT(_meanLocation, 1.0/(_meanScale*_precisionShape), double.PositiveInfinity); } return new StudentT(_meanLocation, Math.Sqrt(_precisionInvScale/(_meanScale*_precisionShape)), 2.0*_precisionShape); @@ -238,7 +238,7 @@ namespace MathNet.Numerics.Distributions /// The mean of the distribution. public MeanPrecisionPair Mean { - get { return Double.IsPositiveInfinity(_precisionInvScale) ? new MeanPrecisionPair(_meanLocation, _precisionShape) : new MeanPrecisionPair(_meanLocation, _precisionShape/_precisionInvScale); } + get { return double.IsPositiveInfinity(_precisionInvScale) ? new MeanPrecisionPair(_meanLocation, _precisionShape) : new MeanPrecisionPair(_meanLocation, _precisionShape/_precisionInvScale); } } /// @@ -268,12 +268,12 @@ namespace MathNet.Numerics.Distributions /// Density value public double Density(double mean, double prec) { - if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) + if (double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) { throw new NotSupportedException(); } - if (Double.IsPositiveInfinity(_precisionInvScale)) + if (double.IsPositiveInfinity(_precisionInvScale)) { throw new NotSupportedException(); } @@ -308,12 +308,12 @@ namespace MathNet.Numerics.Distributions /// The log of the density value public double DensityLn(double mean, double prec) { - if (Double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) + if (double.IsPositiveInfinity(_precisionInvScale) && _meanScale == 0.0) { throw new NotSupportedException(); } - if (Double.IsPositiveInfinity(_precisionInvScale)) + if (double.IsPositiveInfinity(_precisionInvScale)) { throw new NotSupportedException(); } @@ -369,7 +369,7 @@ namespace MathNet.Numerics.Distributions var mp = new MeanPrecisionPair(); // Sample the precision. - mp.Precision = Double.IsPositiveInfinity(precisionInverseScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInverseScale); + mp.Precision = double.IsPositiveInfinity(precisionInverseScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInverseScale); // Sample the mean. mp.Mean = meanScale == 0.0 ? meanLocation : Normal.Sample(rnd, meanLocation, Math.Sqrt(1.0/(meanScale*mp.Precision))); @@ -398,7 +398,7 @@ namespace MathNet.Numerics.Distributions var mp = new MeanPrecisionPair(); // Sample the precision. - mp.Precision = Double.IsPositiveInfinity(precisionInvScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInvScale); + mp.Precision = double.IsPositiveInfinity(precisionInvScale) ? precisionShape : Gamma.Sample(rnd, precisionShape, precisionInvScale); // Sample the mean. mp.Mean = meanScale == 0.0 ? meanLocation : Normal.Sample(rnd, meanLocation, Math.Sqrt(1.0/(meanScale*mp.Precision))); diff --git a/src/Numerics/Distributions/Pareto.cs b/src/Numerics/Distributions/Pareto.cs index 44e5e0eb..1f272757 100644 --- a/src/Numerics/Distributions/Pareto.cs +++ b/src/Numerics/Distributions/Pareto.cs @@ -217,7 +217,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -325,7 +325,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double scale, double shape, double x) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return shape*Math.Pow(scale, shape)/Math.Pow(x, shape + 1.0); } @@ -340,7 +343,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double scale, double shape, double x) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Log(shape) + shape*Math.Log(scale) - (shape + 1.0)*Math.Log(x); } @@ -355,7 +361,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double scale, double shape, double x) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 1.0 - Math.Pow(scale/x, shape); } @@ -371,7 +380,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double scale, double shape, double p) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return scale*Math.Pow(1.0 - p, -1.0/shape); } @@ -385,7 +397,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return scale*Math.Pow(rnd.NextDouble(), -1.0/shape); } @@ -399,7 +414,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, scale, shape); } @@ -414,7 +432,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, scale, shape); } @@ -427,7 +448,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, scale, shape); } @@ -440,7 +464,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, scale, shape); } @@ -454,7 +481,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double scale, double shape) { - if (scale <= 0.0 || shape <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || shape <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, scale, shape); } diff --git a/src/Numerics/Distributions/Poisson.cs b/src/Numerics/Distributions/Poisson.cs index 85d4097a..9156c35c 100644 --- a/src/Numerics/Distributions/Poisson.cs +++ b/src/Numerics/Distributions/Poisson.cs @@ -231,7 +231,11 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double lambda, int k) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return Math.Exp(-lambda + (k*Math.Log(lambda)) - SpecialFunctions.FactorialLn(k)); } @@ -243,7 +247,11 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double lambda, int k) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return -lambda + (k*Math.Log(lambda)) - SpecialFunctions.FactorialLn(k); } @@ -256,7 +264,11 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double lambda, double x) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return 1.0 - SpecialFunctions.GammaLowerRegularized(x + 1, lambda); } @@ -283,6 +295,7 @@ namespace MathNet.Numerics.Distributions { count++; } + values[i] = count; } } @@ -427,7 +440,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the Poisson distribution. public static int Sample(System.Random rnd, double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, lambda); } @@ -440,7 +456,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, lambda); } @@ -454,7 +473,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, int[] values, double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, lambda); } @@ -466,7 +488,10 @@ namespace MathNet.Numerics.Distributions /// A sample from the Poisson distribution. public static int Sample(double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, lambda); } @@ -478,7 +503,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, lambda); } @@ -491,7 +519,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(int[] values, double lambda) { - if (!(lambda > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(lambda > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, lambda); } diff --git a/src/Numerics/Distributions/Rayleigh.cs b/src/Numerics/Distributions/Rayleigh.cs index 0323c6c3..73a5c6df 100644 --- a/src/Numerics/Distributions/Rayleigh.cs +++ b/src/Numerics/Distributions/Rayleigh.cs @@ -188,7 +188,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -293,7 +293,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return (x/(scale*scale))*Math.Exp(-x*x/(2.0*scale*scale)); } @@ -307,7 +310,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return Math.Log(x/(scale*scale)) - (x*x/(2.0*scale*scale)); } @@ -321,7 +327,10 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double scale, double x) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return 1.0 - Math.Exp(-x*x/(2.0*scale*scale)); } @@ -336,7 +345,10 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double scale, double p) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return scale*Math.Sqrt(-2*Math.Log(1 - p)); } @@ -349,7 +361,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, scale); } @@ -362,7 +377,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, scale); } @@ -376,7 +394,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, scale); } @@ -388,7 +409,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, scale); } @@ -400,7 +424,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, scale); } @@ -413,7 +440,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double scale) { - if (scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, scale); } diff --git a/src/Numerics/Distributions/Stable.cs b/src/Numerics/Distributions/Stable.cs index 7c5b71f6..ef6fcdbe 100644 --- a/src/Numerics/Distributions/Stable.cs +++ b/src/Numerics/Distributions/Stable.cs @@ -113,7 +113,7 @@ namespace MathNet.Numerics.Distributions /// The location (μ) of the distribution. public static bool IsValidParameterSet(double alpha, double beta, double scale, double location) { - return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location); + return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !double.IsNaN(location); } /// @@ -185,7 +185,7 @@ namespace MathNet.Numerics.Distributions return 2.0*_scale*_scale; } - return Double.PositiveInfinity; + return double.PositiveInfinity; } } @@ -201,7 +201,7 @@ namespace MathNet.Numerics.Distributions return Constants.Sqrt2*_scale; } - return Double.PositiveInfinity; + return double.PositiveInfinity; } } @@ -277,7 +277,7 @@ namespace MathNet.Numerics.Distributions return 0.0; } - return Double.NegativeInfinity; + return double.NegativeInfinity; } } @@ -286,7 +286,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -341,7 +341,7 @@ namespace MathNet.Numerics.Distributions var part1 = beta*Math.Tan(Constants.PiOver2*alpha); var factor = Math.Pow(1.0 + (part1*part1), 1.0/(2.0*alpha)); - var factor1 = Math.Sin(angle)/Math.Pow(Math.Cos(randTheta), (1.0/alpha)); + var factor1 = Math.Sin(angle)/Math.Pow(Math.Cos(randTheta), 1.0/alpha); var factor2 = Math.Pow(Math.Cos(randTheta - angle)/randW, (1 - alpha)/alpha); return location + scale*(factor*factor1*factor2); @@ -374,7 +374,7 @@ namespace MathNet.Numerics.Distributions var part1 = beta*Math.Tan(Constants.PiOver2*alpha); var factor = Math.Pow(1.0 + (part1*part1), 1.0/(2.0*alpha)); - var factor1 = Math.Sin(angle)/Math.Pow(Math.Cos(randTheta), (1.0/alpha)); + var factor1 = Math.Sin(angle)/Math.Pow(Math.Cos(randTheta), 1.0/alpha); var factor2 = Math.Pow(Math.Cos(randTheta - angle)/randWs[i], (1 - alpha)/alpha); values[i] = location + scale*(factor*factor1*factor2); @@ -442,10 +442,19 @@ namespace MathNet.Numerics.Distributions public static double PDF(double alpha, double beta, double scale, double location, double x) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (alpha == 2d) return Normal.PDF(location, Constants.Sqrt2*scale, x); - if (alpha == 1d && beta == 0d) return Cauchy.PDF(location, scale, x); + if (alpha == 2d) + { + return Normal.PDF(location, Constants.Sqrt2*scale, x); + } + + if (alpha == 1d && beta == 0d) + { + return Cauchy.PDF(location, scale, x); + } if (alpha == 0.5d && beta == 1d && x >= location) { @@ -468,14 +477,23 @@ namespace MathNet.Numerics.Distributions public static double PDFLn(double alpha, double beta, double scale, double location, double x) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (alpha == 2d) return Normal.PDFLn(location, Constants.Sqrt2*scale, x); - if (alpha == 1d && beta == 0d) return Cauchy.PDFLn(location, scale, x); + if (alpha == 2d) + { + return Normal.PDFLn(location, Constants.Sqrt2*scale, x); + } + + if (alpha == 1d && beta == 0d) + { + return Cauchy.PDFLn(location, scale, x); + } if (alpha == 0.5d && beta == 1d && x >= location) { - return (Math.Log(scale/Constants.Pi2))/2 - scale/(2*(x - location)) - 1.5*Math.Log(x - location); + return Math.Log(scale/Constants.Pi2)/2 - scale/(2*(x - location)) - 1.5*Math.Log(x - location); } throw new NotSupportedException(); @@ -494,10 +512,19 @@ namespace MathNet.Numerics.Distributions public static double CDF(double alpha, double beta, double scale, double location, double x) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (alpha == 2d) return Normal.CDF(location, Constants.Sqrt2*scale, x); - if (alpha == 1d && beta == 0d) return Cauchy.CDF(location, scale, x); + if (alpha == 2d) + { + return Normal.CDF(location, Constants.Sqrt2*scale, x); + } + + if (alpha == 1d && beta == 0d) + { + return Cauchy.CDF(location, scale, x); + } if (alpha == 0.5d && beta == 1d) { @@ -519,7 +546,9 @@ namespace MathNet.Numerics.Distributions public static double Sample(System.Random rnd, double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, alpha, beta, scale, location); } @@ -536,7 +565,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(System.Random rnd, double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, alpha, beta, scale, location); } @@ -554,7 +585,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(System.Random rnd, double[] values, double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, alpha, beta, scale, location); } @@ -570,7 +603,9 @@ namespace MathNet.Numerics.Distributions public static double Sample(double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, alpha, beta, scale, location); } @@ -586,7 +621,9 @@ namespace MathNet.Numerics.Distributions public static IEnumerable Samples(double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, alpha, beta, scale, location); } @@ -603,7 +640,9 @@ namespace MathNet.Numerics.Distributions public static void Samples(double[] values, double alpha, double beta, double scale, double location) { if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0) + { throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, alpha, beta, scale, location); } diff --git a/src/Numerics/Distributions/StudentT.cs b/src/Numerics/Distributions/StudentT.cs index e16b5d01..d7de6861 100644 --- a/src/Numerics/Distributions/StudentT.cs +++ b/src/Numerics/Distributions/StudentT.cs @@ -135,7 +135,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (ν) for the distribution. Range: ν > 0. public static bool IsValidParameterSet(double location, double scale, double freedom) { - return scale > 0.0 && freedom > 0.0 && !Double.IsNaN(location); + return scale > 0.0 && freedom > 0.0 && !double.IsNaN(location); } /// @@ -176,7 +176,7 @@ namespace MathNet.Numerics.Distributions /// public double Mean { - get { return _freedom > 1.0 ? _location : Double.NaN; } + get { return _freedom > 1.0 ? _location : double.NaN; } } /// @@ -186,7 +186,7 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_freedom)) + if (double.IsPositiveInfinity(_freedom)) { return _scale*_scale; } @@ -196,7 +196,7 @@ namespace MathNet.Numerics.Distributions return _freedom*_scale*_scale/(_freedom - 2.0); } - return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN; + return _freedom > 1.0 ? double.PositiveInfinity : double.NaN; } } @@ -207,7 +207,7 @@ namespace MathNet.Numerics.Distributions { get { - if (Double.IsPositiveInfinity(_freedom)) + if (double.IsPositiveInfinity(_freedom)) { return Math.Sqrt(_scale*_scale); } @@ -217,7 +217,7 @@ namespace MathNet.Numerics.Distributions return Math.Sqrt(_freedom*_scale*_scale/(_freedom - 2.0)); } - return _freedom > 1.0 ? Double.PositiveInfinity : Double.NaN; + return _freedom > 1.0 ? double.PositiveInfinity : double.NaN; } } @@ -228,7 +228,10 @@ namespace MathNet.Numerics.Distributions { get { - if (_location != 0 || _scale != 1.0) throw new NotSupportedException(); + if (_location != 0 || _scale != 1.0) + { + throw new NotSupportedException(); + } return (((_freedom + 1.0)/2.0)*(SpecialFunctions.DiGamma((1.0 + _freedom)/2.0) - SpecialFunctions.DiGamma(_freedom/2.0))) + Math.Log(Math.Sqrt(_freedom)*SpecialFunctions.Beta(_freedom/2.0, 1.0/2.0)); @@ -242,7 +245,10 @@ namespace MathNet.Numerics.Distributions { get { - if (_freedom <= 3) throw new NotSupportedException(); + if (_freedom <= 3) + { + throw new NotSupportedException(); + } return 0.0; } @@ -269,7 +275,7 @@ namespace MathNet.Numerics.Distributions /// public double Minimum { - get { return Double.NegativeInfinity; } + get { return double.NegativeInfinity; } } /// @@ -277,7 +283,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -393,10 +399,16 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double location, double scale, double freedom, double x) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } // TODO JVG we can probably do a better job for Cauchy special case - if (freedom >= 1e+8d) return Normal.PDF(location, scale, x); + if (freedom >= 1e+8d) + { + return Normal.PDF(location, scale, x); + } var d = (x - location)/scale; return Math.Exp(SpecialFunctions.GammaLn((freedom + 1.0)/2.0) - SpecialFunctions.GammaLn(freedom/2.0)) @@ -416,10 +428,16 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double location, double scale, double freedom, double x) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } // TODO JVG we can probably do a better job for Cauchy special case - if (freedom >= 1e+8d) return Normal.PDFLn(location, scale, x); + if (freedom >= 1e+8d) + { + return Normal.PDFLn(location, scale, x); + } var d = (x - location)/scale; return SpecialFunctions.GammaLn((freedom + 1.0)/2.0) @@ -439,10 +457,16 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double location, double scale, double freedom, double x) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } // TODO JVG we can probably do a better job for Cauchy special case - if (Double.IsPositiveInfinity(freedom)) return Normal.CDF(location, scale, x); + if (double.IsPositiveInfinity(freedom)) + { + return Normal.CDF(location, scale, x); + } var k = (x - location)/scale; var h = freedom/(freedom + (k*k)); @@ -463,11 +487,21 @@ namespace MathNet.Numerics.Distributions /// WARNING: currently not an explicit implementation, hence slow and unreliable. public static double InvCDF(double location, double scale, double freedom, double p) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } // TODO JVG we can probably do a better job for Cauchy special case - if (Double.IsPositiveInfinity(freedom)) return Normal.InvCDF(location, scale, p); - if (p == 0.5d) return location; + if (double.IsPositiveInfinity(freedom)) + { + return Normal.InvCDF(location, scale, p); + } + + if (p == 0.5d) + { + return location; + } // TODO PERF: We must implement this explicitly instead of solving for CDF^-1 return Brent.FindRoot(x => @@ -489,7 +523,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, location, scale, freedom); } @@ -504,7 +541,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, location, scale, freedom); } @@ -520,7 +560,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, location, scale, freedom); } @@ -534,7 +577,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, location, scale, freedom); } @@ -548,7 +594,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, location, scale, freedom); } @@ -563,7 +612,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double location, double scale, double freedom) { - if (scale <= 0.0 || freedom <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (scale <= 0.0 || freedom <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, location, scale, freedom); } diff --git a/src/Numerics/Distributions/Triangular.cs b/src/Numerics/Distributions/Triangular.cs index d835f6b4..d808e53b 100644 --- a/src/Numerics/Distributions/Triangular.cs +++ b/src/Numerics/Distributions/Triangular.cs @@ -111,7 +111,7 @@ namespace MathNet.Numerics.Distributions /// Mode (most frequent value). Range: lower ≤ mode ≤ upper public static bool IsValidParameterSet(double lower, double upper, double mode) { - return upper >= mode && mode >= lower && !Double.IsInfinity(upper) && !Double.IsInfinity(lower) && !Double.IsInfinity(mode); + return upper >= mode && mode >= lower && !double.IsInfinity(upper) && !double.IsInfinity(lower) && !double.IsInfinity(mode); } /// @@ -350,14 +350,25 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double lower, double upper, double mode, double x) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var a = lower; var b = upper; var c = mode; - if (a <= x && x <= c) return 2*(x - a)/((b - a)*(c - a)); - if (c < x & x <= b) return 2*(b - x)/((b - a)*(b - c)); + if (a <= x && x <= c) + { + return 2*(x - a)/((b - a)*(c - a)); + } + + if (c < x & x <= b) + { + return 2*(b - x)/((b - a)*(b - c)); + } + return 0; } @@ -386,15 +397,30 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double lower, double upper, double mode, double x) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var a = lower; var b = upper; var c = mode; - if (x < a) return 0; - if (a <= x && x <= c) return (x - a)*(x - a)/((b - a)*(c - a)); - if (c < x & x <= b) return 1 - (b - x)*(b - x)/((b - a)*(b - c)); + if (x < a) + { + return 0; + } + + if (a <= x && x <= c) + { + return (x - a)*(x - a)/((b - a)*(c - a)); + } + + if (c < x & x <= b) + { + return 1 - (b - x)*(b - x)/((b - a)*(b - c)); + } + return 1; } @@ -410,16 +436,31 @@ namespace MathNet.Numerics.Distributions /// public static double InvCDF(double lower, double upper, double mode, double p) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } var a = lower; var b = upper; var c = mode; - if (p <= 0) return lower; + if (p <= 0) + { + return lower; + } + // Taken from http://www.ntrand.com/triangular-distribution/ - if (p < (c - a)/(b - a)) return a + Math.Sqrt(p*(c - a)*(b - a)); - if (p < 1) return b - Math.Sqrt((1 - p)*(b - c)*(b - a)); + if (p < (c - a)/(b - a)) + { + return a + Math.Sqrt(p*(c - a)*(b - a)); + } + + if (p < 1) + { + return b - Math.Sqrt((1 - p)*(b - c)*(b - a)); + } + return upper; } @@ -433,7 +474,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, lower, upper, mode); } @@ -448,7 +492,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, lower, upper, mode); } @@ -464,7 +511,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, lower, upper, mode); } @@ -478,7 +528,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, lower, upper, mode); } @@ -492,7 +545,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, lower, upper, mode); } @@ -507,7 +563,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double lower, double upper, double mode) { - if (!(upper >= mode && mode >= lower)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(upper >= mode && mode >= lower)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, lower, upper, mode); } diff --git a/src/Numerics/Distributions/Weibull.cs b/src/Numerics/Distributions/Weibull.cs index 4ca49099..896cb3cb 100644 --- a/src/Numerics/Distributions/Weibull.cs +++ b/src/Numerics/Distributions/Weibull.cs @@ -226,7 +226,7 @@ namespace MathNet.Numerics.Distributions /// public double Maximum { - get { return Double.PositiveInfinity; } + get { return double.PositiveInfinity; } } /// @@ -276,7 +276,10 @@ namespace MathNet.Numerics.Distributions /// the cumulative distribution at location . public double CumulativeDistribution(double x) { - if (x < 0.0) return 0.0; + if (x < 0.0) + { + return 0.0; + } return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, _shape)*_scalePowShapeInv); } @@ -342,7 +345,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDF(double shape, double scale, double x) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } if (x >= 0.0) { @@ -370,7 +376,10 @@ namespace MathNet.Numerics.Distributions /// public static double PDFLn(double shape, double scale, double x) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } if (x >= 0.0) { @@ -398,9 +407,15 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double shape, double scale, double x) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } - if (x < 0.0) return 0.0; + if (x < 0.0) + { + return 0.0; + } return -SpecialFunctions.ExponentialMinusOne(-Math.Pow(x, shape)*Math.Pow(scale, -shape)); } @@ -414,7 +429,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(System.Random rnd, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, shape, scale); } @@ -428,7 +446,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(System.Random rnd, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, shape, scale); } @@ -443,7 +464,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(System.Random rnd, double[] values, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, shape, scale); } @@ -456,7 +480,10 @@ namespace MathNet.Numerics.Distributions /// a sample from the distribution. public static double Sample(double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, shape, scale); } @@ -469,7 +496,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static IEnumerable Samples(double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, shape, scale); } @@ -483,7 +513,10 @@ namespace MathNet.Numerics.Distributions /// a sequence of samples from the distribution. public static void Samples(double[] values, double shape, double scale) { - if (shape <= 0.0 || scale <= 0.0) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (shape <= 0.0 || scale <= 0.0) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, shape, scale); } diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs index 41d9b60c..69a927db 100644 --- a/src/Numerics/Distributions/Wishart.cs +++ b/src/Numerics/Distributions/Wishart.cs @@ -120,7 +120,7 @@ namespace MathNet.Numerics.Distributions } } - if (degreesOfFreedom <= 0.0 || Double.IsNaN(degreesOfFreedom)) + if (degreesOfFreedom <= 0.0 || double.IsNaN(degreesOfFreedom)) { return false; } diff --git a/src/Numerics/Distributions/Zipf.cs b/src/Numerics/Distributions/Zipf.cs index 2949e32e..419ec7cc 100644 --- a/src/Numerics/Distributions/Zipf.cs +++ b/src/Numerics/Distributions/Zipf.cs @@ -262,7 +262,11 @@ namespace MathNet.Numerics.Distributions /// the cumulative distribution at location . public double CumulativeDistribution(double x) { - if (x < 1) return 0.0; + if (x < 1) + { + return 0.0; + } + return SpecialFunctions.GeneralHarmonic((int)x, _s)/SpecialFunctions.GeneralHarmonic(_n, _s); } @@ -275,7 +279,11 @@ namespace MathNet.Numerics.Distributions /// the probability mass at location . public static double PMF(double s, int n, int k) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return (1.0/Math.Pow(k, s))/SpecialFunctions.GeneralHarmonic(n, s); } @@ -288,7 +296,11 @@ namespace MathNet.Numerics.Distributions /// the log probability mass at location . public static double PMFLn(double s, int n, int k) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + return Math.Log(PMF(s, n, k)); } @@ -302,8 +314,16 @@ namespace MathNet.Numerics.Distributions /// public static double CDF(double s, int n, double x) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); - if (x < 1) return 0.0; + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + if (x < 1) + { + return 0.0; + } + return SpecialFunctions.GeneralHarmonic((int)x, s)/SpecialFunctions.GeneralHarmonic(n, s); } @@ -387,7 +407,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static int Sample(System.Random rnd, double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(rnd, s, n); } @@ -400,7 +423,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static IEnumerable Samples(System.Random rnd, double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(rnd, s, n); } @@ -414,7 +440,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static void Samples(System.Random rnd, int[] values, double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(rnd, values, s, n); } @@ -426,7 +455,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static int Sample(double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SampleUnchecked(SystemRandomSource.Default, s, n); } @@ -438,7 +470,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static IEnumerable Samples(double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } return SamplesUnchecked(SystemRandomSource.Default, s, n); } @@ -451,7 +486,10 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public static void Samples(int[] values, double s, int n) { - if (!(n > 0 && s > 0.0)) throw new ArgumentException(Resources.InvalidDistributionParameters); + if (!(n > 0 && s > 0.0)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } SamplesUnchecked(SystemRandomSource.Default, values, s, n); } diff --git a/src/Numerics/Financial/AbsoluteReturnMeasures.cs b/src/Numerics/Financial/AbsoluteReturnMeasures.cs index e41d7220..5b31bc31 100644 --- a/src/Numerics/Financial/AbsoluteReturnMeasures.cs +++ b/src/Numerics/Financial/AbsoluteReturnMeasures.cs @@ -52,8 +52,9 @@ namespace MathNet.Numerics.Financial foreach (var item in data) { count++; - compoundReturn *= (1 + item); + compoundReturn *= 1 + item; } + return count == 0 ? double.NaN : Math.Pow(compoundReturn, 1.0/count) - 1.0; } diff --git a/src/Numerics/Financial/AbsoluteRiskMeasures.cs b/src/Numerics/Financial/AbsoluteRiskMeasures.cs index 85c00cbd..8e5e3c03 100644 --- a/src/Numerics/Financial/AbsoluteRiskMeasures.cs +++ b/src/Numerics/Financial/AbsoluteRiskMeasures.cs @@ -37,12 +37,11 @@ namespace MathNet.Numerics.Financial { public static class AbsoluteRiskMeasures { - //Note: The following statistics would be considered an absolute risk statistic in the finance realm as well. - // Standard Deviation + // Note: The following statistics would be considered an absolute risk statistic in the finance realm as well. + // Standard Deviation // Annualized Standard Deviation = Math.Sqrt(Monthly Standard Deviation x ( 12 )) // Skewness - // Kurtosis - + // Kurtosis /// /// Calculation is similar to Standard Deviation , except it calculates an average (mean) return only for periods with a gain diff --git a/src/Numerics/GlobalizationHelper.cs b/src/Numerics/GlobalizationHelper.cs index 719f4727..38d52f46 100644 --- a/src/Numerics/GlobalizationHelper.cs +++ b/src/Numerics/GlobalizationHelper.cs @@ -157,7 +157,7 @@ namespace MathNet.Numerics } double value; - if (!Double.TryParse(token.Value, NumberStyles.Any, CultureInfo.CurrentCulture, out value)) + if (!double.TryParse(token.Value, NumberStyles.Any, CultureInfo.CurrentCulture, out value)) { throw new FormatException(); } @@ -225,7 +225,7 @@ namespace MathNet.Numerics } double value; - if (!Double.TryParse(token.Value, NumberStyles.Any, culture, out value)) + if (!double.TryParse(token.Value, NumberStyles.Any, culture, out value)) { throw new FormatException(); } diff --git a/src/Numerics/IntegralTransforms/Fourier.Bluestein.cs b/src/Numerics/IntegralTransforms/Fourier.Bluestein.cs index be5e7e60..020a672a 100644 --- a/src/Numerics/IntegralTransforms/Fourier.Bluestein.cs +++ b/src/Numerics/IntegralTransforms/Fourier.Bluestein.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.Threading; namespace MathNet.Numerics.IntegralTransforms { - #if !NOSYSNUMERICS using System.Numerics; #endif diff --git a/src/Numerics/IntegralTransforms/Fourier.Naive.cs b/src/Numerics/IntegralTransforms/Fourier.Naive.cs index af9da293..c1ae3c13 100644 --- a/src/Numerics/IntegralTransforms/Fourier.Naive.cs +++ b/src/Numerics/IntegralTransforms/Fourier.Naive.cs @@ -33,7 +33,6 @@ using MathNet.Numerics.Threading; namespace MathNet.Numerics.IntegralTransforms { - #if !NOSYSNUMERICS using Complex = System.Numerics.Complex; #endif diff --git a/src/Numerics/IntegralTransforms/Fourier.RadixN.cs b/src/Numerics/IntegralTransforms/Fourier.RadixN.cs index 24c78ab8..34c5e543 100644 --- a/src/Numerics/IntegralTransforms/Fourier.RadixN.cs +++ b/src/Numerics/IntegralTransforms/Fourier.RadixN.cs @@ -34,7 +34,6 @@ using MathNet.Numerics.Threading; namespace MathNet.Numerics.IntegralTransforms { - #if !NOSYSNUMERICS using System.Numerics; #endif @@ -67,7 +66,8 @@ namespace MathNet.Numerics.IntegralTransforms { m >>= 1; j ^= m; - } while ((j & m) == 0); + } + while ((j & m) == 0); } } diff --git a/src/Numerics/IntegralTransforms/Fourier.cs b/src/Numerics/IntegralTransforms/Fourier.cs index cf7b0035..ba4aa93f 100644 --- a/src/Numerics/IntegralTransforms/Fourier.cs +++ b/src/Numerics/IntegralTransforms/Fourier.cs @@ -32,7 +32,6 @@ using System; namespace MathNet.Numerics.IntegralTransforms { - #if !NOSYSNUMERICS using System.Numerics; #endif @@ -152,7 +151,6 @@ namespace MathNet.Numerics.IntegralTransforms InverseScaleByOptions(options, samples); } - /// /// Extract the exponent sign to be used in forward transforms according to the /// provided convention options. diff --git a/src/Numerics/Integration/DoubleExponentialTransformation.cs b/src/Numerics/Integration/DoubleExponentialTransformation.cs index 6d9d9515..a80354c2 100644 --- a/src/Numerics/Integration/DoubleExponentialTransformation.cs +++ b/src/Numerics/Integration/DoubleExponentialTransformation.cs @@ -120,6 +120,7 @@ namespace MathNet.Numerics.Integration double abcissa = Math.Tanh(Constants.PiOver2*Math.Sinh(arg)); weights[i] = Constants.PiOver2*(1 - (abcissa*abcissa))*Math.Cosh(arg); } + return weights; } diff --git a/src/Numerics/Interpolation/CubicSpline.cs b/src/Numerics/Interpolation/CubicSpline.cs index 5152cc43..6367269f 100644 --- a/src/Numerics/Interpolation/CubicSpline.cs +++ b/src/Numerics/Interpolation/CubicSpline.cs @@ -461,7 +461,7 @@ namespace MathNet.Numerics.Interpolation public double Interpolate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _c0[k] + x*(_c1[k] + x*(_c2[k] + x*_c3[k])); } @@ -473,7 +473,7 @@ namespace MathNet.Numerics.Interpolation public double Differentiate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _c1[k] + x*(2*_c2[k] + x*3*_c3[k]); } @@ -485,7 +485,7 @@ namespace MathNet.Numerics.Interpolation public double Differentiate2(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return 2*_c2[k] + x*6*_c3[k]; } @@ -496,7 +496,7 @@ namespace MathNet.Numerics.Interpolation public double Integrate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*(_c1[k]/2 + x*(_c2[k]/3 + x*_c3[k]/4))); } @@ -518,6 +518,7 @@ namespace MathNet.Numerics.Interpolation double w = _x[i + 1] - _x[i]; integral[i + 1] = integral[i] + w*(_c0[i] + w*(_c1[i]/2 + w*(_c2[i]/3 + w*_c3[i]/4))); } + return integral; } diff --git a/src/Numerics/Interpolation/LinearSpline.cs b/src/Numerics/Interpolation/LinearSpline.cs index 882f8aa6..be7a387e 100644 --- a/src/Numerics/Interpolation/LinearSpline.cs +++ b/src/Numerics/Interpolation/LinearSpline.cs @@ -51,7 +51,7 @@ namespace MathNet.Numerics.Interpolation /// Slopes (N) at the sample points (first order coefficients): N public LinearSpline(double[] x, double[] c0, double[] c1) { - if (x.Length != c0.Length + 1 && x.Length != c0.Length || x.Length != c1.Length + 1) + if ((x.Length != c0.Length + 1 && x.Length != c0.Length) || x.Length != c1.Length + 1) { throw new ArgumentException(Resources.ArgumentVectorsSameLength); } @@ -160,7 +160,7 @@ namespace MathNet.Numerics.Interpolation public double Integrate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*_c1[k]/2); } @@ -182,6 +182,7 @@ namespace MathNet.Numerics.Interpolation double w = _x[i + 1] - _x[i]; integral[i + 1] = integral[i] + w*(_c0[i] + w*_c1[i]/2); } + return integral; } diff --git a/src/Numerics/Interpolation/QuadraticSpline.cs b/src/Numerics/Interpolation/QuadraticSpline.cs index 036e63df..866fd000 100644 --- a/src/Numerics/Interpolation/QuadraticSpline.cs +++ b/src/Numerics/Interpolation/QuadraticSpline.cs @@ -87,7 +87,7 @@ namespace MathNet.Numerics.Interpolation public double Interpolate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _c0[k] + x*(_c1[k] + x*_c2[k]); } @@ -120,7 +120,7 @@ namespace MathNet.Numerics.Interpolation public double Integrate(double t) { int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _indefiniteIntegral.Value[k] + x*(_c0[k] + x*(_c1[k]/2 + x*_c2[k]/3)); } @@ -142,6 +142,7 @@ namespace MathNet.Numerics.Interpolation double w = _x[i + 1] - _x[i]; integral[i + 1] = integral[i] + w*(_c0[i] + w*(_c1[i]/2 + w*_c2[i]/3)); } + return integral; } diff --git a/src/Numerics/Interpolation/StepInterpolation.cs b/src/Numerics/Interpolation/StepInterpolation.cs index b851eb8f..532f92da 100644 --- a/src/Numerics/Interpolation/StepInterpolation.cs +++ b/src/Numerics/Interpolation/StepInterpolation.cs @@ -111,7 +111,9 @@ namespace MathNet.Numerics.Interpolation public double Interpolate(double t) { if (t < _x[0]) + { return 0.0; + } int k = LeftBracketIndex(t); return _y[k]; @@ -126,7 +128,10 @@ namespace MathNet.Numerics.Interpolation { int index = Array.BinarySearch(_x, t); if (index >= 0) + { return double.NaN; + } + return 0d; } @@ -147,10 +152,12 @@ namespace MathNet.Numerics.Interpolation public double Integrate(double t) { if (t <= _x[0]) + { return 0.0; + } int k = LeftBracketIndex(t); - var x = (t - _x[k]); + var x = t - _x[k]; return _indefiniteIntegral.Value[k] + x*_y[k]; } @@ -171,6 +178,7 @@ namespace MathNet.Numerics.Interpolation { integral[i + 1] = integral[i] + (_x[i + 1] - _x[i])*_y[i]; } + return integral; } @@ -181,7 +189,10 @@ namespace MathNet.Numerics.Interpolation { int index = Array.BinarySearch(_x, t); if (index >= 0) + { return index; + } + index = ~index; return index - 1; } diff --git a/src/Numerics/Interpolation/TransformedInterpolation.cs b/src/Numerics/Interpolation/TransformedInterpolation.cs index 8f2b918a..6de6d567 100644 --- a/src/Numerics/Interpolation/TransformedInterpolation.cs +++ b/src/Numerics/Interpolation/TransformedInterpolation.cs @@ -28,14 +28,18 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using MathNet.Numerics.Properties; using System; using System.Collections.Generic; using System.Linq; +using MathNet.Numerics.Properties; using MathNet.Numerics.Threading; namespace MathNet.Numerics.Interpolation { + /// + /// Wraps an interpolation with a transformation of the interpolated values. + /// + /// Neither differentiation nor integration is supported. public class TransformedInterpolation : IInterpolation { readonly IInterpolation _interpolation; @@ -51,8 +55,10 @@ namespace MathNet.Numerics.Interpolation /// Create a linear spline interpolation from a set of (x,y) value pairs, sorted ascendingly by x. /// public static TransformedInterpolation InterpolateSorted( - Func transform, Func transformInverse, - double[] x, double[] y) + Func transform, + Func transformInverse, + double[] x, + double[] y) { if (x.Length != y.Length) { @@ -76,8 +82,10 @@ namespace MathNet.Numerics.Interpolation /// WARNING: Works in-place and can thus causes the data array to be reordered and modified. /// public static TransformedInterpolation InterpolateInplace( - Func transform, Func transformInverse, - double[] x, double[] y) + Func transform, + Func transformInverse, + double[] x, + double[] y) { if (x.Length != y.Length) { @@ -100,8 +108,10 @@ namespace MathNet.Numerics.Interpolation /// Create a linear spline interpolation from an unsorted set of (x,y) value pairs. /// public static TransformedInterpolation Interpolate( - Func transform, Func transformInverse, - IEnumerable x, IEnumerable y) + Func transform, + Func transformInverse, + IEnumerable x, + IEnumerable y) { // note: we must make a copy, even if the input was arrays already return InterpolateInplace(transform, transformInverse, x.ToArray(), y.ToArray()); diff --git a/src/Numerics/LinearRegression/MultipleRegression.cs b/src/Numerics/LinearRegression/MultipleRegression.cs index 0ac8a77c..42e1a705 100644 --- a/src/Numerics/LinearRegression/MultipleRegression.cs +++ b/src/Numerics/LinearRegression/MultipleRegression.cs @@ -165,6 +165,7 @@ namespace MathNet.Numerics.LinearRegression { predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } + var response = Vector.Build.Dense(y); return predictor.TransposeThisAndMultiply(predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(response)).ToArray(); } @@ -221,6 +222,7 @@ namespace MathNet.Numerics.LinearRegression { predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } + return predictor.QR().Solve(Vector.Build.Dense(y)).ToArray(); } @@ -276,6 +278,7 @@ namespace MathNet.Numerics.LinearRegression { predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } + return predictor.Svd().Solve(Vector.Build.Dense(y)).ToArray(); } diff --git a/src/Numerics/LinearRegression/SimpleRegression.cs b/src/Numerics/LinearRegression/SimpleRegression.cs index 081e831d..1b2cbc1a 100644 --- a/src/Numerics/LinearRegression/SimpleRegression.cs +++ b/src/Numerics/LinearRegression/SimpleRegression.cs @@ -45,8 +45,15 @@ namespace MathNet.Numerics.LinearRegression /// Response (dependent) public static Tuple Fit(double[] x, double[] y) { - if (x.Length != y.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); - if (x.Length <= 1) throw new ArgumentException(string.Format(Resources.ArrayTooSmall, 2)); + if (x.Length != y.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + if (x.Length <= 1) + { + throw new ArgumentException(string.Format(Resources.ArrayTooSmall, 2)); + } // First Pass: Mean (Less robust but faster than ArrayStatistics.Mean) double mx = 0.0; @@ -56,6 +63,7 @@ namespace MathNet.Numerics.LinearRegression mx += x[i]; my += y[i]; } + mx /= x.Length; my /= y.Length; diff --git a/src/Numerics/LinearRegression/WeightedRegression.cs b/src/Numerics/LinearRegression/WeightedRegression.cs index 9cef62c7..f47b5143 100644 --- a/src/Numerics/LinearRegression/WeightedRegression.cs +++ b/src/Numerics/LinearRegression/WeightedRegression.cs @@ -72,6 +72,7 @@ namespace MathNet.Numerics.LinearRegression { predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One)); } + var response = Vector.Build.Dense(y); var weights = Matrix.Build.Diagonal(w); return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(weights*response)).ToArray(); @@ -101,6 +102,7 @@ namespace MathNet.Numerics.LinearRegression { w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); } + return Weighted(x, y, w); } @@ -116,6 +118,7 @@ namespace MathNet.Numerics.LinearRegression { w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius)); } + return Weighted(x, y, w); } diff --git a/src/Numerics/Random/CryptoRandomSource.cs b/src/Numerics/Random/CryptoRandomSource.cs index 50964986..700f5645 100644 --- a/src/Numerics/Random/CryptoRandomSource.cs +++ b/src/Numerics/Random/CryptoRandomSource.cs @@ -85,7 +85,6 @@ namespace MathNet.Numerics.Random _crypto = rng; } - /// /// Returns a random number between 0.0 and 1.0. /// diff --git a/src/Numerics/Random/Mcg31m1.cs b/src/Numerics/Random/Mcg31m1.cs index 8d5e1585..44ac6b0c 100644 --- a/src/Numerics/Random/Mcg31m1.cs +++ b/src/Numerics/Random/Mcg31m1.cs @@ -76,6 +76,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modulus; } @@ -90,6 +91,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modulus; } @@ -116,6 +118,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong xn = (uint)seed%Modulus; for (int i = 0; i < values.Length; i++) @@ -147,6 +150,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong xn = (uint)seed%Modulus; while (true) diff --git a/src/Numerics/Random/Mcg59.cs b/src/Numerics/Random/Mcg59.cs index 993b1ae2..87d7cc61 100644 --- a/src/Numerics/Random/Mcg59.cs +++ b/src/Numerics/Random/Mcg59.cs @@ -61,7 +61,6 @@ namespace MathNet.Numerics.Random /// if set to true , the class is thread safe. public Mcg59(bool threadSafe) : this(RandomSeed.Robust(), threadSafe) { - } /// @@ -77,6 +76,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modulus; } @@ -92,6 +92,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modulus; } @@ -118,6 +119,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong xn = (uint)seed%Modulus; for (int i = 0; i < values.Length; i++) @@ -149,6 +151,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong xn = (uint)seed%Modulus; while (true) diff --git a/src/Numerics/Random/MersenneTwister.cs b/src/Numerics/Random/MersenneTwister.cs index dca8f693..50ffc5ac 100644 --- a/src/Numerics/Random/MersenneTwister.cs +++ b/src/Numerics/Random/MersenneTwister.cs @@ -223,7 +223,7 @@ namespace MathNet.Numerics.Random _mt[0] = s & 0xffffffff; for (_mti = 1; _mti < N; _mti++) { - _mt[_mti] = (1812433253*(_mt[_mti - 1] ^ (_mt[_mti - 1] >> 30)) + (uint)_mti); + _mt[_mti] = 1812433253*(_mt[_mti - 1] ^ (_mt[_mti - 1] >> 30)) + (uint)_mti; /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ /* In the previous versions, MSBs of the seed affect */ /* only MSBs of the array _mt[]. */ @@ -233,7 +233,6 @@ namespace MathNet.Numerics.Random } } - /* initialize by an array with array-length */ /* init_key is the array for initializing keys */ /* slight change for C++, 2004/2/26 */ @@ -287,18 +286,22 @@ namespace MathNet.Numerics.Random int kk; if (_mti == N + 1) /* if init_genrand() has not been called, */ + { init_genrand(5489); /* a default initial seed is used */ + } for (kk = 0; kk < N - M; kk++) { y = (_mt[kk] & UpperMask) | (_mt[kk + 1] & LowerMask); _mt[kk] = _mt[kk + M] ^ (y >> 1) ^ Mag01[y & 0x1]; } + for (; kk < N - 1; kk++) { y = (_mt[kk] & UpperMask) | (_mt[kk + 1] & LowerMask); _mt[kk] = _mt[kk + (M - N)] ^ (y >> 1) ^ Mag01[y & 0x1]; } + y = (_mt[N - 1] & UpperMask) | (_mt[0] & LowerMask); _mt[N - 1] = _mt[M - 1] ^ (y >> 1) ^ Mag01[y & 0x1]; @@ -308,10 +311,10 @@ namespace MathNet.Numerics.Random y = _mt[_mti++]; /* Tempering */ - y ^= (y >> 11); + y ^= y >> 11; y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; - y ^= (y >> 18); + y ^= y >> 18; return y; } @@ -350,7 +353,7 @@ namespace MathNet.Numerics.Random t[0] = s & 0xffffffff; for (k = 1; k < N; k++) { - t[k] = (1812433253*(t[k - 1] ^ (t[k - 1] >> 30)) + (uint)k); + t[k] = 1812433253*(t[k - 1] ^ (t[k - 1] >> 30)) + (uint)k; t[k] &= 0xffffffff; } @@ -366,11 +369,13 @@ namespace MathNet.Numerics.Random y = (t[kk] & UpperMask) | (t[kk + 1] & LowerMask); t[kk] = t[kk + M] ^ (y >> 1) ^ Mag01[y & 0x1]; } + for (; kk < N - 1; kk++) { y = (t[kk] & UpperMask) | (t[kk + 1] & LowerMask); t[kk] = t[kk + (M - N)] ^ (y >> 1) ^ Mag01[y & 0x1]; } + y = (t[N - 1] & UpperMask) | (t[0] & LowerMask); t[N - 1] = t[M - 1] ^ (y >> 1) ^ Mag01[y & 0x1]; @@ -380,10 +385,10 @@ namespace MathNet.Numerics.Random y = t[k++]; /* Tempering */ - y ^= (y >> 11); + y ^= y >> 11; y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; - y ^= (y >> 18); + y ^= y >> 18; values[i] = y*Reciprocal; } @@ -414,7 +419,7 @@ namespace MathNet.Numerics.Random t[0] = s & 0xffffffff; for (k = 1; k < N; k++) { - t[k] = (1812433253*(t[k - 1] ^ (t[k - 1] >> 30)) + (uint)k); + t[k] = 1812433253*(t[k - 1] ^ (t[k - 1] >> 30)) + (uint)k; t[k] &= 0xffffffff; } @@ -430,11 +435,13 @@ namespace MathNet.Numerics.Random y = (t[kk] & UpperMask) | (t[kk + 1] & LowerMask); t[kk] = t[kk + M] ^ (y >> 1) ^ Mag01[y & 0x1]; } + for (; kk < N - 1; kk++) { y = (t[kk] & UpperMask) | (t[kk + 1] & LowerMask); t[kk] = t[kk + (M - N)] ^ (y >> 1) ^ Mag01[y & 0x1]; } + y = (t[N - 1] & UpperMask) | (t[0] & LowerMask); t[N - 1] = t[M - 1] ^ (y >> 1) ^ Mag01[y & 0x1]; @@ -444,10 +451,10 @@ namespace MathNet.Numerics.Random y = t[k++]; /* Tempering */ - y ^= (y >> 11); + y ^= y >> 11; y ^= (y << 7) & 0x9d2c5680; y ^= (y << 15) & 0xefc60000; - y ^= (y >> 18); + y ^= y >> 18; yield return y*Reciprocal; } diff --git a/src/Numerics/Random/Mrg32k3a.cs b/src/Numerics/Random/Mrg32k3a.cs index 8a2afab4..51c58e6b 100644 --- a/src/Numerics/Random/Mrg32k3a.cs +++ b/src/Numerics/Random/Mrg32k3a.cs @@ -39,7 +39,7 @@ namespace MathNet.Numerics.Random /// /// A 32-bit combined multiple recursive generator with 2 components of order 3. /// - ///Based off of P. L'Ecuyer, "Combined Multiple Recursive Random Number Generators," Operations Research, 44, 5 (1996), 816--822. + /// Based off of P. L'Ecuyer, "Combined Multiple Recursive Random Number Generators," Operations Research, 44, 5 (1996), 816--822. public class Mrg32k3a : RandomSource { const double A12 = 1403580; @@ -90,6 +90,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn3 = (uint)seed; } @@ -104,10 +105,10 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn3 = (uint)seed; } - /// /// Returns a random number between 0.0 and 1.0. /// @@ -131,6 +132,7 @@ namespace MathNet.Numerics.Random { yn += Modulus2; } + _xn3 = _xn2; _xn2 = _xn1; _xn1 = xn; @@ -142,6 +144,7 @@ namespace MathNet.Numerics.Random { return (xn - yn + Modulus1)*Reciprocal; } + return (xn - yn)*Reciprocal; } @@ -175,6 +178,7 @@ namespace MathNet.Numerics.Random { yn += Modulus2; } + x3 = x2; x2 = x1; x1 = xn; @@ -228,6 +232,7 @@ namespace MathNet.Numerics.Random { yn += Modulus2; } + x3 = x2; x2 = x1; x1 = xn; diff --git a/src/Numerics/Random/Palf.cs b/src/Numerics/Random/Palf.cs index c05d2650..9cc24824 100644 --- a/src/Numerics/Random/Palf.cs +++ b/src/Numerics/Random/Palf.cs @@ -268,6 +268,7 @@ namespace MathNet.Numerics.Random x[j] += x[j - shortLag - index]; } } + k = 0; } @@ -328,6 +329,7 @@ namespace MathNet.Numerics.Random x[j] += x[j - shortLag - index]; } } + k = 0; } diff --git a/src/Numerics/Random/RandomExtensions.cs b/src/Numerics/Random/RandomExtensions.cs index 2a120035..26ca1b89 100644 --- a/src/Numerics/Random/RandomExtensions.cs +++ b/src/Numerics/Random/RandomExtensions.cs @@ -195,10 +195,10 @@ namespace MathNet.Numerics.Random // wrap negative numbers around, mapping every negative number to a distinct nonnegative number // MinValue -> 0, -1 -> MaxValue - candidate &= Int64.MaxValue; + candidate &= long.MaxValue; // skip candidate if it is MaxValue. Recursive since rare. - return (candidate == Int64.MaxValue) ? rnd.NextInt64() : candidate; + return (candidate == long.MaxValue) ? rnd.NextInt64() : candidate; } /// @@ -268,7 +268,8 @@ namespace MathNet.Numerics.Random rnd.NextFullRangeInt32(), false, 28); - } while (candidate >= 1.0m); + } + while (candidate >= 1.0m); return candidate; } diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs index 8b19933e..601ddf9f 100644 --- a/src/Numerics/Random/RandomSource.cs +++ b/src/Numerics/Random/RandomSource.cs @@ -252,6 +252,7 @@ namespace MathNet.Numerics.Random buffer[i] = (byte)(((int)(DoSample()*int.MaxValue))%256); } } + return; } diff --git a/src/Numerics/Random/WH1982.cs b/src/Numerics/Random/WH1982.cs index 1bc04d89..26045bd2 100644 --- a/src/Numerics/Random/WH1982.cs +++ b/src/Numerics/Random/WH1982.cs @@ -41,7 +41,7 @@ namespace MathNet.Numerics.Random /// /// See: Wichmann, B. A. & Hill, I. D. (1982), "Algorithm AS 183: /// An efficient and portable pseudo-random number generator". Applied Statistics 31 (1982) 188-190 - /// + /// public class WH1982 : RandomSource { const uint Modx = 30269; @@ -84,6 +84,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modx; } @@ -100,6 +101,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modx; } @@ -130,6 +132,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + uint xn = (uint)seed%Modx; uint yn = 1; uint zn = 1; @@ -167,6 +170,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + uint xn = (uint)seed%Modx; uint yn = 1; uint zn = 1; diff --git a/src/Numerics/Random/WH2006.cs b/src/Numerics/Random/WH2006.cs index 7f5b2df3..e73f9f55 100644 --- a/src/Numerics/Random/WH2006.cs +++ b/src/Numerics/Random/WH2006.cs @@ -87,6 +87,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modx; } @@ -102,6 +103,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + _xn = (uint)seed%Modx; } @@ -133,6 +135,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong wn = 1; ulong xn = (uint)seed%Modx; ulong yn = 1; @@ -172,6 +175,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong wn = 1; ulong xn = (uint)seed%Modx; ulong yn = 1; diff --git a/src/Numerics/Random/Xorshift.cs b/src/Numerics/Random/Xorshift.cs index 8d585108..d5d1d7f9 100644 --- a/src/Numerics/Random/Xorshift.cs +++ b/src/Numerics/Random/Xorshift.cs @@ -283,6 +283,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong x = (uint)seed; for (int i = 0; i < values.Length; i++) @@ -325,6 +326,7 @@ namespace MathNet.Numerics.Random { seed = 1; } + ulong x = (uint)seed; while (true) diff --git a/src/Numerics/RootFinding/Bisection.cs b/src/Numerics/RootFinding/Bisection.cs index d9701ad2..0539bde2 100644 --- a/src/Numerics/RootFinding/Bisection.cs +++ b/src/Numerics/RootFinding/Bisection.cs @@ -69,6 +69,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailed); } @@ -91,6 +92,7 @@ namespace MathNet.Numerics.RootFinding root = lowerBound; return true; } + if (Math.Abs(fmax) < accuracy) { root = upperBound; diff --git a/src/Numerics/RootFinding/Brent.cs b/src/Numerics/RootFinding/Brent.cs index ac1c6b13..cec23b7c 100644 --- a/src/Numerics/RootFinding/Brent.cs +++ b/src/Numerics/RootFinding/Brent.cs @@ -54,6 +54,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailed); } @@ -135,6 +136,7 @@ namespace MathNet.Numerics.RootFinding // Check whether in bounds q = -q; } + p = Math.Abs(p); if (2.0*p < Math.Min(3.0*xMid*q - Math.Abs(xAcc1*q), Math.Abs(e*q))) { @@ -155,6 +157,7 @@ namespace MathNet.Numerics.RootFinding d = xMid; e = d; } + lowerBound = root; fmin = froot; if (Math.Abs(d) > xAcc1) @@ -165,6 +168,7 @@ namespace MathNet.Numerics.RootFinding { root += Sign(xAcc1, xMid); } + froot = f(root); } diff --git a/src/Numerics/RootFinding/Broyden.cs b/src/Numerics/RootFinding/Broyden.cs index 69c1a0fd..c147c004 100644 --- a/src/Numerics/RootFinding/Broyden.cs +++ b/src/Numerics/RootFinding/Broyden.cs @@ -55,6 +55,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailed); } @@ -86,7 +87,11 @@ namespace MathNet.Numerics.RootFinding { double g2 = g*g; double scale = g2/(g2 + gnew*gnew); - if (scale == 0.0) scale = 1.0e-4; + if (scale == 0.0) + { + scale = 1.0e-4; + } + dx = scale*dx; xnew = x + dx; ynew = new DenseVector(f(xnew.Values)); @@ -130,7 +135,10 @@ namespace MathNet.Numerics.RootFinding for (int j = 0; j < dim; j++) { double h = Math.Abs(x0[j])*1.0e-4; - if (h == 0.0) h = 1.0e-4; + if (h == 0.0) + { + h = 1.0e-4; + } var xj = x[j]; x[j] = xj + h; diff --git a/src/Numerics/RootFinding/Cubic.cs b/src/Numerics/RootFinding/Cubic.cs index 6b206a31..330b8f14 100644 --- a/src/Numerics/RootFinding/Cubic.cs +++ b/src/Numerics/RootFinding/Cubic.cs @@ -50,18 +50,18 @@ namespace MathNet.Numerics.RootFinding /// /// Q and R are transformed variables. /// - private static void QR(double a2, double a1, double a0, out double Q, out double R) - { - Q = (3 * a1 - a2 * a2)/9.0; - R = (9.0 * a2 * a1 - 27 * a0 - 2 * a2 * a2 * a2)/54.0; - } + static void QR(double a2, double a1, double a0, out double Q, out double R) + { + Q = (3*a1 - a2*a2)/9.0; + R = (9.0*a2*a1 - 27*a0 - 2*a2*a2*a2)/54.0; + } /// /// n^(1/3) - work around a negative double raised to (1/3) /// - private static double PowThird(double n) + static double PowThird(double n) { - return Math.Pow(Math.Abs(n), 1d / 3d) * Math.Sign(n); + return Math.Pow(Math.Abs(n), 1d/3d)*Math.Sign(n); } /// @@ -73,32 +73,35 @@ namespace MathNet.Numerics.RootFinding double Q, R; QR(a2, a1, a0, out Q, out R); - var Q3 = Q * Q * Q; - var D = Q3 + R * R; - var shift = -a2 / 3d; + var Q3 = Q*Q*Q; + var D = Q3 + R*R; + var shift = -a2/3d; double x1; double x2 = double.NaN; double x3 = double.NaN; - // when D >= 0, use eqn (54)-(56) where S and T are real if (D >= 0) { + // when D >= 0, use eqn (54)-(56) where S and T are real double sqrtD = Math.Pow(D, 0.5); double S = PowThird(R + sqrtD); double T = PowThird(R - sqrtD); x1 = shift + (S + T); if (D == 0) + { x2 = shift - S; + } } - // 3 real roots, use eqn (70)-(73) to calculate the real roots else { - double theta = Math.Acos(R / Math.Sqrt(-Q3)); - x1 = 2d * Math.Sqrt(-Q) * Math.Cos(theta / 3.0) + shift; - x2 = 2d * Math.Sqrt(-Q) * Math.Cos((theta + 2.0 * Constants.Pi) / 3d) + shift; - x3 = 2d * Math.Sqrt(-Q) * Math.Cos((theta - 2.0 * Constants.Pi) / 3d) + shift; + // 3 real roots, use eqn (70)-(73) to calculate the real roots + double theta = Math.Acos(R/Math.Sqrt(-Q3)); + x1 = 2d*Math.Sqrt(-Q)*Math.Cos(theta/3.0) + shift; + x2 = 2d*Math.Sqrt(-Q)*Math.Cos((theta + 2.0*Constants.Pi)/3d) + shift; + x3 = 2d*Math.Sqrt(-Q)*Math.Cos((theta - 2.0*Constants.Pi)/3d) + shift; } + return new Tuple(x1, x2, x3); } diff --git a/src/Numerics/RootFinding/NewtonRaphson.cs b/src/Numerics/RootFinding/NewtonRaphson.cs index dfd88fa8..9ebbbfa5 100644 --- a/src/Numerics/RootFinding/NewtonRaphson.cs +++ b/src/Numerics/RootFinding/NewtonRaphson.cs @@ -56,6 +56,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson); } @@ -76,6 +77,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailedRecommendRobustNewtonRaphson); } diff --git a/src/Numerics/RootFinding/RobustNewtonRaphson.cs b/src/Numerics/RootFinding/RobustNewtonRaphson.cs index 73a22c81..0e45cf7f 100644 --- a/src/Numerics/RootFinding/RobustNewtonRaphson.cs +++ b/src/Numerics/RootFinding/RobustNewtonRaphson.cs @@ -56,6 +56,7 @@ namespace MathNet.Numerics.RootFinding { return root; } + throw new NonConvergenceException(Resources.RootFindingFailed); } @@ -79,6 +80,7 @@ namespace MathNet.Numerics.RootFinding root = lowerBound; return true; } + if (Math.Abs(fmax) < accuracy) { root = upperBound; @@ -136,6 +138,7 @@ namespace MathNet.Numerics.RootFinding fx = fmin; } } + continue; } diff --git a/src/Numerics/RootFinding/ZeroCrossingBracketing.cs b/src/Numerics/RootFinding/ZeroCrossingBracketing.cs index da20c3c2..aa23b477 100644 --- a/src/Numerics/RootFinding/ZeroCrossingBracketing.cs +++ b/src/Numerics/RootFinding/ZeroCrossingBracketing.cs @@ -39,7 +39,6 @@ namespace MathNet.Numerics.RootFinding public static IEnumerable> FindIntervalsWithin(Func f, double lowerBound, double upperBound, int parts) { // TODO: Consider binary-style search instead of linear scan - double fmin = f(lowerBound); double fmax = f(upperBound); @@ -63,11 +62,13 @@ namespace MathNet.Numerics.RootFinding smin = smax; continue; } + if (Math.Sign(sfmax) != sign) { yield return new Tuple(smin, smax); sign = Math.Sign(sfmax); } + smin = smax; } } diff --git a/src/Numerics/Settings.StyleCop b/src/Numerics/Settings.StyleCop index a27e5f5c..34b30a8c 100644 --- a/src/Numerics/Settings.StyleCop +++ b/src/Numerics/Settings.StyleCop @@ -1,6 +1,395 @@ - + - ..\Settings.StyleCop - Linked + NoMerge + + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + + + False + + + + + False + + + + + + x + y + z + + + + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + + + + + + True + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + False + + + + + + \ No newline at end of file diff --git a/src/Numerics/SpecialFunctions/Erf.cs b/src/Numerics/SpecialFunctions/Erf.cs index 6db5855c..b67d3705 100644 --- a/src/Numerics/SpecialFunctions/Erf.cs +++ b/src/Numerics/SpecialFunctions/Erf.cs @@ -53,8 +53,8 @@ namespace MathNet.Numerics /// the error function evaluated at given value. /// /// - /// returns 1 if x == Double.PositiveInfinity. - /// returns -1 if x == Double.NegativeInfinity. + /// returns 1 if x == double.PositiveInfinity. + /// returns -1 if x == double.NegativeInfinity. /// /// public static double Erf(double x) @@ -64,19 +64,19 @@ namespace MathNet.Numerics return 0; } - if (Double.IsPositiveInfinity(x)) + if (double.IsPositiveInfinity(x)) { return 1; } - if (Double.IsNegativeInfinity(x)) + if (double.IsNegativeInfinity(x)) { return -1; } - if (Double.IsNaN(x)) + if (double.IsNaN(x)) { - return Double.NaN; + return double.NaN; } return ErfImp(x, false); @@ -87,8 +87,8 @@ namespace MathNet.Numerics /// the complementary error function evaluated at given value. /// /// - /// returns 0 if x == Double.PositiveInfinity. - /// returns 2 if x == Double.NegativeInfinity. + /// returns 0 if x == double.PositiveInfinity. + /// returns 2 if x == double.NegativeInfinity. /// /// public static double Erfc(double x) @@ -98,35 +98,35 @@ namespace MathNet.Numerics return 1; } - if (Double.IsPositiveInfinity(x)) + if (double.IsPositiveInfinity(x)) { return 0; } - if (Double.IsNegativeInfinity(x)) + if (double.IsNegativeInfinity(x)) { return 2; } - if (Double.IsNaN(x)) + if (double.IsNaN(x)) { - return Double.NaN; + return double.NaN; } return ErfImp(x, true); } - ///Calculates the inverse error function evaluated at z. + /// Calculates the inverse error function evaluated at z. /// The inverse error function evaluated at given value. /// - /// - /// returns Double.PositiveInfinity if z >= 1.0. - /// returns Double.NegativeInfinity if z <= -1.0. - /// + /// + /// returns double.PositiveInfinity if z >= 1.0. + /// returns double.NegativeInfinity if z <= -1.0. + /// /// - ///Calculates the inverse error function evaluated at z. - ///value to evaluate. - ///the inverse error function evaluated at Z. + /// Calculates the inverse error function evaluated at z. + /// value to evaluate. + /// the inverse error function evaluated at Z. public static double ErfInv(double z) { if (z == 0.0) @@ -138,6 +138,7 @@ namespace MathNet.Numerics { return double.PositiveInfinity; } + if (z <= -1.0) { return double.NegativeInfinity; @@ -177,7 +178,7 @@ namespace MathNet.Numerics if (z < -0.5) { - return 2 - ErfImp((-z), true); + return 2 - ErfImp(-z, true); } return 1 + ErfImp(-z, false); @@ -185,16 +186,12 @@ namespace MathNet.Numerics double result; - // // Big bunch of selection statements now to pick which // implementation to use, try to put most likely options // first: - // if (z < 0.5) { - // // We're going to calculate erf: - // if (z < 1e-10) { result = (z*1.125) + (z*0.003379167095512573896158903121545171688); @@ -202,120 +199,118 @@ namespace MathNet.Numerics else { // Worst case absolute error found: 6.688618532e-21 - double[] nc = {0.00337916709551257388990745, -0.00073695653048167948530905, -0.374732337392919607868241, 0.0817442448733587196071743, -0.0421089319936548595203468, 0.0070165709512095756344528, -0.00495091255982435110337458, 0.000871646599037922480317225}; - double[] dc = {1, -0.218088218087924645390535, 0.412542972725442099083918, -0.0841891147873106755410271, 0.0655338856400241519690695, -0.0120019604454941768171266, 0.00408165558926174048329689, -0.000615900721557769691924509}; + double[] nc = { 0.00337916709551257388990745, -0.00073695653048167948530905, -0.374732337392919607868241, 0.0817442448733587196071743, -0.0421089319936548595203468, 0.0070165709512095756344528, -0.00495091255982435110337458, 0.000871646599037922480317225 }; + double[] dc = { 1, -0.218088218087924645390535, 0.412542972725442099083918, -0.0841891147873106755410271, 0.0655338856400241519690695, -0.0120019604454941768171266, 0.00408165558926174048329689, -0.000615900721557769691924509 }; result = (z*1.125) + (z*Evaluate.Polynomial(z, nc)/Evaluate.Polynomial(z, dc)); } } else if ((z < 110) || ((z < 110) && invert)) { - // // We'll be calculating erfc: - // invert = !invert; double r, b; if (z < 0.75) { // Worst case absolute error found: 5.582813374e-21 - double[] nc = {-0.0361790390718262471360258, 0.292251883444882683221149, 0.281447041797604512774415, 0.125610208862766947294894, 0.0274135028268930549240776, 0.00250839672168065762786937}; - double[] dc = {1, 1.8545005897903486499845, 1.43575803037831418074962, 0.582827658753036572454135, 0.124810476932949746447682, 0.0113724176546353285778481}; + double[] nc = { -0.0361790390718262471360258, 0.292251883444882683221149, 0.281447041797604512774415, 0.125610208862766947294894, 0.0274135028268930549240776, 0.00250839672168065762786937 }; + double[] dc = { 1, 1.8545005897903486499845, 1.43575803037831418074962, 0.582827658753036572454135, 0.124810476932949746447682, 0.0113724176546353285778481 }; r = Evaluate.Polynomial(z - 0.5, nc)/Evaluate.Polynomial(z - 0.5, dc); b = 0.3440242112F; } else if (z < 1.25) { // Worst case absolute error found: 4.01854729e-21 - double[] nc = {-0.0397876892611136856954425, 0.153165212467878293257683, 0.191260295600936245503129, 0.10276327061989304213645, 0.029637090615738836726027, 0.0046093486780275489468812, 0.000307607820348680180548455}; - double[] dc = {1, 1.95520072987627704987886, 1.64762317199384860109595, 0.768238607022126250082483, 0.209793185936509782784315, 0.0319569316899913392596356, 0.00213363160895785378615014}; + double[] nc = { -0.0397876892611136856954425, 0.153165212467878293257683, 0.191260295600936245503129, 0.10276327061989304213645, 0.029637090615738836726027, 0.0046093486780275489468812, 0.000307607820348680180548455 }; + double[] dc = { 1, 1.95520072987627704987886, 1.64762317199384860109595, 0.768238607022126250082483, 0.209793185936509782784315, 0.0319569316899913392596356, 0.00213363160895785378615014 }; r = Evaluate.Polynomial(z - 0.75, nc)/Evaluate.Polynomial(z - 0.75, dc); b = 0.419990927F; } else if (z < 2.25) { // Worst case absolute error found: 2.866005373e-21 - double[] nc = {-0.0300838560557949717328341, 0.0538578829844454508530552, 0.0726211541651914182692959, 0.0367628469888049348429018, 0.00964629015572527529605267, 0.00133453480075291076745275, 0.778087599782504251917881e-4}; - double[] dc = {1, 1.75967098147167528287343, 1.32883571437961120556307, 0.552528596508757581287907, 0.133793056941332861912279, 0.0179509645176280768640766, 0.00104712440019937356634038, -0.106640381820357337177643e-7}; + double[] nc = { -0.0300838560557949717328341, 0.0538578829844454508530552, 0.0726211541651914182692959, 0.0367628469888049348429018, 0.00964629015572527529605267, 0.00133453480075291076745275, 0.778087599782504251917881e-4 }; + double[] dc = { 1, 1.75967098147167528287343, 1.32883571437961120556307, 0.552528596508757581287907, 0.133793056941332861912279, 0.0179509645176280768640766, 0.00104712440019937356634038, -0.106640381820357337177643e-7 }; r = Evaluate.Polynomial(z - 1.25, nc)/Evaluate.Polynomial(z - 1.25, dc); b = 0.4898625016F; } else if (z < 3.5) { // Worst case absolute error found: 1.045355789e-21 - double[] nc = {-0.0117907570137227847827732, 0.014262132090538809896674, 0.0202234435902960820020765, 0.00930668299990432009042239, 0.00213357802422065994322516, 0.00025022987386460102395382, 0.120534912219588189822126e-4}; - double[] dc = {1, 1.50376225203620482047419, 0.965397786204462896346934, 0.339265230476796681555511, 0.0689740649541569716897427, 0.00771060262491768307365526, 0.000371421101531069302990367}; + double[] nc = { -0.0117907570137227847827732, 0.014262132090538809896674, 0.0202234435902960820020765, 0.00930668299990432009042239, 0.00213357802422065994322516, 0.00025022987386460102395382, 0.120534912219588189822126e-4 }; + double[] dc = { 1, 1.50376225203620482047419, 0.965397786204462896346934, 0.339265230476796681555511, 0.0689740649541569716897427, 0.00771060262491768307365526, 0.000371421101531069302990367 }; r = Evaluate.Polynomial(z - 2.25, nc)/Evaluate.Polynomial(z - 2.25, dc); b = 0.5317370892F; } else if (z < 5.25) { // Worst case absolute error found: 8.300028706e-22 - double[] nc = {-0.00546954795538729307482955, 0.00404190278731707110245394, 0.0054963369553161170521356, 0.00212616472603945399437862, 0.000394984014495083900689956, 0.365565477064442377259271e-4, 0.135485897109932323253786e-5}; - double[] dc = {1, 1.21019697773630784832251, 0.620914668221143886601045, 0.173038430661142762569515, 0.0276550813773432047594539, 0.00240625974424309709745382, 0.891811817251336577241006e-4, -0.465528836283382684461025e-11}; + double[] nc = { -0.00546954795538729307482955, 0.00404190278731707110245394, 0.0054963369553161170521356, 0.00212616472603945399437862, 0.000394984014495083900689956, 0.365565477064442377259271e-4, 0.135485897109932323253786e-5 }; + double[] dc = { 1, 1.21019697773630784832251, 0.620914668221143886601045, 0.173038430661142762569515, 0.0276550813773432047594539, 0.00240625974424309709745382, 0.891811817251336577241006e-4, -0.465528836283382684461025e-11 }; r = Evaluate.Polynomial(z - 3.5, nc)/Evaluate.Polynomial(z - 3.5, dc); b = 0.5489973426F; } else if (z < 8) { // Worst case absolute error found: 1.700157534e-21 - double[] nc = {-0.00270722535905778347999196, 0.0013187563425029400461378, 0.00119925933261002333923989, 0.00027849619811344664248235, 0.267822988218331849989363e-4, 0.923043672315028197865066e-6}; - double[] dc = {1, 0.814632808543141591118279, 0.268901665856299542168425, 0.0449877216103041118694989, 0.00381759663320248459168994, 0.000131571897888596914350697, 0.404815359675764138445257e-11}; + double[] nc = { -0.00270722535905778347999196, 0.0013187563425029400461378, 0.00119925933261002333923989, 0.00027849619811344664248235, 0.267822988218331849989363e-4, 0.923043672315028197865066e-6 }; + double[] dc = { 1, 0.814632808543141591118279, 0.268901665856299542168425, 0.0449877216103041118694989, 0.00381759663320248459168994, 0.000131571897888596914350697, 0.404815359675764138445257e-11 }; r = Evaluate.Polynomial(z - 5.25, nc)/Evaluate.Polynomial(z - 5.25, dc); b = 0.5571740866F; } else if (z < 11.5) { // Worst case absolute error found: 3.002278011e-22 - double[] nc = {-0.00109946720691742196814323, 0.000406425442750422675169153, 0.000274499489416900707787024, 0.465293770646659383436343e-4, 0.320955425395767463401993e-5, 0.778286018145020892261936e-7}; - double[] dc = {1, 0.588173710611846046373373, 0.139363331289409746077541, 0.0166329340417083678763028, 0.00100023921310234908642639, 0.24254837521587225125068e-4}; + double[] nc = { -0.00109946720691742196814323, 0.000406425442750422675169153, 0.000274499489416900707787024, 0.465293770646659383436343e-4, 0.320955425395767463401993e-5, 0.778286018145020892261936e-7 }; + double[] dc = { 1, 0.588173710611846046373373, 0.139363331289409746077541, 0.0166329340417083678763028, 0.00100023921310234908642639, 0.24254837521587225125068e-4 }; r = Evaluate.Polynomial(z - 8, nc)/Evaluate.Polynomial(z - 8, dc); b = 0.5609807968F; } else if (z < 17) { // Worst case absolute error found: 6.741114695e-21 - double[] nc = {-0.00056907993601094962855594, 0.000169498540373762264416984, 0.518472354581100890120501e-4, 0.382819312231928859704678e-5, 0.824989931281894431781794e-7}; - double[] dc = {1, 0.339637250051139347430323, 0.043472647870310663055044, 0.00248549335224637114641629, 0.535633305337152900549536e-4, -0.117490944405459578783846e-12}; + double[] nc = { -0.00056907993601094962855594, 0.000169498540373762264416984, 0.518472354581100890120501e-4, 0.382819312231928859704678e-5, 0.824989931281894431781794e-7 }; + double[] dc = { 1, 0.339637250051139347430323, 0.043472647870310663055044, 0.00248549335224637114641629, 0.535633305337152900549536e-4, -0.117490944405459578783846e-12 }; r = Evaluate.Polynomial(z - 11.5, nc)/Evaluate.Polynomial(z - 11.5, dc); b = 0.5626493692F; } else if (z < 24) { // Worst case absolute error found: 7.802346984e-22 - double[] nc = {-0.000241313599483991337479091, 0.574224975202501512365975e-4, 0.115998962927383778460557e-4, 0.581762134402593739370875e-6, 0.853971555085673614607418e-8}; - double[] dc = {1, 0.233044138299687841018015, 0.0204186940546440312625597, 0.000797185647564398289151125, 0.117019281670172327758019e-4}; + double[] nc = { -0.000241313599483991337479091, 0.574224975202501512365975e-4, 0.115998962927383778460557e-4, 0.581762134402593739370875e-6, 0.853971555085673614607418e-8 }; + double[] dc = { 1, 0.233044138299687841018015, 0.0204186940546440312625597, 0.000797185647564398289151125, 0.117019281670172327758019e-4 }; r = Evaluate.Polynomial(z - 17, nc)/Evaluate.Polynomial(z - 17, dc); b = 0.5634598136F; } else if (z < 38) { // Worst case absolute error found: 2.414228989e-22 - double[] nc = {-0.000146674699277760365803642, 0.162666552112280519955647e-4, 0.269116248509165239294897e-5, 0.979584479468091935086972e-7, 0.101994647625723465722285e-8}; - double[] dc = {1, 0.165907812944847226546036, 0.0103361716191505884359634, 0.000286593026373868366935721, 0.298401570840900340874568e-5}; + double[] nc = { -0.000146674699277760365803642, 0.162666552112280519955647e-4, 0.269116248509165239294897e-5, 0.979584479468091935086972e-7, 0.101994647625723465722285e-8 }; + double[] dc = { 1, 0.165907812944847226546036, 0.0103361716191505884359634, 0.000286593026373868366935721, 0.298401570840900340874568e-5 }; r = Evaluate.Polynomial(z - 24, nc)/Evaluate.Polynomial(z - 24, dc); b = 0.5638477802F; } else if (z < 60) { // Worst case absolute error found: 5.896543869e-24 - double[] nc = {-0.583905797629771786720406e-4, 0.412510325105496173512992e-5, 0.431790922420250949096906e-6, 0.993365155590013193345569e-8, 0.653480510020104699270084e-10}; - double[] dc = {1, 0.105077086072039915406159, 0.00414278428675475620830226, 0.726338754644523769144108e-4, 0.477818471047398785369849e-6}; + double[] nc = { -0.583905797629771786720406e-4, 0.412510325105496173512992e-5, 0.431790922420250949096906e-6, 0.993365155590013193345569e-8, 0.653480510020104699270084e-10 }; + double[] dc = { 1, 0.105077086072039915406159, 0.00414278428675475620830226, 0.726338754644523769144108e-4, 0.477818471047398785369849e-6 }; r = Evaluate.Polynomial(z - 38, nc)/Evaluate.Polynomial(z - 38, dc); b = 0.5640528202F; } else if (z < 85) { // Worst case absolute error found: 3.080612264e-21 - double[] nc = {-0.196457797609229579459841e-4, 0.157243887666800692441195e-5, 0.543902511192700878690335e-7, 0.317472492369117710852685e-9}; - double[] dc = {1, 0.052803989240957632204885, 0.000926876069151753290378112, 0.541011723226630257077328e-5, 0.535093845803642394908747e-15}; + double[] nc = { -0.196457797609229579459841e-4, 0.157243887666800692441195e-5, 0.543902511192700878690335e-7, 0.317472492369117710852685e-9 }; + double[] dc = { 1, 0.052803989240957632204885, 0.000926876069151753290378112, 0.541011723226630257077328e-5, 0.535093845803642394908747e-15 }; r = Evaluate.Polynomial(z - 60, nc)/Evaluate.Polynomial(z - 60, dc); b = 0.5641309023F; } else { // Worst case absolute error found: 8.094633491e-22 - double[] nc = {-0.789224703978722689089794e-5, 0.622088451660986955124162e-6, 0.145728445676882396797184e-7, 0.603715505542715364529243e-10}; - double[] dc = {1, 0.0375328846356293715248719, 0.000467919535974625308126054, 0.193847039275845656900547e-5}; + double[] nc = { -0.789224703978722689089794e-5, 0.622088451660986955124162e-6, 0.145728445676882396797184e-7, 0.603715505542715364529243e-10 }; + double[] dc = { 1, 0.0375328846356293715248719, 0.000467919535974625308126054, 0.193847039275845656900547e-5 }; r = Evaluate.Polynomial(z - 85, nc)/Evaluate.Polynomial(z - 85, dc); b = 0.5641584396F; } @@ -325,9 +320,7 @@ namespace MathNet.Numerics } else { - // // Any value of z larger than 28 will underflow to zero: - // result = 0; invert = !invert; } @@ -345,8 +338,8 @@ namespace MathNet.Numerics /// We have tested this implementation against the arbitrary precision mpmath library /// and found cases where we can only guarantee 9 significant figures correct. /// - /// returns Double.PositiveInfinity if z <= 0.0. - /// returns Double.NegativeInfinity if z >= 2.0. + /// returns double.PositiveInfinity if z <= 0.0. + /// returns double.NegativeInfinity if z >= 2.0. /// /// /// calculates the complementary inverse error function evaluated at z. @@ -394,7 +387,6 @@ namespace MathNet.Numerics if (p <= 0.5) { - // // Evaluate inverse erf using the rational approximation: // // x = p(p+10)(Y+R(p)) @@ -405,17 +397,15 @@ namespace MathNet.Numerics // double: Max error found: 2.001849e-18 // long double: Max error found: 1.017064e-20 // Maximum Deviation Found (actual error term at infinite precision) 8.030e-21 - // const float y = 0.0891314744949340820313f; - double[] nc = {-0.000508781949658280665617, -0.00836874819741736770379, 0.0334806625409744615033, -0.0126926147662974029034, -0.0365637971411762664006, 0.0219878681111168899165, 0.00822687874676915743155, -0.00538772965071242932965}; - double[] dc = {1, -0.970005043303290640362, -1.56574558234175846809, 1.56221558398423026363, 0.662328840472002992063, -0.71228902341542847553, -0.0527396382340099713954, 0.0795283687341571680018, -0.00233393759374190016776, 0.000886216390456424707504}; + double[] nc = { -0.000508781949658280665617, -0.00836874819741736770379, 0.0334806625409744615033, -0.0126926147662974029034, -0.0365637971411762664006, 0.0219878681111168899165, 0.00822687874676915743155, -0.00538772965071242932965 }; + double[] dc = { 1, -0.970005043303290640362, -1.56574558234175846809, 1.56221558398423026363, 0.662328840472002992063, -0.71228902341542847553, -0.0527396382340099713954, 0.0795283687341571680018, -0.00233393759374190016776, 0.000886216390456424707504 }; double g = p*(p + 10); double r = Evaluate.Polynomial(p, nc)/Evaluate.Polynomial(p, dc); result = (g*y) + (g*r); } else if (q >= 0.25) { - // // Rational approximation for 0.5 > q >= 0.25 // // x = sqrt(-2*log(q)) / (Y + R(q)) @@ -426,10 +416,9 @@ namespace MathNet.Numerics // double : Max error found: 7.403372e-17 // long double : Max error found: 6.084616e-20 // Maximum Deviation Found (error term) 4.811e-20 - // const float y = 2.249481201171875f; - double[] nc = {-0.202433508355938759655, 0.105264680699391713268, 8.37050328343119927838, 17.6447298408374015486, -18.8510648058714251895, -44.6382324441786960818, 17.445385985570866523, 21.1294655448340526258, -3.67192254707729348546}; - double[] dc = {1, 6.24264124854247537712, 3.9713437953343869095, -28.6608180499800029974, -20.1432634680485188801, 48.5609213108739935468, 10.8268667355460159008, -22.6436933413139721736, 1.72114765761200282724}; + double[] nc = { -0.202433508355938759655, 0.105264680699391713268, 8.37050328343119927838, 17.6447298408374015486, -18.8510648058714251895, -44.6382324441786960818, 17.445385985570866523, 21.1294655448340526258, -3.67192254707729348546 }; + double[] dc = { 1, 6.24264124854247537712, 3.9713437953343869095, -28.6608180499800029974, -20.1432634680485188801, 48.5609213108739935468, 10.8268667355460159008, -22.6436933413139721736, 1.72114765761200282724 }; double g = Math.Sqrt(-2*Math.Log(q)); double xs = q - 0.25; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); @@ -437,7 +426,6 @@ namespace MathNet.Numerics } else { - // // For q < 0.25 we have a series of rational approximations all // of the general form: // @@ -447,7 +435,7 @@ namespace MathNet.Numerics // // x(Y+R(x-B)) // - // where Y is a constant, B is the lowest value of x for which + // where Y is a constant, B is the lowest value of x for which // the approximation is valid, and R(x-B) is optimized for a low // absolute error compared to Y. // @@ -455,14 +443,13 @@ namespace MathNet.Numerics // or maybe second approximation. After than we're dealing with very // small input values indeed: 80 and 128 bit long double's go all the // way down to ~ 1e-5000 so the "tail" is rather long... - // double x = Math.Sqrt(-Math.Log(q)); if (x < 3) { // Max error found: 1.089051e-20 const float y = 0.807220458984375f; - double[] nc = {-0.131102781679951906451, -0.163794047193317060787, 0.117030156341995252019, 0.387079738972604337464, 0.337785538912035898924, 0.142869534408157156766, 0.0290157910005329060432, 0.00214558995388805277169, -0.679465575181126350155e-6, 0.285225331782217055858e-7, -0.681149956853776992068e-9}; - double[] dc = {1, 3.46625407242567245975, 5.38168345707006855425, 4.77846592945843778382, 2.59301921623620271374, 0.848854343457902036425, 0.152264338295331783612, 0.01105924229346489121}; + double[] nc = { -0.131102781679951906451, -0.163794047193317060787, 0.117030156341995252019, 0.387079738972604337464, 0.337785538912035898924, 0.142869534408157156766, 0.0290157910005329060432, 0.00214558995388805277169, -0.679465575181126350155e-6, 0.285225331782217055858e-7, -0.681149956853776992068e-9 }; + double[] dc = { 1, 3.46625407242567245975, 5.38168345707006855425, 4.77846592945843778382, 2.59301921623620271374, 0.848854343457902036425, 0.152264338295331783612, 0.01105924229346489121 }; double xs = x - 1.125; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); result = (y*x) + (r*x); @@ -471,8 +458,8 @@ namespace MathNet.Numerics { // Max error found: 8.389174e-21 const float y = 0.93995571136474609375f; - double[] nc = {-0.0350353787183177984712, -0.00222426529213447927281, 0.0185573306514231072324, 0.00950804701325919603619, 0.00187123492819559223345, 0.000157544617424960554631, 0.460469890584317994083e-5, -0.230404776911882601748e-9, 0.266339227425782031962e-11}; - double[] dc = {1, 1.3653349817554063097, 0.762059164553623404043, 0.220091105764131249824, 0.0341589143670947727934, 0.00263861676657015992959, 0.764675292302794483503e-4}; + double[] nc = { -0.0350353787183177984712, -0.00222426529213447927281, 0.0185573306514231072324, 0.00950804701325919603619, 0.00187123492819559223345, 0.000157544617424960554631, 0.460469890584317994083e-5, -0.230404776911882601748e-9, 0.266339227425782031962e-11 }; + double[] dc = { 1, 1.3653349817554063097, 0.762059164553623404043, 0.220091105764131249824, 0.0341589143670947727934, 0.00263861676657015992959, 0.764675292302794483503e-4 }; double xs = x - 3; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); result = (y*x) + (r*x); @@ -481,8 +468,8 @@ namespace MathNet.Numerics { // Max error found: 1.481312e-19 const float y = 0.98362827301025390625f; - double[] nc = {-0.0167431005076633737133, -0.00112951438745580278863, 0.00105628862152492910091, 0.000209386317487588078668, 0.149624783758342370182e-4, 0.449696789927706453732e-6, 0.462596163522878599135e-8, -0.281128735628831791805e-13, 0.99055709973310326855e-16}; - double[] dc = {1, 0.591429344886417493481, 0.138151865749083321638, 0.0160746087093676504695, 0.000964011807005165528527, 0.275335474764726041141e-4, 0.282243172016108031869e-6}; + double[] nc = { -0.0167431005076633737133, -0.00112951438745580278863, 0.00105628862152492910091, 0.000209386317487588078668, 0.149624783758342370182e-4, 0.449696789927706453732e-6, 0.462596163522878599135e-8, -0.281128735628831791805e-13, 0.99055709973310326855e-16 }; + double[] dc = { 1, 0.591429344886417493481, 0.138151865749083321638, 0.0160746087093676504695, 0.000964011807005165528527, 0.275335474764726041141e-4, 0.282243172016108031869e-6 }; double xs = x - 6; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); result = (y*x) + (r*x); @@ -491,8 +478,8 @@ namespace MathNet.Numerics { // Max error found: 5.697761e-20 const float y = 0.99714565277099609375f; - double[] nc = {-0.0024978212791898131227, -0.779190719229053954292e-5, 0.254723037413027451751e-4, 0.162397777342510920873e-5, 0.396341011304801168516e-7, 0.411632831190944208473e-9, 0.145596286718675035587e-11, -0.116765012397184275695e-17}; - double[] dc = {1, 0.207123112214422517181, 0.0169410838120975906478, 0.000690538265622684595676, 0.145007359818232637924e-4, 0.144437756628144157666e-6, 0.509761276599778486139e-9}; + double[] nc = { -0.0024978212791898131227, -0.779190719229053954292e-5, 0.254723037413027451751e-4, 0.162397777342510920873e-5, 0.396341011304801168516e-7, 0.411632831190944208473e-9, 0.145596286718675035587e-11, -0.116765012397184275695e-17 }; + double[] dc = { 1, 0.207123112214422517181, 0.0169410838120975906478, 0.000690538265622684595676, 0.145007359818232637924e-4, 0.144437756628144157666e-6, 0.509761276599778486139e-9 }; double xs = x - 18; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); result = (y*x) + (r*x); @@ -501,8 +488,8 @@ namespace MathNet.Numerics { // Max error found: 1.279746e-20 const float y = 0.99941349029541015625f; - double[] nc = {-0.000539042911019078575891, -0.28398759004727721098e-6, 0.899465114892291446442e-6, 0.229345859265920864296e-7, 0.225561444863500149219e-9, 0.947846627503022684216e-12, 0.135880130108924861008e-14, -0.348890393399948882918e-21}; - double[] dc = {1, 0.0845746234001899436914, 0.00282092984726264681981, 0.468292921940894236786e-4, 0.399968812193862100054e-6, 0.161809290887904476097e-8, 0.231558608310259605225e-11}; + double[] nc = { -0.000539042911019078575891, -0.28398759004727721098e-6, 0.899465114892291446442e-6, 0.229345859265920864296e-7, 0.225561444863500149219e-9, 0.947846627503022684216e-12, 0.135880130108924861008e-14, -0.348890393399948882918e-21 }; + double[] dc = { 1, 0.0845746234001899436914, 0.00282092984726264681981, 0.468292921940894236786e-4, 0.399968812193862100054e-6, 0.161809290887904476097e-8, 0.231558608310259605225e-11 }; double xs = x - 44; double r = Evaluate.Polynomial(xs, nc)/Evaluate.Polynomial(xs, dc); result = (y*x) + (r*x); diff --git a/src/Numerics/SpecialFunctions/Evaluate.cs b/src/Numerics/SpecialFunctions/Evaluate.cs index 73fc6ed2..52d6dd86 100644 --- a/src/Numerics/SpecialFunctions/Evaluate.cs +++ b/src/Numerics/SpecialFunctions/Evaluate.cs @@ -186,7 +186,6 @@ namespace MathNet.Numerics internal static double ChebyshevA(double[] coefficients, double x) { // TODO: Unify, normalize, then make public - double b2; int p = 0; @@ -203,7 +202,7 @@ namespace MathNet.Numerics } while (--i > 0); - return (0.5 * (b0 - b2)); + return 0.5 * (b0 - b2); } /// diff --git a/src/Numerics/SpecialFunctions/Factorial.cs b/src/Numerics/SpecialFunctions/Factorial.cs index ece7ea0d..20e75b65 100644 --- a/src/Numerics/SpecialFunctions/Factorial.cs +++ b/src/Numerics/SpecialFunctions/Factorial.cs @@ -86,7 +86,7 @@ namespace MathNet.Numerics return _factorialCache[x]; } - return Double.PositiveInfinity; + return double.PositiveInfinity; } #if !NOSYSNUMERICS @@ -99,13 +99,18 @@ namespace MathNet.Numerics { throw new ArgumentOutOfRangeException("x", Resources.ArgumentPositive); } + if (x == 0) { return BigInteger.One; } BigInteger r = x; - while (--x > 1) r *= x; + while (--x > 1) + { + r *= x; + } + return r; } #endif @@ -160,7 +165,7 @@ namespace MathNet.Numerics { if (k < 0 || n < 0 || k > n) { - return Double.NegativeInfinity; + return double.NegativeInfinity; } return FactorialLn(n) - FactorialLn(k) - FactorialLn(n - k); @@ -181,6 +186,7 @@ namespace MathNet.Numerics { throw new ArgumentException(Resources.ArgumentMustBePositive, "n"); } + if (ni == null) { throw new ArgumentNullException("ni"); @@ -202,7 +208,7 @@ namespace MathNet.Numerics // Before returning, check that the sum of all elements was equal to n. if (sum != n) { - throw new ArgumentException(Resources.ArgumentParameterSetInvalid , "ni"); + throw new ArgumentException(Resources.ArgumentParameterSetInvalid, "ni"); } return Math.Floor(0.5 + Math.Exp(ret)); diff --git a/src/Numerics/SpecialFunctions/Gamma.cs b/src/Numerics/SpecialFunctions/Gamma.cs index fcbeda17..f2d4e4b7 100644 --- a/src/Numerics/SpecialFunctions/Gamma.cs +++ b/src/Numerics/SpecialFunctions/Gamma.cs @@ -44,12 +44,12 @@ namespace MathNet.Numerics /// /// The order of the approximation. /// - private const int GammaN = 10; + const int GammaN = 10; /// /// Auxiliary variable when evaluating the function. /// - private const double GammaR = 10.900511; + const double GammaR = 10.900511; /// /// Polynomial coefficients for the approximation. @@ -88,26 +88,26 @@ namespace MathNet.Numerics double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { - s += GammaDk[i] / (i - z); + s += GammaDk[i]/(i - z); } return Constants.LnPi - - Math.Log(Math.Sin(Math.PI * z)) + - Math.Log(Math.Sin(Math.PI*z)) - Math.Log(s) - Constants.LogTwoSqrtEOverPi - - ((0.5 - z) * Math.Log((0.5 - z + GammaR) / Math.E)); + - ((0.5 - z)*Math.Log((0.5 - z + GammaR)/Math.E)); } else { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { - s += GammaDk[i] / (z + i - 1.0); + s += GammaDk[i]/(z + i - 1.0); } return Math.Log(s) + Constants.LogTwoSqrtEOverPi - + ((z - 0.5) * Math.Log((z - 0.5 + GammaR) / Math.E)); + + ((z - 0.5)*Math.Log((z - 0.5 + GammaR)/Math.E)); } } @@ -132,23 +132,23 @@ namespace MathNet.Numerics double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { - s += GammaDk[i] / (i - z); + s += GammaDk[i]/(i - z); } - return Math.PI / (Math.Sin(Math.PI * z) - * s - * Constants.TwoSqrtEOverPi - * Math.Pow((0.5 - z + GammaR) / Math.E, 0.5 - z)); + return Math.PI/(Math.Sin(Math.PI*z) + *s + *Constants.TwoSqrtEOverPi + *Math.Pow((0.5 - z + GammaR)/Math.E, 0.5 - z)); } else { double s = GammaDk[0]; for (int i = 1; i <= GammaN; i++) { - s += GammaDk[i] / (z + i - 1.0); + s += GammaDk[i]/(z + i - 1.0); } - return s * Constants.TwoSqrtEOverPi * Math.Pow((z - 0.5 + GammaR) / Math.E, z - 0.5); + return s*Constants.TwoSqrtEOverPi*Math.Pow((z - 0.5 + GammaR)/Math.E, z - 0.5); } } @@ -175,7 +175,7 @@ namespace MathNet.Numerics return 1d - GammaLowerRegularized(a, x); } - double ax = a * Math.Log(x) - x - GammaLn(a); + double ax = a*Math.Log(x) - x - GammaLn(a); if (ax < -709.78271289338399) { return a < x ? 0d : 1d; @@ -189,20 +189,20 @@ namespace MathNet.Numerics double pkm2 = 1; double qkm2 = x; double pkm1 = x + 1; - double qkm1 = z * x; - double ans = pkm1 / qkm1; + double qkm1 = z*x; + double ans = pkm1/qkm1; do { c = c + 1; y = y + 1; z = z + 2; - double yc = y * c; - double pk = pkm1 * z - pkm2 * yc; - double qk = qkm1 * z - qkm2 * yc; + double yc = y*c; + double pk = pkm1*z - pkm2*yc; + double qk = qkm1*z - qkm2*yc; if (qk != 0) { - double r = pk / qk; - t = Math.Abs((ans - r) / r); + double r = pk/qk; + t = Math.Abs((ans - r)/r); ans = r; } else @@ -217,15 +217,15 @@ namespace MathNet.Numerics if (Math.Abs(pk) > big) { - pkm2 = pkm2 * bigInv; - pkm1 = pkm1 * bigInv; - qkm2 = qkm2 * bigInv; - qkm1 = qkm1 * bigInv; + pkm2 = pkm2*bigInv; + pkm1 = pkm1*bigInv; + qkm2 = qkm2*bigInv; + qkm1 = qkm1*bigInv; } } while (t > epsilon); - return ans * ax; + return ans*ax; } /// @@ -237,7 +237,7 @@ namespace MathNet.Numerics /// The upper incomplete gamma function. public static double GammaUpperIncomplete(double a, double x) { - return GammaUpperRegularized(a, x) * Gamma(a); + return GammaUpperRegularized(a, x)*Gamma(a); } /// @@ -249,7 +249,7 @@ namespace MathNet.Numerics /// The lower incomplete gamma function. public static double GammaLowerIncomplete(double a, double x) { - return GammaLowerRegularized(a, x) * Gamma(a); + return GammaLowerRegularized(a, x)*Gamma(a); } /// @@ -269,6 +269,7 @@ namespace MathNet.Numerics { throw new ArgumentOutOfRangeException("a", Properties.Resources.ArgumentNotNegative); } + if (x < 0d) { throw new ArgumentOutOfRangeException("x", Properties.Resources.ArgumentNotNegative); @@ -279,7 +280,7 @@ namespace MathNet.Numerics if (x.AlmostEqual(0.0)) { // either 0 or 1, depending on the limit direction - return Double.NaN; + return double.NaN; } return 1d; @@ -290,7 +291,7 @@ namespace MathNet.Numerics return 0d; } - double ax = (a * Math.Log(x)) - x - GammaLn(a); + double ax = (a*Math.Log(x)) - x - GammaLn(a); if (ax < -709.78271289338399) { return a < x ? 1d : 0d; @@ -305,12 +306,12 @@ namespace MathNet.Numerics do { r2 = r2 + 1; - c2 = c2 * x / r2; + c2 = c2*x/r2; ans2 += c2; } - while ((c2 / ans2) > epsilon); + while ((c2/ans2) > epsilon); - return Math.Exp(ax) * ans2 / a; + return Math.Exp(ax)*ans2/a; } int c = 0; @@ -320,8 +321,8 @@ namespace MathNet.Numerics double p3 = 1; double q3 = x; double p2 = x + 1; - double q2 = z * x; - double ans = p2 / q2; + double q2 = z*x; + double ans = p2/q2; double error; @@ -330,15 +331,15 @@ namespace MathNet.Numerics c++; y += 1; z += 2; - double yc = y * c; + double yc = y*c; - double p = (p2 * z) - (p3 * yc); - double q = (q2 * z) - (q3 * yc); + double p = (p2*z) - (p3*yc); + double q = (q2*z) - (q3*yc); if (q != 0) { - double nextans = p / q; - error = Math.Abs((ans - nextans) / nextans); + double nextans = p/q; + error = Math.Abs((ans - nextans)/nextans); ans = nextans; } else @@ -364,7 +365,7 @@ namespace MathNet.Numerics } while (error > epsilon); - return 1d - (Math.Exp(ax) * ans); + return 1d - (Math.Exp(ax)*ans); } /// @@ -387,6 +388,7 @@ namespace MathNet.Numerics { throw new ArgumentOutOfRangeException("a"); } + if (y0 < 0 || y0 > 1) { throw new ArgumentOutOfRangeException("y0"); @@ -399,7 +401,7 @@ namespace MathNet.Numerics if (y0.AlmostEqual(1.0)) { - return Double.PositiveInfinity; + return double.PositiveInfinity; } y0 = 1 - y0; @@ -578,48 +580,48 @@ namespace MathNet.Numerics const double d1 = -0.57721566490153286; const double d2 = 1.6449340668482264365; const double s = 1e-6; - const double s3 = 1.0 / 12.0; - const double s4 = 1.0 / 120.0; - const double s5 = 1.0 / 252.0; - const double s6 = 1.0 / 240.0; - const double s7 = 1.0 / 132.0; + const double s3 = 1.0/12.0; + const double s4 = 1.0/120.0; + const double s5 = 1.0/252.0; + const double s6 = 1.0/240.0; + const double s7 = 1.0/132.0; - if (Double.IsNegativeInfinity(x) || Double.IsNaN(x)) + if (double.IsNegativeInfinity(x) || double.IsNaN(x)) { - return Double.NaN; + return double.NaN; } // Handle special cases. if (x <= 0 && Math.Floor(x) == x) { - return Double.NegativeInfinity; + return double.NegativeInfinity; } // Use inversion formula for negative numbers. if (x < 0) { - return DiGamma(1.0 - x) + (Math.PI / Math.Tan(-Math.PI * x)); + return DiGamma(1.0 - x) + (Math.PI/Math.Tan(-Math.PI*x)); } if (x <= s) { - return d1 - (1 / x) + (d2 * x); + return d1 - (1/x) + (d2*x); } double result = 0; while (x < c) { - result -= 1 / x; + result -= 1/x; x++; } if (x >= c) { - var r = 1 / x; - result += Math.Log(x) - (0.5 * r); + var r = 1/x; + result += Math.Log(x) - (0.5*r); r *= r; - result -= r * (s3 - (r * (s4 - (r * (s5 - (r * (s6 - (r * s7)))))))); + result -= r*(s3 - (r*(s4 - (r*(s5 - (r*(s6 - (r*s7)))))))); } return result; @@ -634,28 +636,28 @@ namespace MathNet.Numerics /// The positive solution to the inverse DiGamma function at . public static double DiGammaInv(double p) { - if (Double.IsNaN(p)) + if (double.IsNaN(p)) { - return Double.NaN; + return double.NaN; } - if (Double.IsNegativeInfinity(p)) + if (double.IsNegativeInfinity(p)) { return 0.0; } - if (Double.IsPositiveInfinity(p)) + if (double.IsPositiveInfinity(p)) { - return Double.PositiveInfinity; + return double.PositiveInfinity; } var x = Math.Exp(p); for (var d = 1.0; d > 1.0e-15; d /= 2.0) { - x += d * Math.Sign(p - DiGamma(x)); + x += d*Math.Sign(p - DiGamma(x)); } return x; } } -} \ No newline at end of file +} diff --git a/src/Numerics/SpecialFunctions/Harmonic.cs b/src/Numerics/SpecialFunctions/Harmonic.cs index 2fe97214..0f723f73 100644 --- a/src/Numerics/SpecialFunctions/Harmonic.cs +++ b/src/Numerics/SpecialFunctions/Harmonic.cs @@ -44,7 +44,6 @@ namespace MathNet.Numerics /// public static partial class SpecialFunctions { - /// /// Computes the 'th Harmonic number. /// @@ -68,6 +67,7 @@ namespace MathNet.Numerics { sum += Math.Pow(i + 1, -m); } + return sum; } } diff --git a/src/Numerics/SpecialFunctions/ModifiedBessel.cs b/src/Numerics/SpecialFunctions/ModifiedBessel.cs index aaee3a4e..9d13f3c1 100644 --- a/src/Numerics/SpecialFunctions/ModifiedBessel.cs +++ b/src/Numerics/SpecialFunctions/ModifiedBessel.cs @@ -148,14 +148,15 @@ namespace MathNet.Numerics { x = -x; } + if (x <= 8.0) { double y = (x / 2.0) - 2.0; - return (Math.Exp(x) * Evaluate.ChebyshevA(BesselI0A, y)); + return Math.Exp(x) * Evaluate.ChebyshevA(BesselI0A, y); } double x1 = 32.0 / x - 2.0; - return (Math.Exp(x) * Evaluate.ChebyshevA(BesselI0B, x1) / Math.Sqrt(x)); + return Math.Exp(x) * Evaluate.ChebyshevA(BesselI0B, x1) / Math.Sqrt(x); } /// Returns the modified Bessel function of first kind, @@ -182,10 +183,12 @@ namespace MathNet.Numerics double x1 = 32.0 / z - 2.0; z = Math.Exp(z) * Evaluate.ChebyshevA(BesselI1B, x1) / Math.Sqrt(z); } + if (x < 0.0) { z = -z; } + return z; } @@ -204,6 +207,7 @@ namespace MathNet.Numerics { throw new ArithmeticException(); } + if (x <= 2.0) { double y = x * x - 2.0; @@ -225,6 +229,7 @@ namespace MathNet.Numerics { throw new ArithmeticException(); } + if (x <= 2.0) { double y = x * x - 2.0; @@ -251,6 +256,7 @@ namespace MathNet.Numerics { throw new ArithmeticException(); } + if (x <= 2.0) { double y = x * x - 2.0; @@ -274,6 +280,7 @@ namespace MathNet.Numerics { throw new ArithmeticException(); } + if (x <= 2.0) { double y = x * x - 2.0; diff --git a/src/Numerics/SpecialFunctions/ModifiedStruve.cs b/src/Numerics/SpecialFunctions/ModifiedStruve.cs index 5984a59b..35fab64a 100644 --- a/src/Numerics/SpecialFunctions/ModifiedStruve.cs +++ b/src/Numerics/SpecialFunctions/ModifiedStruve.cs @@ -95,7 +95,7 @@ namespace MathNet.Numerics // // NTERM2 - INTEGER - The no. of terms for the array ARL0AS. // The recommended value is such that - // ABS(ARL0AS(NTERM2)) < EPS/100 + // ABS(ARL0AS(NTERM2)) < EPS/100 // // NTERM3 - INTEGER - The no. of terms for the array AI0ML0. // The recommended value is such that @@ -233,9 +233,13 @@ namespace MathNet.Numerics AI0ML0[23] = 0.16e-18; // MACHINE-DEPENDENT VALUES (Suitable for IEEE-arithmetic machines) - const int nterm1 = 25; const int nterm2 = 14; const int nterm3 = 21; - const double xlow = 4.4703484e-8; const double xmax = 1.797693e308; - const double xhigh1 = 5.1982303e8; const double xhigh2 = 2.5220158e17; + const int nterm1 = 25; + const int nterm2 = 14; + const int nterm3 = 21; + const double xlow = 4.4703484e-8; + const double xmax = 1.797693e308; + const double xhigh1 = 5.1982303e8; + const double xhigh2 = 2.5220158e17; // Code for |xvalue| <= 16 if (x <= 16.0) @@ -467,9 +471,14 @@ namespace MathNet.Numerics AI1ML1[25] = -0.1e-19; // MACHINE-DEPENDENT VALUES (Suitable for IEEE-arithmetic machines) - const int nterm1 = 24; const int nterm2 = 13; const int nterm3 = 22; - const double xlow1 = 5.7711949e-8; const double xlow2 = 3.3354714e-154; const double xmax = 1.797693e308; - const double xhigh1 = 5.19823025e8; const double xhigh2 = 2.7021597e17; + const int nterm1 = 24; + const int nterm2 = 13; + const int nterm3 = 22; + const double xlow1 = 5.7711949e-8; + const double xlow2 = 3.3354714e-154; + const double xmax = 1.797693e308; + const double xhigh1 = 5.19823025e8; + const double xhigh2 = 2.7021597e17; // CODE FOR |x| <= 16 if (x <= 16.0) diff --git a/src/Numerics/SpecialFunctions/Stability.cs b/src/Numerics/SpecialFunctions/Stability.cs index db7101e7..471959fb 100644 --- a/src/Numerics/SpecialFunctions/Stability.cs +++ b/src/Numerics/SpecialFunctions/Stability.cs @@ -68,8 +68,7 @@ namespace MathNet.Numerics term *= power; term /= k; return term; - } - ); + }); } /// diff --git a/src/Numerics/SpecialFunctions/Test.cs b/src/Numerics/SpecialFunctions/Test.cs index 949675c1..78f9c049 100644 --- a/src/Numerics/SpecialFunctions/Test.cs +++ b/src/Numerics/SpecialFunctions/Test.cs @@ -68,6 +68,7 @@ namespace MathNet.Numerics { sum += Rosenbrock(x[i - 1], x[i]); } + return sum; } diff --git a/src/Numerics/Statistics/ArrayStatistics.cs b/src/Numerics/Statistics/ArrayStatistics.cs index 9c25938b..4fd0b26e 100644 --- a/src/Numerics/Statistics/ArrayStatistics.cs +++ b/src/Numerics/Statistics/ArrayStatistics.cs @@ -52,7 +52,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static double Minimum(double[] data) { - if (data.Length == 0) return double.NaN; + if (data.Length == 0) + { + return double.NaN; + } var min = double.PositiveInfinity; for (int i = 0; i < data.Length; i++) @@ -62,6 +65,7 @@ namespace MathNet.Numerics.Statistics min = data[i]; } } + return min; } @@ -72,7 +76,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static float Minimum(float[] data) { - if (data.Length == 0) return float.NaN; + if (data.Length == 0) + { + return float.NaN; + } var min = float.PositiveInfinity; for (int i = 0; i < data.Length; i++) @@ -82,6 +89,7 @@ namespace MathNet.Numerics.Statistics min = data[i]; } } + return min; } @@ -92,7 +100,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static double Maximum(double[] data) { - if (data.Length == 0) return double.NaN; + if (data.Length == 0) + { + return double.NaN; + } var max = double.NegativeInfinity; for (int i = 0; i < data.Length; i++) @@ -102,6 +113,7 @@ namespace MathNet.Numerics.Statistics max = data[i]; } } + return max; } @@ -112,7 +124,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static float Maximum(float[] data) { - if (data.Length == 0) return float.NaN; + if (data.Length == 0) + { + return float.NaN; + } var max = float.NegativeInfinity; for (int i = 0; i < data.Length; i++) @@ -122,6 +137,7 @@ namespace MathNet.Numerics.Statistics max = data[i]; } } + return max; } @@ -132,7 +148,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static double Mean(double[] data) { - if (data.Length == 0) return double.NaN; + if (data.Length == 0) + { + return double.NaN; + } double mean = 0; ulong m = 0; @@ -140,6 +159,7 @@ namespace MathNet.Numerics.Statistics { mean += (data[i] - mean)/++m; } + return mean; } @@ -151,7 +171,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static double Variance(double[] samples) { - if (samples.Length <= 1) return double.NaN; + if (samples.Length <= 1) + { + return double.NaN; + } double variance = 0; double t = samples[0]; @@ -161,6 +184,7 @@ namespace MathNet.Numerics.Statistics double diff = ((i + 1)*samples[i]) - t; variance += (diff*diff)/((i + 1.0)*i); } + return variance/(samples.Length - 1); } @@ -172,7 +196,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. public static double PopulationVariance(double[] population) { - if (population.Length == 0) return double.NaN; + if (population.Length == 0) + { + return double.NaN; + } double variance = 0; double t = population[0]; @@ -182,6 +209,7 @@ namespace MathNet.Numerics.Statistics double diff = ((i + 1)*population[i]) - t; variance += (diff*diff)/((i + 1.0)*i); } + return variance/population.Length; } @@ -238,8 +266,15 @@ namespace MathNet.Numerics.Statistics /// Second sample array. public static double Covariance(double[] samples1, double[] samples2) { - if (samples1.Length != samples2.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); - if (samples1.Length <= 1) return double.NaN; + if (samples1.Length != samples2.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + if (samples1.Length <= 1) + { + return double.NaN; + } var mean1 = Mean(samples1); var mean2 = Mean(samples2); @@ -248,6 +283,7 @@ namespace MathNet.Numerics.Statistics { covariance += (samples1[i] - mean1)*(samples2[i] - mean2); } + return covariance/(samples1.Length - 1); } @@ -260,8 +296,15 @@ namespace MathNet.Numerics.Statistics /// Second population array. public static double PopulationCovariance(double[] population1, double[] population2) { - if (population1.Length != population2.Length) throw new ArgumentException(Resources.ArgumentVectorsSameLength); - if (population1.Length == 0) return double.NaN; + if (population1.Length != population2.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength); + } + + if (population1.Length == 0) + { + return double.NaN; + } var mean1 = Mean(population1); var mean2 = Mean(population2); @@ -270,6 +313,7 @@ namespace MathNet.Numerics.Statistics { covariance += (population1[i] - mean1)*(population2[i] - mean2); } + return covariance/population1.Length; } @@ -281,10 +325,21 @@ namespace MathNet.Numerics.Statistics /// One-based order of the statistic, must be between 1 and N (inclusive). public static double OrderStatisticInplace(double[] data, int order) { - if (order < 1 || order > data.Length) return double.NaN; + if (order < 1 || order > data.Length) + { + return double.NaN; + } + + if (order == 1) + { + return Minimum(data); + } + + if (order == data.Length) + { + return Maximum(data); + } - if (order == 1) return Minimum(data); - if (order == data.Length) return Maximum(data); return SelectInplace(data, order - 1); } @@ -355,7 +410,10 @@ namespace MathNet.Numerics.Statistics /// Sample array, no sorting is assumed. Will be reordered. public static double[] FiveNumberSummaryInplace(double[] data) { - if (data.Length == 0) return new[] { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN }; + if (data.Length == 0) + { + return new[] { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN }; + } // TODO: Benchmark: is this still faster than sorting the array then using SortedArrayStatistics instead? return new[] { Minimum(data), QuantileInplace(data, 0.25), QuantileInplace(data, 0.50), QuantileInplace(data, 0.75), Maximum(data) }; @@ -377,7 +435,10 @@ namespace MathNet.Numerics.Statistics /// public static double QuantileInplace(double[] data, double tau) { - if (tau < 0d || tau > 1d || data.Length == 0) return double.NaN; + if (tau < 0d || tau > 1d || data.Length == 0) + { + return double.NaN; + } double h = (data.Length + 1d/3d)*tau + 1d/3d; var hf = (int)h; @@ -386,6 +447,7 @@ namespace MathNet.Numerics.Statistics { return Minimum(data); } + if (hf >= data.Length || tau == 1d) { return Maximum(data); @@ -411,7 +473,10 @@ namespace MathNet.Numerics.Statistics /// d-parameter public static double QuantileCustomInplace(double[] data, double tau, double a, double b, double c, double d) { - if (tau < 0d || tau > 1d || data.Length == 0) return double.NaN; + if (tau < 0d || tau > 1d || data.Length == 0) + { + return double.NaN; + } var x = a + (data.Length + b)*tau - 1; #if PORTABLE @@ -443,9 +508,20 @@ namespace MathNet.Numerics.Statistics /// Quantile definition, to choose what product/definition it should be consistent with public static double QuantileCustomInplace(double[] data, double tau, QuantileDefinition definition) { - if (tau < 0d || tau > 1d || data.Length == 0) return double.NaN; - if (tau == 0d || data.Length == 1) return Minimum(data); - if (tau == 1d) return Maximum(data); + if (tau < 0d || tau > 1d || data.Length == 0) + { + return double.NaN; + } + + if (tau == 0d || data.Length == 1) + { + return Minimum(data); + } + + if (tau == 1d) + { + return Maximum(data); + } switch (definition) { @@ -454,16 +530,19 @@ namespace MathNet.Numerics.Statistics double h = data.Length*tau + 0.5d; return SelectInplace(data, (int)Math.Ceiling(h - 0.5d) - 1); } + case QuantileDefinition.R2: { double h = data.Length*tau + 0.5d; return (SelectInplace(data, (int)Math.Ceiling(h - 0.5d) - 1) + SelectInplace(data, (int)(h + 0.5d) - 1))*0.5d; } + case QuantileDefinition.R3: { double h = data.Length*tau; return SelectInplace(data, (int)Math.Round(h) - 1); } + case QuantileDefinition.R4: { double h = data.Length*tau; @@ -472,6 +551,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + case QuantileDefinition.R5: { double h = data.Length*tau + 0.5d; @@ -480,6 +560,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + case QuantileDefinition.R6: { double h = (data.Length + 1)*tau; @@ -488,6 +569,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + case QuantileDefinition.R7: { double h = (data.Length - 1)*tau + 1d; @@ -496,6 +578,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + case QuantileDefinition.R8: { double h = (data.Length + 1/3d)*tau + 1/3d; @@ -504,6 +587,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + case QuantileDefinition.R9: { double h = (data.Length + 0.25d)*tau + 0.375d; @@ -512,6 +596,7 @@ namespace MathNet.Numerics.Statistics var upper = SelectInplace(data, hf); return lower + (h - hf)*(upper - lower); } + default: throw new NotSupportedException(); } @@ -521,9 +606,15 @@ namespace MathNet.Numerics.Statistics { // Numerical Recipes: select // http://en.wikipedia.org/wiki/Selection_algorithm + if (rank <= 0) + { + return Minimum(workingData); + } - if (rank <= 0) return Minimum(workingData); - if (rank >= workingData.Length - 1) return Maximum(workingData); + if (rank >= workingData.Length - 1) + { + return Maximum(workingData); + } var a = workingData; int low = 0; @@ -555,12 +646,14 @@ namespace MathNet.Numerics.Statistics a[low] = a[high]; a[high] = tmp; } + if (a[low + 1] > a[high]) { var tmp = a[low + 1]; a[low + 1] = a[high]; a[high] = tmp; } + if (a[low] > a[low + 1]) { var tmp = a[low]; @@ -574,10 +667,22 @@ namespace MathNet.Numerics.Statistics while (true) { - do begin++; while (a[begin] < pivot); - do end--; while (a[end] > pivot); + do + { + begin++; + } + while (a[begin] < pivot); - if (end < begin) break; + do + { + end--; + } + while (a[end] > pivot); + + if (end < begin) + { + break; + } var tmp = a[begin]; a[begin] = a[end]; @@ -587,8 +692,15 @@ namespace MathNet.Numerics.Statistics a[low + 1] = a[end]; a[end] = pivot; - if (end >= rank) high = end - 1; - if (end <= rank) low = begin; + if (end >= rank) + { + high = end - 1; + } + + if (end <= rank) + { + low = begin; + } } } @@ -614,6 +726,7 @@ namespace MathNet.Numerics.Statistics { ranks[index[i]] = i + 1; } + return ranks; } @@ -645,7 +758,6 @@ namespace MathNet.Numerics.Statistics static void RanksTies(double[] ranks, int[] index, int a, int b, RankDefinition definition) { // TODO: potential for PERF optimization - double rank; switch (definition) { @@ -654,16 +766,19 @@ namespace MathNet.Numerics.Statistics rank = (b + a - 1)/2d + 1; break; } + case RankDefinition.Min: { rank = a + 1; break; } + case RankDefinition.Max: { rank = b; break; } + default: throw new NotSupportedException(); } diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index b687310e..ce4122da 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -52,6 +52,7 @@ namespace MathNet.Numerics.Statistics ? ArrayStatistics.Minimum(array) : StreamingStatistics.Minimum(data); } + /// /// Returns the minimum value in the sample data. /// Returns NaN if data is empty or if any entry is NaN. @@ -477,7 +478,7 @@ namespace MathNet.Numerics.Statistics /// Approximately median-unbiased regardless of the sample distribution (R8). /// /// The data sample sequence. - public static Func QuantileFunc(this IEnumerable data) + public static Func QuantileFunc(this IEnumerable data) { var array = data.ToArray(); Array.Sort(array); diff --git a/src/Numerics/Threading/CommonParallel.cs b/src/Numerics/Threading/CommonParallel.cs index fa37fb0f..4dc7f42e 100644 --- a/src/Numerics/Threading/CommonParallel.cs +++ b/src/Numerics/Threading/CommonParallel.cs @@ -28,18 +28,18 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + namespace MathNet.Numerics.Threading { - using System; - using System.Collections.Generic; - using System.Threading.Tasks; - #if NET35 using Partitioner = MathNet.Numerics.Partitioner; #endif - #if !PORTABLE using System.Collections.Concurrent; + #endif /// @@ -47,13 +47,13 @@ namespace MathNet.Numerics.Threading /// internal static class CommonParallel { - private static ParallelOptions CreateParallelOptions() + static ParallelOptions CreateParallelOptions() { return new ParallelOptions - { - MaxDegreeOfParallelism = Control.MaxDegreeOfParallelism, - TaskScheduler = Control.TaskScheduler, - }; + { + MaxDegreeOfParallelism = Control.MaxDegreeOfParallelism, + TaskScheduler = Control.TaskScheduler, + }; } /// @@ -76,10 +76,25 @@ namespace MathNet.Numerics.Threading /// The body to be invoked for each iteration range. public static void For(int fromInclusive, int toExclusive, int rangeSize, Action body) { - if (body == null) throw new ArgumentNullException("body"); - if (fromInclusive < 0) throw new ArgumentOutOfRangeException("fromInclusive"); - if (fromInclusive > toExclusive) throw new ArgumentOutOfRangeException("toExclusive"); - if (rangeSize < 1) throw new ArgumentOutOfRangeException("rangeSize"); + if (body == null) + { + throw new ArgumentNullException("body"); + } + + if (fromInclusive < 0) + { + throw new ArgumentOutOfRangeException("fromInclusive"); + } + + if (fromInclusive > toExclusive) + { + throw new ArgumentOutOfRangeException("toExclusive"); + } + + if (rangeSize < 1) + { + throw new ArgumentOutOfRangeException("rangeSize"); + } var length = toExclusive - fromInclusive; @@ -90,7 +105,7 @@ namespace MathNet.Numerics.Threading } // Special case: not worth to parallelize, inline - if (Control.MaxDegreeOfParallelism < 2 || (rangeSize * 2) > length) + if (Control.MaxDegreeOfParallelism < 2 || (rangeSize*2) > length) { body(fromInclusive, toExclusive); return; @@ -131,6 +146,7 @@ namespace MathNet.Numerics.Threading { actions[i](); } + return; } @@ -150,8 +166,15 @@ namespace MathNet.Numerics.Threading /// The selected value. public static T Aggregate(int fromInclusive, int toExclusive, Func select, Func reduce) { - if (select == null) throw new ArgumentNullException("select"); - if (reduce == null) throw new ArgumentNullException("reduce"); + if (select == null) + { + throw new ArgumentNullException("select"); + } + + if (reduce == null) + { + throw new ArgumentNullException("reduce"); + } // Special case: no action if (fromInclusive >= toExclusive) @@ -162,7 +185,7 @@ namespace MathNet.Numerics.Threading // Special case: single action, inline if (fromInclusive == (toExclusive - 1)) { - return reduce(new[] {select(fromInclusive)}); + return reduce(new[] { select(fromInclusive) }); } // Special case: straight execution without parallelism @@ -173,6 +196,7 @@ namespace MathNet.Numerics.Threading { mapped[k] = select(k + fromInclusive); } + return reduce(mapped); } @@ -184,22 +208,23 @@ namespace MathNet.Numerics.Threading CreateParallelOptions(), () => new List(), (range, loop, localData) => + { + var mapped = new T[range.Item2 - range.Item1]; + for (int k = 0; k < mapped.Length; k++) { - var mapped = new T[range.Item2 - range.Item1]; - for (int k = 0; k < mapped.Length; k++) - { - mapped[k] = select(k + range.Item1); - } - localData.Add(reduce(mapped)); - return localData; - }, + mapped[k] = select(k + range.Item1); + } + + localData.Add(reduce(mapped)); + return localData; + }, localResult => + { + lock (syncLock) { - lock (syncLock) - { - intermediateResults.Add(reduce(localResult.ToArray())); - } - }); + intermediateResults.Add(reduce(localResult.ToArray())); + } + }); return reduce(intermediateResults.ToArray()); } @@ -212,8 +237,15 @@ namespace MathNet.Numerics.Threading /// The selected value. public static TOut Aggregate(T[] array, Func select, Func reduce) { - if (select == null) throw new ArgumentNullException("select"); - if (reduce == null) throw new ArgumentNullException("reduce"); + if (select == null) + { + throw new ArgumentNullException("select"); + } + + if (reduce == null) + { + throw new ArgumentNullException("reduce"); + } // Special case: no action if (array == null || array.Length == 0) @@ -224,7 +256,7 @@ namespace MathNet.Numerics.Threading // Special case: single action, inline if (array.Length == 1) { - return reduce(new[] {select(0, array[0])}); + return reduce(new[] { select(0, array[0]) }); } // Special case: straight execution without parallelism @@ -235,6 +267,7 @@ namespace MathNet.Numerics.Threading { mapped[k] = select(k, array[k]); } + return reduce(mapped); } @@ -246,22 +279,24 @@ namespace MathNet.Numerics.Threading CreateParallelOptions(), () => new List(), (range, loop, localData) => + { + var mapped = new TOut[range.Item2 - range.Item1]; + for (int k = 0; k < mapped.Length; k++) { - var mapped = new TOut[range.Item2 - range.Item1]; - for (int k = 0; k < mapped.Length; k++) - { - mapped[k] = select(k + range.Item1, array[k + range.Item1]); - } - localData.Add(reduce(mapped)); - return localData; - }, + mapped[k] = select(k + range.Item1, array[k + range.Item1]); + } + + localData.Add(reduce(mapped)); + return localData; + }, localResult => + { + lock (syncLock) { - lock (syncLock) - { - intermediateResults.Add(reduce(localResult.ToArray())); - } - }); + intermediateResults.Add(reduce(localResult.ToArray())); + } + }); + return reduce(intermediateResults.ToArray()); } @@ -277,24 +312,25 @@ namespace MathNet.Numerics.Threading public static T Aggregate(int fromInclusive, int toExclusive, Func select, Func reducePair, T reduceDefault) { return Aggregate(fromInclusive, toExclusive, select, results => + { + if (results == null || results.Length == 0) { - if (results == null || results.Length == 0) - { - return reduceDefault; - } + return reduceDefault; + } - if (results.Length == 1) - { - return results[0]; - } + if (results.Length == 1) + { + return results[0]; + } - T result = results[0]; - for (int i = 1; i < results.Length; i++) - { - result = reducePair(result, results[i]); - } - return result; - }); + T result = results[0]; + for (int i = 1; i < results.Length; i++) + { + result = reducePair(result, results[i]); + } + + return result; + }); } /// @@ -308,24 +344,25 @@ namespace MathNet.Numerics.Threading public static TOut Aggregate(T[] array, Func select, Func reducePair, TOut reduceDefault) { return Aggregate(array, select, results => + { + if (results == null || results.Length == 0) { - if (results == null || results.Length == 0) - { - return reduceDefault; - } + return reduceDefault; + } - if (results.Length == 1) - { - return results[0]; - } + if (results.Length == 1) + { + return results[0]; + } - TOut result = results[0]; - for (int i = 1; i < results.Length; i++) - { - result = reducePair(result, results[i]); - } - return result; - }); + TOut result = results[0]; + for (int i = 1; i < results.Length; i++) + { + result = reducePair(result, results[i]); + } + + return result; + }); } } -} \ No newline at end of file +} diff --git a/src/Settings.StyleCop b/src/Settings.StyleCop deleted file mode 100644 index 57769b93..00000000 --- a/src/Settings.StyleCop +++ /dev/null @@ -1,76 +0,0 @@ - - - - - False - - - - - - - - - False - - - - - False - - - - - False - - - - - False - - - - - False - - - - - False - - - - - False - - - - - False - - - - - - - - - - False - - - - - - - - - - False - - - - - - - \ No newline at end of file