diff --git a/src/Numerics/Distributions/Bernoulli.cs b/src/Numerics/Distributions/Bernoulli.cs index 390ba0fd..c49a6243 100644 --- a/src/Numerics/Distributions/Bernoulli.cs +++ b/src/Numerics/Distributions/Bernoulli.cs @@ -45,7 +45,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _p; + readonly double _p; /// /// Initializes a new instance of the Bernoulli class. @@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions /// If the Bernoulli parameter is not in the range [0,1]. public Bernoulli(double p) { + if (!IsValidParameterSet(p)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(p); + _p = p; } /// @@ -66,8 +71,13 @@ namespace MathNet.Numerics.Distributions /// If the Bernoulli parameter is not in the range [0,1]. public Bernoulli(double p, System.Random randomSource) { + if (!IsValidParameterSet(p)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(p); + _p = p; } /// @@ -88,28 +98,12 @@ namespace MathNet.Numerics.Distributions return p >= 0.0 && p <= 1.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. - /// When the parameters are out of range. - void SetParameters(double p) - { - if (!(p >= 0.0 && p <= 1.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _p = p; - } - /// /// Gets or sets the probability of generating a one. Range: 0 ≤ p ≤ 1. /// public double P { get { return _p; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/Beta.cs b/src/Numerics/Distributions/Beta.cs index b3868572..26bd6258 100644 --- a/src/Numerics/Distributions/Beta.cs +++ b/src/Numerics/Distributions/Beta.cs @@ -53,8 +53,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _shapeA; - double _shapeB; + readonly double _shapeA; + readonly double _shapeB; /// /// Initializes a new instance of the Beta class. @@ -63,8 +63,14 @@ namespace MathNet.Numerics.Distributions /// The β shape parameter of the Beta distribution. Range: β ≥ 0. public Beta(double a, double b) { + if (!IsValidParameterSet(a, b)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(a, b); + _shapeA = a; + _shapeB = b; } /// @@ -75,8 +81,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Beta(double a, double b, System.Random randomSource) { + if (!IsValidParameterSet(a, b)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(a, b); + _shapeA = a; + _shapeB = b; } /// @@ -98,30 +110,12 @@ namespace MathNet.Numerics.Distributions return a >= 0.0 && b >= 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The α shape parameter of the Beta distribution. Range: α ≥ 0. - /// The β shape parameter of the Beta distribution. Range: β ≥ 0. - /// When the parameters are out of range. - void SetParameters(double a, double b) - { - if (a < 0.0 || b < 0.0 || Double.IsNaN(a) || Double.IsNaN(b)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _shapeA = a; - _shapeB = b; - } - /// /// Gets or sets the α shape parameter of the Beta distribution. Range: α ≥ 0. /// public double A { get { return _shapeA; } - set { SetParameters(value, _shapeB); } } /// @@ -130,7 +124,6 @@ namespace MathNet.Numerics.Distributions public double B { get { return _shapeB; } - set { SetParameters(_shapeA, value); } } /// diff --git a/src/Numerics/Distributions/Binomial.cs b/src/Numerics/Distributions/Binomial.cs index 688e9b2e..028350f5 100644 --- a/src/Numerics/Distributions/Binomial.cs +++ b/src/Numerics/Distributions/Binomial.cs @@ -47,8 +47,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _p; - int _trials; + readonly double _p; + readonly int _trials; /// /// Initializes a new instance of the Binomial class. @@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Binomial(double p, int n) { + if (!IsValidParameterSet(p, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(p, n); + _p = p; + _trials = n; } /// @@ -73,8 +79,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Binomial(double p, int n, System.Random randomSource) { + if (!IsValidParameterSet(p, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(p, n); + _p = p; + _trials = n; } /// @@ -96,31 +108,12 @@ namespace MathNet.Numerics.Distributions return p >= 0.0 && p <= 1.0 && n >= 0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The success probability (p) in each trial. Range: 0 ≤ p ≤ 1. - /// The number of trials (n). Range: n ≥ 0. - /// If is not in the interval [0.0,1.0]. - /// If is negative. - void SetParameters(double p, int n) - { - if (!(p >= 0.0 && p <= 1.0 && n >= 0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _p = p; - _trials = n; - } - /// /// Gets or sets the success probability in each trial. Range: 0 ≤ p ≤ 1. /// public double P { get { return _p; } - set { SetParameters(value, _trials); } } /// @@ -129,7 +122,6 @@ namespace MathNet.Numerics.Distributions public int N { get { return _trials; } - set { SetParameters(_p, value); } } /// diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs index f0ade93c..7acee836 100644 --- a/src/Numerics/Distributions/Categorical.cs +++ b/src/Numerics/Distributions/Categorical.cs @@ -55,8 +55,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double[] _pmfNormalized; - double[] _cdfUnnormalized; + readonly double[] _pmfNormalized; + readonly double[] _cdfUnnormalized; /// /// Initializes a new instance of the Categorical class. @@ -65,9 +65,8 @@ namespace MathNet.Numerics.Distributions /// as this is often impossible using floating point arithmetic. /// If any of the probabilities are negative or do not sum to one. public Categorical(double[] probabilityMass) + : this(probabilityMass, SystemRandomSource.Default) { - _random = SystemRandomSource.Default; - SetParameters(probabilityMass); } /// @@ -79,8 +78,28 @@ namespace MathNet.Numerics.Distributions /// If any of the probabilities are negative or do not sum to one. public Categorical(double[] probabilityMass, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidProbabilityMass(probabilityMass)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(probabilityMass); + + // Extract unnormalized cumulative distribution + _cdfUnnormalized = new double[probabilityMass.Length]; + _cdfUnnormalized[0] = probabilityMass[0]; + for (int i = 1; i < probabilityMass.Length; i++) + { + _cdfUnnormalized[i] = _cdfUnnormalized[i - 1] + probabilityMass[i]; + } + + // Extract normalized probability mass + var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1]; + _pmfNormalized = new double[probabilityMass.Length]; + for (int i = 0; i < probabilityMass.Length; i++) + { + _pmfNormalized[i] = probabilityMass[i]/sum; + } } /// @@ -106,7 +125,27 @@ namespace MathNet.Numerics.Distributions } _random = SystemRandomSource.Default; - SetParameters(p); + + if (Control.CheckDistributionParameters && !IsValidProbabilityMass(p)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + // Extract unnormalized cumulative distribution + _cdfUnnormalized = new double[p.Length]; + _cdfUnnormalized[0] = p[0]; + for (int i1 = 1; i1 < p.Length; i1++) + { + _cdfUnnormalized[i1] = _cdfUnnormalized[i1 - 1] + p[i1]; + } + + // Extract normalized probability mass + var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1]; + _pmfNormalized = new double[p.Length]; + for (int i2 = 0; i2 < p.Length; i2++) + { + _pmfNormalized[i2] = p[i2]/sum; + } } /// @@ -162,36 +201,6 @@ namespace MathNet.Numerics.Distributions return last > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// An array of nonnegative ratios: this array does not need to be normalized - /// as this is often impossible using floating point arithmetic. - /// When the parameters are out of range. - void SetParameters(double[] p) - { - if (Control.CheckDistributionParameters && !IsValidProbabilityMass(p)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - // Extract unnormalized cumulative distribution - _cdfUnnormalized = new double[p.Length]; - _cdfUnnormalized[0] = p[0]; - for (int i = 1; i < p.Length; i++) - { - _cdfUnnormalized[i] = _cdfUnnormalized[i - 1] + p[i]; - } - - // Extract normalized probability mass - var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1]; - _pmfNormalized = new double[p.Length]; - for (int i = 0; i < p.Length; i++) - { - _pmfNormalized[i] = p[i]/sum; - } - } - /// /// Gets or sets the probability mass vector (non-negative ratios) of the multinomial. /// @@ -199,7 +208,6 @@ namespace MathNet.Numerics.Distributions public double[] P { get { return (double[])_pmfNormalized.Clone(); } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs index 5db3835d..38b91037 100644 --- a/src/Numerics/Distributions/Cauchy.cs +++ b/src/Numerics/Distributions/Cauchy.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _location; - double _scale; + readonly double _location; + readonly double _scale; /// /// Initializes a new instance of the class with the location parameter set to 0 and the scale parameter set to 1 @@ -62,8 +62,14 @@ namespace MathNet.Numerics.Distributions /// The scale (γ) of the distribution. Range: γ > 0. public Cauchy(double location, double scale) { + if (!IsValidParameterSet(location, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(location, scale); + _location = location; + _scale = scale; } /// @@ -74,8 +80,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Cauchy(double location, double scale, System.Random randomSource) { + if (!IsValidParameterSet(location, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(location, scale); + _location = location; + _scale = scale; } /// @@ -97,30 +109,12 @@ namespace MathNet.Numerics.Distributions return scale > 0.0 && !Double.IsNaN(location); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The location (x0) of the distribution. - /// The scale (γ) of the distribution. Range: γ > 0. - /// When the parameters are out of range. - void SetParameters(double location, double scale) - { - if (scale <= 0.0 || Double.IsNaN(location) || Double.IsNaN(scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _location = location; - _scale = scale; - } - /// /// Gets or sets the location (x0) of the distribution. /// public double Location { get { return _location; } - set { SetParameters(value, _scale); } } /// @@ -129,7 +123,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_location, value); } } /// diff --git a/src/Numerics/Distributions/Chi.cs b/src/Numerics/Distributions/Chi.cs index 4d03072a..1b8d04e2 100644 --- a/src/Numerics/Distributions/Chi.cs +++ b/src/Numerics/Distributions/Chi.cs @@ -46,7 +46,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _freedom; + readonly double _freedom; /// /// Initializes a new instance of the class. @@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (k) of the distribution. Range: k > 0. public Chi(double freedom) { + if (!IsValidParameterSet(freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(freedom); + _freedom = freedom; } /// @@ -65,8 +70,13 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Chi(double freedom, System.Random randomSource) { + if (!IsValidParameterSet(freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(freedom); + _freedom = freedom; } /// @@ -87,28 +97,12 @@ namespace MathNet.Numerics.Distributions return freedom > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The degrees of freedom (k) of the distribution. Range: k > 0. - /// When the parameters are out of range. - void SetParameters(double freedom) - { - if (freedom <= 0.0 || Double.IsNaN(freedom)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _freedom = freedom; - } - /// /// Gets or sets the degrees of freedom (k) of the Chi distribution. Range: k > 0. /// public double DegreesOfFreedom { get { return _freedom; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/ChiSquared.cs b/src/Numerics/Distributions/ChiSquared.cs index 6c62f1cf..16c4214a 100644 --- a/src/Numerics/Distributions/ChiSquared.cs +++ b/src/Numerics/Distributions/ChiSquared.cs @@ -44,7 +44,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _freedom; + readonly double _freedom; /// /// Initializes a new instance of the class. @@ -52,8 +52,13 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (k) of the distribution. Range: k > 0. public ChiSquared(double freedom) { + if (!IsValidParameterSet(freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(freedom); + _freedom = freedom; } /// @@ -63,8 +68,13 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public ChiSquared(double freedom, System.Random randomSource) { + if (!IsValidParameterSet(freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(freedom); + _freedom = freedom; } /// @@ -85,28 +95,12 @@ namespace MathNet.Numerics.Distributions return freedom > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The degrees of freedom (k) of the distribution. Range: k > 0. - /// When the parameters are out of range. - void SetParameters(double freedom) - { - if (freedom <= 0.0 || Double.IsNaN(freedom)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _freedom = freedom; - } - /// /// Gets or sets the degrees of freedom (k) of the Chi-Squared distribution. Range: k > 0. /// public double DegreesOfFreedom { get { return _freedom; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/ContinuousUniform.cs b/src/Numerics/Distributions/ContinuousUniform.cs index 32ee56c7..b4e0363a 100644 --- a/src/Numerics/Distributions/ContinuousUniform.cs +++ b/src/Numerics/Distributions/ContinuousUniform.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _lower; - double _upper; + readonly double _lower; + readonly double _upper; /// /// Initializes a new instance of the ContinuousUniform class with lower bound 0 and upper bound 1. @@ -63,8 +63,14 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the lower bound. public ContinuousUniform(double lower, double upper) { + if (!IsValidParameterSet(lower, upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(lower, upper); + _lower = lower; + _upper = upper; } /// @@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the lower bound. public ContinuousUniform(double lower, double upper, System.Random randomSource) { + if (!IsValidParameterSet(lower, upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(lower, upper); + _lower = lower; + _upper = upper; } /// @@ -96,24 +108,7 @@ namespace MathNet.Numerics.Distributions /// Upper bound. Range: lower ≤ upper. public static bool IsValidParameterSet(double lower, double upper) { - return lower <= upper && !Double.IsInfinity(lower) && !Double.IsInfinity(upper); - } - - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// Lower bound. Range: lower ≤ upper. - /// Upper bound. Range: lower ≤ upper. - /// When the parameters are out of range. - void SetParameters(double lower, double upper) - { - if (upper < lower || Double.IsNaN(upper) || Double.IsNaN(lower)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _lower = lower; - _upper = upper; + return lower <= upper; } /// @@ -122,7 +117,6 @@ namespace MathNet.Numerics.Distributions public double LowerBound { get { return _lower; } - set { SetParameters(value, _upper); } } /// @@ -131,7 +125,6 @@ namespace MathNet.Numerics.Distributions public double UpperBound { get { return _upper; } - set { SetParameters(_lower, value); } } /// diff --git a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs index 83c2a6af..5b92cf53 100644 --- a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs +++ b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs @@ -51,8 +51,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _lambda; - double _nu; + readonly double _lambda; + readonly double _nu; /// /// The mean of the distribution. @@ -82,8 +82,14 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public ConwayMaxwellPoisson(double lambda, double nu) { + if (!IsValidParameterSet(lambda, nu)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(lambda, nu); + _lambda = lambda; + _nu = nu; } /// @@ -94,8 +100,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public ConwayMaxwellPoisson(double lambda, double nu, System.Random randomSource) { + if (!IsValidParameterSet(lambda, nu)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(lambda, nu); + _lambda = lambda; + _nu = nu; } /// @@ -117,30 +129,12 @@ namespace MathNet.Numerics.Distributions return lambda > 0.0 && nu >= 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The lambda (λ) parameter. Range: λ > 0. - /// The rate of decay (ν) parameter. Range: ν ≥ 0. - /// When the parameters are out of range. - void SetParameters(double lambda, double nu) - { - if (!(lambda > 0.0 && nu >= 0.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _lambda = lambda; - _nu = nu; - } - /// /// Gets or sets the lambda (λ) parameter. Range: λ > 0. /// public double Lambda { get { return _lambda; } - set { SetParameters(value, _nu); } } /// @@ -149,7 +143,6 @@ namespace MathNet.Numerics.Distributions public double Nu { get { return _nu; } - set { SetParameters(_lambda, value); } } /// diff --git a/src/Numerics/Distributions/Dirichlet.cs b/src/Numerics/Distributions/Dirichlet.cs index ac69706c..722e6270 100644 --- a/src/Numerics/Distributions/Dirichlet.cs +++ b/src/Numerics/Distributions/Dirichlet.cs @@ -43,7 +43,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double[] _alpha; + readonly double[] _alpha; /// /// Initializes a new instance of the Dirichlet class. The distribution will @@ -52,8 +52,13 @@ namespace MathNet.Numerics.Distributions /// An array with the Dirichlet parameters. public Dirichlet(double[] alpha) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(alpha); + _alpha = (double[]) alpha.Clone(); } /// @@ -64,8 +69,13 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Dirichlet(double[] alpha, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(alpha); + _alpha = (double[]) alpha.Clone(); } /// @@ -83,7 +93,12 @@ namespace MathNet.Numerics.Distributions } _random = SystemRandomSource.Default; - SetParameters(parm); + if (Control.CheckDistributionParameters && !IsValidParameterSet(parm)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + _alpha = (double[]) parm.Clone(); } /// @@ -102,7 +117,12 @@ namespace MathNet.Numerics.Distributions } _random = randomSource ?? SystemRandomSource.Default; - SetParameters(parm); + if (Control.CheckDistributionParameters && !IsValidParameterSet(parm)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + _alpha = (double[]) parm.Clone(); } /// @@ -135,28 +155,12 @@ namespace MathNet.Numerics.Distributions return !allzero; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The parameters of the Dirichlet distribution. - /// When the parameters are out of range. - void SetParameters(double[] alpha) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(alpha)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _alpha = (double[]) alpha.Clone(); - } - /// /// Gets or sets the parameters of the Dirichlet distribution. /// public double[] Alpha { get { return _alpha; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/DiscreteUniform.cs b/src/Numerics/Distributions/DiscreteUniform.cs index 8baf31b9..cf6479e2 100644 --- a/src/Numerics/Distributions/DiscreteUniform.cs +++ b/src/Numerics/Distributions/DiscreteUniform.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - int _lower; - int _upper; + readonly int _lower; + readonly int _upper; /// /// Initializes a new instance of the DiscreteUniform class. @@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions /// Upper bound. Range: lower ≤ upper. public DiscreteUniform(int lower, int upper) { + if (!IsValidParameterSet(lower, upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(lower, upper); + _lower = lower; + _upper = upper; } /// @@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public DiscreteUniform(int lower, int upper, System.Random randomSource) { + if (!IsValidParameterSet(lower, upper)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(lower, upper); + _lower = lower; + _upper = upper; } /// @@ -92,30 +104,12 @@ namespace MathNet.Numerics.Distributions return lower <= upper; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// Lower bound. Range: lower ≤ upper. - /// Upper bound. Range: lower ≤ upper. - /// When the parameters are out of range. - void SetParameters(int lower, int upper) - { - if (!(lower <= upper)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _lower = lower; - _upper = upper; - } - /// /// Gets or sets the lower bound of the probability distribution. /// public int LowerBound { get { return _lower; } - set { SetParameters(value, _upper); } } /// @@ -124,7 +118,6 @@ namespace MathNet.Numerics.Distributions public int UpperBound { get { return _upper; } - set { SetParameters(_lower, value); } } /// diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs index c57f0c25..e6e6eae3 100644 --- a/src/Numerics/Distributions/Erlang.cs +++ b/src/Numerics/Distributions/Erlang.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _shape; - double _rate; + readonly double _shape; + readonly double _rate; /// /// Initializes a new instance of the class. @@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions /// The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0. public Erlang(int shape, double rate) { + if (!IsValidParameterSet(shape, rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(shape, rate); + _shape = shape; + _rate = rate; } /// @@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Erlang(int shape, double rate, System.Random randomSource) { + if (!IsValidParameterSet(shape, rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(shape, rate); + _shape = shape; + _rate = rate; } /// @@ -114,30 +126,12 @@ namespace MathNet.Numerics.Distributions return shape >= 0.0 && rate >= 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The shape (k) of the Erlang distribution. Range: k ≥ 0. - /// The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0. - /// When the parameters are out of range. - void SetParameters(double shape, double rate) - { - if (shape < 0.0 || rate < 0.0 || Double.IsNaN(shape) || Double.IsNaN(rate)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _shape = shape; - _rate = rate; - } - /// /// Gets or sets the shape (k) of the Erlang distribution. Range: k ≥ 0. /// public int Shape { get { return (int)_shape; } - set { SetParameters(value, _rate); } } /// @@ -146,7 +140,6 @@ namespace MathNet.Numerics.Distributions public double Rate { get { return _rate; } - set { SetParameters(_shape, value); } } /// @@ -155,15 +148,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return 1.0 / _rate; } - set - { - var invScale = 1.0 / value; - if (Double.IsNegativeInfinity(invScale)) - { - invScale = -invScale; - } - SetParameters(_shape, invScale); - } } /// diff --git a/src/Numerics/Distributions/Exponential.cs b/src/Numerics/Distributions/Exponential.cs index 038fd415..4f210c5d 100644 --- a/src/Numerics/Distributions/Exponential.cs +++ b/src/Numerics/Distributions/Exponential.cs @@ -46,7 +46,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _rate; + readonly double _rate; /// /// Initializes a new instance of the class. @@ -54,8 +54,13 @@ namespace MathNet.Numerics.Distributions /// The rate (λ) parameter of the distribution. Range: λ ≥ 0. public Exponential(double rate) { + if (!IsValidParameterSet(rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(rate); + _rate = rate; } /// @@ -65,8 +70,13 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Exponential(double rate, System.Random randomSource) { + if (!IsValidParameterSet(rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(rate); + _rate = rate; } /// @@ -87,28 +97,12 @@ namespace MathNet.Numerics.Distributions return rate >= 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The rate (λ) parameter of the distribution. Range: λ ≥ 0. - /// When the parameters are out of range. - void SetParameters(double rate) - { - if (rate < 0.0 || Double.IsNaN(rate)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _rate = rate; - } - /// /// Gets or sets the rate (λ) parameter of the distribution. Range: λ ≥ 0. /// public double Rate { get { return _rate; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs index b379b51a..0ad7af65 100644 --- a/src/Numerics/Distributions/FisherSnedecor.cs +++ b/src/Numerics/Distributions/FisherSnedecor.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _freedom1; - double _freedom2; + readonly double _freedom1; + readonly double _freedom2; /// /// Initializes a new instance of the class. @@ -55,8 +55,14 @@ namespace MathNet.Numerics.Distributions /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. public FisherSnedecor(double d1, double d2) { + if (!IsValidParameterSet(d1, d2)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(d1, d2); + _freedom1 = d1; + _freedom2 = d2; } /// @@ -67,8 +73,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public FisherSnedecor(double d1, double d2, System.Random randomSource) { + if (!IsValidParameterSet(d1, d2)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(d1, d2); + _freedom1 = d1; + _freedom2 = d2; } /// @@ -90,30 +102,12 @@ namespace MathNet.Numerics.Distributions return d1 > 0.0 && d2 > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The first degree of freedom (d1) of the distribution. Range: d1 > 0. - /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. - /// When the parameters are out of range. - void SetParameters(double d1, double d2) - { - if (d1 <= 0.0 || d2 <= 0.0 || Double.IsNaN(d1) || Double.IsNaN(d2)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _freedom1 = d1; - _freedom2 = d2; - } - /// /// Gets or sets the first degree of freedom (d1) of the distribution. Range: d1 > 0. /// public double DegreesOfFreedom1 { get { return _freedom1; } - set { SetParameters(value, _freedom2); } } /// @@ -122,7 +116,6 @@ namespace MathNet.Numerics.Distributions public double DegreesOfFreedom2 { get { return _freedom2; } - set { SetParameters(_freedom1, value); } } /// diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs index 7987ce5f..bd0f0da1 100644 --- a/src/Numerics/Distributions/Gamma.cs +++ b/src/Numerics/Distributions/Gamma.cs @@ -54,8 +54,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _shape; - double _rate; + readonly double _shape; + readonly double _rate; /// /// Initializes a new instance of the Gamma class. @@ -64,8 +64,14 @@ namespace MathNet.Numerics.Distributions /// The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0. public Gamma(double shape, double rate) { + if (!IsValidParameterSet(shape, rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(shape, rate); + _shape = shape; + _rate = rate; } /// @@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Gamma(double shape, double rate, System.Random randomSource) { + if (!IsValidParameterSet(shape, rate)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(shape, rate); + _shape = shape; + _rate = rate; } /// @@ -123,30 +135,12 @@ namespace MathNet.Numerics.Distributions return shape >= 0.0 && rate >= 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The shape (k, α) of the Gamma distribution. Range: α ≥ 0. - /// The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0. - /// When the parameters are out of range. - void SetParameters(double shape, double rate) - { - if (shape < 0.0 || rate < 0.0 || Double.IsNaN(shape) || Double.IsNaN(rate)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _shape = shape; - _rate = rate; - } - /// /// Gets or sets the shape (k, α) of the Gamma distribution. Range: α ≥ 0. /// public double Shape { get { return _shape; } - set { SetParameters(value, _rate); } } /// @@ -155,7 +149,6 @@ namespace MathNet.Numerics.Distributions public double Rate { get { return _rate; } - set { SetParameters(_shape, value); } } /// @@ -164,15 +157,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return 1.0 / _rate; } - set - { - var rate = 1.0 / value; - if (Double.IsNegativeInfinity(rate)) - { - rate = -rate; - } - SetParameters(_shape, rate); - } } /// diff --git a/src/Numerics/Distributions/Geometric.cs b/src/Numerics/Distributions/Geometric.cs index e7bbc056..c68afe39 100644 --- a/src/Numerics/Distributions/Geometric.cs +++ b/src/Numerics/Distributions/Geometric.cs @@ -45,7 +45,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _p; + readonly double _p; /// /// Initializes a new instance of the Geometric class. @@ -53,8 +53,13 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public Geometric(double p) { + if (!IsValidParameterSet(p)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(p); + _p = p; } /// @@ -64,8 +69,13 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Geometric(double p, System.Random randomSource) { + if (!IsValidParameterSet(p)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(p); + _p = p; } /// @@ -86,28 +96,12 @@ namespace MathNet.Numerics.Distributions return p >= 0.0 && p <= 1.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. - /// When the parameters are out of range. - void SetParameters(double p) - { - if (!(p >= 0.0 && p <= 1.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _p = p; - } - /// /// Gets or sets the probability of generating a one. Range: 0 ≤ p ≤ 1. /// public double P { get { return _p; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/Hypergeometric.cs b/src/Numerics/Distributions/Hypergeometric.cs index 774507e6..9897063d 100644 --- a/src/Numerics/Distributions/Hypergeometric.cs +++ b/src/Numerics/Distributions/Hypergeometric.cs @@ -46,9 +46,9 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - int _population; - int _success; - int _draws; + readonly int _population; + readonly int _success; + readonly int _draws; /// /// Initializes a new instance of the Hypergeometric class. @@ -58,8 +58,15 @@ namespace MathNet.Numerics.Distributions /// The number of draws without replacement (n). public Hypergeometric(int population, int success, int draws) { + if (!IsValidParameterSet(population, success, draws)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(population, success, draws); + _population = population; + _success = success; + _draws = draws; } /// @@ -71,8 +78,15 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Hypergeometric(int population, int success, int draws, System.Random randomSource) { + if (!IsValidParameterSet(population, success, draws)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(population, success, draws); + _population = population; + _success = success; + _draws = draws; } /// @@ -97,25 +111,6 @@ namespace MathNet.Numerics.Distributions return population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The size of the population (N). - /// The number successes within the population (K, M). - /// The number of draws without replacement (n). - /// When the parameters are out of range. - void SetParameters(int population, int success, int draws) - { - if (!(population >= 0 && success >= 0 && draws >= 0 && success <= population && draws <= population)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _population = population; - _success = success; - _draws = draws; - } - /// /// Gets or sets the random number generator which is used to draw random samples. /// @@ -131,7 +126,6 @@ namespace MathNet.Numerics.Distributions public int Population { get { return _population; } - set { SetParameters(value, _success, _draws); } } /// @@ -140,7 +134,6 @@ namespace MathNet.Numerics.Distributions public int Draws { get { return _draws; } - set { SetParameters(_population, value, _draws); } } /// @@ -149,37 +142,6 @@ namespace MathNet.Numerics.Distributions public int Success { get { return _success; } - set { SetParameters(_population, _success, value); } - } - - /// - /// Gets or sets the size of the population (N). - /// - [Obsolete("Use Population instead. Scheduled for removal in v3.0.")] - public int PopulationSize - { - get { return _population; } - set { SetParameters(value, _success, _draws); } - } - - /// - /// Gets or sets the number of draws without replacement (n). - /// - [Obsolete("Use Draws instead. Scheduled for removal in v3.0.")] - public int N - { - get { return _draws; } - set { SetParameters(_population, value, _draws); } - } - - /// - /// Gets or sets the number successes within the population (K, M). - /// - [Obsolete("Use Success instead. Scheduled for removal in v3.0.")] - public int M - { - get { return _success; } - set { SetParameters(_population, _success, value); } } /// diff --git a/src/Numerics/Distributions/InverseGamma.cs b/src/Numerics/Distributions/InverseGamma.cs index 92e64a86..774d7eb5 100644 --- a/src/Numerics/Distributions/InverseGamma.cs +++ b/src/Numerics/Distributions/InverseGamma.cs @@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _shape; - double _scale; + readonly double _shape; + readonly double _scale; /// /// Initializes a new instance of the class. @@ -56,8 +56,14 @@ namespace MathNet.Numerics.Distributions /// The scale (β) of the distribution. Range: β > 0. public InverseGamma(double shape, double scale) { + if (!IsValidParameterSet(shape, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(shape, scale); + _shape = shape; + _scale = scale; } /// @@ -68,8 +74,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public InverseGamma(double shape, double scale, System.Random randomSource) { + if (!IsValidParameterSet(shape, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(shape, scale); + _shape = shape; + _scale = scale; } /// @@ -91,30 +103,12 @@ namespace MathNet.Numerics.Distributions return shape > 0.0 && scale > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The shape (α) of the distribution. Range: α > 0. - /// The scale (β) of the distribution. Range: β > 0. - /// When the parameters are out of range. - void SetParameters(double shape, double scale) - { - if (shape <= 0.0 || scale <= 0.0 || Double.IsNaN(shape) || Double.IsNaN(scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _shape = shape; - _scale = scale; - } - /// /// Gets or sets the shape (α) parameter. Range: α > 0. /// public double Shape { get { return _shape; } - set { SetParameters(value, _scale); } } /// @@ -123,7 +117,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_shape, value); } } /// diff --git a/src/Numerics/Distributions/InverseWishart.cs b/src/Numerics/Distributions/InverseWishart.cs index 7bf0242e..a56f451e 100644 --- a/src/Numerics/Distributions/InverseWishart.cs +++ b/src/Numerics/Distributions/InverseWishart.cs @@ -46,13 +46,13 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _freedom; - Matrix _scale; + readonly double _freedom; + readonly Matrix _scale; /// /// Caches the Cholesky factorization of the scale matrix. /// - Cholesky _chol; + readonly Cholesky _chol; /// /// Initializes a new instance of the class. @@ -61,8 +61,15 @@ namespace MathNet.Numerics.Distributions /// The scale matrix (Ψ) for the inverse Wishart distribution. public InverseWishart(double degreesOfFreedom, Matrix scale) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(degreesOfFreedom, scale); + _freedom = degreesOfFreedom; + _scale = scale; + _chol = _scale.Cholesky(); } /// @@ -73,8 +80,15 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public InverseWishart(double degreesOfFreedom, Matrix scale, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(degreesOfFreedom, scale); + _freedom = degreesOfFreedom; + _scale = scale; + _chol = _scale.Cholesky(); } /// @@ -109,31 +123,12 @@ namespace MathNet.Numerics.Distributions return degreesOfFreedom > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The degree of freedom (ν) for the inverse Wishart distribution. - /// The scale matrix (Ψ) for the inverse Wishart distribution. - /// When the parameters are out of range. - void SetParameters(double degreesOfFreedom, Matrix scale) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _freedom = degreesOfFreedom; - _scale = scale; - _chol = _scale.Cholesky(); - } - /// /// Gets or sets the degree of freedom (ν) for the inverse Wishart distribution. /// public double DegreesOfFreedom { get { return _freedom; } - set { SetParameters(value, _scale); } } /// @@ -142,7 +137,6 @@ namespace MathNet.Numerics.Distributions public Matrix Scale { get { return _scale; } - set { SetParameters(_freedom, value); } } /// diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs index a8fb0518..43869fe1 100644 --- a/src/Numerics/Distributions/Laplace.cs +++ b/src/Numerics/Distributions/Laplace.cs @@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _location; - double _scale; + readonly double _location; + readonly double _scale; /// /// Initializes a new instance of the class (location = 0, scale = 1). @@ -65,8 +65,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Laplace(double location, double scale) { + if (!IsValidParameterSet(location, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(location, scale); + _location = location; + _scale = scale; } /// @@ -78,8 +84,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Laplace(double location, double scale, System.Random randomSource) { + if (!IsValidParameterSet(location, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(location, scale); + _location = location; + _scale = scale; } /// @@ -101,30 +113,12 @@ namespace MathNet.Numerics.Distributions return scale > 0.0 && !Double.IsNaN(location); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The location (μ) of the distribution. - /// The scale (b) of the distribution. Range: b > 0. - /// When the parameters are out of range. - void SetParameters(double location, double scale) - { - if (scale <= 0.0 || Double.IsNaN(location) || Double.IsNaN(scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _location = location; - _scale = scale; - } - /// /// Gets or sets the location (μ) of the Laplace distribution. /// public double Location { get { return _location; } - set { SetParameters(value, _scale); } } /// @@ -133,7 +127,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_location, value); } } /// diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs index eb298892..dd8c821a 100644 --- a/src/Numerics/Distributions/LogNormal.cs +++ b/src/Numerics/Distributions/LogNormal.cs @@ -47,8 +47,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _mu; - double _sigma; + readonly double _mu; + readonly double _sigma; /// /// Initializes a new instance of the class. @@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions /// The shape (σ) of the logarithm of the distribution. Range: σ ≥ 0. public LogNormal(double mu, double sigma) { + if (!IsValidParameterSet(mu, sigma)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(mu, sigma); + _mu = mu; + _sigma = sigma; } /// @@ -73,8 +79,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public LogNormal(double mu, double sigma, System.Random randomSource) { + if (!IsValidParameterSet(mu, sigma)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(mu, sigma); + _mu = mu; + _sigma = sigma; } /// @@ -134,30 +146,12 @@ namespace MathNet.Numerics.Distributions return sigma >= 0.0 && !Double.IsNaN(mu); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The log-scale (μ) of the distribution. - /// The shape (σ) of the distribution. Range: σ ≥ 0. - /// When the parameters are out of range. - void SetParameters(double mu, double sigma) - { - if (sigma < 0.0 || Double.IsNaN(mu) || Double.IsNaN(sigma)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _mu = mu; - _sigma = sigma; - } - /// /// Gets or sets the log-scale (μ) (mean of the logarithm) of the distribution. /// public double Mu { get { return _mu; } - set { SetParameters(value, _sigma); } } /// @@ -166,7 +160,6 @@ namespace MathNet.Numerics.Distributions public double Sigma { get { return _sigma; } - set { SetParameters(_mu, value); } } /// diff --git a/src/Numerics/Distributions/MatrixNormal.cs b/src/Numerics/Distributions/MatrixNormal.cs index ecbbb402..efbcaf3d 100644 --- a/src/Numerics/Distributions/MatrixNormal.cs +++ b/src/Numerics/Distributions/MatrixNormal.cs @@ -49,17 +49,17 @@ namespace MathNet.Numerics.Distributions /// /// The mean of the matrix normal distribution. /// - Matrix _m; + readonly Matrix _m; /// /// The covariance matrix for the rows. /// - Matrix _v; + readonly Matrix _v; /// /// The covariance matrix for the columns. /// - Matrix _k; + readonly Matrix _k; /// /// Initializes a new instance of the class. @@ -70,8 +70,15 @@ namespace MathNet.Numerics.Distributions /// If the dimensions of the mean and two covariance matrices don't match. public MatrixNormal(Matrix m, Matrix v, Matrix k) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(m, v, k); + _m = m; + _v = v; + _k = k; } /// @@ -84,8 +91,15 @@ namespace MathNet.Numerics.Distributions /// If the dimensions of the mean and two covariance matrices don't match. public MatrixNormal(Matrix m, Matrix v, Matrix k, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(m, v, k); + _m = m; + _v = v; + _k = k; } /// @@ -138,25 +152,6 @@ namespace MathNet.Numerics.Distributions return true; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The mean of the matrix normal. - /// The covariance matrix for the rows. - /// The covariance matrix for the columns. - /// When the parameters are out of range. - void SetParameters(Matrix m, Matrix v, Matrix k) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(m, v, k)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _m = m; - _v = v; - _k = k; - } - /// /// Gets or sets the mean. (M) /// @@ -164,7 +159,6 @@ namespace MathNet.Numerics.Distributions public Matrix Mean { get { return _m; } - set { SetParameters(value, _v, _k); } } /// @@ -174,7 +168,6 @@ namespace MathNet.Numerics.Distributions public Matrix RowCovariance { get { return _v; } - set { SetParameters(_m, value, _k); } } /// @@ -184,7 +177,6 @@ namespace MathNet.Numerics.Distributions public Matrix ColumnCovariance { get { return _k; } - set { SetParameters(_m, _v, value); } } /// diff --git a/src/Numerics/Distributions/Multinomial.cs b/src/Numerics/Distributions/Multinomial.cs index 2a287278..8f78718c 100644 --- a/src/Numerics/Distributions/Multinomial.cs +++ b/src/Numerics/Distributions/Multinomial.cs @@ -55,12 +55,12 @@ namespace MathNet.Numerics.Distributions /// /// Stores the normalized multinomial probabilities. /// - double[] _p; + readonly double[] _p; /// /// The number of trials. /// - int _trials; + readonly int _trials; /// /// Initializes a new instance of the Multinomial class. @@ -72,8 +72,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Multinomial(double[] p, int n) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(p, n); + _p = (double[]) p.Clone(); + _trials = n; } /// @@ -87,8 +93,14 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Multinomial(double[] p, int n, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(p, n); + _p = (double[]) p.Clone(); + _trials = n; } /// @@ -115,7 +127,13 @@ namespace MathNet.Numerics.Distributions p[i] = h[i].Count; } - SetParameters(p, n); + if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + + _p = (double[]) p.Clone(); + _trials = n; RandomSource = SystemRandomSource.Default; } @@ -157,31 +175,12 @@ namespace MathNet.Numerics.Distributions return n >= 0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// An array of nonnegative ratios: this array does not need to be normalized - /// as this is often impossible using floating point arithmetic. - /// The number of trials. - /// When the parameters are out of range. - void SetParameters(double[] p, int n) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(p, n)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _p = (double[]) p.Clone(); - _trials = n; - } - /// /// Gets or sets the proportion of ratios. /// public double[] P { get { return (double[])_p.Clone(); } - set { SetParameters(value, _trials); } } /// @@ -190,7 +189,6 @@ namespace MathNet.Numerics.Distributions public int N { get { return _trials; } - set { SetParameters(_p, value); } } /// diff --git a/src/Numerics/Distributions/NegativeBinomial.cs b/src/Numerics/Distributions/NegativeBinomial.cs index 3fb83997..dae87356 100644 --- a/src/Numerics/Distributions/NegativeBinomial.cs +++ b/src/Numerics/Distributions/NegativeBinomial.cs @@ -46,8 +46,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _trials; - double _p; + readonly double _trials; + readonly double _p; /// /// Initializes a new instance of the class. @@ -56,8 +56,14 @@ 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)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(r, p); + _p = p; + _trials = r; } /// @@ -68,8 +74,14 @@ 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)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(r, p); + _p = p; + _trials = r; } /// @@ -93,30 +105,12 @@ namespace MathNet.Numerics.Distributions return r >= 0.0 && p >= 0.0 && p <= 1.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The number of failures (r) until the experiment stopped. Range: r ≥ 0. - /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. - /// When the parameters are out of range. - void SetParameters(double r, double p) - { - if (!(r >= 0.0 && p >= 0.0 && p <= 1.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _p = p; - _trials = r; - } - /// /// Gets or sets the number of trials. Range: r ≥ 0. /// public double R { get { return _trials; } - set { SetParameters(value, _p); } } /// @@ -125,7 +119,6 @@ namespace MathNet.Numerics.Distributions public double P { get { return _p; } - set { SetParameters(_trials, value); } } /// diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs index 677dbedb..dd1d9c1c 100644 --- a/src/Numerics/Distributions/Normal.cs +++ b/src/Numerics/Distributions/Normal.cs @@ -45,8 +45,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _mean; - double _stdDev; + readonly double _mean; + readonly double _stdDev; /// /// Initializes a new instance of the Normal class. This is a normal distribution with mean 0.0 @@ -77,8 +77,14 @@ namespace MathNet.Numerics.Distributions /// The standard deviation (σ) of the normal distribution. Range: σ ≥ 0. public Normal(double mean, double stddev) { + if (!IsValidParameterSet(mean, stddev)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(mean, stddev); + _mean = mean; + _stdDev = stddev; } /// @@ -90,8 +96,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Normal(double mean, double stddev, System.Random randomSource) { + if (!IsValidParameterSet(mean, stddev)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(mean, stddev); + _mean = mean; + _stdDev = stddev; } /// @@ -162,30 +174,12 @@ namespace MathNet.Numerics.Distributions return stddev >= 0.0 && !Double.IsNaN(mean); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The mean (μ) of the normal distribution. - /// The standard deviation (σ) of the normal distribution. Range: σ ≥ 0. - /// When the parameters are out of range. - void SetParameters(double mean, double stddev) - { - if (stddev < 0.0 || Double.IsNaN(mean) || Double.IsNaN(stddev)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _mean = mean; - _stdDev = stddev; - } - /// /// Gets or sets the mean (μ) of the normal distribution. /// public double Mean { get { return _mean; } - set { SetParameters(value, _stdDev); } } /// @@ -194,7 +188,6 @@ namespace MathNet.Numerics.Distributions public double StdDev { get { return _stdDev; } - set { SetParameters(_mean, value); } } /// @@ -203,7 +196,6 @@ namespace MathNet.Numerics.Distributions public double Variance { get { return _stdDev*_stdDev; } - set { SetParameters(_mean, Math.Sqrt(value)); } } /// @@ -212,16 +204,6 @@ namespace MathNet.Numerics.Distributions public double Precision { get { return 1.0/(_stdDev*_stdDev); } - set - { - var sdev = 1.0/Math.Sqrt(value); - // Handle the case when the precision is -0. - if (Double.IsInfinity(sdev)) - { - sdev = Double.PositiveInfinity; - } - SetParameters(_mean, sdev); - } } /// diff --git a/src/Numerics/Distributions/NormalGamma.cs b/src/Numerics/Distributions/NormalGamma.cs index 0ccf863b..0f7194ba 100644 --- a/src/Numerics/Distributions/NormalGamma.cs +++ b/src/Numerics/Distributions/NormalGamma.cs @@ -68,7 +68,6 @@ namespace MathNet.Numerics.Distributions public double Mean { get { return _mean; } - set { _mean = value; } } @@ -78,7 +77,6 @@ namespace MathNet.Numerics.Distributions public double Precision { get { return _precision; } - set { _precision = value; } } } @@ -100,10 +98,10 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _meanLocation; - double _meanScale; - double _precisionShape; - double _precisionInvScale; + readonly double _meanLocation; + readonly double _meanScale; + readonly double _precisionShape; + readonly double _precisionInvScale; /// /// Initializes a new instance of the class. @@ -114,8 +112,16 @@ namespace MathNet.Numerics.Distributions /// The inverse scale of the precision. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); + _meanLocation = meanLocation; + _meanScale = meanScale; + _precisionShape = precisionShape; + _precisionInvScale = precisionInverseScale; } /// @@ -128,8 +134,16 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precisionShape, precisionInverseScale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); + _meanLocation = meanLocation; + _meanScale = meanScale; + _precisionShape = precisionShape; + _precisionInvScale = precisionInverseScale; } /// @@ -154,34 +168,12 @@ namespace MathNet.Numerics.Distributions return meanScale > 0.0 && precShape > 0.0 && precInvScale > 0.0 && !Double.IsNaN(meanLocation); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The location of the mean. - /// The scale of the mean. - /// The shape of the precision. - /// The inverse scale of the precision. - /// When the parameters are out of range. - void SetParameters(double meanLocation, double meanScale, double precShape, double precInvScale) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(meanLocation, meanScale, precShape, precInvScale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _meanLocation = meanLocation; - _meanScale = meanScale; - _precisionShape = precShape; - _precisionInvScale = precInvScale; - } - /// /// Gets or sets the location of the mean. /// public double MeanLocation { get { return _meanLocation; } - set { SetParameters(value, _meanScale, _precisionShape, _precisionInvScale); } } /// @@ -190,7 +182,6 @@ namespace MathNet.Numerics.Distributions public double MeanScale { get { return _meanScale; } - set { SetParameters(_meanLocation, value, _precisionShape, _precisionInvScale); } } /// @@ -199,7 +190,6 @@ namespace MathNet.Numerics.Distributions public double PrecisionShape { get { return _precisionShape; } - set { SetParameters(_meanLocation, _meanScale, value, _precisionInvScale); } } /// @@ -208,7 +198,6 @@ namespace MathNet.Numerics.Distributions public double PrecisionInverseScale { get { return _precisionInvScale; } - set { SetParameters(_meanLocation, _meanScale, _precisionShape, value); } } /// diff --git a/src/Numerics/Distributions/Pareto.cs b/src/Numerics/Distributions/Pareto.cs index a4378ffa..c368bdb4 100644 --- a/src/Numerics/Distributions/Pareto.cs +++ b/src/Numerics/Distributions/Pareto.cs @@ -48,8 +48,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _scale; - double _shape; + readonly double _scale; + readonly double _shape; /// /// Initializes a new instance of the class. @@ -59,8 +59,14 @@ namespace MathNet.Numerics.Distributions /// If or are negative. public Pareto(double scale, double shape) { + if (!IsValidParameterSet(scale, shape)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(scale, shape); + _scale = scale; + _shape = shape; } /// @@ -72,8 +78,14 @@ namespace MathNet.Numerics.Distributions /// If or are negative. public Pareto(double scale, double shape, System.Random randomSource) { + if (!IsValidParameterSet(scale, shape)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(scale, shape); + _scale = scale; + _shape = shape; } /// @@ -95,30 +107,12 @@ namespace MathNet.Numerics.Distributions return scale > 0.0 && shape > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The scale (xm) of the distribution. Range: xm > 0. - /// The shape (α) of the distribution. Range: α > 0. - /// When the parameters are out of range. - void SetParameters(double scale, double shape) - { - if (scale <= 0.0 || shape <= 0.0 || Double.IsNaN(scale) || Double.IsNaN(shape)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _scale = scale; - _shape = shape; - } - /// /// Gets or sets the scale (xm) of the distribution. Range: xm > 0. /// public double Scale { get { return _scale; } - set { SetParameters(value, _shape); } } /// @@ -127,7 +121,6 @@ namespace MathNet.Numerics.Distributions public double Shape { get { return _shape; } - set { SetParameters(_scale, value); } } /// diff --git a/src/Numerics/Distributions/Poisson.cs b/src/Numerics/Distributions/Poisson.cs index d9d56eb8..2775a3e6 100644 --- a/src/Numerics/Distributions/Poisson.cs +++ b/src/Numerics/Distributions/Poisson.cs @@ -47,7 +47,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _lambda; + readonly double _lambda; /// /// Initializes a new instance of the class. @@ -56,8 +56,13 @@ namespace MathNet.Numerics.Distributions /// If is equal or less then 0.0. public Poisson(double lambda) { + if (!IsValidParameterSet(lambda)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(lambda); + _lambda = lambda; } /// @@ -68,8 +73,13 @@ namespace MathNet.Numerics.Distributions /// If is equal or less then 0.0. public Poisson(double lambda, System.Random randomSource) { + if (!IsValidParameterSet(lambda)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(lambda); + _lambda = lambda; } /// @@ -92,28 +102,12 @@ namespace MathNet.Numerics.Distributions return lambda > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The lambda (λ) parameter of the Poisson distribution. Range: λ > 0. - /// When the parameters are out of range. - void SetParameters(double lambda) - { - if (!(lambda > 0.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _lambda = lambda; - } - /// /// Gets or sets the Poisson distribution parameter λ. Range: λ > 0. /// public double Lambda { get { return _lambda; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/Rayleigh.cs b/src/Numerics/Distributions/Rayleigh.cs index 773a6edd..374d256d 100644 --- a/src/Numerics/Distributions/Rayleigh.cs +++ b/src/Numerics/Distributions/Rayleigh.cs @@ -49,7 +49,7 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _scale; + readonly double _scale; /// /// Initializes a new instance of the class. @@ -58,8 +58,13 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Rayleigh(double scale) { + if (!IsValidParameterSet(scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(scale); + _scale = scale; } /// @@ -70,8 +75,13 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Rayleigh(double scale, System.Random randomSource) { + if (!IsValidParameterSet(scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(scale); + _scale = scale; } /// @@ -92,28 +102,12 @@ namespace MathNet.Numerics.Distributions return scale > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The scale (σ) of the distribution. Range: σ > 0. - /// When the parameters are out of range. - void SetParameters(double scale) - { - if (scale <= 0.0 || Double.IsNaN(scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _scale = scale; - } - /// /// Gets or sets the scale (σ) of the distribution. Range: σ > 0. /// public double Scale { get { return _scale; } - set { SetParameters(value); } } /// diff --git a/src/Numerics/Distributions/Stable.cs b/src/Numerics/Distributions/Stable.cs index 9417abb2..27b23a89 100644 --- a/src/Numerics/Distributions/Stable.cs +++ b/src/Numerics/Distributions/Stable.cs @@ -47,10 +47,10 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _alpha; - double _beta; - double _scale; - double _location; + readonly double _alpha; + readonly double _beta; + readonly double _scale; + readonly double _location; /// /// Initializes a new instance of the class. @@ -61,8 +61,16 @@ namespace MathNet.Numerics.Distributions /// The location (μ) of the distribution. public Stable(double alpha, double beta, double scale, double location) { + if (!IsValidParameterSet(alpha, beta, scale, location)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(alpha, beta, scale, location); + _alpha = alpha; + _beta = beta; + _scale = scale; + _location = location; } /// @@ -75,8 +83,16 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Stable(double alpha, double beta, double scale, double location, System.Random randomSource) { + if (!IsValidParameterSet(alpha, beta, scale, location)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(alpha, beta, scale, location); + _alpha = alpha; + _beta = beta; + _scale = scale; + _location = location; } /// @@ -100,35 +116,12 @@ namespace MathNet.Numerics.Distributions return alpha > 0.0 && alpha <= 2.0 && beta >= -1.0 && beta <= 1.0 && scale > 0.0 && !Double.IsNaN(location); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The stability (α) of the distribution. Range: 2 ≥ α > 0. - /// The skewness (β) of the distribution. Range: 1 ≥ β ≥ -1. - /// The scale (c) of the distribution. Range: c > 0. - /// The location (μ) of the distribution. - /// When the parameters are out of range. - void SetParameters(double alpha, double beta, double scale, double location) - { - if (alpha <= 0.0 || alpha > 2.0 || beta < -1.0 || beta > 1.0 || scale <= 0.0 - || Double.IsNaN(alpha) || Double.IsNaN(beta) || Double.IsNaN(scale) || Double.IsNaN(location)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _alpha = alpha; - _beta = beta; - _scale = scale; - _location = location; - } - /// /// Gets or sets the stability (α) of the distribution. Range: 2 ≥ α > 0. /// public double Alpha { get { return _alpha; } - set { SetParameters(value, _beta, _scale, _location); } } /// @@ -137,7 +130,6 @@ namespace MathNet.Numerics.Distributions public double Beta { get { return _beta; } - set { SetParameters(_alpha, value, _scale, _location); } } /// @@ -146,7 +138,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_alpha, _beta, value, _location); } } /// @@ -155,7 +146,6 @@ namespace MathNet.Numerics.Distributions public double Location { get { return _location; } - set { SetParameters(_alpha, _beta, _scale, value); } } /// diff --git a/src/Numerics/Distributions/StudentT.cs b/src/Numerics/Distributions/StudentT.cs index ab30c847..5991b08c 100644 --- a/src/Numerics/Distributions/StudentT.cs +++ b/src/Numerics/Distributions/StudentT.cs @@ -61,9 +61,9 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _location; - double _scale; - double _freedom; + readonly double _location; + readonly double _scale; + readonly double _freedom; /// /// Initializes a new instance of the StudentT class. This is a Student t-distribution with location 0.0 @@ -72,7 +72,9 @@ namespace MathNet.Numerics.Distributions public StudentT() { _random = SystemRandomSource.Default; - SetParameters(0.0, 1.0, 1.0); + _location = 0.0; + _scale = 1.0; + _freedom = 1.0; } /// @@ -84,8 +86,15 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (ν) for the distribution. Range: ν > 0. public StudentT(double location, double scale, double freedom) { + if (!IsValidParameterSet(location, scale, freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(location, scale, freedom); + _location = location; + _scale = scale; + _freedom = freedom; } /// @@ -98,8 +107,15 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public StudentT(double location, double scale, double freedom, System.Random randomSource) { + if (!IsValidParameterSet(location, scale, freedom)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(location, scale, freedom); + _location = location; + _scale = scale; + _freedom = freedom; } /// @@ -119,26 +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; - } - - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The location (μ) of the distribution. - /// The scale (σ) of the distribution. Range: σ > 0. - /// The degrees of freedom (ν) for the distribution. Range: ν > 0. - /// When the parameters are out of range. - void SetParameters(double location, double scale, double freedom) - { - if (scale <= 0.0 || freedom <= 0.0 || Double.IsNaN(scale) || Double.IsNaN(location) || Double.IsNaN(freedom)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _location = location; - _scale = scale; - _freedom = freedom; + return scale > 0.0 && freedom > 0.0 && !Double.IsNaN(location); } /// @@ -147,7 +144,6 @@ namespace MathNet.Numerics.Distributions public double Location { get { return _location; } - set { SetParameters(value, _scale, _freedom); } } /// @@ -156,7 +152,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_location, value, _freedom); } } /// @@ -165,7 +160,6 @@ namespace MathNet.Numerics.Distributions public double DegreesOfFreedom { get { return _freedom; } - set { SetParameters(_location, _scale, value); } } /// diff --git a/src/Numerics/Distributions/Triangular.cs b/src/Numerics/Distributions/Triangular.cs index 8b25c5e3..169fb710 100644 --- a/src/Numerics/Distributions/Triangular.cs +++ b/src/Numerics/Distributions/Triangular.cs @@ -49,9 +49,9 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _lower; - double _upper; - double _mode; + readonly double _lower; + readonly double _upper; + readonly double _mode; /// /// Initializes a new instance of the Triangular class with the given lower bound, upper bound and mode. @@ -62,8 +62,15 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the mode or if the mode is smaller than the lower bound. public Triangular(double lower, double upper, double mode) { + if (!IsValidParameterSet(lower, upper, mode)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(lower, upper, mode); + _lower = lower; + _upper = upper; + _mode = mode; } /// @@ -76,8 +83,15 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the mode or if the mode is smaller than the lower bound. public Triangular(double lower, double upper, double mode, System.Random randomSource) { + if (!IsValidParameterSet(lower, upper, mode)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(lower, upper, mode); + _lower = lower; + _upper = upper; + _mode = mode; } /// @@ -100,34 +114,12 @@ namespace MathNet.Numerics.Distributions return upper >= mode && mode >= lower && !Double.IsInfinity(upper) && !Double.IsInfinity(lower) && !Double.IsInfinity(mode); } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// Lower bound. Range: lower ≤ mode ≤ upper - /// Upper bound. Range: lower ≤ mode ≤ upper - /// Mode (most frequent value). Range: lower ≤ mode ≤ upper - /// When the parameters are out of range. - void SetParameters(double lower, double upper, double mode) - { - if (upper < mode || mode < lower || Double.IsNaN(upper) || Double.IsNaN(lower) || Double.IsNaN(mode) - || Double.IsInfinity(upper) || Double.IsInfinity(lower) || Double.IsInfinity(mode) - ) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _lower = lower; - _upper = upper; - _mode = mode; - } - /// /// Gets or sets the lower bound of the distribution. /// public double LowerBound { get { return _lower; } - set { SetParameters(value, _upper, _mode); } } /// @@ -136,7 +128,6 @@ namespace MathNet.Numerics.Distributions public double UpperBound { get { return _upper; } - set { SetParameters(_lower, value, _mode); } } /// @@ -209,7 +200,6 @@ namespace MathNet.Numerics.Distributions public double Mode { get { return _mode; } - set { SetParameters(_lower, _upper, value); } } /// diff --git a/src/Numerics/Distributions/Weibull.cs b/src/Numerics/Distributions/Weibull.cs index c88f87bb..517df6c9 100644 --- a/src/Numerics/Distributions/Weibull.cs +++ b/src/Numerics/Distributions/Weibull.cs @@ -49,8 +49,8 @@ namespace MathNet.Numerics.Distributions { System.Random _random; - double _shape; - double _scale; + readonly double _shape; + readonly double _scale; /// /// Reusable intermediate result 1 / (_scale ^ _shape) @@ -59,7 +59,7 @@ namespace MathNet.Numerics.Distributions /// By caching this parameter we can get slightly better numerics precision /// in certain constellations without any additional computations. /// - double _scalePowShapeInv; + readonly double _scalePowShapeInv; /// /// Initializes a new instance of the Weibull class. @@ -68,8 +68,15 @@ namespace MathNet.Numerics.Distributions /// The scale (λ) of the Weibull distribution. Range: λ > 0. public Weibull(double shape, double scale) { + if (!IsValidParameterSet(shape, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(shape, scale); + _shape = shape; + _scale = scale; + _scalePowShapeInv = Math.Pow(scale, -shape); } /// @@ -80,8 +87,15 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Weibull(double shape, double scale, System.Random randomSource) { + if (!IsValidParameterSet(shape, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(shape, scale); + _shape = shape; + _scale = scale; + _scalePowShapeInv = Math.Pow(scale, -shape); } /// @@ -103,31 +117,12 @@ namespace MathNet.Numerics.Distributions return shape > 0.0 && scale > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The shape (k) of the Weibull distribution. Range: k > 0. - /// The scale (λ) of the Weibull distribution. Range: λ > 0. - /// When the parameters are out of range. - void SetParameters(double shape, double scale) - { - if (shape <= 0.0 || scale <= 0.0 || Double.IsNaN(shape) || Double.IsNaN(scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _shape = shape; - _scale = scale; - _scalePowShapeInv = Math.Pow(scale, -shape); - } - /// /// Gets or sets the shape (k) of the Weibull distribution. Range: k > 0. /// public double Shape { get { return _shape; } - set { SetParameters(value, _scale); } } /// @@ -136,7 +131,6 @@ namespace MathNet.Numerics.Distributions public double Scale { get { return _scale; } - set { SetParameters(_shape, value); } } /// diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs index 997958e3..41d9b60c 100644 --- a/src/Numerics/Distributions/Wishart.cs +++ b/src/Numerics/Distributions/Wishart.cs @@ -51,17 +51,17 @@ namespace MathNet.Numerics.Distributions /// /// The degrees of freedom for the Wishart distribution. /// - double _degreesOfFreedom; + readonly double _degreesOfFreedom; /// /// The scale matrix for the Wishart distribution. /// - Matrix _scale; + readonly Matrix _scale; /// /// Caches the Cholesky factorization of the scale matrix. /// - Cholesky _chol; + readonly Cholesky _chol; /// /// Initializes a new instance of the class. @@ -70,8 +70,15 @@ namespace MathNet.Numerics.Distributions /// The scale matrix (V) for the Wishart distribution. public Wishart(double degreesOfFreedom, Matrix scale) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(degreesOfFreedom, scale); + _degreesOfFreedom = degreesOfFreedom; + _scale = scale; + _chol = _scale.Cholesky(); } /// @@ -82,8 +89,15 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Wishart(double degreesOfFreedom, Matrix scale, System.Random randomSource) { + if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(degreesOfFreedom, scale); + _degreesOfFreedom = degreesOfFreedom; + _scale = scale; + _chol = _scale.Cholesky(); } /// @@ -114,31 +128,12 @@ namespace MathNet.Numerics.Distributions return true; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The degrees of freedom (n) for the Wishart distribution. - /// The scale matrix (V) for the Wishart distribution. - /// When the parameters are out of range. - void SetParameters(double degreesOfFreedom, Matrix scale) - { - if (Control.CheckDistributionParameters && !IsValidParameterSet(degreesOfFreedom, scale)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _degreesOfFreedom = degreesOfFreedom; - _scale = scale; - _chol = _scale.Cholesky(); - } - /// /// Gets or sets the degrees of freedom (n) for the Wishart distribution. /// public double DegreesOfFreedom { get { return _degreesOfFreedom; } - set { SetParameters(value, _scale); } } /// @@ -147,7 +142,6 @@ namespace MathNet.Numerics.Distributions public Matrix Scale { get { return _scale; } - set { SetParameters(_degreesOfFreedom, value); } } /// diff --git a/src/Numerics/Distributions/Zipf.cs b/src/Numerics/Distributions/Zipf.cs index a2679ea7..5a998d46 100644 --- a/src/Numerics/Distributions/Zipf.cs +++ b/src/Numerics/Distributions/Zipf.cs @@ -50,12 +50,12 @@ namespace MathNet.Numerics.Distributions /// /// The s parameter of the distribution. /// - double _s; + readonly double _s; /// /// The n parameter of the distribution. /// - int _n; + readonly int _n; /// /// Initializes a new instance of the class. @@ -64,8 +64,14 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public Zipf(double s, int n) { + if (!IsValidParameterSet(s, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = SystemRandomSource.Default; - SetParameters(s, n); + _s = s; + _n = n; } /// @@ -76,8 +82,14 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Zipf(double s, int n, System.Random randomSource) { + if (!IsValidParameterSet(s, n)) + { + throw new ArgumentException(Resources.InvalidDistributionParameters); + } + _random = randomSource ?? SystemRandomSource.Default; - SetParameters(s, n); + _s = s; + _n = n; } /// @@ -99,30 +111,12 @@ namespace MathNet.Numerics.Distributions return n > 0 && s > 0.0; } - /// - /// Sets the parameters of the distribution after checking their validity. - /// - /// The s parameter of the distribution. - /// The n parameter of the distribution. - /// When the parameters are out of range. - void SetParameters(double s, int n) - { - if (!(n > 0 && s > 0.0)) - { - throw new ArgumentException(Resources.InvalidDistributionParameters); - } - - _s = s; - _n = n; - } - /// /// Gets or sets the s parameter of the distribution. /// public double S { get { return _s; } - set { SetParameters(value, _n); } } /// @@ -131,7 +125,6 @@ namespace MathNet.Numerics.Distributions public int N { get { return _n; } - set { SetParameters(_s, value); } } /// diff --git a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs index 78563865..7c0efb51 100644 --- a/src/UnitTests/DistributionTests/Continuous/BetaTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/BetaTests.cs @@ -89,62 +89,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Beta(α = 1, β = 2)", n.ToString()); } - /// - /// Can Set Shape A - /// - /// New A value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetShapeA(double a) - { - GC.KeepAlive(new Beta(1.0, 1.0) - { - A = a - }); - } - - /// - /// Set A fails with negative A. - /// - [Test] - public void SetShapeAFailsWithNegativeA() - { - var n = new Beta(1.0, 1.0); - Assert.That(() => n.A = -1.0, Throws.ArgumentException); - } - - /// - /// Can set shape B. - /// - /// New B value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetShapeB(double b) - { - GC.KeepAlive(new Beta(1.0, 1.0) - { - B = b - }); - } - - /// - /// Set shape B fails with negative B. - /// - [Test] - public void SetShapeBFailsWithNegativeB() - { - var n = new Beta(1.0, 1.0); - Assert.That(() => n.B = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/CauchyTests.cs b/src/UnitTests/DistributionTests/Continuous/CauchyTests.cs index 8cf44c75..49216106 100644 --- a/src/UnitTests/DistributionTests/Continuous/CauchyTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/CauchyTests.cs @@ -94,58 +94,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Cauchy(x0 = 1, γ = 2)", n.ToString()); } - /// - /// Can set location. - /// - /// Location value. - [TestCase(-10.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - public void CanSetLocation(double location) - { - GC.KeepAlive(new Cauchy - { - Location = location - }); - } - - /// - /// Set bad location fails. - /// - [Test] - public void SetBadLocationFail() - { - var n = new Cauchy(); - Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(1.0)] - [TestCase(2.0)] - [TestCase(12.0)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Cauchy - { - Scale = scale - }); - } - - /// - /// Set bad scale fails. - /// - [Test] - public void SetBadScaleFail() - { - var n = new Cauchy(); - Assert.That(() => n.Scale = -1.0, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs b/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs index f976c538..1047c718 100644 --- a/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ChiSquareTests.cs @@ -80,35 +80,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("ChiSquared(k = 1)", n.ToString()); } - /// - /// Can set degrees of freedom. - /// - /// Degrees of freedom. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetDoF(double dof) - { - GC.KeepAlive(new ChiSquared(1.0) - { - DegreesOfFreedom = dof - }); - } - - /// - /// Set Degrees of freedom fails with non-positive value. - /// - /// Degrees of freedom. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetDofFailsWithNonPositiveDoF(double dof) - { - var n = new ChiSquared(1.0); - Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ChiTests.cs b/src/UnitTests/DistributionTests/Continuous/ChiTests.cs index 99c73f56..bcee6d10 100644 --- a/src/UnitTests/DistributionTests/Continuous/ChiTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ChiTests.cs @@ -78,35 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Chi(k = 1)", n.ToString()); } - /// - /// Can set degrees of freedom. - /// - /// Degrees of freedom. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetDoF(double dof) - { - GC.KeepAlive(new Chi(1.0) - { - DegreesOfFreedom = dof - }); - } - - /// - /// Set Degrees of freedom fails with non-positive value. - /// - /// Degrees of freedom. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetDofFailsWithNonPositiveDoF(double dof) - { - var n = new Chi(1.0); - Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs b/src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs index 18db10c4..6aafa4e1 100644 --- a/src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ContinuousUniformTests.cs @@ -97,58 +97,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("ContinuousUniform(Lower = 1, Upper = 2)", n.ToString()); } - /// - /// Can set lower bound. - /// - /// Lower bound. - [TestCase(-10.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - public void CanSetLower(double lower) - { - GC.KeepAlive(new ContinuousUniform - { - LowerBound = lower - }); - } - - /// - /// Set bad lower bound fails. - /// - [Test] - public void SetBadLowerFails() - { - var n = new ContinuousUniform(); - Assert.That(() => n.LowerBound = 3.0, Throws.ArgumentException); - } - - /// - /// Can set upper bound. - /// - /// Upper bound. - [TestCase(1.0)] - [TestCase(2.0)] - [TestCase(12.0)] - public void CanSetUpper(double upper) - { - GC.KeepAlive(new ContinuousUniform - { - UpperBound = upper - }); - } - - /// - /// Set bad upper fails. - /// - [Test] - public void SetBadUpperFails() - { - var n = new ContinuousUniform(); - Assert.That(() => n.UpperBound = -1.0, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ErlangTests.cs b/src/UnitTests/DistributionTests/Continuous/ErlangTests.cs index 47c8e76e..824bf243 100644 --- a/src/UnitTests/DistributionTests/Continuous/ErlangTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ErlangTests.cs @@ -120,88 +120,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Erlang(k = 1, λ = 2)", n.ToString()); } - /// - /// Can set shape. - /// - /// New shape value. - [TestCase(-0)] - [TestCase(0)] - [TestCase(1)] - [TestCase(10)] - public void CanSetShape(int shape) - { - GC.KeepAlive(new Erlang(1, 1.0) - { - Shape = shape - }); - } - - /// - /// Set shape fails with negative shape. - /// - [Test] - public void SetShapeFailsWithNegativeShape() - { - var n = new Erlang(1, 1.0); - Assert.That(() => n.Shape = -1, Throws.ArgumentException); - } - - /// - /// Can set scale - /// - /// New scale value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Erlang(1, 1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative scale. - /// - [Test] - public void SetScaleFailsWithNegativeScale() - { - var n = new Erlang(1, 1.0); - Assert.That(() => n.Scale = -1.0, Throws.ArgumentException); - } - - /// - /// Can set inverse scale. - /// - /// Inverse scale value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetInvScale(double invScale) - { - GC.KeepAlive(new Erlang(1, 1.0) - { - Rate = invScale - }); - } - - /// - /// Set inverse scale fails with negative inverse scale. - /// - [Test] - public void SetInvScaleFailsWithNegativeInvScale() - { - var n = new Erlang(1, 1.0); - Assert.That(() => n.Rate = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs b/src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs index 012af382..20dc560b 100644 --- a/src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ExponentialTests.cs @@ -78,34 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Exponential(λ = 2)", n.ToString()); } - /// - /// Can set lambda. - /// - /// Lambda value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetLambda(double lambda) - { - GC.KeepAlive(new Exponential(1.0) - { - Rate = lambda - }); - } - - /// - /// Set lambda fails with negative lambda. - /// - [Test] - public void SetLambdaFailsWithNegativeLambda() - { - var n = new Exponential(1.0); - Assert.That(() => n.Rate = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs b/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs index 82c3fc4e..935db6d6 100644 --- a/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/FisherSnedecorTests.cs @@ -101,58 +101,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("FisherSnedecor(d1 = 2, d2 = 1)", n.ToString()); } - /// - /// Can set degree of freedom 1. - /// - /// Degrees of freedom 1 - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetDegreesOfFreedom1(double d1) - { - GC.KeepAlive(new FisherSnedecor(1.0, 2.0) - { - DegreesOfFreedom1 = d1 - }); - } - - /// - /// Set degree of freedom 1 fails with negative value. - /// - [Test] - public void SetDegreesOfFreedom1FailsWithNegativeDegreeOfFreedom() - { - var n = new FisherSnedecor(1.0, 2.0); - Assert.That(() => n.DegreesOfFreedom1 = -1.0, Throws.ArgumentException); - } - - /// - /// Can set degree of freedom 2. - /// - /// Degrees of freedom 2 - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetDegreesOfFreedom2(double d2) - { - GC.KeepAlive(new FisherSnedecor(1.0, 2.0) - { - DegreesOfFreedom2 = d2 - }); - } - - /// - /// Set degree of freedom 2 fails with negative value. - /// - [Test] - public void SetDegreesOfFreedom2FailsWithNegativeDegreeOfFreedom() - { - var n = new FisherSnedecor(1.0, 2.0); - Assert.That(() => n.DegreesOfFreedom2 = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/GammaTests.cs b/src/UnitTests/DistributionTests/Continuous/GammaTests.cs index f93d7b1d..71b866b2 100644 --- a/src/UnitTests/DistributionTests/Continuous/GammaTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/GammaTests.cs @@ -122,90 +122,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Gamma(α = 1, β = 2)", n.ToString()); } - /// - /// Can set shape. - /// - /// Shape value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetShape(double shape) - { - GC.KeepAlive(new Gamma(1.0, 1.0) - { - Shape = shape - }); - } - - /// - /// Set shape fails with negative shape. - /// - [Test] - public void SetShapeFailsWithNegativeShape() - { - var n = new Gamma(1.0, 1.0); - Assert.That(() => n.Shape = -1.0, Throws.ArgumentException); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Gamma(1.0, 1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative scale. - /// - [Test] - public void SetScaleFailsWithNegativeScale() - { - var n = new Gamma(1.0, 1.0); - Assert.That(() => n.Scale = -1.0, Throws.ArgumentException); - } - - /// - /// Can set inverse scale. - /// - /// Inverse scale value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetInvScale(double invScale) - { - GC.KeepAlive(new Gamma(1.0, 1.0) - { - Rate = invScale - }); - } - - /// - /// Set inverse scale fails with negative value. - /// - [Test] - public void SetInvScaleFailsWithNegativeInvScale() - { - var n = new Gamma(1.0, 1.0); - Assert.That(() => n.Rate = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs b/src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs index ee5be676..a0a29cdb 100644 --- a/src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/InverseGammaTests.cs @@ -86,64 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("InverseGamma(α = 1.1, β = 2.1)", n.ToString()); } - /// - /// Can set A. - /// - /// A parameter. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetA(double a) - { - GC.KeepAlive(new InverseGamma(1.0, 1.0) - { - Shape = a - }); - } - - /// - /// Set A fails with non-positive value. - /// - /// A parameter. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetAFailsWithNonPositiveA(double a) - { - var n = new InverseGamma(1.0, 1.0); - Assert.That(() => n.Shape = a, Throws.ArgumentException); - } - - /// - /// Can set B. - /// - /// B parameter. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetB(double b) - { - GC.KeepAlive(new InverseGamma(1.0, 1.0) - { - Scale = b - }); - } - - /// - /// Set B fails with non-positive value. - /// - /// B parameter. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetBFailsWithNonPositiveB(double b) - { - var n = new InverseGamma(1.0, 1.0); - Assert.That(() => n.Scale = b, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs b/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs index 0a144e68..ac440292 100644 --- a/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/LaplaceTests.cs @@ -80,65 +80,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Laplace(μ = -1, b = 2)", n.ToString()); } - /// - /// Can set location. - /// - /// Location value. - [TestCase(Double.NegativeInfinity)] - [TestCase(-5.0 - 1.0)] - [TestCase(0.0)] - [TestCase(1.0)] - [TestCase(5.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetLocation(double location) - { - GC.KeepAlive(new Laplace - { - Location = location - }); - } - - /// - /// Set location fails with negative value. - /// - [Test] - public void SetLocationFailsWithNegativeLocation() - { - var n = new Laplace(); - Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Laplace - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative value. - /// - /// Scale value. - [TestCase(0.0)] - [TestCase(-1.0)] - [TestCase(-5.0)] - [TestCase(Double.NegativeInfinity)] - [TestCase(Double.NaN)] - public void SetScaleFailsWithNegativeScale(double scale) - { - var n = new Laplace(); - Assert.That(() => n.Scale = scale, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs b/src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs index 014058c0..43dffb18 100644 --- a/src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/LogNormalTests.cs @@ -85,54 +85,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("LogNormal(μ = 1, σ = 2)", n.ToString()); } - /// - /// Can set sigma. - /// - /// Sigma value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetSigma(double sigma) - { - GC.KeepAlive(new LogNormal(1.0, 2.0) - { - Sigma = sigma - }); - } - - /// - /// Set sigma fails with negative value. - /// - [Test] - public void SetSigmaFailsWithNegativeSigma() - { - var n = new LogNormal(1.0, 2.0); - Assert.That(() => n.Sigma = -1.0, Throws.ArgumentException); - } - - /// - /// Can set mu. - /// - /// Mu parameter. - [TestCase(Double.NegativeInfinity)] - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetMu(double mu) - { - GC.KeepAlive(new LogNormal(1.0, 2.0) - { - Mu = mu - }); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Continuous/NormalTests.cs b/src/UnitTests/DistributionTests/Continuous/NormalTests.cs index dfa8c0c5..3f0ad4ae 100644 --- a/src/UnitTests/DistributionTests/Continuous/NormalTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/NormalTests.cs @@ -150,113 +150,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Normal(μ = 1, σ = 2)", n.ToString()); } - /// - /// Can set precision. - /// - /// Precision value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetPrecision(double prec) - { - GC.KeepAlive(new Normal - { - Precision = prec - }); - } - - /// - /// Set precision fails with negative value. - /// - [Test] - public void SetPrecisionFailsWithNegativePrecision() - { - var n = new Normal(); - Assert.That(() => n.Precision = -1.0, Throws.ArgumentException); - } - - /// - /// Can set variance. - /// - /// Variance value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetVariance(double var) - { - var dist = new Normal - { - Variance = var - }; - - Assert.AreEqual(var, dist.Variance, 1e-14); - } - - /// - /// Set variance fails with negative value. - /// - [Test] - public void SetVarianceFailsWithNegativeVariance() - { - var n = new Normal(); - Assert.That(() => n.Variance = -1.0, Throws.ArgumentException); - } - - /// - /// Can set standard deviation. - /// - /// Standard deviation value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetStdDev(double sdev) - { - var dist = new Normal - { - StdDev = sdev - }; - - Assert.AreEqual(sdev, dist.StdDev, 1e-14); - } - - /// - /// Set standard deviation fails with negative value. - /// - [Test] - public void SetStdDevFailsWithNegativeStdDev() - { - var n = new Normal(); - Assert.That(() => n.StdDev = -1.0, Throws.ArgumentException); - } - - /// - /// Can set mean. - /// - /// Mean value. - [TestCase(Double.NegativeInfinity)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetMean(double mean) - { - GC.KeepAlive(new Normal - { - Mean = mean - }); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Continuous/ParetoTests.cs b/src/UnitTests/DistributionTests/Continuous/ParetoTests.cs index 39262537..bcbaf18a 100644 --- a/src/UnitTests/DistributionTests/Continuous/ParetoTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/ParetoTests.cs @@ -86,58 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Pareto(xm = 1, α = 2)", n.ToString()); } - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Pareto(1.0, 1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative scale. - /// - [Test] - public void SetScaleFailsWithNegativeScale() - { - var n = new Pareto(1.0, 1.0); - Assert.That(() => n.Scale = -1.0, Throws.ArgumentException); - } - - /// - /// Can set shape. - /// - /// Shape value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetShape(double shape) - { - GC.KeepAlive(new Pareto(1.0, 1.0) - { - Shape = shape - }); - } - - /// - /// Set shape fails with negative shape. - /// - [Test] - public void SetShapeFailsWithNegativeShape() - { - var n = new Pareto(1.0, 1.0); - Assert.That(() => n.Shape = -1.0, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/RayleighTests.cs b/src/UnitTests/DistributionTests/Continuous/RayleighTests.cs index ea131915..04e7ed2e 100644 --- a/src/UnitTests/DistributionTests/Continuous/RayleighTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/RayleighTests.cs @@ -78,37 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Rayleigh(σ = 2)", n.ToString()); } - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Rayleigh(1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative scale. - /// - /// Scale value. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(-1.0)] - [TestCase(Double.NegativeInfinity)] - [TestCase(Double.NaN)] - public void SetScaleFailsWithNegativeScale(double scale) - { - var n = new Rayleigh(1.0); - Assert.That(() => n.Scale = scale, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/StableTests.cs b/src/UnitTests/DistributionTests/Continuous/StableTests.cs index 9bd9fe6e..c884b31a 100644 --- a/src/UnitTests/DistributionTests/Continuous/StableTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/StableTests.cs @@ -102,128 +102,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Stable(α = 1.2, β = 0.3, c = 1, μ = 2)", n.ToString()); } - /// - /// Can set alpha. - /// - /// Alpha value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(2.0)] - public void CanSetAlpha(double alpha) - { - GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0) - { - Alpha = alpha - }); - } - - /// - /// Set alpha fails with bad values. - /// - /// Alpha value. - [TestCase(Double.NaN)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(2.1)] - [TestCase(Double.NegativeInfinity)] - [TestCase(Double.PositiveInfinity)] - public void SetAlphaFail(double alpha) - { - var n = new Stable(1.0, 1.0, 1.0, 1.0); - Assert.That(() => n.Alpha = alpha, Throws.ArgumentException); - } - - /// - /// Can set beta. - /// - /// Beta value. - [TestCase(-1.0)] - [TestCase(-0.1)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - public void CanSetBeta(double beta) - { - GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0) - { - Beta = beta - }); - } - - /// - /// Set beta fails with bad values. - /// - /// Beta value. - [TestCase(Double.NaN)] - [TestCase(-1.1)] - [TestCase(1.1)] - [TestCase(Double.NegativeInfinity)] - [TestCase(Double.PositiveInfinity)] - public void SetBetaFail(double beta) - { - var n = new Stable(1.0, 1.0, 1.0, 1.0); - Assert.That(() => n.Beta = beta, Throws.ArgumentException); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with bad values. - /// - /// Scale value. - [TestCase(Double.NaN)] - [TestCase(0.0)] - public void SetScaleFail(double scale) - { - var n = new Stable(1.0, 1.0, 1.0, 1.0); - Assert.That(() => n.Scale = scale, Throws.ArgumentException); - } - - /// - /// Can set location. - /// - /// Location value. - [TestCase(Double.NegativeInfinity)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.1)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetLocation(double location) - { - GC.KeepAlive(new Stable(1.0, 1.0, 1.0, 1.0) - { - Location = location - }); - } - - /// - /// Set location fails with bad values. - /// - [Test] - public void SetLocationFail() - { - var n = new Stable(1.0, 1.0, 1.0, 1.0); - Assert.That(() => n.Location = Double.NaN, Throws.ArgumentException); - } - /// /// Validate entropy throws NotSupportedException. /// diff --git a/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs b/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs index 64a0e933..04ff8b78 100644 --- a/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/StudentTTests.cs @@ -98,84 +98,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("StudentT(μ = 1, σ = 2, ν = 1)", n.ToString()); } - /// - /// Can set location. - /// - /// Location value. - [TestCase(Double.NegativeInfinity)] - [TestCase(-5.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetLocation(double loc) - { - GC.KeepAlive(new StudentT - { - Location = loc - }); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new StudentT - { - Scale = scale - }); - } - - /// - /// Set scale fails with non-positive scale. - /// - /// Scale value. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetScaleFailsWithNonPositiveScale(double scale) - { - var n = new StudentT(); - Assert.That(() => n.Scale = scale, Throws.ArgumentException); - } - - /// - /// Can set degrees of freedom. - /// - /// Degrees of freedom. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetDoF(double dof) - { - GC.KeepAlive(new StudentT - { - DegreesOfFreedom = dof - }); - } - - /// - /// Set degrees of freedom fails with non-positive value. - /// - /// Degrees of freedom. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetDofFailsWithNonPositiveDoF(double dof) - { - var n = new StudentT(); - Assert.That(() => n.DegreesOfFreedom = dof, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Continuous/TriangularTests.cs b/src/UnitTests/DistributionTests/Continuous/TriangularTests.cs index aa3da64f..e6e1fd93 100644 --- a/src/UnitTests/DistributionTests/Continuous/TriangularTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/TriangularTests.cs @@ -92,36 +92,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous var n = new Triangular(0d, 2d, 1d); Assert.AreEqual("Triangular(Lower = 0, Upper = 2, Mode = 1)", n.ToString()); } - - /// - /// Can Set Lower Bound - /// - /// New Lower Bound. - [TestCase(-0.0)] - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - public void CanSetLowerBound(double lower) - { - GC.KeepAlive(new Triangular(0.0, 20.0, 10.0) - { - LowerBound = lower - }); - } - - /// - /// Set LowerBound fails if greater than Mode. - /// - [TestCase(1.0, 0.5)] - [TestCase(Double.PositiveInfinity, 0.5)] - public void SetLowerBoundFailsIfGreaterThanMode(double lower, double mode) - { - var n = new Triangular(0.0, 100.0, mode); - Assert.That(() => n.LowerBound = lower, Throws.ArgumentException); - } - - // Todo: Add tests for setting upper bound and mode values. // Todo: Add tests for: // - Mean, diff --git a/src/UnitTests/DistributionTests/Continuous/WeibullTests.cs b/src/UnitTests/DistributionTests/Continuous/WeibullTests.cs index b203e4f3..4b659725 100644 --- a/src/UnitTests/DistributionTests/Continuous/WeibullTests.cs +++ b/src/UnitTests/DistributionTests/Continuous/WeibullTests.cs @@ -88,64 +88,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Continuous Assert.AreEqual("Weibull(k = 1, λ = 2)", n.ToString()); } - /// - /// Can set shape. - /// - /// Shape value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetShape(double shape) - { - GC.KeepAlive(new Weibull(1.0, 1.0) - { - Shape = shape - }); - } - - /// - /// Set shape fails with negative shape. - /// - /// Shape value. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetShapeFailsWithNegativeShape(double shape) - { - var n = new Weibull(1.0, 1.0); - Assert.That(() => n.Shape = shape, Throws.ArgumentException); - } - - /// - /// Can set scale. - /// - /// Scale value. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetScale(double scale) - { - GC.KeepAlive(new Weibull(1.0, 1.0) - { - Scale = scale - }); - } - - /// - /// Set scale fails with negative scale. - /// - /// Scale value. - [TestCase(-1.0)] - [TestCase(-0.0)] - [TestCase(0.0)] - public void SetScaleFailsWithNegativeScale(double scale) - { - var n = new Weibull(1.0, 1.0); - Assert.That(() => n.Scale = scale, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs b/src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs index 9b2c073c..4578983b 100644 --- a/src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/BernoulliTests.cs @@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Bernoulli(p = 0.3)", b.ToString()); } - /// - /// Can set probability of one. - /// - /// Probability of one. - [TestCase(0.0)] - [TestCase(0.3)] - [TestCase(1.0)] - public void CanSetProbabilityOfOne(double p) - { - GC.KeepAlive(new Bernoulli(0.3) - { - P = p - }); - } - - /// - /// Set probability of one fails with bad values. - /// - /// Probability of one. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(2.0)] - public void SetProbabilityOfOneFails(double p) - { - var b = new Bernoulli(0.3); - Assert.That(() => b.P = p, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Discrete/BinomialTests.cs b/src/UnitTests/DistributionTests/Discrete/BinomialTests.cs index d2fb6a4e..0e127425 100644 --- a/src/UnitTests/DistributionTests/Discrete/BinomialTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/BinomialTests.cs @@ -79,35 +79,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Binomial(p = 0.3, n = 2)", b.ToString()); } - /// - /// Can set success probability. - /// - /// Success probability. - /// Number of trials. - [TestCase(0.0, 4)] - [TestCase(0.3, 3)] - [TestCase(1.0, 2)] - public void CanSetSuccessProbability(double p, int n) - { - GC.KeepAlive(new Binomial(0.3, n) - { - P = p - }); - } - - /// - /// Set success probability fails with bad values. - /// - /// Success probability. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(2.0)] - public void SetProbabilityOfOneFails(double p) - { - var b = new Binomial(0.3, 1); - Assert.That(() => b.P = p, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs b/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs index dea380fa..2368a601 100644 --- a/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/CategoricalTests.cs @@ -139,28 +139,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Categorical(Dimension = 3)", b.ToString()); } - /// - /// Can set probability. - /// - [Test] - public void CanSetProbability() - { - GC.KeepAlive(new Categorical(_largeP) - { - P = _smallP - }); - } - - /// - /// Set probability with a bad array fails. - /// - [Test] - public void SetProbabilityFails() - { - var b = new Categorical(_largeP); - Assert.That(() => b.P = _badP, Throws.ArgumentException); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs b/src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs index 1aebd006..65515d9a 100644 --- a/src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/ConwayMaxwellPoissonTests.cs @@ -77,66 +77,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("ConwayMaxwellPoisson(λ = 1, ν = 2)", d.ToString()); } - /// - /// Can set lambda. - /// - /// Lambda value. - [TestCase(0.1)] - [TestCase(3.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetLambda(double lambda) - { - GC.KeepAlive(new ConwayMaxwellPoisson(1.0, 2.0) - { - Lambda = lambda - }); - } - - /// - /// Can set DegreesOfFreedom. - /// - /// DegreesOfFreedom parameter. - [TestCase(0.0)] - [TestCase(3.0)] - [TestCase(10.0)] - [TestCase(Double.PositiveInfinity)] - public void CanSetNu(double nu) - { - GC.KeepAlive(new ConwayMaxwellPoisson(1.0, 2.0) - { - Nu = nu - }); - } - - /// - /// Set lambda with bad values fails. - /// - /// Lambda value. - [TestCase(0.0)] - [TestCase(-0.0)] - [TestCase(-1.0)] - [TestCase(Double.NegativeInfinity)] - public void SetLambdaFails(double lambda) - { - var d = new ConwayMaxwellPoisson(1.0, 2.0); - Assert.That(() => d.Lambda = lambda, Throws.ArgumentException); - } - - /// - /// Set DegreesOfFreedom with bad values fails. - /// - /// DegreesOfFreedom parameter. - [TestCase(-0.1)] - [TestCase(-1.0)] - [TestCase(-10.0)] - [TestCase(Double.NegativeInfinity)] - public void SetNuFails(double nu) - { - var d = new ConwayMaxwellPoisson(1.0, 2.0); - Assert.That(() => d.Nu = nu, Throws.ArgumentException); - } - /// /// Validate entropy throws NotSupportedException. /// diff --git a/src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs b/src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs index 313bf0b5..0781bfdb 100644 --- a/src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/DiscreteUniformTests.cs @@ -79,66 +79,12 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("DiscreteUniform(Lower = 0, Upper = 10)", b.ToString()); } - /// - /// Can set lower bound. - /// - /// Lower bound. - [TestCase(0)] - [TestCase(3)] - [TestCase(10)] - public void CanSetLowerBound(int p) - { - GC.KeepAlive(new DiscreteUniform(0, 10) - { - LowerBound = p - }); - } - - /// - /// Can set upper bound. - /// - /// Upper bound. - [TestCase(0)] - [TestCase(3)] - [TestCase(10)] - public void CanSetUpperBound(int p) - { - GC.KeepAlive(new DiscreteUniform(0, 10) - { - UpperBound = p - }); - } - - /// - /// Set lower bound with bad values fails. - /// - /// Lower bound. - [TestCase(11)] - [TestCase(20)] - public void SetLowerBoundFails(int p) - { - var b = new DiscreteUniform(0, 10); - Assert.That(() => b.LowerBound = p, Throws.ArgumentException); - } - - /// - /// Set upper bound with bad values fails - /// - /// Upper bound. - [TestCase(-11)] - [TestCase(-20)] - public void SetUpperBoundFails(int p) - { - var b = new DiscreteUniform(0, 10); - Assert.That(() => b.UpperBound = p, Throws.ArgumentException); - } - /// /// Validate entropy. /// /// Lower bound. /// Upper bound. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, 3.0445224377234229965005979803657054342845752874046093)] [TestCase(0, 4, 1.6094379124341003746007593332261876395256013542685181)] [TestCase(10, 20, 2.3978952727983705440619435779651292998217068539374197)] @@ -169,7 +115,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// /// Lower bound. /// Upper bound. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, 0)] [TestCase(0, 4, 2)] [TestCase(10, 20, 15)] @@ -185,7 +131,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// /// Lower bound. /// Upper bound. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, 0)] [TestCase(0, 4, 2)] [TestCase(10, 20, 15)] @@ -201,7 +147,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// /// Lower bound. /// Upper bound. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, 0)] [TestCase(0, 4, 2)] [TestCase(10, 20, 15)] @@ -238,7 +184,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// Lower bound. /// Upper bound. /// Input X value. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, -5, 1 / 21.0)] [TestCase(-10, 10, 1, 1 / 21.0)] [TestCase(-10, 10, 10, 1 / 21.0)] @@ -256,7 +202,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// Lower bound. /// Upper bound. /// Input X value. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, -5, -3.0445224377234229965005979803657054342845752874046093)] [TestCase(-10, 10, 1, -3.0445224377234229965005979803657054342845752874046093)] [TestCase(-10, 10, 10, -3.0445224377234229965005979803657054342845752874046093)] @@ -332,7 +278,7 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete /// Lower bound. /// Upper bound. /// Input X value. - /// Expceted value. + /// Expected value. [TestCase(-10, 10, -5, 6.0 / 21.0)] [TestCase(-10, 10, 1, 12.0 / 21.0)] [TestCase(-10, 10, 10, 1.0)] diff --git a/src/UnitTests/DistributionTests/Discrete/GeometricTests.cs b/src/UnitTests/DistributionTests/Discrete/GeometricTests.cs index d2e8a4ce..8726889d 100644 --- a/src/UnitTests/DistributionTests/Discrete/GeometricTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/GeometricTests.cs @@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Geometric(p = 0.3)", d.ToString()); } - /// - /// Can set probability of one. - /// - /// Probability of generating a one. - [TestCase(0.0)] - [TestCase(0.3)] - [TestCase(1.0)] - public void CanSetProbabilityOfOne(double p) - { - GC.KeepAlive(new Geometric(0.3) - { - P = p - }); - } - - /// - /// Set probability of one with a bad value fails. - /// - /// Probability of generating a one. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(2.0)] - public void SetProbabilityOfOneFails(double p) - { - var d = new Geometric(0.3); - Assert.That(() => d.P = p, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs b/src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs index eda4e36f..6e0f8f54 100644 --- a/src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/HypergeometricTests.cs @@ -86,89 +86,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Hypergeometric(N = 10, M = 1, n = 1)", d.ToString()); } - /// - /// Can set size. - /// - /// Population size. - [TestCase(5)] - [TestCase(10)] - [TestCase(20)] - public void CanSetPopulation(int population) - { - GC.KeepAlive(new Hypergeometric(10, 1, 1) - { - Population = population - }); - } - - /// - /// Set size fails with bad values. - /// - /// Population size. - [TestCase(-1)] - [TestCase(0)] - public void SetPopulationFails(int population) - { - var d = new Hypergeometric(10, 1, 1); - Assert.That(() => d.Population = population, Throws.ArgumentException); - } - - /// - /// Can set M. - /// - /// M parameter. - [TestCase(0)] - [TestCase(1)] - [TestCase(2)] - [TestCase(5)] - public void CanSetSuccess(int success) - { - GC.KeepAlive(new Hypergeometric(10, 1, 1) - { - Success = success - }); - } - - /// - /// Set M fails with bad values. - /// - /// M parameter. - [TestCase(11)] - [TestCase(-1)] - public void SetSuccessFails(int success) - { - var d = new Hypergeometric(10, 1, 1); - Assert.That(() => d.Success = success, Throws.ArgumentException); - } - - /// - /// Can set N. - /// - /// N parameter. - [TestCase(0)] - [TestCase(1)] - [TestCase(2)] - [TestCase(5)] - public void CanSetDraws(int draws) - { - GC.KeepAlive(new Hypergeometric(10, 1, 1) - { - Draws = draws - }); - } - - /// - /// Set N fails with bad values. - /// - /// N parameter. - [TestCase(11)] - [TestCase(-1)] - public void SetDrawsFails(int draws) - { - var d = new Hypergeometric(10, 1, 1); - Assert.That(() => d.Draws = draws, Throws.ArgumentException); - } - /// /// Validate entropy throws NotSupportedException. /// diff --git a/src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs b/src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs index 9e0172df..8a62ddde 100644 --- a/src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/NegativeBinomialTests.cs @@ -84,62 +84,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual(String.Format("NegativeBinomial(R = {0}, P = {1})", d.R, d.P), d.ToString()); } - /// - /// Can set R. - /// - /// Number of trials. - [TestCase(0.0)] - [TestCase(0.1)] - [TestCase(1.0)] - public void CanSetR(double r) - { - GC.KeepAlive(new NegativeBinomial(1.0, 0.5) - { - R = r - }); - } - - /// - /// Set R fails with bad values. - /// - /// Number of trials. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(Double.NegativeInfinity)] - public void SetRFails(double r) - { - var d = new NegativeBinomial(1.0, 0.5); - Assert.That(() => d.R = r, Throws.ArgumentException); - } - - /// - /// Can set probability of one. - /// - /// Probability of success. - [TestCase(0.0)] - [TestCase(0.3)] - [TestCase(1.0)] - public void CanSetProbabilityOfOne(double p) - { - GC.KeepAlive(new NegativeBinomial(1.0, 0.5) - { - P = p - }); - } - - /// - /// Set probability of one fails with bad values. - /// - /// Probability of success. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(2.0)] - public void SetProbabilityOfOneFails(double p) - { - var d = new NegativeBinomial(1.0, 0.5); - Assert.That(() => d.P = p, Throws.ArgumentException); - } - /// /// Validate entropy throws NotSupportedException. /// diff --git a/src/UnitTests/DistributionTests/Discrete/PoissonTests.cs b/src/UnitTests/DistributionTests/Discrete/PoissonTests.cs index 959aa604..36fecaf9 100644 --- a/src/UnitTests/DistributionTests/Discrete/PoissonTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/PoissonTests.cs @@ -76,34 +76,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual(String.Format("Poisson(λ = {0})", 0.3), d.ToString()); } - /// - /// Can set probability of one. - /// - /// Lambda value. - [TestCase(1.5)] - [TestCase(5.4)] - [TestCase(10.8)] - public void CanSetProbabilityOfOne(double lambda) - { - GC.KeepAlive(new Poisson(0.3) - { - Lambda = lambda - }); - } - - /// - /// Set probability of one fails with bad value. - /// - /// Lambda value. - [TestCase(Double.NaN)] - [TestCase(-1.5)] - [TestCase(0.0)] - public void SetProbabilityOfOneFails(double lambda) - { - var d = new Poisson(0.3); - Assert.That(() => d.Lambda = lambda, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Discrete/ZipfTests.cs b/src/UnitTests/DistributionTests/Discrete/ZipfTests.cs index fff416d8..7bdc52a4 100644 --- a/src/UnitTests/DistributionTests/Discrete/ZipfTests.cs +++ b/src/UnitTests/DistributionTests/Discrete/ZipfTests.cs @@ -78,61 +78,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Discrete Assert.AreEqual("Zipf(S = 1, N = 5)", d.ToString()); } - /// - /// Can set S. - /// - /// S parameter. - [TestCase(0.1)] - [TestCase(1.0)] - [TestCase(5.0)] - public void CanSetS(double s) - { - GC.KeepAlive(new Zipf(1.0, 5) - { - S = s - }); - } - - /// - /// Set S fails with bad values. - /// - /// S parameter. - [TestCase(Double.NaN)] - [TestCase(-1.0)] - [TestCase(Double.NegativeInfinity)] - public void SetSFails(double s) - { - var d = new Zipf(1.0, 5); - Assert.That(() => d.S = s, Throws.ArgumentException); - } - - /// - /// Can set N. - /// - /// N parameter. - [TestCase(1)] - [TestCase(20)] - [TestCase(50)] - public void CanSetN(int n) - { - GC.KeepAlive(new Zipf(1.0, 5) - { - N = n - }); - } - - /// - /// Set N fails with bad values. - /// - /// N parameter. - [TestCase(-1)] - [TestCase(0)] - public void SetNFails(int n) - { - var d = new Zipf(1.0, 5); - Assert.That(() => d.N = n, Throws.ArgumentException); - } - /// /// Validate entropy. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs index f180040d..b70a97b2 100644 --- a/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/DirichletTests.cs @@ -145,23 +145,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set alpha. - /// - [Test] - public void CanSetAlpha() - { - var d = new Dirichlet(0.3, 10); - - var alpha = new double[10]; - for (var i = 0; i < 10; i++) - { - alpha[i] = i; - } - - d.Alpha = alpha; - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs b/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs index a4da69fd..7b06843c 100644 --- a/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/InverseWishartTests.cs @@ -152,21 +152,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(nu, d.DegreesOfFreedom); } - /// - /// Can set DegreesOfFreedom. - /// - /// DegreesOfFreedom parameter. - [TestCase(1.0)] - [TestCase(2.0)] - [TestCase(5.0)] - public void CanSetNu(double nu) - { - GC.KeepAlive(new InverseWishart(1.0, Matrix.Build.RandomPositiveDefinite(2, 1)) - { - DegreesOfFreedom = nu - }); - } - /// /// Can get scale matrix. /// @@ -186,18 +171,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set scale matrix. - /// - [Test] - public void CanSetS() - { - GC.KeepAlive(new InverseWishart(1.0, Matrix.Build.RandomPositiveDefinite(2, 1)) - { - Scale = Matrix.Build.RandomPositiveDefinite(2, 1) - }); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs b/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs index 0fc4d340..964cddca 100644 --- a/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/MatrixNormalTests.cs @@ -173,22 +173,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set M. - /// - /// Matrix rows count. - /// Matrix columns count. - [TestCase(1, 1)] - [TestCase(3, 3)] - [TestCase(10, 10)] - public void CanSetM(int n, int p) - { - GC.KeepAlive(new MatrixNormal(Matrix.Build.Random(n, p, 1), Matrix.Build.RandomPositiveDefinite(n, 1), Matrix.Build.RandomPositiveDefinite(p, 1)) - { - Mean = Matrix.Build.Random(n, p, 1) - }); - } - /// /// Can get V matrix. /// @@ -210,22 +194,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set V matrix. - /// - /// Matrix rows count. - /// Matrix columns count. - [TestCase(1, 1)] - [TestCase(3, 3)] - [TestCase(10, 10)] - public void CanSetV(int n, int p) - { - GC.KeepAlive(new MatrixNormal(Matrix.Build.Random(n, p, 1), Matrix.Build.RandomPositiveDefinite(n, 1), Matrix.Build.RandomPositiveDefinite(p, 1)) - { - RowCovariance = Matrix.Build.RandomPositiveDefinite(n, 1) - }); - } - /// /// Can get K matrix. /// @@ -247,22 +215,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set K matrix. - /// - /// Matrix rows count. - /// Matrix columns count. - [TestCase(1, 1)] - [TestCase(3, 3)] - [TestCase(10, 10)] - public void CanSetK(int n, int p) - { - GC.KeepAlive(new MatrixNormal(Matrix.Build.Random(n, p, 1), Matrix.Build.RandomPositiveDefinite(n, 1), Matrix.Build.RandomPositiveDefinite(p, 1)) - { - ColumnCovariance = Matrix.Build.RandomPositiveDefinite(p, 1) - }); - } - /// /// Validate density. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs b/src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs index 36c50040..fe913f1f 100644 --- a/src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/MultinomialTests.cs @@ -219,28 +219,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate AssertHelpers.AlmostEqualRelative(b.ProbabilityLn(x), Math.Log(b.Probability(x)), 12); } - /// - /// Can set probability. - /// - [Test] - public void CanSetProbability() - { - GC.KeepAlive(new Multinomial(_largeP, 4) - { - P = _smallP - }); - } - - /// - /// Set bad values of probability fails. - /// - [Test] - public void SetProbabilityFails() - { - var b = new Multinomial(_largeP, 4); - Assert.That(() => b.P = _badP, Throws.ArgumentException); - } - /// /// Can sample static. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs index cf64ac27..cf3266a9 100644 --- a/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/NormalGammaTests.cs @@ -97,28 +97,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(meanLocation, ng.MeanLocation); } - /// - /// Can set mean location. - /// - /// Mean location. - /// Mean scale. - /// Precision shape. - /// Precision inverse scale. - [TestCase(0.0, 1.0, 1.0, 1.0)] - [TestCase(10.0, 2.0, 2.0, 2.0)] - public void CanSetMeanLocation(double meanLocation, double meanScale, double precShape, double precInvScale) - { - var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale) - { - MeanLocation = -5.0 - }; - - Assert.AreEqual(-5.0, ng.MeanLocation); - Assert.AreEqual(meanScale, ng.MeanScale); - Assert.AreEqual(precShape, ng.PrecisionShape); - Assert.AreEqual(precInvScale, ng.PrecisionInverseScale); - } - /// /// Can get mean scale. /// @@ -134,27 +112,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(meanScale, ng.MeanScale); } - /// - /// Can set mean scale. - /// - /// Mean location. - /// Mean scale. - /// Precision shape. - /// Precision inverse scale. - [TestCase(0.0, 1.0, 1.0, 1.0)] - [TestCase(10.0, 2.0, 2.0, 2.0)] - public void CanSetMeanScale(double meanLocation, double meanScale, double precShape, double precInvScale) - { - var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale) - { - MeanScale = 5.0 - }; - Assert.AreEqual(meanLocation, ng.MeanLocation); - Assert.AreEqual(5.0, ng.MeanScale); - Assert.AreEqual(precShape, ng.PrecisionShape); - Assert.AreEqual(precInvScale, ng.PrecisionInverseScale); - } - /// /// Can get precision shape. /// @@ -170,27 +127,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(precShape, ng.PrecisionShape); } - /// - /// Can set precision shape. - /// - /// Mean location. - /// Mean scale. - /// Precision shape. - /// Precision inverse scale. - [TestCase(0.0, 1.0, 1.0, 1.0)] - [TestCase(10.0, 2.0, 2.0, 2.0)] - public void CanSetPrecisionShape(double meanLocation, double meanScale, double precShape, double precInvScale) - { - var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale) - { - PrecisionShape = 5.0 - }; - Assert.AreEqual(meanLocation, ng.MeanLocation); - Assert.AreEqual(meanScale, ng.MeanScale); - Assert.AreEqual(5.0, ng.PrecisionShape); - Assert.AreEqual(precInvScale, ng.PrecisionInverseScale); - } - /// /// Can get precision inverse scale. /// @@ -206,27 +142,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(precInvScale, ng.PrecisionInverseScale); } - /// - /// Can set precision inverse scale. - /// - /// Mean location. - /// Mean scale. - /// Precision shape. - /// Precision inverse scale. - [TestCase(0.0, 1.0, 1.0, 1.0)] - [TestCase(10.0, 2.0, 2.0, 2.0)] - public void CanSetPrecisionPrecisionInverseScale(double meanLocation, double meanScale, double precShape, double precInvScale) - { - var ng = new NormalGamma(meanLocation, meanScale, precShape, precInvScale) - { - PrecisionInverseScale = 5.0 - }; - Assert.AreEqual(meanLocation, ng.MeanLocation); - Assert.AreEqual(meanScale, ng.MeanScale); - Assert.AreEqual(precShape, ng.PrecisionShape); - Assert.AreEqual(5.0, ng.PrecisionInverseScale); - } - /// /// Can get mean marginals. /// diff --git a/src/UnitTests/DistributionTests/Multivariate/WishartTests.cs b/src/UnitTests/DistributionTests/Multivariate/WishartTests.cs index 83305e9d..30e1aac1 100644 --- a/src/UnitTests/DistributionTests/Multivariate/WishartTests.cs +++ b/src/UnitTests/DistributionTests/Multivariate/WishartTests.cs @@ -146,21 +146,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate Assert.AreEqual(nu, d.DegreesOfFreedom); } - /// - /// Can set DegreesOfFreedom. - /// - /// DegreesOfFreedom parameter. - [TestCase(1.0)] - [TestCase(2.0)] - [TestCase(5.0)] - public void CanSetNu(double nu) - { - GC.KeepAlive(new Wishart(1.0, Matrix.Build.RandomPositiveDefinite(2, 1)) - { - DegreesOfFreedom = nu - }); - } - /// /// Can get scale matrix. /// @@ -180,18 +165,6 @@ namespace MathNet.Numerics.UnitTests.DistributionTests.Multivariate } } - /// - /// Can set scale matrix. - /// - [Test] - public void CanSetS() - { - GC.KeepAlive(new Wishart(1.0, Matrix.Build.RandomPositiveDefinite(2, 1)) - { - Scale = Matrix.Build.RandomPositiveDefinite(2, 1) - }); - } - /// /// Validate mean. /// diff --git a/src/UnitTests/InterpolationTests/CubicSplineTest.cs b/src/UnitTests/InterpolationTests/CubicSplineTest.cs index b12dbd2a..b0364afe 100644 --- a/src/UnitTests/InterpolationTests/CubicSplineTest.cs +++ b/src/UnitTests/InterpolationTests/CubicSplineTest.cs @@ -30,7 +30,6 @@ using System; using System.Linq; -using System.Threading.Tasks; using MathNet.Numerics.Interpolation; using NUnit.Framework; @@ -177,6 +176,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests } } +#if !NET35 && !PORTABLE [Test] public void InterpolateAkimaSorted_MustBeThreadSafe_GitHub219([Values(8, 32, 256, 1024)] int samples) { @@ -188,7 +188,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests } var yipol = new double[samples]; - Parallel.For(0, samples, i => + System.Threading.Tasks.Parallel.For(0, samples, i => { var spline = CubicSpline.InterpolateAkimaSorted(x, y[i]); yipol[i] = spline.Interpolate(1.0); @@ -196,5 +196,7 @@ namespace MathNet.Numerics.UnitTests.InterpolationTests CollectionAssert.DoesNotContain(yipol, Double.NaN); } +#endif + } }