Browse Source

fixed a couple style copy issues

la-knuth
Marcus Cuda 16 years ago
parent
commit
14a3c0bf50
  1. 129
      src/Numerics/Complex32.cs
  2. 6
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  3. 11
      src/Numerics/Permutation.cs
  4. 5
      src/Numerics/Precision.cs
  5. 86
      src/Numerics/SpecialFunctions.cs

129
src/Numerics/Complex32.cs

@ -131,8 +131,8 @@ namespace MathNet.Numerics
#endif #endif
public Complex32(float real, float imaginary) public Complex32(float real, float imaginary)
{ {
this._real = real; _real = real;
this._imag = imaginary; _imag = imaginary;
} }
#endregion #endregion
@ -220,7 +220,7 @@ namespace MathNet.Numerics
#endif #endif
get get
{ {
return this._real; return _real;
} }
} }
@ -235,7 +235,7 @@ namespace MathNet.Numerics
#endif #endif
get get
{ {
return this._imag; return _imag;
} }
} }
@ -245,7 +245,7 @@ namespace MathNet.Numerics
/// <returns><c>true</c> if this instance is zero; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if this instance is zero; otherwise, <c>false</c>.</returns>
public bool IsZero() public bool IsZero()
{ {
return this._real == 0.0f && this._imag == 0.0f; return _real == 0.0f && _imag == 0.0f;
} }
/// <summary> /// <summary>
@ -254,7 +254,7 @@ namespace MathNet.Numerics
/// <returns><c>true</c> if this instance is one; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if this instance is one; otherwise, <c>false</c>.</returns>
public bool IsOne() public bool IsOne()
{ {
return this._real == 1.0f && this._imag == 0.0f; return _real == 1.0f && _imag == 0.0f;
} }
/// <summary> /// <summary>
@ -263,7 +263,7 @@ namespace MathNet.Numerics
/// <returns><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</returns>
public bool IsImaginaryOne() public bool IsImaginaryOne()
{ {
return this._real == 0.0f && this._imag == 1.0f; return _real == 0.0f && _imag == 1.0f;
} }
/// <summary> /// <summary>
@ -276,7 +276,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public bool IsNaN() public bool IsNaN()
{ {
return float.IsNaN(this._real) || float.IsNaN(this._imag); return float.IsNaN(_real) || float.IsNaN(_imag);
} }
/// <summary> /// <summary>
@ -292,7 +292,7 @@ namespace MathNet.Numerics
/// </remarks> /// </remarks>
public bool IsInfinity() public bool IsInfinity()
{ {
return float.IsInfinity(this._real) || float.IsInfinity(this._imag); return float.IsInfinity(_real) || float.IsInfinity(_imag);
} }
/// <summary> /// <summary>
@ -301,7 +301,7 @@ namespace MathNet.Numerics
/// <returns><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</returns>
public bool IsReal() public bool IsReal()
{ {
return this._imag == 0.0f; return _imag == 0.0f;
} }
/// <summary> /// <summary>
@ -312,7 +312,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public bool IsRealNonNegative() public bool IsRealNonNegative()
{ {
return this._imag == 0.0f && this._real >= 0; return _imag == 0.0f && _real >= 0;
} }
/// <summary> /// <summary>
@ -333,7 +333,7 @@ namespace MathNet.Numerics
/// <returns>The conjugate of this <c>Complex32</c></returns> /// <returns>The conjugate of this <c>Complex32</c></returns>
public Complex32 Conjugate() public Complex32 Conjugate()
{ {
return new Complex32(this._real, -this._imag); return new Complex32(_real, -_imag);
} }
/// <summary> /// <summary>
@ -345,7 +345,7 @@ namespace MathNet.Numerics
{ {
get get
{ {
return (float)Math.Sqrt((this._real * this._real) + (this._imag * this._imag)); return (float)Math.Sqrt((_real * _real) + (_imag * _imag));
} }
} }
@ -357,7 +357,7 @@ namespace MathNet.Numerics
{ {
get get
{ {
return (this._real * this._real) + (this._imag * this._imag); return (_real * _real) + (_imag * _imag);
} }
} }
@ -374,12 +374,12 @@ namespace MathNet.Numerics
{ {
get get
{ {
if (this.IsReal() && this._real < 0) if (IsReal() && _real < 0)
{ {
return (float)Math.PI; return (float)Math.PI;
} }
return this.IsRealNonNegative() ? 0.0f : (float)Math.Atan2(this._imag, this._real); return IsRealNonNegative() ? 0.0f : (float)Math.Atan2(_imag, _real);
} }
} }
@ -391,34 +391,34 @@ namespace MathNet.Numerics
{ {
get get
{ {
if (float.IsPositiveInfinity(this._real) && float.IsPositiveInfinity(this._imag)) if (float.IsPositiveInfinity(_real) && float.IsPositiveInfinity(_imag))
{ {
return new Complex32((float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2); return new Complex32((float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2);
} }
if (float.IsPositiveInfinity(this._real) && float.IsNegativeInfinity(this._imag)) if (float.IsPositiveInfinity(_real) && float.IsNegativeInfinity(_imag))
{ {
return new Complex32((float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2); return new Complex32((float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2);
} }
if (float.IsNegativeInfinity(this._real) && float.IsPositiveInfinity(this._imag)) if (float.IsNegativeInfinity(_real) && float.IsPositiveInfinity(_imag))
{ {
return new Complex32(-(float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2); return new Complex32(-(float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2);
} }
if (float.IsNegativeInfinity(this._real) && float.IsNegativeInfinity(this._imag)) if (float.IsNegativeInfinity(_real) && float.IsNegativeInfinity(_imag))
{ {
return new Complex32(-(float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2); return new Complex32(-(float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2);
} }
// don't replace this with "Magnitude"! // don't replace this with "Magnitude"!
var mod = SpecialFunctions.Hypotenuse(this._real, this._imag); var mod = SpecialFunctions.Hypotenuse(_real, _imag);
if (mod == 0.0f) if (mod == 0.0f)
{ {
return Zero; return Zero;
} }
return new Complex32((float)(this._real / mod), (float)(this._imag / mod)); return new Complex32((float)(_real / mod), (float)(_imag / mod));
} }
} }
@ -432,13 +432,13 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Exponential() public Complex32 Exponential()
{ {
var exp = (float)Math.Exp(this._real); var exp = (float)Math.Exp(_real);
if (this.IsReal()) if (IsReal())
{ {
return new Complex32(exp, 0.0f); return new Complex32(exp, 0.0f);
} }
return new Complex32(exp * (float)Trig.Cosine(this._imag), exp * (float)Trig.Sine(this._imag)); return new Complex32(exp * (float)Trig.Cosine(_imag), exp * (float)Trig.Sine(_imag));
} }
/// <summary> /// <summary>
@ -449,12 +449,12 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 NaturalLogarithm() public Complex32 NaturalLogarithm()
{ {
if (this.IsRealNonNegative()) if (IsRealNonNegative())
{ {
return new Complex32((float)Math.Log(this._real), 0.0f); return new Complex32((float)Math.Log(_real), 0.0f);
} }
return new Complex32(0.5f * (float)Math.Log(this.MagnitudeSquared), this.Phase); return new Complex32(0.5f * (float)Math.Log(MagnitudeSquared), Phase);
} }
/// <summary> /// <summary>
@ -468,7 +468,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Power(Complex32 exponent) public Complex32 Power(Complex32 exponent)
{ {
if (this.IsZero()) if (IsZero())
{ {
if (exponent.IsZero()) if (exponent.IsZero())
{ {
@ -493,7 +493,7 @@ namespace MathNet.Numerics
return NaN; return NaN;
} }
return (exponent * this.NaturalLogarithm()).Exponential(); return (exponent * NaturalLogarithm()).Exponential();
} }
/// <summary> /// <summary>
@ -507,7 +507,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Root(Complex32 rootExponent) public Complex32 Root(Complex32 rootExponent)
{ {
return this.Power(1 / rootExponent); return Power(1 / rootExponent);
} }
/// <summary> /// <summary>
@ -518,12 +518,12 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Square() public Complex32 Square()
{ {
if (this.IsReal()) if (IsReal())
{ {
return new Complex32(this._real * this._real, 0.0f); return new Complex32(_real * _real, 0.0f);
} }
return new Complex32((this._real * this._real) - (this._imag * this._imag), 2 * this._real * this._imag); return new Complex32((_real * _real) - (_imag * _imag), 2 * _real * _imag);
} }
/// <summary> /// <summary>
@ -534,32 +534,32 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 SquareRoot() public Complex32 SquareRoot()
{ {
if (this.IsRealNonNegative()) if (IsRealNonNegative())
{ {
return new Complex32((float)Math.Sqrt(this._real), 0.0f); return new Complex32((float)Math.Sqrt(_real), 0.0f);
} }
Complex32 result; Complex32 result;
var absReal = Math.Abs(this.Real); var absReal = Math.Abs(Real);
var absImag = Math.Abs(this.Imaginary); var absImag = Math.Abs(Imaginary);
double w; double w;
if (absReal >= absImag) if (absReal >= absImag)
{ {
var ratio = this.Imaginary / this.Real; var ratio = Imaginary / Real;
w = Math.Sqrt(absReal) * Math.Sqrt(0.5 * (1.0f + Math.Sqrt(1.0f + (ratio * ratio)))); w = Math.Sqrt(absReal) * Math.Sqrt(0.5 * (1.0f + Math.Sqrt(1.0f + (ratio * ratio))));
} }
else else
{ {
var ratio = this.Real / this.Imaginary; var ratio = Real / Imaginary;
w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0f + (ratio * ratio)))); w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0f + (ratio * ratio))));
} }
if (this.Real >= 0.0f) if (Real >= 0.0f)
{ {
result = new Complex32((float)w, (float)(this.Imaginary / (2.0f * w))); result = new Complex32((float)w, (float)(Imaginary / (2.0f * w)));
} }
else if (this.Imaginary >= 0.0f) else if (Imaginary >= 0.0f)
{ {
result = new Complex32((float)(absImag / (2.0 * w)), (float)w); result = new Complex32((float)(absImag / (2.0 * w)), (float)w);
} }
@ -628,7 +628,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>
@ -643,7 +643,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>
@ -658,7 +658,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public string ToString(IFormatProvider formatProvider) public string ToString(IFormatProvider formatProvider)
{ {
return this.ToString(null, formatProvider); return ToString(null, formatProvider);
} }
/// <summary> /// <summary>
@ -684,38 +684,31 @@ namespace MathNet.Numerics
{ {
var numberFormatInfo = formatProvider.GetNumberFormatInfo(); var numberFormatInfo = formatProvider.GetNumberFormatInfo();
if (this.IsNaN()) if (IsNaN())
{ {
return numberFormatInfo.NaNSymbol; return numberFormatInfo.NaNSymbol;
} }
if (this.IsInfinity()) if (IsInfinity())
{ {
return numberFormatInfo.PositiveInfinitySymbol; return numberFormatInfo.PositiveInfinitySymbol;
} }
var ret = new StringBuilder(); var ret = new StringBuilder();
if (this._real != 0.0f) if (_real != 0.0f)
{ {
ret.Append(this._real.ToString(format, formatProvider)); ret.Append(_real.ToString(format, formatProvider));
} }
if (this._imag != 0.0f) if (_imag != 0.0f)
{ {
if (this._real != 0.0f) if (_real != 0.0f)
{ {
if (this._imag < 0) ret.Append(_imag < 0 ? " " : " + ");
{
ret.Append(" ");
}
else
{
ret.Append(" + ");
}
} }
ret.Append(this._imag.ToString(format, formatProvider)).Append("i"); ret.Append(_imag.ToString(format, formatProvider)).Append("i");
} }
if (ret.Length == 0) if (ret.Length == 0)
@ -743,17 +736,17 @@ namespace MathNet.Numerics
/// </param> /// </param>
public bool Equals(Complex32 other) public bool Equals(Complex32 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>
@ -768,7 +761,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>
@ -784,7 +777,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return (obj is Complex32) && this.Equals((Complex32)obj); return (obj is Complex32) && Equals((Complex32)obj);
} }
#endregion #endregion
@ -1071,7 +1064,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
double IPrecisionSupport<Complex32>.Norm() double IPrecisionSupport<Complex32>.Norm()
{ {
return this.MagnitudeSquared; return MagnitudeSquared;
} }
/// <summary> /// <summary>
@ -1485,7 +1478,7 @@ namespace MathNet.Numerics
/// <returns>A <see cref="Complex"/> with the same values as this <c>Complex32</c>.</returns> /// <returns>A <see cref="Complex"/> with the same values as this <c>Complex32</c>.</returns>
public Complex ToComplex() public Complex ToComplex()
{ {
return new Complex(this._real, this._imag); return new Complex(_real, _imag);
} }
#endregion #endregion

6
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -1494,7 +1494,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
public virtual Matrix<T> ConjugateTranspose() public virtual Matrix<T> ConjugateTranspose()
{ {
// In case of real return regulart transpose // In case of real return regulart transpose
if ((typeof(T) == typeof(double)) || ((typeof(T) == typeof(float)))) if ((typeof(T) == typeof(double)) || (typeof(T) == typeof(float)))
{ {
return Transpose(); return Transpose();
} }
@ -1936,14 +1936,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{ {
object obj = val1; object obj = val1;
object conj = Complex.Conjugate((Complex)obj); object conj = Complex.Conjugate((Complex)obj);
return (T)(conj); return (T)conj;
} }
if (typeof(T) == typeof(Complex32)) if (typeof(T) == typeof(Complex32))
{ {
object obj = val1; object obj = val1;
object conj = ((Complex32)obj).Conjugate(); object conj = ((Complex32)obj).Conjugate();
return (T)(conj); return (T)conj;
} }
if (typeof(T) == typeof(double)) if (typeof(T) == typeof(double))

11
src/Numerics/Permutation.cs

@ -51,7 +51,7 @@ namespace MathNet.Numerics
#region Constructor #region Constructor
/// <summary> /// <summary>
/// Initializes a new instance of the Permutation structure. /// Initializes a new instance of the Permutation class.
/// </summary> /// </summary>
/// <param name="indices">An array which represents where each integer is permuted too: indices[i] represents that integer i /// <param name="indices">An array which represents where each integer is permuted too: indices[i] represents that integer i
/// is permuted to location indices[i].</param> /// is permuted to location indices[i].</param>
@ -62,13 +62,13 @@ namespace MathNet.Numerics
throw new ArgumentException(Resources.PermutationAsIntArrayInvalid, "indices"); throw new ArgumentException(Resources.PermutationAsIntArrayInvalid, "indices");
} }
_indices = (int[]) indices.Clone(); _indices = (int[])indices.Clone();
} }
#endregion #endregion
/// <summary> /// <summary>
/// The number of elements this permutation is over. /// Gets the number of elements this permutation is over.
/// </summary> /// </summary>
public int Dimension public int Dimension
{ {
@ -91,7 +91,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Computes the inverse of the permutation. /// Computes the inverse of the permutation.
/// </summary> /// </summary>
/// <returns></returns> /// <returns>The inverse of the permutation.</returns>
public Permutation Inverse() public Permutation Inverse()
{ {
var invIdx = new int[Dimension]; var invIdx = new int[Dimension];
@ -119,7 +119,8 @@ namespace MathNet.Numerics
{ {
idx[i] = i; idx[i] = i;
} }
for (int i = inv.Length-1; i >= 0; i--)
for (int i = inv.Length - 1; i >= 0; i--)
{ {
if (idx[i] != inv[i]) if (idx[i] != inv[i])
{ {

5
src/Numerics/Precision.cs

@ -1741,6 +1741,11 @@ namespace MathNet.Numerics
return 2 * EpsilonOf(value); return 2 * EpsilonOf(value);
} }
/// <summary>
/// Converts a float valut to a bit array stored in an int.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>The bit array.</returns>
internal static int FloatToInt32Bits(float value) internal static int FloatToInt32Bits(float value)
{ {
return BitConverter.ToInt32(BitConverter.GetBytes(value), 0); return BitConverter.ToInt32(BitConverter.GetBytes(value), 0);

86
src/Numerics/SpecialFunctions.cs

@ -3,9 +3,7 @@
// http://numerics.mathdotnet.com // http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics // http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com // http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2010 Math.NET // Copyright (c) 2009-2010 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
@ -14,10 +12,8 @@
// 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
@ -93,7 +89,7 @@ namespace MathNet.Numerics
/// <exception cref="ArgumentException">If <paramref name="z"/> or <paramref name="w"/> are not positive.</exception> /// <exception cref="ArgumentException">If <paramref name="z"/> or <paramref name="w"/> are not positive.</exception>
public static double Beta(double z, double w) public static double Beta(double z, double w)
{ {
return System.Math.Exp(BetaLn(z, w)); return Math.Exp(BetaLn(z, w));
} }
/// <summary> /// <summary>
@ -110,15 +106,15 @@ namespace MathNet.Numerics
/// <returns>The value of the DiGamma function at <paramref name="x"/>.</returns> /// <returns>The value of the DiGamma function at <paramref name="x"/>.</returns>
public static double DiGamma(double x) public static double DiGamma(double x)
{ {
const double c = 12.0, const double C = 12.0;
d1 = -0.57721566490153286, const double D1 = -0.57721566490153286;
d2 = 1.6449340668482264365, const double D2 = 1.6449340668482264365;
s = 1e-6, const double S = 1e-6;
s3 = 1.0 / 12.0, const double S3 = 1.0 / 12.0;
s4 = 1.0 / 120.0, const double S4 = 1.0 / 120.0;
s5 = 1.0 / 252.0, const double S5 = 1.0 / 252.0;
s6 = 1.0 / 240.0, const double S6 = 1.0 / 240.0;
s7 = 1.0 / 132.0; const double S7 = 1.0 / 132.0;
if (Double.IsNegativeInfinity(x) || Double.IsNaN(x)) if (Double.IsNegativeInfinity(x) || Double.IsNaN(x))
{ {
@ -137,25 +133,25 @@ namespace MathNet.Numerics
return DiGamma(1.0 - x) + (Math.PI / Math.Tan(-Math.PI * x)); return DiGamma(1.0 - x) + (Math.PI / Math.Tan(-Math.PI * x));
} }
if (x <= s) if (x <= S)
{ {
return d1 - (1 / x) + (d2 * x); return D1 - (1 / x) + (D2 * x);
} }
double result = 0; double result = 0;
while (x < c) while (x < C)
{ {
result -= 1 / x; result -= 1 / x;
x++; x++;
} }
if (x >= c) if (x >= C)
{ {
double r = 1 / x; var r = 1 / x;
result += Math.Log(x) - (0.5 * r); result += Math.Log(x) - (0.5 * r);
r *= r; r *= r;
result -= r * (s3 - (r * (s4 - (r * (s5 - (r * (s6 - (r * s7)))))))); result -= r * (S3 - (r * (S4 - (r * (S5 - (r * (S6 - (r * S7))))))));
} }
return result; return result;
@ -185,8 +181,8 @@ namespace MathNet.Numerics
return Double.PositiveInfinity; return Double.PositiveInfinity;
} }
double x = Math.Exp(p); var x = Math.Exp(p);
for (double d = 1.0; d > 1.0e-15; d /= 2.0) for (var d = 1.0; d > 1.0e-15; d /= 2.0)
{ {
x += d * Math.Sign(p - DiGamma(x)); x += d * Math.Sign(p - DiGamma(x));
} }
@ -217,41 +213,45 @@ namespace MathNet.Numerics
/// <returns>The regularized lower incomplete beta function.</returns> /// <returns>The regularized lower incomplete beta function.</returns>
public static double BetaRegularized(double a, double b, double x) public static double BetaRegularized(double a, double b, double x)
{ {
if (a < 0.0 || b < 0.0) if (a < 0.0)
{ {
throw new ArgumentOutOfRangeException("a,b", Properties.Resources.ArgumentNotNegative); throw new ArgumentOutOfRangeException("a", Resources.ArgumentNotNegative);
}
if (b < 0.0)
{
throw new ArgumentOutOfRangeException("b", Resources.ArgumentNotNegative);
} }
if (x < 0.0 || x > 1.0) if (x < 0.0 || x > 1.0)
{ {
throw new ArgumentOutOfRangeException("x", Properties.Resources.ArgumentInIntervalXYInclusive); throw new ArgumentOutOfRangeException("x", Resources.ArgumentInIntervalXYInclusive);
} }
double bt = (x == 0.0 || x == 1.0) var bt = (x == 0.0 || x == 1.0)
? 0.0 ? 0.0
: Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + (a * Math.Log(x)) + (b * Math.Log(1.0 - x))); : Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + (a * Math.Log(x)) + (b * Math.Log(1.0 - x)));
bool symmetryTransformation = x >= (a + 1.0) / (a + b + 2.0); var symmetryTransformation = x >= (a + 1.0) / (a + b + 2.0);
/* Continued fraction representation */ /* Continued fraction representation */
const int MaxIterations = 100; const int MaxIterations = 100;
double eps = Precision.DoubleMachinePrecision; var eps = Precision.DoubleMachinePrecision;
double fpmin = Precision.Increment(0.0) / eps; var fpmin = 0.0.Increment() / eps;
if (symmetryTransformation) if (symmetryTransformation)
{ {
x = 1.0 - x; x = 1.0 - x;
double swap = a; var swap = a;
a = b; a = b;
b = swap; b = swap;
} }
double qab = a + b; var qab = a + b;
double qap = a + 1.0; var qap = a + 1.0;
double qam = a - 1.0; var qam = a - 1.0;
double c = 1.0; var c = 1.0;
double d = 1.0 - (qab * x / qap); var d = 1.0 - (qab * x / qap);
if (Math.Abs(d) < fpmin) if (Math.Abs(d) < fpmin)
{ {
@ -259,11 +259,11 @@ namespace MathNet.Numerics
} }
d = 1.0 / d; d = 1.0 / d;
double h = d; var h = d;
for (int m = 1, m2 = 2; m <= MaxIterations; m++, m2 += 2) for (int m = 1, m2 = 2; m <= MaxIterations; m++, m2 += 2)
{ {
double aa = m * (b - m) * x / ((qam + m2) * (a + m2)); var aa = m * (b - m) * x / ((qam + m2) * (a + m2));
d = 1.0 + (aa * d); d = 1.0 + (aa * d);
if (Math.Abs(d) < fpmin) if (Math.Abs(d) < fpmin)
@ -295,7 +295,7 @@ namespace MathNet.Numerics
} }
d = 1.0 / d; d = 1.0 / d;
double del = d * c; var del = d * c;
h *= del; h *= del;
if (Math.Abs(del - 1.0) <= eps) if (Math.Abs(del - 1.0) <= eps)
@ -309,7 +309,7 @@ namespace MathNet.Numerics
} }
} }
throw new ArgumentException(Properties.Resources.ArgumentTooLargeForIterationLimit, "a,b"); throw new ArgumentException(Resources.ArgumentTooLargeForIterationLimit);
} }
/// <summary> /// <summary>
@ -338,4 +338,4 @@ namespace MathNet.Numerics
return 1.0 / (Math.Exp(-p) + 1.0); return 1.0 / (Math.Exp(-p) + 1.0);
} }
} }
} }

Loading…
Cancel
Save