diff --git a/src/MathNet.Numerics.4.5.resharper b/src/MathNet.Numerics.4.5.resharper index d33d1843..d6c48cd3 100644 --- a/src/MathNet.Numerics.4.5.resharper +++ b/src/MathNet.Numerics.4.5.resharper @@ -754,6 +754,8 @@ Bluestein ALWAYS_ADD ALWAYS_ADD ALWAYS_ADD + True + True 4 NEXT_LINE @@ -773,8 +775,20 @@ Bluestein volatile False + False + True + False True + False + False True + True + True + CHOP_IF_LONG + True + True + CHOP_ALWAYS + CHOP_IF_LONG @@ -813,7 +827,7 @@ Bluestein - + diff --git a/src/Numerics/Complex.cs b/src/Numerics/Complex.cs index f7157798..26f0c1f0 100644 --- a/src/Numerics/Complex.cs +++ b/src/Numerics/Complex.cs @@ -1,7 +1,9 @@ // // Math.NET Numerics, part of the Math.NET Project // http://mathnet.opensourcedotnet.info +// // Copyright (c) 2009 Math.NET +// // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -10,8 +12,10 @@ // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: +// // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND @@ -28,8 +32,7 @@ namespace MathNet.Numerics using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; - - using MathNet.Numerics.Properties; + using Properties; /// /// Complex numbers class. @@ -40,7 +43,7 @@ namespace MathNet.Numerics /// on complex numbers. All the operators +, -, /// *, /, ==, != are defined in the /// canonical way. Additional complex trigonometric functions such - /// as , ... + /// as , ... /// are also provided. Note that the Complex structures /// has two special constant values and /// . @@ -73,35 +76,35 @@ namespace MathNet.Numerics /// /// Regular expression used to parse strings into complex numbers. /// - private static readonly Regex ParseExpression = + private static readonly Regex _parseExpression = new Regex( - @"^((?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$", + @"^((?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); /// /// Represents imaginary unit number. /// - private static readonly Complex i = new Complex(0, 1); + private static readonly Complex _i = new Complex(0, 1); /// /// Represents a infinite complex number /// - private static readonly Complex infinity = new Complex(double.PositiveInfinity, double.PositiveInfinity); + private static readonly Complex _infinity = new Complex(double.PositiveInfinity, double.PositiveInfinity); /// /// Represents not-a-number. /// - private static readonly Complex nan = new Complex(Double.NaN, Double.NaN); + private static readonly Complex _nan = new Complex(Double.NaN, Double.NaN); /// /// Representing the one value. /// - private static readonly Complex one = new Complex(1.0, 0.0); + private static readonly Complex _one = new Complex(1.0, 0.0); /// /// Representing the zero value. /// - private static readonly Complex zero = new Complex(0.0, 0.0); + private static readonly Complex _zero = new Complex(0.0, 0.0); /// /// The real component of the complex number. @@ -129,8 +132,8 @@ namespace MathNet.Numerics /// public Complex(double real, double imaginary) { - this._real = real; - this._imag = imaginary; + _real = real; + _imag = imaginary; } #endregion @@ -152,10 +155,7 @@ namespace MathNet.Numerics /// A value representing the infinity value. public static Complex Infinity { - get - { - return infinity; - } + get { return _infinity; } } /// @@ -164,10 +164,7 @@ namespace MathNet.Numerics /// A value representing not-a-number. public static Complex NaN { - get - { - return nan; - } + get { return _nan; } } /// @@ -176,10 +173,7 @@ namespace MathNet.Numerics /// A value representing the imaginary unit number. public static Complex I { - get - { - return i; - } + get { return _i; } } /// @@ -188,10 +182,7 @@ namespace MathNet.Numerics /// A value representing the zero value. public static Complex Zero { - get - { - return new Complex(0.0, 0.0); - } + get { return new Complex(0.0, 0.0); } } /// @@ -200,10 +191,7 @@ namespace MathNet.Numerics /// A value representing the 1 value. public static Complex One { - get - { - return one; - } + get { return _one; } } #endregion Properties @@ -214,10 +202,7 @@ namespace MathNet.Numerics /// The real component of the complex number. public double Real { - get - { - return this._real; - } + get { return _real; } } /// @@ -226,10 +211,7 @@ namespace MathNet.Numerics /// The real imaginary component of the complex number. public double Imaginary { - get - { - return this._imag; - } + get { return _imag; } } /// @@ -238,10 +220,7 @@ namespace MathNet.Numerics /// true if this instance is zero; otherwise, false. public bool IsZero { - get - { - return this._real.AlmostZero() && this._imag.AlmostZero(); - } + get { return _real.AlmostZero() && _imag.AlmostZero(); } } /// @@ -250,10 +229,7 @@ namespace MathNet.Numerics /// true if this instance is one; otherwise, false. public bool IsOne { - get - { - return this._real.AlmostEqual(1.0) && this._imag.AlmostZero(); - } + get { return _real.AlmostEqual(1.0) && _imag.AlmostZero(); } } /// @@ -262,10 +238,7 @@ namespace MathNet.Numerics /// true if this instance is I; otherwise, false. public bool IsI { - get - { - return this._real.AlmostZero() && this._imag.AlmostEqual(1.0); - } + get { return _real.AlmostZero() && _imag.AlmostEqual(1.0); } } /// @@ -275,10 +248,7 @@ namespace MathNet.Numerics /// true if this instance is NaN; otherwise, false. public bool IsNaN { - get - { - return double.IsNaN(this._real) || double.IsNaN(this._imag); - } + get { return double.IsNaN(_real) || double.IsNaN(_imag); } } /// @@ -294,10 +264,7 @@ namespace MathNet.Numerics /// public bool IsInfinity { - get - { - return double.IsInfinity(this._real) || double.IsInfinity(this._imag); - } + get { return double.IsInfinity(_real) || double.IsInfinity(_imag); } } /// @@ -306,10 +273,7 @@ namespace MathNet.Numerics /// true if this instance is a real number; otherwise, false. public bool IsReal { - get - { - return this._imag.AlmostZero(); - } + get { return _imag.AlmostZero(); } } /// @@ -320,10 +284,7 @@ namespace MathNet.Numerics /// public bool IsRealNonNegative { - get - { - return this._imag.AlmostZero() && this._real >= 0; - } + get { return _imag.AlmostZero() && _real >= 0; } } /// @@ -343,10 +304,7 @@ namespace MathNet.Numerics /// public Complex Conjugate { - get - { - return new Complex(this._real, -this._imag); - } + get { return new Complex(_real, -_imag); } } /// @@ -355,10 +313,7 @@ namespace MathNet.Numerics /// public double Modulus { - get - { - return Math.Sqrt((this._real * this._real) + (this._imag * this._imag)); - } + get { return Math.Sqrt((_real * _real) + (_imag * _imag)); } } /// @@ -367,10 +322,7 @@ namespace MathNet.Numerics /// public double ModulusSquared { - get - { - return (this._real * this._real) + (this._imag * this._imag); - } + get { return (_real * _real) + (_imag * _imag); } } /// @@ -379,18 +331,18 @@ namespace MathNet.Numerics /// /// Argument always returns a value bigger than negative Pi and /// smaller or equal to Pi. If this Complex is zero, the Complex - /// is assumed to be positive _real with an argument of zero. + /// is assumed to be positive real with an argument of zero. /// public double Argument { get { - if (this.IsReal && this._real < 0) + if (IsReal && _real < 0) { return Math.PI; } - return this.IsRealNonNegative ? 0 : Math.Atan2(this._imag, this._real); + return IsRealNonNegative ? 0 : Math.Atan2(_imag, _real); } } @@ -401,34 +353,34 @@ namespace MathNet.Numerics { get { - if (double.IsPositiveInfinity(this._real) && double.IsPositiveInfinity(this._imag)) + if (double.IsPositiveInfinity(_real) && double.IsPositiveInfinity(_imag)) { return new Complex(Constants.Sqrt1Over2, Constants.Sqrt1Over2); } - if (double.IsPositiveInfinity(this._real) && double.IsNegativeInfinity(this._imag)) + if (double.IsPositiveInfinity(_real) && double.IsNegativeInfinity(_imag)) { return new Complex(Constants.Sqrt1Over2, -Constants.Sqrt1Over2); } - if (double.IsNegativeInfinity(this._real) && double.IsPositiveInfinity(this._imag)) + if (double.IsNegativeInfinity(_real) && double.IsPositiveInfinity(_imag)) { return new Complex(-Constants.Sqrt1Over2, -Constants.Sqrt1Over2); } - if (double.IsNegativeInfinity(this._real) && double.IsNegativeInfinity(this._imag)) + if (double.IsNegativeInfinity(_real) && double.IsNegativeInfinity(_imag)) { return new Complex(-Constants.Sqrt1Over2, Constants.Sqrt1Over2); } // don't replace this with "Modulus"! - var mod = SpecialFunctions.Hypotenuse(this._real, this._imag); + var mod = SpecialFunctions.Hypotenuse(_real, _imag); if (mod.AlmostZero()) { return Zero; } - return new Complex(this._real / mod, this._imag / mod); + return new Complex(_real / mod, _imag / mod); } } @@ -509,15 +461,15 @@ namespace MathNet.Numerics /// /// Raise this Complex to the inverse of the given value. /// - /// + /// /// The root exponent. /// /// /// The complex raised to the inverse of the given exponent. /// - public Complex Root(Complex rootexponent) + public Complex Root(Complex rootExponent) { - return Power(1 / rootexponent); + return Power(1 / rootExponent); } /// @@ -638,7 +590,7 @@ namespace MathNet.Numerics /// public override string ToString() { - return this.ToString(null, null); + return ToString(null, null); } /// @@ -653,7 +605,7 @@ namespace MathNet.Numerics /// public string ToString(string format) { - return this.ToString(format, null); + return ToString(format, null); } /// @@ -664,11 +616,11 @@ namespace MathNet.Numerics /// format provider. /// /// - /// An IFormatProvider that supplies culture-specific formatting information. + /// An that supplies culture-specific formatting information. /// public string ToString(IFormatProvider formatProvider) { - return this.ToString(null, formatProvider); + return ToString(null, formatProvider); } /// @@ -688,32 +640,32 @@ namespace MathNet.Numerics /// A format specification. /// /// - /// An IFormatProvider that supplies culture-specific formatting information. + /// An that supplies culture-specific formatting information. /// public string ToString(string format, IFormatProvider formatProvider) { - if (this.IsNaN) + if (IsNaN) { return "NaN"; } - if (this.IsInfinity) + if (IsInfinity) { return "Infinity"; } var ret = new StringBuilder(); - if (!this._real.AlmostZero()) + if (!_real.AlmostZero()) { - ret.Append(this._real.ToString(format, formatProvider)); + ret.Append(_real.ToString(format, formatProvider)); } - if (!this._imag.AlmostZero()) + if (!_imag.AlmostZero()) { - if (!this._real.AlmostZero()) + if (!_real.AlmostZero()) { - if (this._imag < 0) + if (_imag < 0) { ret.Append(" "); } @@ -723,7 +675,7 @@ namespace MathNet.Numerics } } - ret.Append(this._imag.ToString(format, formatProvider)).Append("i"); + ret.Append(_imag.ToString(format, formatProvider)).Append("i"); } return ret.ToString(); @@ -738,25 +690,25 @@ namespace MathNet.Numerics /// corresponding real and imaginary components are equal. /// /// - /// Returns true if the two objects are the same object, or if their corresponding - /// real and imaginary components are equal, false otherwise. + /// Returns true if the two objects are the same object, or if their corresponding + /// real and imaginary components are equal, false otherwise. /// /// /// The complex number to compare to with. /// public bool Equals(Complex other) { - if (this.IsNaN || other.IsNaN) + if (IsNaN || other.IsNaN) { return false; } - if (this.IsInfinity && other.IsInfinity) + if (IsInfinity && other.IsInfinity) { return true; } - return this._real.AlmostEqual(other._real) && this._imag.AlmostEqual(other._imag); + return _real.AlmostEqual(other._real) && _imag.AlmostEqual(other._imag); } /// @@ -771,7 +723,7 @@ namespace MathNet.Numerics /// public override int GetHashCode() { - return this._real.GetHashCode() ^ (-this._imag.GetHashCode()); + return _real.GetHashCode() ^ (-_imag.GetHashCode()); } /// @@ -779,15 +731,15 @@ namespace MathNet.Numerics /// corresponding real and imaginary components are equal. /// /// - /// Returns true if the two objects are the same object, or if their corresponding - /// real and imaginary components are equal, false otherwise. + /// Returns true if the two objects are the same object, or if their corresponding + /// real and imaginary components are equal, false otherwise. /// /// /// The complex number to compare to with. /// public override bool Equals(object obj) { - return (obj is Complex) && this.Equals((Complex)obj); + return (obj is Complex) && Equals((Complex)obj); } #endregion @@ -799,7 +751,7 @@ namespace MathNet.Numerics /// /// One of complex numbers to compare. /// The other complex numbers to compare. - /// true if the real and imaginary components of the two complex numbers are equal; false otherwise. + /// true if the real and imaginary components of the two complex numbers are equal; false otherwise. public static bool operator ==(Complex complex1, Complex complex2) { return complex1.Equals(complex2); @@ -810,7 +762,7 @@ namespace MathNet.Numerics /// /// One of complex numbers to compare. /// The other complex numbers to compare. - /// true if the real or imaginary components of the two complex numbers are not equal; false otherwise. + /// true if the real or imaginary components of the two complex numbers are not equal; false otherwise. public static bool operator !=(Complex complex1, Complex complex2) { return !complex1.Equals(complex2); @@ -897,7 +849,7 @@ namespace MathNet.Numerics public static Complex operator *(Complex multiplicand, Complex multiplier) { return new Complex( - (multiplicand._real * multiplier._real) - (multiplicand._imag * multiplier._imag), + (multiplicand._real * multiplier._real) - (multiplicand._imag * multiplier._imag), (multiplicand._real * multiplier._imag) + (multiplicand._imag * multiplier._real)); } @@ -932,7 +884,7 @@ namespace MathNet.Numerics var modSquared = divisor.ModulusSquared; return new Complex( - ((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, + ((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared); } @@ -1116,7 +1068,7 @@ namespace MathNet.Numerics /// the string to parse. /// /// - /// An IFormatProvider that supplies culture-specific formatting information. + /// An that supplies culture-specific formatting information. /// public static Complex Parse(string value, IFormatProvider formatProvider) { @@ -1178,7 +1130,7 @@ namespace MathNet.Numerics } else { - var matchResult = ParseExpression.Match(value); + var matchResult = _parseExpression.Match(value); if (matchResult.Success) { var realStr = matchResult.Groups["r"].Value; @@ -1241,7 +1193,7 @@ namespace MathNet.Numerics /// A string containing a complex number to convert. /// /// - /// An IFormatProvider that supplies culture-specific formatting information about value. + /// An that supplies culture-specific formatting information about value. /// /// /// The parsed value. @@ -1260,12 +1212,12 @@ namespace MathNet.Numerics } catch (ArgumentNullException) { - result = zero; + result = _zero; ret = false; } catch (FormatException) { - result = zero; + result = _zero; ret = false; } @@ -1274,4 +1226,4 @@ namespace MathNet.Numerics #endregion } -} \ No newline at end of file +} diff --git a/src/Numerics/Constants.cs b/src/Numerics/Constants.cs index 62df8c6a..11c78f82 100644 --- a/src/Numerics/Constants.cs +++ b/src/Numerics/Constants.cs @@ -130,15 +130,9 @@ namespace MathNet.Numerics public const double Grad = 0.015707963267948966192313216916397514420985846996876d; /// The number ln(10)/20 - factor to convert from Power Decibel (dB) to Neper (Np). Use this version when the Decibel represent a power gain but the compared values are not powers (e.g. amplitude, current, voltage). - /// - /// - /// public const double PowerDecibel = 0.11512925464970228420089957273421821038005507443144d; /// The number ln(10)/10 - factor to convert from Neutral Decibel (dB) to Neper (Np). Use this version when either both or neither of the Decibel and the compared values represent powers. - /// - /// - /// public const double NeutralDecibel = 0.23025850929940456840179914546843642076011014886288d; /// The Catalan constant diff --git a/src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs b/src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs index 4f097d6b..b738d175 100644 --- a/src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs +++ b/src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs @@ -61,7 +61,8 @@ namespace MathNet.Numerics.IntegralTransforms.Algorithms { m >>= 1; j ^= m; - } while ((j & m) == 0); + } + while ((j & m) == 0); } } @@ -159,4 +160,4 @@ namespace MathNet.Numerics.IntegralTransforms.Algorithms InverseScaleByOptions(options, samples); } } -} \ No newline at end of file +} diff --git a/src/Numerics/NumberTheory/IntegerTheory.cs b/src/Numerics/NumberTheory/IntegerTheory.cs index 46799aae..d00a2543 100644 --- a/src/Numerics/NumberTheory/IntegerTheory.cs +++ b/src/Numerics/NumberTheory/IntegerTheory.cs @@ -104,13 +104,13 @@ namespace MathNet.Numerics.NumberTheory /// public static int CeilingToPowerOfTwo(this int number) { - if(number == Int32.MinValue) + if (number == Int32.MinValue) { return 0; } - const int maxPowerOfTwo = 0x40000000; - if(number > maxPowerOfTwo) + const int MaxPowerOfTwo = 0x40000000; + if (number > MaxPowerOfTwo) { throw new ArgumentOutOfRangeException("number"); } @@ -138,8 +138,8 @@ namespace MathNet.Numerics.NumberTheory return 0; } - const long maxPowerOfTwo = 0x4000000000000000; - if (number > maxPowerOfTwo) + const long MaxPowerOfTwo = 0x4000000000000000; + if (number > MaxPowerOfTwo) { throw new ArgumentOutOfRangeException("number"); } @@ -169,7 +169,7 @@ namespace MathNet.Numerics.NumberTheory return 1 << exponent; } - + /// /// Raises 2 to the provided integer exponent (0 <= exponent < 63). /// diff --git a/src/Numerics/Sorting.cs b/src/Numerics/Sorting.cs index 5d0ff9f6..6bdc54ae 100644 --- a/src/Numerics/Sorting.cs +++ b/src/Numerics/Sorting.cs @@ -37,7 +37,7 @@ namespace MathNet.Numerics public static class Sorting { /// - /// Sort a list of keys, inplace using the quick sort algorithm. + /// Sort a list of keys, in place using the quick sort algorithm. /// /// The type of elements stored in the list. /// List to sort. @@ -47,33 +47,33 @@ namespace MathNet.Numerics } /// - /// Sort a list of keys and items with respect to the keys, inplace using the quick sort algorithm. + /// Sort a list of keys and items with respect to the keys, in place using the quick sort algorithm. /// /// The type of elements stored in the key list. /// The type of elements stored in the item list. /// List to sort. - /// List to permutate the same way as the key list. + /// List to permute the same way as the key list. public static void Sort(IList keys, IList items) { Sort(keys, items, Comparer.Default); } /// - /// Sort a list of keys, items1 and items2 with respect to the keys, inplace using the quick sort algorithm. + /// Sort a list of keys, items1 and items2 with respect to the keys, in place using the quick sort algorithm. /// /// The type of elements stored in the key list. /// The type of elements stored in the first item list. /// The type of elements stored in the second item list. /// List to sort. - /// First list to permutate the same way as the key list. - /// Second list to permutate the same way as the key list. + /// First list to permute the same way as the key list. + /// Second list to permute the same way as the key list. public static void Sort(IList keys, IList items1, IList items2) { Sort(keys, items1, items2, Comparer.Default); } /// - /// Sort a range of a list of keys, inplace using the quick sort algorithm. + /// Sort a range of a list of keys, in place using the quick sort algorithm. /// /// The type of elements in the key list. /// List to sort. @@ -85,7 +85,7 @@ namespace MathNet.Numerics } /// - /// Sort a list of keys, inplace using the quick sort algorithm using the quick sort algorithm. + /// Sort a list of keys, in place using the quick sort algorithm using the quick sort algorithm. /// /// The type of elements in the key list. /// List to sort. @@ -139,12 +139,12 @@ namespace MathNet.Numerics } /// - /// Sort a list of keys and items with respect to the keys, inplace using the quick sort algorithm. + /// Sort a list of keys and items with respect to the keys, in place using the quick sort algorithm. /// /// The type of elements in the key list. /// The type of elements in the item list. /// List to sort. - /// List to permutate the same way as the key list. + /// List to permute the same way as the key list. /// Comparison, defining the sort order. public static void Sort(IList keys, IList items, IComparer comparer) { @@ -177,14 +177,14 @@ namespace MathNet.Numerics } /// - /// Sort a list of keys, items1 and items2 with respect to the keys, inplace using the quick sort algorithm. + /// Sort a list of keys, items1 and items2 with respect to the keys, in place using the quick sort algorithm. /// /// The type of elements in the key list. /// The type of elements in the first item list. /// The type of elements in the second item list. /// List to sort. - /// First list to permutate the same way as the key list. - /// Second list to permutate the same way as the key list. + /// First list to permute the same way as the key list. + /// Second list to permute the same way as the key list. /// Comparison, defining the sort order. public static void Sort( IList keys, IList items1, IList items2, IComparer comparer) @@ -214,7 +214,7 @@ namespace MathNet.Numerics } /// - /// Sort a range of a list of keys, inplace using the quick sort algorithm. + /// Sort a range of a list of keys, in place using the quick sort algorithm. /// /// The type of element in the list. /// List to sort. @@ -280,7 +280,7 @@ namespace MathNet.Numerics } /// - /// Recursive implementation for an inplace quick sort on a list. + /// Recursive implementation for an in place quick sort on a list. /// /// The type of the list on which the quick sort is performed. /// The list which is sorted using quick sort. @@ -342,7 +342,8 @@ namespace MathNet.Numerics a++; b--; - } while (a <= b); + } + while (a <= b); // In order to limit the recusion depth to log(n), we sort the // shorter partition recusively and the longer partition iteratively. @@ -364,11 +365,12 @@ namespace MathNet.Numerics right = b; } - } while (left < right); + } + while (left < right); } /// - /// Recursive implementation for an inplace quick sort on a list while reordering one other list accordingly. + /// Recursive implementation for an in place quick sort on a list while reordering one other list accordingly. /// /// The type of the list on which the quick sort is performed. /// The type of the list which is automatically reordered accordingly. @@ -437,7 +439,8 @@ namespace MathNet.Numerics a++; b--; - } while (a <= b); + } + while (a <= b); // In order to limit the recusion depth to log(n), we sort the // shorter partition recusively and the longer partition iteratively. @@ -459,11 +462,12 @@ namespace MathNet.Numerics right = b; } - } while (left < right); + } + while (left < right); } /// - /// Recursive implementation for an inplace quick sort on one list while reordering two other lists accordingly. + /// Recursive implementation for an in place quick sort on one list while reordering two other lists accordingly. /// /// The type of the list on which the quick sort is performed. /// The type of the first list which is automatically reordered accordingly. @@ -539,7 +543,8 @@ namespace MathNet.Numerics a++; b--; - } while (a <= b); + } + while (a <= b); // In order to limit the recusion depth to log(n), we sort the // shorter partition recusively and the longer partition iteratively. @@ -561,7 +566,8 @@ namespace MathNet.Numerics right = b; } - } while (left < right); + } + while (left < right); } /// @@ -573,12 +579,14 @@ namespace MathNet.Numerics /// The index of the second element of the swap. internal static void Swap(IList keys, int a, int b) { - if (a != b) + if (a == b) { - T local = keys[a]; - keys[a] = keys[b]; - keys[b] = local; + return; } + + T local = keys[a]; + keys[a] = keys[b]; + keys[b] = local; } } -} \ No newline at end of file +} diff --git a/src/Numerics/Statistics/DescriptiveStatistics.cs b/src/Numerics/Statistics/DescriptiveStatistics.cs index 3e3a0b1c..ba042fb5 100644 --- a/src/Numerics/Statistics/DescriptiveStatistics.cs +++ b/src/Numerics/Statistics/DescriptiveStatistics.cs @@ -58,10 +58,14 @@ namespace MathNet.Numerics.Statistics /// Initializes a new instance of the class. /// /// The sample data. - /// if set to true, increased accuracy mode used. - /// Increased accuracy mode uses types for internal calculations. - /// Don't use increased accuracy for data sets containing large values (in absolute value). - /// This may cause the calculations to overflow. + /// + /// If set to true, increased accuracy mode used. + /// Increased accuracy mode uses types for internal calculations. + /// + /// + /// Don't use increased accuracy for data sets containing large values (in absolute value). + /// This may cause the calculations to overflow. + /// public DescriptiveStatistics(IEnumerable data, bool increasedAccuracy) { if (increasedAccuracy) @@ -82,10 +86,14 @@ namespace MathNet.Numerics.Statistics /// Initializes a new instance of the class. /// /// The sample data. - /// if set to true, increased accuracy mode used. - /// Increased accuracy mode uses types for internal calculations. - /// Don't use increased accuracy for data sets containing large values (in absolute value). - /// This may cause the calculations to overflow. + /// + /// If set to true, increased accuracy mode used. + /// Increased accuracy mode uses types for internal calculations. + /// + /// + /// Don't use increased accuracy for data sets containing large values (in absolute value). + /// This may cause the calculations to overflow. + /// public DescriptiveStatistics(IEnumerable data, bool increasedAccuracy) { if (increasedAccuracy) @@ -174,7 +182,7 @@ namespace MathNet.Numerics.Statistics { double diff = xi - Mean; correction += diff; - double tmp = diff*diff; + double tmp = diff * diff; variance += tmp; tmp *= diff; skewness += tmp; @@ -184,18 +192,21 @@ namespace MathNet.Numerics.Statistics } Count = n; - Variance = (variance - (correction*correction)/n)/(n - 1); + Variance = (variance - (correction * correction / n)) / (n - 1); StandardDeviation = System.Math.Sqrt(Variance); if (Variance != 0) { if (n > 2) { - Skewness = (double) n/((n - 1)*(n - 2))*(skewness/(Variance*StandardDeviation)); + Skewness = (double)n / ((n - 1) * (n - 2)) * (skewness / (Variance * StandardDeviation)); } if (n > 3) { - Kurtosis = ((double) n*(n + 1))/((n - 1)*(n - 2)*(n - 3))*(kurtosis/(Variance*Variance)) - (3.0*(n - 1)*(n - 1))/((n - 2)*(n - 3)); + Kurtosis = (((double)n * (n + 1)) + / ((n - 1) * (n - 2) * (n - 3)) + * (kurtosis / (Variance * Variance))) + - ((3.0 * (n - 1) * (n - 1)) / ((n - 2) * (n - 3))); } } } @@ -217,7 +228,7 @@ namespace MathNet.Numerics.Statistics if (xi.HasValue) { double diff = xi.Value - Mean; - double tmp = diff*diff; + double tmp = diff * diff; correction += diff; variance += tmp; tmp *= diff; @@ -231,18 +242,21 @@ namespace MathNet.Numerics.Statistics Count = n; if (n > 0) { - Variance = (variance - (correction*correction)/n)/(n - 1); + Variance = (variance - (correction * correction / n)) / (n - 1); StandardDeviation = System.Math.Sqrt(Variance); if (Variance != 0) { if (n > 2) { - Skewness = (double) n/((n - 1)*(n - 2))*(skewness/(Variance*StandardDeviation)); + Skewness = (double)n / ((n - 1) * (n - 2)) * (skewness / (Variance * StandardDeviation)); } if (n > 3) { - Kurtosis = ((double) n*(n + 1))/((n - 1)*(n - 2)*(n - 3))*(kurtosis/(Variance*Variance)) - (3.0*(n - 1)*(n - 1))/((n - 2)*(n - 3)); + Kurtosis = (((double)n * (n + 1)) + / ((n - 1) * (n - 2) * (n - 3)) + * (kurtosis / (Variance * Variance))) + - ((3.0 * (n - 1) * (n - 1)) / ((n - 2) * (n - 3))); } } } @@ -255,7 +269,7 @@ namespace MathNet.Numerics.Statistics private void ComputeHA(IEnumerable data) { Mean = data.Mean(); - decimal mean = (decimal) Mean; + decimal mean = (decimal)Mean; decimal variance = 0; decimal correction = 0; decimal skewness = 0; @@ -264,7 +278,7 @@ namespace MathNet.Numerics.Statistics foreach (decimal xi in data) { decimal diff = xi - mean; - decimal tmp = diff*diff; + decimal tmp = diff * diff; correction += diff; variance += tmp; tmp *= diff; @@ -275,18 +289,21 @@ namespace MathNet.Numerics.Statistics } Count = n; - Variance = (double) (variance - (correction*correction)/n)/(n - 1); + Variance = (double)(variance - (correction * correction / n)) / (n - 1); StandardDeviation = System.Math.Sqrt(Variance); if (Variance != 0) { if (n > 2) { - Skewness = (double) n/((n - 1)*(n - 2))*((double) skewness/(Variance*StandardDeviation)); + Skewness = (double)n / ((n - 1) * (n - 2)) * ((double)skewness / (Variance * StandardDeviation)); } if (n > 3) { - Kurtosis = ((double) n*(n + 1))/((n - 1)*(n - 2)*(n - 3))*((double) kurtosis/(Variance*Variance)) - (3.0*(n - 1)*(n - 1))/((n - 2)*(n - 3)); + Kurtosis = (((double)n * (n + 1)) + / ((n - 1) * (n - 2) * (n - 3)) + * ((double)kurtosis / (Variance * Variance))) + - ((3.0 * (n - 1) * (n - 1)) / ((n - 2) * (n - 3))); } } } @@ -298,7 +315,7 @@ namespace MathNet.Numerics.Statistics private void ComputeHA(IEnumerable data) { Mean = data.Mean(); - decimal mean = (decimal) Mean; + decimal mean = (decimal)Mean; decimal variance = 0; decimal correction = 0; decimal skewness = 0; @@ -309,7 +326,7 @@ namespace MathNet.Numerics.Statistics if (xi.HasValue) { decimal diff = xi.Value - mean; - decimal tmp = diff*diff; + decimal tmp = diff * diff; correction += diff; variance += tmp; tmp *= diff; @@ -323,21 +340,24 @@ namespace MathNet.Numerics.Statistics Count = n; if (n > 0) { - Variance = (double) (variance - (correction*correction)/n)/(n - 1); + Variance = (double)(variance - (correction * correction / n)) / (n - 1); StandardDeviation = System.Math.Sqrt(Variance); if (Variance != 0) { if (n > 2) { - Skewness = (double) n/((n - 1)*(n - 2))*((double) skewness/(Variance*StandardDeviation)); + Skewness = (double)n / ((n - 1) * (n - 2)) * ((double)skewness / (Variance * StandardDeviation)); } if (n > 3) { - Kurtosis = ((double) n*(n + 1))/((n - 1)*(n - 2)*(n - 3))*((double) kurtosis/(Variance*Variance)) - (3.0*(n - 1)*(n - 1))/((n - 2)*(n - 3)); + Kurtosis = (((double)n * (n + 1)) + / ((n - 1) * (n - 2) * (n - 3)) + * ((double)kurtosis / (Variance * Variance))) + - ((3.0 * (n - 1) * (n - 1)) / ((n - 2) * (n - 3))); } } } } } -} \ No newline at end of file +} diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 3057fa08..62539484 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -30,8 +30,7 @@ namespace MathNet.Numerics.Statistics { using System; using System.Collections.Generic; - using MathNet.Numerics.Properties; - using MathNet.Numerics.NumberTheory; + using Properties; /// /// Extension methods to return basic statistics on set of data. @@ -54,7 +53,7 @@ namespace MathNet.Numerics.Statistics int m = 0; foreach (var item in data) { - mean += (item - mean)/++m; + mean += (item - mean) / ++m; } return mean; @@ -78,7 +77,7 @@ namespace MathNet.Numerics.Statistics { if (item.HasValue) { - mean += (item.Value - mean)/++m; + mean += (item.Value - mean) / ++m; } } @@ -113,11 +112,11 @@ namespace MathNet.Numerics.Statistics j++; double xi = iterator.Current; t += xi; - double diff = j*xi - t; - variance += (diff*diff)/(j*(j - 1)); + double diff = (j * xi) - t; + variance += (diff * diff) / (j * (j - 1)); } - return variance/(j - 1); + return variance / (j - 1); } /// @@ -145,6 +144,7 @@ namespace MathNet.Numerics.Statistics { break; } + if (iterator.Current.HasValue) { j++; @@ -160,12 +160,12 @@ namespace MathNet.Numerics.Statistics j++; double xi = iterator.Current.Value; t += xi; - double diff = j*xi - t; - variance += (diff*diff)/(j*(j - 1)); + double diff = (j * xi) - t; + variance += (diff * diff) / (j * (j - 1)); } } - return variance/(j - 1); + return variance / (j - 1); } /// @@ -196,7 +196,7 @@ namespace MathNet.Numerics.Statistics j++; double xi = iterator.Current; t += xi; - double diff = j * xi - t; + double diff = (j * xi) - t; variance += (diff * diff) / (j * (j - 1)); } @@ -228,6 +228,7 @@ namespace MathNet.Numerics.Statistics { break; } + if (iterator.Current.HasValue) { j++; @@ -243,7 +244,7 @@ namespace MathNet.Numerics.Statistics j++; double xi = iterator.Current.Value; t += xi; - double diff = j * xi - t; + double diff = (j * xi) - t; variance += (diff * diff) / (j * (j - 1)); } } @@ -263,7 +264,7 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - return System.Math.Sqrt(Variance(data)); + return Math.Sqrt(Variance(data)); } /// @@ -278,7 +279,7 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - return System.Math.Sqrt(Variance(data)); + return Math.Sqrt(Variance(data)); } /// @@ -293,7 +294,7 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - return System.Math.Sqrt(PopulationVariance(data)); + return Math.Sqrt(PopulationVariance(data)); } /// @@ -308,7 +309,7 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - return System.Math.Sqrt(PopulationVariance(data)); + return Math.Sqrt(PopulationVariance(data)); } /// @@ -329,7 +330,7 @@ namespace MathNet.Numerics.Statistics { if (d.HasValue) { - min = System.Math.Min(min, d.Value); + min = Math.Min(min, d.Value); count++; } } @@ -360,7 +361,7 @@ namespace MathNet.Numerics.Statistics { if (d.HasValue) { - max = System.Math.Max(max, d.Value); + max = Math.Max(max, d.Value); count++; } } @@ -389,7 +390,7 @@ namespace MathNet.Numerics.Statistics int count = 0; foreach (double d in data) { - min = System.Math.Min(min, d); + min = Math.Min(min, d); count++; } @@ -417,7 +418,7 @@ namespace MathNet.Numerics.Statistics int count = 0; foreach (double d in data) { - max = System.Math.Max(max, d); + max = Math.Max(max, d); count++; } @@ -441,18 +442,16 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - List dataArray = new List(data); - int index = dataArray.Count/2 + 1; + var dataArray = new List(data); + int index = (dataArray.Count / 2) + 1; if (dataArray.Count % 2 == 0) { double lower = OrderSelect(dataArray, 0, dataArray.Count - 1, index - 1); double upper = OrderSelect(dataArray, 0, dataArray.Count - 1, index); return (lower + upper) / 2.0; } - else - { - return OrderSelect(dataArray, 0, dataArray.Count - 1, index); - } + + return OrderSelect(dataArray, 0, dataArray.Count - 1, index); } /// @@ -467,7 +466,7 @@ namespace MathNet.Numerics.Statistics throw new ArgumentNullException("data"); } - List nonNull = new List(); + var nonNull = new List(); foreach (double? value in data) { if (value.HasValue) @@ -487,7 +486,8 @@ namespace MathNet.Numerics.Statistics /// /// Evaluate the i-order (1..N) statistic of the provided samples. /// - /// The sample data. + /// The sample data. + /// Order of the statistic to evaluate. /// The i'th order statistic in the sample data. public static double OrderStatistic(IEnumerable samples, int order) { @@ -497,7 +497,7 @@ namespace MathNet.Numerics.Statistics return Minimum(samples); } - List list = new List(samples); + var list = new List(samples); if (order < 1 || order > list.Count) { throw new ArgumentOutOfRangeException("order", Resources.ArgumentInIntervalXYInclusive); @@ -521,16 +521,12 @@ namespace MathNet.Numerics.Statistics /// The right bound in which to order select. /// The order we are trying to find. /// The order statistic. - static double OrderSelect(IList samples, int left, int right, int order) + private static double OrderSelect(IList samples, int left, int right, int order) { - // Order most always be positive. - System.Diagnostics.Debug.Assert(order > 0); - // Left side must always be positive and smaller than right side. - System.Diagnostics.Debug.Assert(left >= 0 && left <= right); - // Right side must always be smaller than number of elements in list. - System.Diagnostics.Debug.Assert(right < samples.Count); - // Make sure there are at least order items in the segment [left, right]. - System.Diagnostics.Debug.Assert(right - left + 1 >= order); + System.Diagnostics.Debug.Assert(order > 0, "Order must always be positive."); + System.Diagnostics.Debug.Assert(left >= 0 && left <= right, "Left side must always be positive and smaller than right side."); + System.Diagnostics.Debug.Assert(right < samples.Count, "Right side must always be smaller than number of elements in list."); + System.Diagnostics.Debug.Assert(right - left + 1 >= order, "Make sure there are at least order items in the segment [left, right]."); if (left == right) { @@ -542,29 +538,29 @@ namespace MathNet.Numerics.Statistics // The partioning code. int i = left - 1; - for(int j = left; j <= right - 1; j++) + for (int j = left; j <= right - 1; j++) { - if(samples[j] <= pivot) + if (samples[j] <= pivot) { i++; Sorting.Swap(samples, i, j); } } - Sorting.Swap(samples, i+1, right); + + Sorting.Swap(samples, i + 1, right); // Recursive order finding algorithm. - if(order == (i-left)+2) + if (order == (i - left) + 2) { return pivot; } - else if (order < (i-left)+2) + + if (order < (i - left) + 2) { return OrderSelect(samples, left, i, order); } - else - { - return OrderSelect(samples, i+2, right, order - i + left - 2); - } + + return OrderSelect(samples, i + 2, right, order - i + left - 2); } } -} \ No newline at end of file +}