Browse Source

code style

Signed-off-by: Christoph Ruegg <git@cdrnet.ch>
pull/2/head
Christoph Ruegg 17 years ago
parent
commit
ab1b6a1d39
  1. 16
      src/MathNet.Numerics.4.5.resharper
  2. 200
      src/Numerics/Complex.cs
  3. 6
      src/Numerics/Constants.cs
  4. 5
      src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs
  5. 12
      src/Numerics/NumberTheory/IntegerTheory.cs
  6. 64
      src/Numerics/Sorting.cs
  7. 74
      src/Numerics/Statistics/DescriptiveStatistics.cs
  8. 92
      src/Numerics/Statistics/Statistics.cs

16
src/MathNet.Numerics.4.5.resharper

@ -754,6 +754,8 @@ Bluestein
<FORCE_IFELSE_BRACES_STYLE>ALWAYS_ADD</FORCE_IFELSE_BRACES_STYLE> <FORCE_IFELSE_BRACES_STYLE>ALWAYS_ADD</FORCE_IFELSE_BRACES_STYLE>
<FORCE_USING_BRACES_STYLE>ALWAYS_ADD</FORCE_USING_BRACES_STYLE> <FORCE_USING_BRACES_STYLE>ALWAYS_ADD</FORCE_USING_BRACES_STYLE>
<FORCE_WHILE_BRACES_STYLE>ALWAYS_ADD</FORCE_WHILE_BRACES_STYLE> <FORCE_WHILE_BRACES_STYLE>ALWAYS_ADD</FORCE_WHILE_BRACES_STYLE>
<INDENT_NESTED_FIXED_STMT>True</INDENT_NESTED_FIXED_STMT>
<INDENT_NESTED_USINGS_STMT>True</INDENT_NESTED_USINGS_STMT>
<INDENT_SIZE>4</INDENT_SIZE> <INDENT_SIZE>4</INDENT_SIZE>
<INITIALIZER_BRACES>NEXT_LINE</INITIALIZER_BRACES> <INITIALIZER_BRACES>NEXT_LINE</INITIALIZER_BRACES>
<MODIFIERS_ORDER IsNull="False"> <MODIFIERS_ORDER IsNull="False">
@ -773,8 +775,20 @@ Bluestein
<Item>volatile</Item> <Item>volatile</Item>
</MODIFIERS_ORDER> </MODIFIERS_ORDER>
<PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE>False</PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE> <PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE>False</PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE>
<PLACE_TYPE_CONSTRAINTS_ON_SAME_LINE>False</PLACE_TYPE_CONSTRAINTS_ON_SAME_LINE>
<PLACE_WHILE_ON_NEW_LINE>True</PLACE_WHILE_ON_NEW_LINE>
<SPACE_AFTER_TYPECAST_PARENTHESES>False</SPACE_AFTER_TYPECAST_PARENTHESES>
<SPACE_AROUND_MULTIPLICATIVE_OP>True</SPACE_AROUND_MULTIPLICATIVE_OP> <SPACE_AROUND_MULTIPLICATIVE_OP>True</SPACE_AROUND_MULTIPLICATIVE_OP>
<SPACE_BEFORE_SIZEOF_PARENTHESES>False</SPACE_BEFORE_SIZEOF_PARENTHESES>
<SPACE_BEFORE_TYPEOF_PARENTHESES>False</SPACE_BEFORE_TYPEOF_PARENTHESES>
<SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES>True</SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES> <SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES>True</SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES>
<WRAP_AFTER_DECLARATION_LPAR>True</WRAP_AFTER_DECLARATION_LPAR>
<WRAP_AFTER_INVOCATION_LPAR>True</WRAP_AFTER_INVOCATION_LPAR>
<WRAP_ARGUMENTS_STYLE>CHOP_IF_LONG</WRAP_ARGUMENTS_STYLE>
<WRAP_BEFORE_BINARY_OPSIGN>True</WRAP_BEFORE_BINARY_OPSIGN>
<WRAP_BEFORE_FIRST_TYPE_PARAMETER_CONSTRAINT>True</WRAP_BEFORE_FIRST_TYPE_PARAMETER_CONSTRAINT>
<WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE>CHOP_ALWAYS</WRAP_OBJECT_AND_COLLECTION_INITIALIZER_STYLE>
<WRAP_PARAMETERS_STYLE>CHOP_IF_LONG</WRAP_PARAMETERS_STYLE>
</FormatSettings> </FormatSettings>
<UsingsSettings /> <UsingsSettings />
<Naming2> <Naming2>
@ -813,7 +827,7 @@ Bluestein
<PredefinedRule Inspect="True" Prefix="T" Suffix="" Style="AaBb" ElementKind="TypeParameters" /> <PredefinedRule Inspect="True" Prefix="T" Suffix="" Style="AaBb" ElementKind="TypeParameters" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="MethodPropertyEvent" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="MethodPropertyEvent" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Locals" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Locals" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="LocalConstants" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="LocalConstants" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Parameters" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="aaBb" ElementKind="Parameters" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PublicFields" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="PublicFields" />
<PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Constants" /> <PredefinedRule Inspect="True" Prefix="" Suffix="" Style="AaBb" ElementKind="Constants" />

200
src/Numerics/Complex.cs

@ -1,7 +1,9 @@
// <copyright file="Complex.cs" company="Math.NET"> // <copyright file="Complex.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project // Math.NET Numerics, part of the Math.NET Project
// http://mathnet.opensourcedotnet.info // http://mathnet.opensourcedotnet.info
//
// Copyright (c) 2009 Math.NET // Copyright (c) 2009 Math.NET
//
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation // obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without // files (the "Software"), to deal in the Software without
@ -10,8 +12,10 @@
// copies of the Software, and to permit persons to whom the // copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following // Software is furnished to do so, subject to the following
// conditions: // conditions:
//
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@ -28,8 +32,7 @@ namespace MathNet.Numerics
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Properties;
using MathNet.Numerics.Properties;
/// <summary> /// <summary>
/// Complex numbers class. /// Complex numbers class.
@ -40,7 +43,7 @@ namespace MathNet.Numerics
/// on complex numbers. All the operators <c>+</c>, <c>-</c>, /// on complex numbers. All the operators <c>+</c>, <c>-</c>,
/// <c>*</c>, <c>/</c>, <c>==</c>, <c>!=</c> are defined in the /// <c>*</c>, <c>/</c>, <c>==</c>, <c>!=</c> are defined in the
/// canonical way. Additional complex trigonometric functions such /// canonical way. Additional complex trigonometric functions such
/// as <see cref="Complex.Cosine"/>, ... /// as <see cref="Trig.Cosine(Complex)"/>, ...
/// are also provided. Note that the <c>Complex</c> structures /// are also provided. Note that the <c>Complex</c> structures
/// has two special constant values <see cref="Complex.NaN"/> and /// has two special constant values <see cref="Complex.NaN"/> and
/// <see cref="Complex.Infinity"/>. /// <see cref="Complex.Infinity"/>.
@ -73,35 +76,35 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Regular expression used to parse strings into complex numbers. /// Regular expression used to parse strings into complex numbers.
/// </summary> /// </summary>
private static readonly Regex ParseExpression = private static readonly Regex _parseExpression =
new Regex( new Regex(
@"^((?<r>(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?<i>(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?<r>(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?<i>(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$", @"^((?<r>(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?<i>(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?<r>(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?<i>(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
/// <summary> /// <summary>
/// Represents imaginary unit number. /// Represents imaginary unit number.
/// </summary> /// </summary>
private static readonly Complex i = new Complex(0, 1); private static readonly Complex _i = new Complex(0, 1);
/// <summary> /// <summary>
/// Represents a infinite complex number /// Represents a infinite complex number
/// </summary> /// </summary>
private static readonly Complex infinity = new Complex(double.PositiveInfinity, double.PositiveInfinity); private static readonly Complex _infinity = new Complex(double.PositiveInfinity, double.PositiveInfinity);
/// <summary> /// <summary>
/// Represents not-a-number. /// Represents not-a-number.
/// </summary> /// </summary>
private static readonly Complex nan = new Complex(Double.NaN, Double.NaN); private static readonly Complex _nan = new Complex(Double.NaN, Double.NaN);
/// <summary> /// <summary>
/// Representing the one value. /// Representing the one value.
/// </summary> /// </summary>
private static readonly Complex one = new Complex(1.0, 0.0); private static readonly Complex _one = new Complex(1.0, 0.0);
/// <summary> /// <summary>
/// Representing the zero value. /// Representing the zero value.
/// </summary> /// </summary>
private static readonly Complex zero = new Complex(0.0, 0.0); private static readonly Complex _zero = new Complex(0.0, 0.0);
/// <summary> /// <summary>
/// The real component of the complex number. /// The real component of the complex number.
@ -129,8 +132,8 @@ namespace MathNet.Numerics
/// </param> /// </param>
public Complex(double real, double imaginary) public Complex(double real, double imaginary)
{ {
this._real = real; _real = real;
this._imag = imaginary; _imag = imaginary;
} }
#endregion #endregion
@ -152,10 +155,7 @@ namespace MathNet.Numerics
/// <value>A value representing the infinity value.</value> /// <value>A value representing the infinity value.</value>
public static Complex Infinity public static Complex Infinity
{ {
get get { return _infinity; }
{
return infinity;
}
} }
/// <summary> /// <summary>
@ -164,10 +164,7 @@ namespace MathNet.Numerics
/// <value>A value representing not-a-number.</value> /// <value>A value representing not-a-number.</value>
public static Complex NaN public static Complex NaN
{ {
get get { return _nan; }
{
return nan;
}
} }
/// <summary> /// <summary>
@ -176,10 +173,7 @@ namespace MathNet.Numerics
/// <value>A value representing the imaginary unit number.</value> /// <value>A value representing the imaginary unit number.</value>
public static Complex I public static Complex I
{ {
get get { return _i; }
{
return i;
}
} }
/// <summary> /// <summary>
@ -188,10 +182,7 @@ namespace MathNet.Numerics
/// <value>A value representing the zero value.</value> /// <value>A value representing the zero value.</value>
public static Complex Zero public static Complex Zero
{ {
get get { return new Complex(0.0, 0.0); }
{
return new Complex(0.0, 0.0);
}
} }
/// <summary> /// <summary>
@ -200,10 +191,7 @@ namespace MathNet.Numerics
/// <value>A value representing the <c>1</c> value.</value> /// <value>A value representing the <c>1</c> value.</value>
public static Complex One public static Complex One
{ {
get get { return _one; }
{
return one;
}
} }
#endregion Properties #endregion Properties
@ -214,10 +202,7 @@ namespace MathNet.Numerics
/// <value>The real component of the complex number.</value> /// <value>The real component of the complex number.</value>
public double Real public double Real
{ {
get get { return _real; }
{
return this._real;
}
} }
/// <summary> /// <summary>
@ -226,10 +211,7 @@ namespace MathNet.Numerics
/// <value>The real imaginary component of the complex number.</value> /// <value>The real imaginary component of the complex number.</value>
public double Imaginary public double Imaginary
{ {
get get { return _imag; }
{
return this._imag;
}
} }
/// <summary> /// <summary>
@ -238,10 +220,7 @@ namespace MathNet.Numerics
/// <value><c>true</c> if this instance is zero; otherwise, <c>false</c>.</value> /// <value><c>true</c> if this instance is zero; otherwise, <c>false</c>.</value>
public bool IsZero public bool IsZero
{ {
get get { return _real.AlmostZero() && _imag.AlmostZero(); }
{
return this._real.AlmostZero() && this._imag.AlmostZero();
}
} }
/// <summary> /// <summary>
@ -250,10 +229,7 @@ namespace MathNet.Numerics
/// <value><c>true</c> if this instance is one; otherwise, <c>false</c>.</value> /// <value><c>true</c> if this instance is one; otherwise, <c>false</c>.</value>
public bool IsOne public bool IsOne
{ {
get get { return _real.AlmostEqual(1.0) && _imag.AlmostZero(); }
{
return this._real.AlmostEqual(1.0) && this._imag.AlmostZero();
}
} }
/// <summary> /// <summary>
@ -262,10 +238,7 @@ namespace MathNet.Numerics
/// <value><c>true</c> if this instance is I; otherwise, <c>false</c>.</value> /// <value><c>true</c> if this instance is I; otherwise, <c>false</c>.</value>
public bool IsI public bool IsI
{ {
get get { return _real.AlmostZero() && _imag.AlmostEqual(1.0); }
{
return this._real.AlmostZero() && this._imag.AlmostEqual(1.0);
}
} }
/// <summary> /// <summary>
@ -275,10 +248,7 @@ namespace MathNet.Numerics
/// <value><c>true</c> if this instance is NaN; otherwise, <c>false</c>.</value> /// <value><c>true</c> if this instance is NaN; otherwise, <c>false</c>.</value>
public bool IsNaN public bool IsNaN
{ {
get get { return double.IsNaN(_real) || double.IsNaN(_imag); }
{
return double.IsNaN(this._real) || double.IsNaN(this._imag);
}
} }
/// <summary> /// <summary>
@ -294,10 +264,7 @@ namespace MathNet.Numerics
/// </remarks> /// </remarks>
public bool IsInfinity public bool IsInfinity
{ {
get get { return double.IsInfinity(_real) || double.IsInfinity(_imag); }
{
return double.IsInfinity(this._real) || double.IsInfinity(this._imag);
}
} }
/// <summary> /// <summary>
@ -306,10 +273,7 @@ namespace MathNet.Numerics
/// <value><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</value> /// <value><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</value>
public bool IsReal public bool IsReal
{ {
get get { return _imag.AlmostZero(); }
{
return this._imag.AlmostZero();
}
} }
/// <summary> /// <summary>
@ -320,10 +284,7 @@ namespace MathNet.Numerics
/// </value> /// </value>
public bool IsRealNonNegative public bool IsRealNonNegative
{ {
get get { return _imag.AlmostZero() && _real >= 0; }
{
return this._imag.AlmostZero() && this._real >= 0;
}
} }
/// <summary> /// <summary>
@ -343,10 +304,7 @@ namespace MathNet.Numerics
/// </remarks> /// </remarks>
public Complex Conjugate public Complex Conjugate
{ {
get get { return new Complex(_real, -_imag); }
{
return new Complex(this._real, -this._imag);
}
} }
/// <summary> /// <summary>
@ -355,10 +313,7 @@ namespace MathNet.Numerics
/// <seealso cref="Argument"/> /// <seealso cref="Argument"/>
public double Modulus public double Modulus
{ {
get get { return Math.Sqrt((_real * _real) + (_imag * _imag)); }
{
return Math.Sqrt((this._real * this._real) + (this._imag * this._imag));
}
} }
/// <summary> /// <summary>
@ -367,10 +322,7 @@ namespace MathNet.Numerics
/// <seealso cref="Argument"/> /// <seealso cref="Argument"/>
public double ModulusSquared public double ModulusSquared
{ {
get get { return (_real * _real) + (_imag * _imag); }
{
return (this._real * this._real) + (this._imag * this._imag);
}
} }
/// <summary> /// <summary>
@ -379,18 +331,18 @@ namespace MathNet.Numerics
/// <remarks> /// <remarks>
/// Argument always returns a value bigger than negative Pi and /// Argument always returns a value bigger than negative Pi and
/// smaller or equal to Pi. If this <c>Complex</c> is zero, the Complex /// smaller or equal to Pi. If this <c>Complex</c> 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.
/// </remarks> /// </remarks>
public double Argument public double Argument
{ {
get get
{ {
if (this.IsReal && this._real < 0) if (IsReal && _real < 0)
{ {
return Math.PI; 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 get
{ {
if (double.IsPositiveInfinity(this._real) && double.IsPositiveInfinity(this._imag)) if (double.IsPositiveInfinity(_real) && double.IsPositiveInfinity(_imag))
{ {
return new Complex(Constants.Sqrt1Over2, Constants.Sqrt1Over2); 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); 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); 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); return new Complex(-Constants.Sqrt1Over2, Constants.Sqrt1Over2);
} }
// don't replace this with "Modulus"! // don't replace this with "Modulus"!
var mod = SpecialFunctions.Hypotenuse(this._real, this._imag); var mod = SpecialFunctions.Hypotenuse(_real, _imag);
if (mod.AlmostZero()) if (mod.AlmostZero())
{ {
return Zero; 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
/// <summary> /// <summary>
/// Raise this <c>Complex</c> to the inverse of the given value. /// Raise this <c>Complex</c> to the inverse of the given value.
/// </summary> /// </summary>
/// <param name="rootexponent"> /// <param name="rootExponent">
/// The root exponent. /// The root exponent.
/// </param> /// </param>
/// <returns> /// <returns>
/// The complex raised to the inverse of the given exponent. /// The complex raised to the inverse of the given exponent.
/// </returns> /// </returns>
public Complex Root(Complex rootexponent) public Complex Root(Complex rootExponent)
{ {
return Power(1 / rootexponent); return Power(1 / rootExponent);
} }
/// <summary> /// <summary>
@ -638,7 +590,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public override string ToString() public override string ToString()
{ {
return this.ToString(null, null); return ToString(null, null);
} }
/// <summary> /// <summary>
@ -653,7 +605,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public string ToString(string format) public string ToString(string format)
{ {
return this.ToString(format, null); return ToString(format, null);
} }
/// <summary> /// <summary>
@ -664,11 +616,11 @@ namespace MathNet.Numerics
/// format provider. /// format provider.
/// </returns> /// </returns>
/// <param name="formatProvider"> /// <param name="formatProvider">
/// An IFormatProvider that supplies culture-specific formatting information. /// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
/// </param> /// </param>
public string ToString(IFormatProvider formatProvider) public string ToString(IFormatProvider formatProvider)
{ {
return this.ToString(null, formatProvider); return ToString(null, formatProvider);
} }
/// <summary> /// <summary>
@ -688,32 +640,32 @@ namespace MathNet.Numerics
/// A format specification. /// A format specification.
/// </param> /// </param>
/// <param name="formatProvider"> /// <param name="formatProvider">
/// An IFormatProvider that supplies culture-specific formatting information. /// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
/// </param> /// </param>
public string ToString(string format, IFormatProvider formatProvider) public string ToString(string format, IFormatProvider formatProvider)
{ {
if (this.IsNaN) if (IsNaN)
{ {
return "NaN"; return "NaN";
} }
if (this.IsInfinity) if (IsInfinity)
{ {
return "Infinity"; return "Infinity";
} }
var ret = new StringBuilder(); 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(" "); 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(); return ret.ToString();
@ -738,25 +690,25 @@ namespace MathNet.Numerics
/// corresponding real and imaginary components are equal. /// corresponding real and imaginary components are equal.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Returns true if the two objects are the same object, or if their corresponding /// Returns <c>true</c> if the two objects are the same object, or if their corresponding
/// real and imaginary components are equal, false otherwise. /// real and imaginary components are equal, <c>false</c> otherwise.
/// </returns> /// </returns>
/// <param name="other"> /// <param name="other">
/// The complex number to compare to with. /// The complex number to compare to with.
/// </param> /// </param>
public bool Equals(Complex other) public bool Equals(Complex other)
{ {
if (this.IsNaN || other.IsNaN) if (IsNaN || other.IsNaN)
{ {
return false; return false;
} }
if (this.IsInfinity && other.IsInfinity) if (IsInfinity && other.IsInfinity)
{ {
return true; return true;
} }
return this._real.AlmostEqual(other._real) && this._imag.AlmostEqual(other._imag); return _real.AlmostEqual(other._real) && _imag.AlmostEqual(other._imag);
} }
/// <summary> /// <summary>
@ -771,7 +723,7 @@ namespace MathNet.Numerics
/// </remarks> /// </remarks>
public override int GetHashCode() public override int GetHashCode()
{ {
return this._real.GetHashCode() ^ (-this._imag.GetHashCode()); return _real.GetHashCode() ^ (-_imag.GetHashCode());
} }
/// <summary> /// <summary>
@ -779,15 +731,15 @@ namespace MathNet.Numerics
/// corresponding real and imaginary components are equal. /// corresponding real and imaginary components are equal.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Returns true if the two objects are the same object, or if their corresponding /// Returns <c>true</c> if the two objects are the same object, or if their corresponding
/// real and imaginary components are equal, false otherwise. /// real and imaginary components are equal, <c>false</c> otherwise.
/// </returns> /// </returns>
/// <param name="obj"> /// <param name="obj">
/// The complex number to compare to with. /// The complex number to compare to with.
/// </param> /// </param>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return (obj is Complex) && this.Equals((Complex)obj); return (obj is Complex) && Equals((Complex)obj);
} }
#endregion #endregion
@ -799,7 +751,7 @@ namespace MathNet.Numerics
/// </summary> /// </summary>
/// <param name="complex1">One of complex numbers to compare.</param> /// <param name="complex1">One of complex numbers to compare.</param>
/// <param name="complex2">The other complex numbers to compare.</param> /// <param name="complex2">The other complex numbers to compare.</param>
/// <returns>true if the real and imaginary components of the two complex numbers are equal; false otherwise.</returns> /// <returns><c>true</c> if the real and imaginary components of the two complex numbers are equal; <c>false</c> otherwise.</returns>
public static bool operator ==(Complex complex1, Complex complex2) public static bool operator ==(Complex complex1, Complex complex2)
{ {
return complex1.Equals(complex2); return complex1.Equals(complex2);
@ -810,7 +762,7 @@ namespace MathNet.Numerics
/// </summary> /// </summary>
/// <param name="complex1">One of complex numbers to compare.</param> /// <param name="complex1">One of complex numbers to compare.</param>
/// <param name="complex2">The other complex numbers to compare.</param> /// <param name="complex2">The other complex numbers to compare.</param>
/// <returns>true if the real or imaginary components of the two complex numbers are not equal; false otherwise.</returns> /// <returns><c>true</c> if the real or imaginary components of the two complex numbers are not equal; <c>false</c> otherwise.</returns>
public static bool operator !=(Complex complex1, Complex complex2) public static bool operator !=(Complex complex1, Complex complex2)
{ {
return !complex1.Equals(complex2); return !complex1.Equals(complex2);
@ -897,7 +849,7 @@ namespace MathNet.Numerics
public static Complex operator *(Complex multiplicand, Complex multiplier) public static Complex operator *(Complex multiplicand, Complex multiplier)
{ {
return new Complex( 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)); (multiplicand._real * multiplier._imag) + (multiplicand._imag * multiplier._real));
} }
@ -932,7 +884,7 @@ namespace MathNet.Numerics
var modSquared = divisor.ModulusSquared; var modSquared = divisor.ModulusSquared;
return new Complex( 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); ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared);
} }
@ -1116,7 +1068,7 @@ namespace MathNet.Numerics
/// the string to parse. /// the string to parse.
/// </param> /// </param>
/// <param name="formatProvider"> /// <param name="formatProvider">
/// An IFormatProvider that supplies culture-specific formatting information. /// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.
/// </param> /// </param>
public static Complex Parse(string value, IFormatProvider formatProvider) public static Complex Parse(string value, IFormatProvider formatProvider)
{ {
@ -1178,7 +1130,7 @@ namespace MathNet.Numerics
} }
else else
{ {
var matchResult = ParseExpression.Match(value); var matchResult = _parseExpression.Match(value);
if (matchResult.Success) if (matchResult.Success)
{ {
var realStr = matchResult.Groups["r"].Value; var realStr = matchResult.Groups["r"].Value;
@ -1241,7 +1193,7 @@ namespace MathNet.Numerics
/// A string containing a complex number to convert. /// A string containing a complex number to convert.
/// </param> /// </param>
/// <param name="formatProvider"> /// <param name="formatProvider">
/// An IFormatProvider that supplies culture-specific formatting information about value. /// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information about value.
/// </param> /// </param>
/// <param name="result"> /// <param name="result">
/// The parsed value. /// The parsed value.
@ -1260,12 +1212,12 @@ namespace MathNet.Numerics
} }
catch (ArgumentNullException) catch (ArgumentNullException)
{ {
result = zero; result = _zero;
ret = false; ret = false;
} }
catch (FormatException) catch (FormatException)
{ {
result = zero; result = _zero;
ret = false; ret = false;
} }
@ -1274,4 +1226,4 @@ namespace MathNet.Numerics
#endregion #endregion
} }
} }

6
src/Numerics/Constants.cs

@ -130,15 +130,9 @@ namespace MathNet.Numerics
public const double Grad = 0.015707963267948966192313216916397514420985846996876d; public const double Grad = 0.015707963267948966192313216916397514420985846996876d;
/// <summary>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).</summary> /// <summary>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).</summary>
/// <seealso cref="Ratios.RatioToPowerDecibel(double)"/>
/// <seealso cref="Ratios.PowerDecibelToRatio"/>
/// <seealso cref="Ratios.PowerDecibelToValue"/>
public const double PowerDecibel = 0.11512925464970228420089957273421821038005507443144d; public const double PowerDecibel = 0.11512925464970228420089957273421821038005507443144d;
/// <summary>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.</summary> /// <summary>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.</summary>
/// <seealso cref="Ratios.RatioToDecibel(double)"/>
/// <seealso cref="Ratios.DecibelToRatio"/>
/// <seealso cref="Ratios.DecibelToValue"/>
public const double NeutralDecibel = 0.23025850929940456840179914546843642076011014886288d; public const double NeutralDecibel = 0.23025850929940456840179914546843642076011014886288d;
/// <summary>The Catalan constant</summary> /// <summary>The Catalan constant</summary>

5
src/Numerics/IntegralTransforms/Algorithms/DiscreteFourierTransform.RadixN.cs

@ -61,7 +61,8 @@ namespace MathNet.Numerics.IntegralTransforms.Algorithms
{ {
m >>= 1; m >>= 1;
j ^= m; j ^= m;
} while ((j & m) == 0); }
while ((j & m) == 0);
} }
} }
@ -159,4 +160,4 @@ namespace MathNet.Numerics.IntegralTransforms.Algorithms
InverseScaleByOptions(options, samples); InverseScaleByOptions(options, samples);
} }
} }
} }

12
src/Numerics/NumberTheory/IntegerTheory.cs

@ -104,13 +104,13 @@ namespace MathNet.Numerics.NumberTheory
/// <exception cref="ArgumentOutOfRangeException"/> /// <exception cref="ArgumentOutOfRangeException"/>
public static int CeilingToPowerOfTwo(this int number) public static int CeilingToPowerOfTwo(this int number)
{ {
if(number == Int32.MinValue) if (number == Int32.MinValue)
{ {
return 0; return 0;
} }
const int maxPowerOfTwo = 0x40000000; const int MaxPowerOfTwo = 0x40000000;
if(number > maxPowerOfTwo) if (number > MaxPowerOfTwo)
{ {
throw new ArgumentOutOfRangeException("number"); throw new ArgumentOutOfRangeException("number");
} }
@ -138,8 +138,8 @@ namespace MathNet.Numerics.NumberTheory
return 0; return 0;
} }
const long maxPowerOfTwo = 0x4000000000000000; const long MaxPowerOfTwo = 0x4000000000000000;
if (number > maxPowerOfTwo) if (number > MaxPowerOfTwo)
{ {
throw new ArgumentOutOfRangeException("number"); throw new ArgumentOutOfRangeException("number");
} }
@ -169,7 +169,7 @@ namespace MathNet.Numerics.NumberTheory
return 1 << exponent; return 1 << exponent;
} }
/// <summary> /// <summary>
/// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 63). /// Raises 2 to the provided integer exponent (0 &lt;= exponent &lt; 63).
/// </summary> /// </summary>

64
src/Numerics/Sorting.cs

@ -37,7 +37,7 @@ namespace MathNet.Numerics
public static class Sorting public static class Sorting
{ {
/// <summary> /// <summary>
/// Sort a list of keys, inplace using the quick sort algorithm. /// Sort a list of keys, in place using the quick sort algorithm.
/// </summary> /// </summary>
/// <typeparam name="T">The type of elements stored in the list.</typeparam> /// <typeparam name="T">The type of elements stored in the list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
@ -47,33 +47,33 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="TKey">The type of elements stored in the key list.</typeparam> /// <typeparam name="TKey">The type of elements stored in the key list.</typeparam>
/// <typeparam name="TItem">The type of elements stored in the item list.</typeparam> /// <typeparam name="TItem">The type of elements stored in the item list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
/// <param name="items">List to permutate the same way as the key list.</param> /// <param name="items">List to permute the same way as the key list.</param>
public static void Sort<TKey, TItem>(IList<TKey> keys, IList<TItem> items) public static void Sort<TKey, TItem>(IList<TKey> keys, IList<TItem> items)
{ {
Sort(keys, items, Comparer<TKey>.Default); Sort(keys, items, Comparer<TKey>.Default);
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="TKey">The type of elements stored in the key list.</typeparam> /// <typeparam name="TKey">The type of elements stored in the key list.</typeparam>
/// <typeparam name="TItem1">The type of elements stored in the first item list.</typeparam> /// <typeparam name="TItem1">The type of elements stored in the first item list.</typeparam>
/// <typeparam name="TItem2">The type of elements stored in the second item list.</typeparam> /// <typeparam name="TItem2">The type of elements stored in the second item list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
/// <param name="items1">First list to permutate the same way as the key list.</param> /// <param name="items1">First list to permute the same way as the key list.</param>
/// <param name="items2">Second list to permutate the same way as the key list.</param> /// <param name="items2">Second list to permute the same way as the key list.</param>
public static void Sort<TKey, TItem1, TItem2>(IList<TKey> keys, IList<TItem1> items1, IList<TItem2> items2) public static void Sort<TKey, TItem1, TItem2>(IList<TKey> keys, IList<TItem1> items1, IList<TItem2> items2)
{ {
Sort(keys, items1, items2, Comparer<TKey>.Default); Sort(keys, items1, items2, Comparer<TKey>.Default);
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="T">The type of elements in the key list.</typeparam> /// <typeparam name="T">The type of elements in the key list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
@ -85,7 +85,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="T">The type of elements in the key list.</typeparam> /// <typeparam name="T">The type of elements in the key list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
@ -139,12 +139,12 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="TKey">The type of elements in the key list.</typeparam> /// <typeparam name="TKey">The type of elements in the key list.</typeparam>
/// <typeparam name="TItem">The type of elements in the item list.</typeparam> /// <typeparam name="TItem">The type of elements in the item list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
/// <param name="items">List to permutate the same way as the key list.</param> /// <param name="items">List to permute the same way as the key list.</param>
/// <param name="comparer">Comparison, defining the sort order.</param> /// <param name="comparer">Comparison, defining the sort order.</param>
public static void Sort<TKey, TItem>(IList<TKey> keys, IList<TItem> items, IComparer<TKey> comparer) public static void Sort<TKey, TItem>(IList<TKey> keys, IList<TItem> items, IComparer<TKey> comparer)
{ {
@ -177,14 +177,14 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="TKey">The type of elements in the key list.</typeparam> /// <typeparam name="TKey">The type of elements in the key list.</typeparam>
/// <typeparam name="TItem1">The type of elements in the first item list.</typeparam> /// <typeparam name="TItem1">The type of elements in the first item list.</typeparam>
/// <typeparam name="TItem2">The type of elements in the second item list.</typeparam> /// <typeparam name="TItem2">The type of elements in the second item list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
/// <param name="items1">First list to permutate the same way as the key list.</param> /// <param name="items1">First list to permute the same way as the key list.</param>
/// <param name="items2">Second list to permutate the same way as the key list.</param> /// <param name="items2">Second list to permute the same way as the key list.</param>
/// <param name="comparer">Comparison, defining the sort order.</param> /// <param name="comparer">Comparison, defining the sort order.</param>
public static void Sort<TKey, TItem1, TItem2>( public static void Sort<TKey, TItem1, TItem2>(
IList<TKey> keys, IList<TItem1> items1, IList<TItem2> items2, IComparer<TKey> comparer) IList<TKey> keys, IList<TItem1> items1, IList<TItem2> items2, IComparer<TKey> comparer)
@ -214,7 +214,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="T">The type of element in the list.</typeparam> /// <typeparam name="T">The type of element in the list.</typeparam>
/// <param name="keys">List to sort.</param> /// <param name="keys">List to sort.</param>
@ -280,7 +280,7 @@ namespace MathNet.Numerics
} }
/// <summary> /// <summary>
/// Recursive implementation for an inplace quick sort on a list. /// Recursive implementation for an in place quick sort on a list.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam> /// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam>
/// <param name="keys">The list which is sorted using quick sort.</param> /// <param name="keys">The list which is sorted using quick sort.</param>
@ -342,7 +342,8 @@ namespace MathNet.Numerics
a++; a++;
b--; b--;
} while (a <= b); }
while (a <= b);
// In order to limit the recusion depth to log(n), we sort the // In order to limit the recusion depth to log(n), we sort the
// shorter partition recusively and the longer partition iteratively. // shorter partition recusively and the longer partition iteratively.
@ -364,11 +365,12 @@ namespace MathNet.Numerics
right = b; right = b;
} }
} while (left < right); }
while (left < right);
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam> /// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam>
/// <typeparam name="TItems">The type of the list which is automatically reordered accordingly.</typeparam> /// <typeparam name="TItems">The type of the list which is automatically reordered accordingly.</typeparam>
@ -437,7 +439,8 @@ namespace MathNet.Numerics
a++; a++;
b--; b--;
} while (a <= b); }
while (a <= b);
// In order to limit the recusion depth to log(n), we sort the // In order to limit the recusion depth to log(n), we sort the
// shorter partition recusively and the longer partition iteratively. // shorter partition recusively and the longer partition iteratively.
@ -459,11 +462,12 @@ namespace MathNet.Numerics
right = b; right = b;
} }
} while (left < right); }
while (left < right);
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam> /// <typeparam name="T">The type of the list on which the quick sort is performed.</typeparam>
/// <typeparam name="TItems1">The type of the first list which is automatically reordered accordingly.</typeparam> /// <typeparam name="TItems1">The type of the first list which is automatically reordered accordingly.</typeparam>
@ -539,7 +543,8 @@ namespace MathNet.Numerics
a++; a++;
b--; b--;
} while (a <= b); }
while (a <= b);
// In order to limit the recusion depth to log(n), we sort the // In order to limit the recusion depth to log(n), we sort the
// shorter partition recusively and the longer partition iteratively. // shorter partition recusively and the longer partition iteratively.
@ -561,7 +566,8 @@ namespace MathNet.Numerics
right = b; right = b;
} }
} while (left < right); }
while (left < right);
} }
/// <summary> /// <summary>
@ -573,12 +579,14 @@ namespace MathNet.Numerics
/// <param name="b">The index of the second element of the swap.</param> /// <param name="b">The index of the second element of the swap.</param>
internal static void Swap<T>(IList<T> keys, int a, int b) internal static void Swap<T>(IList<T> keys, int a, int b)
{ {
if (a != b) if (a == b)
{ {
T local = keys[a]; return;
keys[a] = keys[b];
keys[b] = local;
} }
T local = keys[a];
keys[a] = keys[b];
keys[b] = local;
} }
} }
} }

74
src/Numerics/Statistics/DescriptiveStatistics.cs

@ -58,10 +58,14 @@ namespace MathNet.Numerics.Statistics
/// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class. /// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <param name="increasedAccuracy">if set to <c>true</c>, increased accuracy mode used. /// <param name="increasedAccuracy">
/// Increased accuracy mode uses <see cref="decimal"/> types for internal calculations.</param> /// If set to <c>true</c>, increased accuracy mode used.
/// <remarks>Don't use increased accuracy for data sets containing large values (in absolute value). /// Increased accuracy mode uses <see cref="decimal"/> types for internal calculations.
/// This may cause the calculations to overflow.</remarks> /// </param>
/// <remarks>
/// Don't use increased accuracy for data sets containing large values (in absolute value).
/// This may cause the calculations to overflow.
/// </remarks>
public DescriptiveStatistics(IEnumerable<double> data, bool increasedAccuracy) public DescriptiveStatistics(IEnumerable<double> data, bool increasedAccuracy)
{ {
if (increasedAccuracy) if (increasedAccuracy)
@ -82,10 +86,14 @@ namespace MathNet.Numerics.Statistics
/// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class. /// Initializes a new instance of the <see cref="DescriptiveStatistics"/> class.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="data">The sample data.</param>
/// <param name="increasedAccuracy">if set to <c>true</c>, increased accuracy mode used. /// <param name="increasedAccuracy">
/// Increased accuracy mode uses <see cref="decimal"/> types for internal calculations.</param> /// If set to <c>true</c>, increased accuracy mode used.
/// <remarks>Don't use increased accuracy for data sets containing large values (in absolute value). /// Increased accuracy mode uses <see cref="decimal"/> types for internal calculations.
/// This may cause the calculations to overflow.</remarks> /// </param>
/// <remarks>
/// Don't use increased accuracy for data sets containing large values (in absolute value).
/// This may cause the calculations to overflow.
/// </remarks>
public DescriptiveStatistics(IEnumerable<double?> data, bool increasedAccuracy) public DescriptiveStatistics(IEnumerable<double?> data, bool increasedAccuracy)
{ {
if (increasedAccuracy) if (increasedAccuracy)
@ -174,7 +182,7 @@ namespace MathNet.Numerics.Statistics
{ {
double diff = xi - Mean; double diff = xi - Mean;
correction += diff; correction += diff;
double tmp = diff*diff; double tmp = diff * diff;
variance += tmp; variance += tmp;
tmp *= diff; tmp *= diff;
skewness += tmp; skewness += tmp;
@ -184,18 +192,21 @@ namespace MathNet.Numerics.Statistics
} }
Count = n; Count = n;
Variance = (variance - (correction*correction)/n)/(n - 1); Variance = (variance - (correction * correction / n)) / (n - 1);
StandardDeviation = System.Math.Sqrt(Variance); StandardDeviation = System.Math.Sqrt(Variance);
if (Variance != 0) if (Variance != 0)
{ {
if (n > 2) 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) 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) if (xi.HasValue)
{ {
double diff = xi.Value - Mean; double diff = xi.Value - Mean;
double tmp = diff*diff; double tmp = diff * diff;
correction += diff; correction += diff;
variance += tmp; variance += tmp;
tmp *= diff; tmp *= diff;
@ -231,18 +242,21 @@ namespace MathNet.Numerics.Statistics
Count = n; Count = n;
if (n > 0) if (n > 0)
{ {
Variance = (variance - (correction*correction)/n)/(n - 1); Variance = (variance - (correction * correction / n)) / (n - 1);
StandardDeviation = System.Math.Sqrt(Variance); StandardDeviation = System.Math.Sqrt(Variance);
if (Variance != 0) if (Variance != 0)
{ {
if (n > 2) 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) 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<double> data) private void ComputeHA(IEnumerable<double> data)
{ {
Mean = data.Mean(); Mean = data.Mean();
decimal mean = (decimal) Mean; decimal mean = (decimal)Mean;
decimal variance = 0; decimal variance = 0;
decimal correction = 0; decimal correction = 0;
decimal skewness = 0; decimal skewness = 0;
@ -264,7 +278,7 @@ namespace MathNet.Numerics.Statistics
foreach (decimal xi in data) foreach (decimal xi in data)
{ {
decimal diff = xi - mean; decimal diff = xi - mean;
decimal tmp = diff*diff; decimal tmp = diff * diff;
correction += diff; correction += diff;
variance += tmp; variance += tmp;
tmp *= diff; tmp *= diff;
@ -275,18 +289,21 @@ namespace MathNet.Numerics.Statistics
} }
Count = n; Count = n;
Variance = (double) (variance - (correction*correction)/n)/(n - 1); Variance = (double)(variance - (correction * correction / n)) / (n - 1);
StandardDeviation = System.Math.Sqrt(Variance); StandardDeviation = System.Math.Sqrt(Variance);
if (Variance != 0) if (Variance != 0)
{ {
if (n > 2) 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) 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<double?> data) private void ComputeHA(IEnumerable<double?> data)
{ {
Mean = data.Mean(); Mean = data.Mean();
decimal mean = (decimal) Mean; decimal mean = (decimal)Mean;
decimal variance = 0; decimal variance = 0;
decimal correction = 0; decimal correction = 0;
decimal skewness = 0; decimal skewness = 0;
@ -309,7 +326,7 @@ namespace MathNet.Numerics.Statistics
if (xi.HasValue) if (xi.HasValue)
{ {
decimal diff = xi.Value - mean; decimal diff = xi.Value - mean;
decimal tmp = diff*diff; decimal tmp = diff * diff;
correction += diff; correction += diff;
variance += tmp; variance += tmp;
tmp *= diff; tmp *= diff;
@ -323,21 +340,24 @@ namespace MathNet.Numerics.Statistics
Count = n; Count = n;
if (n > 0) if (n > 0)
{ {
Variance = (double) (variance - (correction*correction)/n)/(n - 1); Variance = (double)(variance - (correction * correction / n)) / (n - 1);
StandardDeviation = System.Math.Sqrt(Variance); StandardDeviation = System.Math.Sqrt(Variance);
if (Variance != 0) if (Variance != 0)
{ {
if (n > 2) 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) 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)));
} }
} }
} }
} }
} }
} }

92
src/Numerics/Statistics/Statistics.cs

@ -30,8 +30,7 @@ namespace MathNet.Numerics.Statistics
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MathNet.Numerics.Properties; using Properties;
using MathNet.Numerics.NumberTheory;
/// <summary> /// <summary>
/// Extension methods to return basic statistics on set of data. /// Extension methods to return basic statistics on set of data.
@ -54,7 +53,7 @@ namespace MathNet.Numerics.Statistics
int m = 0; int m = 0;
foreach (var item in data) foreach (var item in data)
{ {
mean += (item - mean)/++m; mean += (item - mean) / ++m;
} }
return mean; return mean;
@ -78,7 +77,7 @@ namespace MathNet.Numerics.Statistics
{ {
if (item.HasValue) if (item.HasValue)
{ {
mean += (item.Value - mean)/++m; mean += (item.Value - mean) / ++m;
} }
} }
@ -113,11 +112,11 @@ namespace MathNet.Numerics.Statistics
j++; j++;
double xi = iterator.Current; double xi = iterator.Current;
t += xi; t += xi;
double diff = j*xi - t; double diff = (j * xi) - t;
variance += (diff*diff)/(j*(j - 1)); variance += (diff * diff) / (j * (j - 1));
} }
return variance/(j - 1); return variance / (j - 1);
} }
/// <summary> /// <summary>
@ -145,6 +144,7 @@ namespace MathNet.Numerics.Statistics
{ {
break; break;
} }
if (iterator.Current.HasValue) if (iterator.Current.HasValue)
{ {
j++; j++;
@ -160,12 +160,12 @@ namespace MathNet.Numerics.Statistics
j++; j++;
double xi = iterator.Current.Value; double xi = iterator.Current.Value;
t += xi; t += xi;
double diff = j*xi - t; double diff = (j * xi) - t;
variance += (diff*diff)/(j*(j - 1)); variance += (diff * diff) / (j * (j - 1));
} }
} }
return variance/(j - 1); return variance / (j - 1);
} }
/// <summary> /// <summary>
@ -196,7 +196,7 @@ namespace MathNet.Numerics.Statistics
j++; j++;
double xi = iterator.Current; double xi = iterator.Current;
t += xi; t += xi;
double diff = j * xi - t; double diff = (j * xi) - t;
variance += (diff * diff) / (j * (j - 1)); variance += (diff * diff) / (j * (j - 1));
} }
@ -228,6 +228,7 @@ namespace MathNet.Numerics.Statistics
{ {
break; break;
} }
if (iterator.Current.HasValue) if (iterator.Current.HasValue)
{ {
j++; j++;
@ -243,7 +244,7 @@ namespace MathNet.Numerics.Statistics
j++; j++;
double xi = iterator.Current.Value; double xi = iterator.Current.Value;
t += xi; t += xi;
double diff = j * xi - t; double diff = (j * xi) - t;
variance += (diff * diff) / (j * (j - 1)); variance += (diff * diff) / (j * (j - 1));
} }
} }
@ -263,7 +264,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
return System.Math.Sqrt(Variance(data)); return Math.Sqrt(Variance(data));
} }
/// <summary> /// <summary>
@ -278,7 +279,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
return System.Math.Sqrt(Variance(data)); return Math.Sqrt(Variance(data));
} }
/// <summary> /// <summary>
@ -293,7 +294,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
return System.Math.Sqrt(PopulationVariance(data)); return Math.Sqrt(PopulationVariance(data));
} }
/// <summary> /// <summary>
@ -308,7 +309,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
return System.Math.Sqrt(PopulationVariance(data)); return Math.Sqrt(PopulationVariance(data));
} }
/// <summary> /// <summary>
@ -329,7 +330,7 @@ namespace MathNet.Numerics.Statistics
{ {
if (d.HasValue) if (d.HasValue)
{ {
min = System.Math.Min(min, d.Value); min = Math.Min(min, d.Value);
count++; count++;
} }
} }
@ -360,7 +361,7 @@ namespace MathNet.Numerics.Statistics
{ {
if (d.HasValue) if (d.HasValue)
{ {
max = System.Math.Max(max, d.Value); max = Math.Max(max, d.Value);
count++; count++;
} }
} }
@ -389,7 +390,7 @@ namespace MathNet.Numerics.Statistics
int count = 0; int count = 0;
foreach (double d in data) foreach (double d in data)
{ {
min = System.Math.Min(min, d); min = Math.Min(min, d);
count++; count++;
} }
@ -417,7 +418,7 @@ namespace MathNet.Numerics.Statistics
int count = 0; int count = 0;
foreach (double d in data) foreach (double d in data)
{ {
max = System.Math.Max(max, d); max = Math.Max(max, d);
count++; count++;
} }
@ -441,18 +442,16 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
List<double> dataArray = new List<double>(data); var dataArray = new List<double>(data);
int index = dataArray.Count/2 + 1; int index = (dataArray.Count / 2) + 1;
if (dataArray.Count % 2 == 0) if (dataArray.Count % 2 == 0)
{ {
double lower = OrderSelect(dataArray, 0, dataArray.Count - 1, index - 1); double lower = OrderSelect(dataArray, 0, dataArray.Count - 1, index - 1);
double upper = OrderSelect(dataArray, 0, dataArray.Count - 1, index); double upper = OrderSelect(dataArray, 0, dataArray.Count - 1, index);
return (lower + upper) / 2.0; return (lower + upper) / 2.0;
} }
else
{ return OrderSelect(dataArray, 0, dataArray.Count - 1, index);
return OrderSelect(dataArray, 0, dataArray.Count - 1, index);
}
} }
/// <summary> /// <summary>
@ -467,7 +466,7 @@ namespace MathNet.Numerics.Statistics
throw new ArgumentNullException("data"); throw new ArgumentNullException("data");
} }
List<double> nonNull = new List<double>(); var nonNull = new List<double>();
foreach (double? value in data) foreach (double? value in data)
{ {
if (value.HasValue) if (value.HasValue)
@ -487,7 +486,8 @@ namespace MathNet.Numerics.Statistics
/// <summary> /// <summary>
/// Evaluate the i-order (1..N) statistic of the provided samples. /// Evaluate the i-order (1..N) statistic of the provided samples.
/// </summary> /// </summary>
/// <param name="data">The sample data.</param> /// <param name="samples">The sample data.</param>
/// <param name="order">Order of the statistic to evaluate.</param>
/// <returns>The i'th order statistic in the sample data.</returns> /// <returns>The i'th order statistic in the sample data.</returns>
public static double OrderStatistic(IEnumerable<double> samples, int order) public static double OrderStatistic(IEnumerable<double> samples, int order)
{ {
@ -497,7 +497,7 @@ namespace MathNet.Numerics.Statistics
return Minimum(samples); return Minimum(samples);
} }
List<double> list = new List<double>(samples); var list = new List<double>(samples);
if (order < 1 || order > list.Count) if (order < 1 || order > list.Count)
{ {
throw new ArgumentOutOfRangeException("order", Resources.ArgumentInIntervalXYInclusive); throw new ArgumentOutOfRangeException("order", Resources.ArgumentInIntervalXYInclusive);
@ -521,16 +521,12 @@ namespace MathNet.Numerics.Statistics
/// <param name="right">The right bound in which to order select.</param> /// <param name="right">The right bound in which to order select.</param>
/// <param name="order">The order we are trying to find.</param> /// <param name="order">The order we are trying to find.</param>
/// <returns>The <paramref name="order"/> order statistic.</returns> /// <returns>The <paramref name="order"/> order statistic.</returns>
static double OrderSelect(IList<double> samples, int left, int right, int order) private static double OrderSelect(IList<double> samples, int left, int right, int order)
{ {
// Order most always be positive. System.Diagnostics.Debug.Assert(order > 0, "Order must always be positive.");
System.Diagnostics.Debug.Assert(order > 0); System.Diagnostics.Debug.Assert(left >= 0 && left <= right, "Left side must always be positive and smaller than right side.");
// 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(left >= 0 && left <= right); System.Diagnostics.Debug.Assert(right - left + 1 >= order, "Make sure there are at least order items in the segment [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);
if (left == right) if (left == right)
{ {
@ -542,29 +538,29 @@ namespace MathNet.Numerics.Statistics
// The partioning code. // The partioning code.
int i = left - 1; 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++; i++;
Sorting.Swap(samples, i, j); Sorting.Swap(samples, i, j);
} }
} }
Sorting.Swap(samples, i+1, right);
Sorting.Swap(samples, i + 1, right);
// Recursive order finding algorithm. // Recursive order finding algorithm.
if(order == (i-left)+2) if (order == (i - left) + 2)
{ {
return pivot; return pivot;
} }
else if (order < (i-left)+2)
if (order < (i - left) + 2)
{ {
return OrderSelect(samples, left, i, order); 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);
}
} }
} }
} }

Loading…
Cancel
Save