diff --git a/src/Numerics/Complex32.cs b/src/Numerics/Complex32.cs index a2eb05d4..84ca9272 100644 --- a/src/Numerics/Complex32.cs +++ b/src/Numerics/Complex32.cs @@ -131,8 +131,8 @@ namespace MathNet.Numerics #endif public Complex32(float real, float imaginary) { - this._real = real; - this._imag = imaginary; + _real = real; + _imag = imaginary; } #endregion @@ -220,7 +220,7 @@ namespace MathNet.Numerics #endif get { - return this._real; + return _real; } } @@ -235,7 +235,7 @@ namespace MathNet.Numerics #endif get { - return this._imag; + return _imag; } } @@ -245,7 +245,7 @@ namespace MathNet.Numerics /// true if this instance is zero; otherwise, false. public bool IsZero() { - return this._real == 0.0f && this._imag == 0.0f; + return _real == 0.0f && _imag == 0.0f; } /// @@ -254,7 +254,7 @@ namespace MathNet.Numerics /// true if this instance is one; otherwise, false. public bool IsOne() { - return this._real == 1.0f && this._imag == 0.0f; + return _real == 1.0f && _imag == 0.0f; } /// @@ -263,7 +263,7 @@ namespace MathNet.Numerics /// true if this instance is ImaginaryOne; otherwise, false. public bool IsImaginaryOne() { - return this._real == 0.0f && this._imag == 1.0f; + return _real == 0.0f && _imag == 1.0f; } /// @@ -276,7 +276,7 @@ namespace MathNet.Numerics /// public bool IsNaN() { - return float.IsNaN(this._real) || float.IsNaN(this._imag); + return float.IsNaN(_real) || float.IsNaN(_imag); } /// @@ -292,7 +292,7 @@ namespace MathNet.Numerics /// public bool IsInfinity() { - return float.IsInfinity(this._real) || float.IsInfinity(this._imag); + return float.IsInfinity(_real) || float.IsInfinity(_imag); } /// @@ -301,7 +301,7 @@ namespace MathNet.Numerics /// true if this instance is a real number; otherwise, false. public bool IsReal() { - return this._imag == 0.0f; + return _imag == 0.0f; } /// @@ -312,7 +312,7 @@ namespace MathNet.Numerics /// public bool IsRealNonNegative() { - return this._imag == 0.0f && this._real >= 0; + return _imag == 0.0f && _real >= 0; } /// @@ -333,7 +333,7 @@ namespace MathNet.Numerics /// The conjugate of this Complex32 public Complex32 Conjugate() { - return new Complex32(this._real, -this._imag); + return new Complex32(_real, -_imag); } /// @@ -345,7 +345,7 @@ namespace MathNet.Numerics { 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 { - return (this._real * this._real) + (this._imag * this._imag); + return (_real * _real) + (_imag * _imag); } } @@ -374,12 +374,12 @@ namespace MathNet.Numerics { get { - if (this.IsReal() && this._real < 0) + if (IsReal() && _real < 0) { 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 { - 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); } - 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); } - 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); } - 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); } // don't replace this with "Magnitude"! - var mod = SpecialFunctions.Hypotenuse(this._real, this._imag); + var mod = SpecialFunctions.Hypotenuse(_real, _imag); if (mod == 0.0f) { 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 /// public Complex32 Exponential() { - var exp = (float)Math.Exp(this._real); - if (this.IsReal()) + var exp = (float)Math.Exp(_real); + if (IsReal()) { 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)); } /// @@ -449,12 +449,12 @@ namespace MathNet.Numerics /// 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); } /// @@ -468,7 +468,7 @@ namespace MathNet.Numerics /// public Complex32 Power(Complex32 exponent) { - if (this.IsZero()) + if (IsZero()) { if (exponent.IsZero()) { @@ -493,7 +493,7 @@ namespace MathNet.Numerics return NaN; } - return (exponent * this.NaturalLogarithm()).Exponential(); + return (exponent * NaturalLogarithm()).Exponential(); } /// @@ -507,7 +507,7 @@ namespace MathNet.Numerics /// public Complex32 Root(Complex32 rootExponent) { - return this.Power(1 / rootExponent); + return Power(1 / rootExponent); } /// @@ -518,12 +518,12 @@ namespace MathNet.Numerics /// 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); } /// @@ -534,32 +534,32 @@ namespace MathNet.Numerics /// 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; - var absReal = Math.Abs(this.Real); - var absImag = Math.Abs(this.Imaginary); + var absReal = Math.Abs(Real); + var absImag = Math.Abs(Imaginary); double w; 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)))); } 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)))); } - 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); } @@ -628,7 +628,7 @@ namespace MathNet.Numerics /// public override string ToString() { - return this.ToString(null, null); + return ToString(null, null); } /// @@ -643,7 +643,7 @@ namespace MathNet.Numerics /// public string ToString(string format) { - return this.ToString(format, null); + return ToString(format, null); } /// @@ -658,7 +658,7 @@ namespace MathNet.Numerics /// public string ToString(IFormatProvider formatProvider) { - return this.ToString(null, formatProvider); + return ToString(null, formatProvider); } /// @@ -684,38 +684,31 @@ namespace MathNet.Numerics { var numberFormatInfo = formatProvider.GetNumberFormatInfo(); - if (this.IsNaN()) + if (IsNaN()) { return numberFormatInfo.NaNSymbol; } - if (this.IsInfinity()) + if (IsInfinity()) { return numberFormatInfo.PositiveInfinitySymbol; } 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(" "); - } - else - { - ret.Append(" + "); - } + ret.Append(_imag < 0 ? " " : " + "); } - ret.Append(this._imag.ToString(format, formatProvider)).Append("i"); + ret.Append(_imag.ToString(format, formatProvider)).Append("i"); } if (ret.Length == 0) @@ -743,17 +736,17 @@ namespace MathNet.Numerics /// public bool Equals(Complex32 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); } /// @@ -768,7 +761,7 @@ namespace MathNet.Numerics /// public override int GetHashCode() { - return this._real.GetHashCode() ^ (-this._imag.GetHashCode()); + return _real.GetHashCode() ^ (-_imag.GetHashCode()); } /// @@ -784,7 +777,7 @@ namespace MathNet.Numerics /// public override bool Equals(object obj) { - return (obj is Complex32) && this.Equals((Complex32)obj); + return (obj is Complex32) && Equals((Complex32)obj); } #endregion @@ -1071,7 +1064,7 @@ namespace MathNet.Numerics /// double IPrecisionSupport.Norm() { - return this.MagnitudeSquared; + return MagnitudeSquared; } /// @@ -1485,7 +1478,7 @@ namespace MathNet.Numerics /// A with the same values as this Complex32. public Complex ToComplex() { - return new Complex(this._real, this._imag); + return new Complex(_real, _imag); } #endregion diff --git a/src/Numerics/LinearAlgebra/Generic/Matrix.cs b/src/Numerics/LinearAlgebra/Generic/Matrix.cs index 378819ef..2c5dc5fa 100644 --- a/src/Numerics/LinearAlgebra/Generic/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Generic/Matrix.cs @@ -1494,7 +1494,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public virtual Matrix ConjugateTranspose() { // 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(); } @@ -1936,14 +1936,14 @@ namespace MathNet.Numerics.LinearAlgebra.Generic { object obj = val1; object conj = Complex.Conjugate((Complex)obj); - return (T)(conj); + return (T)conj; } if (typeof(T) == typeof(Complex32)) { object obj = val1; object conj = ((Complex32)obj).Conjugate(); - return (T)(conj); + return (T)conj; } if (typeof(T) == typeof(double)) diff --git a/src/Numerics/Permutation.cs b/src/Numerics/Permutation.cs index c6305ee1..5d821ac4 100644 --- a/src/Numerics/Permutation.cs +++ b/src/Numerics/Permutation.cs @@ -51,7 +51,7 @@ namespace MathNet.Numerics #region Constructor /// - /// Initializes a new instance of the Permutation structure. + /// Initializes a new instance of the Permutation class. /// /// An array which represents where each integer is permuted too: indices[i] represents that integer i /// is permuted to location indices[i]. @@ -62,13 +62,13 @@ namespace MathNet.Numerics throw new ArgumentException(Resources.PermutationAsIntArrayInvalid, "indices"); } - _indices = (int[]) indices.Clone(); + _indices = (int[])indices.Clone(); } #endregion /// - /// The number of elements this permutation is over. + /// Gets the number of elements this permutation is over. /// public int Dimension { @@ -91,7 +91,7 @@ namespace MathNet.Numerics /// /// Computes the inverse of the permutation. /// - /// + /// The inverse of the permutation. public Permutation Inverse() { var invIdx = new int[Dimension]; @@ -119,7 +119,8 @@ namespace MathNet.Numerics { 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]) { diff --git a/src/Numerics/Precision.cs b/src/Numerics/Precision.cs index 14fdf856..f78c7ca9 100644 --- a/src/Numerics/Precision.cs +++ b/src/Numerics/Precision.cs @@ -1741,6 +1741,11 @@ namespace MathNet.Numerics return 2 * EpsilonOf(value); } + /// + /// Converts a float valut to a bit array stored in an int. + /// + /// The value to convert. + /// The bit array. internal static int FloatToInt32Bits(float value) { return BitConverter.ToInt32(BitConverter.GetBytes(value), 0); diff --git a/src/Numerics/SpecialFunctions.cs b/src/Numerics/SpecialFunctions.cs index 29c362c0..b64089fe 100644 --- a/src/Numerics/SpecialFunctions.cs +++ b/src/Numerics/SpecialFunctions.cs @@ -3,9 +3,7 @@ // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com -// // Copyright (c) 2009-2010 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 @@ -14,10 +12,8 @@ // 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 @@ -93,7 +89,7 @@ namespace MathNet.Numerics /// If or are not positive. public static double Beta(double z, double w) { - return System.Math.Exp(BetaLn(z, w)); + return Math.Exp(BetaLn(z, w)); } /// @@ -110,15 +106,15 @@ namespace MathNet.Numerics /// The value of the DiGamma function at . public static double DiGamma(double x) { - const double c = 12.0, - d1 = -0.57721566490153286, - d2 = 1.6449340668482264365, - s = 1e-6, - s3 = 1.0 / 12.0, - s4 = 1.0 / 120.0, - s5 = 1.0 / 252.0, - s6 = 1.0 / 240.0, - s7 = 1.0 / 132.0; + const double C = 12.0; + const double D1 = -0.57721566490153286; + const double D2 = 1.6449340668482264365; + const double S = 1e-6; + const double S3 = 1.0 / 12.0; + const double S4 = 1.0 / 120.0; + const double S5 = 1.0 / 252.0; + const double S6 = 1.0 / 240.0; + const double S7 = 1.0 / 132.0; 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)); } - if (x <= s) + if (x <= S) { - return d1 - (1 / x) + (d2 * x); + return D1 - (1 / x) + (D2 * x); } double result = 0; - while (x < c) + while (x < C) { result -= 1 / x; x++; } - if (x >= c) + if (x >= C) { - double r = 1 / x; + var r = 1 / x; result += Math.Log(x) - (0.5 * r); r *= r; - result -= r * (s3 - (r * (s4 - (r * (s5 - (r * (s6 - (r * s7)))))))); + result -= r * (S3 - (r * (S4 - (r * (S5 - (r * (S6 - (r * S7)))))))); } return result; @@ -185,8 +181,8 @@ namespace MathNet.Numerics return Double.PositiveInfinity; } - double x = Math.Exp(p); - for (double d = 1.0; d > 1.0e-15; d /= 2.0) + var x = Math.Exp(p); + for (var d = 1.0; d > 1.0e-15; d /= 2.0) { x += d * Math.Sign(p - DiGamma(x)); } @@ -217,41 +213,45 @@ namespace MathNet.Numerics /// The regularized lower incomplete beta function. 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) { - throw new ArgumentOutOfRangeException("x", Properties.Resources.ArgumentInIntervalXYInclusive); + throw new ArgumentOutOfRangeException("x", Resources.ArgumentInIntervalXYInclusive); } - double bt = (x == 0.0 || x == 1.0) - ? 0.0 - : Math.Exp(GammaLn(a + b) - GammaLn(a) - GammaLn(b) + (a * Math.Log(x)) + (b * Math.Log(1.0 - x))); + var bt = (x == 0.0 || x == 1.0) + ? 0.0 + : 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 */ - const int MaxIterations = 100; - double eps = Precision.DoubleMachinePrecision; - double fpmin = Precision.Increment(0.0) / eps; + var eps = Precision.DoubleMachinePrecision; + var fpmin = 0.0.Increment() / eps; if (symmetryTransformation) { x = 1.0 - x; - double swap = a; + var swap = a; a = b; b = swap; } - double qab = a + b; - double qap = a + 1.0; - double qam = a - 1.0; - double c = 1.0; - double d = 1.0 - (qab * x / qap); + var qab = a + b; + var qap = a + 1.0; + var qam = a - 1.0; + var c = 1.0; + var d = 1.0 - (qab * x / qap); if (Math.Abs(d) < fpmin) { @@ -259,11 +259,11 @@ namespace MathNet.Numerics } d = 1.0 / d; - double h = d; + var h = d; 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); if (Math.Abs(d) < fpmin) @@ -295,7 +295,7 @@ namespace MathNet.Numerics } d = 1.0 / d; - double del = d * c; + var del = d * c; h *= del; 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); } /// @@ -338,4 +338,4 @@ namespace MathNet.Numerics return 1.0 / (Math.Exp(-p) + 1.0); } } -} \ No newline at end of file +}