diff --git a/src/Managed.UnitTests/ComplexTest.cs b/src/Managed.UnitTests/ComplexTests/ComplexTest.cs similarity index 100% rename from src/Managed.UnitTests/ComplexTest.cs rename to src/Managed.UnitTests/ComplexTests/ComplexTest.cs diff --git a/src/Managed.UnitTests/Managed.UnitTests.csproj b/src/Managed.UnitTests/Managed.UnitTests.csproj index 279adacb..7201e5eb 100644 --- a/src/Managed.UnitTests/Managed.UnitTests.csproj +++ b/src/Managed.UnitTests/Managed.UnitTests.csproj @@ -61,7 +61,7 @@ - + diff --git a/src/Managed/Complex.cs b/src/Managed/Complex.cs index 04f8060c..517f3411 100644 --- a/src/Managed/Complex.cs +++ b/src/Managed/Complex.cs @@ -1,9 +1,7 @@ // // Math.NET Numerics, part of the Math.NET Project // http://mathnet.opensourcedotnet.info -// // Copyright (c) 2009 Math.NET -// // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without @@ -12,10 +10,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 @@ -26,44 +22,47 @@ // OTHER DEALINGS IN THE SOFTWARE. // -using System; -using System.Runtime.InteropServices; -using System.Text; -using System.Text.RegularExpressions; -using MathNet.Numerics.Properties; - namespace MathNet.Numerics { + using System; + using System.Runtime.InteropServices; + using System.Text; + using System.Text.RegularExpressions; + + using Properties; + /// /// Complex numbers class. /// /// - /// The class Complex provides all elementary operations + /// + /// The class Complex provides all elementary operations /// on complex numbers. All the operators +, -, /// *, /, ==, != are defined in the /// canonical way. Additional complex trigonometric functions such /// as , ... /// are also provided. Note that the Complex structures /// has two special constant values and - /// . - /// In order to avoid possible ambiguities resulting from a + /// . + /// + /// + /// In order to avoid possible ambiguities resulting from a /// Complex(double, double) constructor, the static methods /// and - /// are provided instead. - /// + /// are provided instead. + /// + /// + /// /// Complex x = Complex.FromRealImaginary(1d, 2d); /// Complex y = Complex.FromModulusArgument(1d, Math.Pi); /// Complex z = (x + y) / (x - y); - /// - /// Since there is no canonical order among the complex numbers, - /// Complex does not implement IComparable but several - /// lexicographic IComparer implementations are provided, see - /// , - /// and - /// . - /// For mathematical details about complex numbers, please + /// + /// + /// + /// For mathematical details about complex numbers, please /// have a look at the - /// Wikipedia + /// Wikipedia + /// /// [Serializable] [StructLayout(LayoutKind.Sequential)] @@ -72,9 +71,12 @@ namespace MathNet.Numerics #region fields /// - /// Regular expressionused to parse strings into complex numbers. + /// Regular expression used to parse strings into complex numbers. /// - private static readonly Regex parseExpression = new Regex(@"^((?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); + private static readonly Regex ParseExpression = + new Regex( + @"^((?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))|(?(([-+]?((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity))?[i]))|(?(([-+]?(\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|(NaN)|([-+]?Infinity)))(?(([-+]((\d+\.?\d*|\d*\.?\d+)([Ee][-+]?[0-9]+)?)|[-+](NaN)|([-+]Infinity))?[i])))$", + RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); /// /// Represents imaginary unit number. @@ -82,12 +84,12 @@ namespace MathNet.Numerics private static readonly Complex i = new Complex(0, 1); /// - /// Represents a infite complex number + /// Represents a infinite complex number /// private static readonly Complex infinity = new Complex(double.PositiveInfinity, double.PositiveInfinity); /// - /// Reprensents not-a-number. + /// Represents not-a-number. /// private static readonly Complex nan = new Complex(Double.NaN, Double.NaN); @@ -119,12 +121,16 @@ namespace MathNet.Numerics /// Initializes a new instance of the Complex struct with the given real /// and imaginary parts. /// - /// The value for the real component. - /// The value for the imaginary component. + /// + /// The value for the real component. + /// + /// + /// The value for the imaginary component. + /// public Complex(double real, double imaginary) { - _real = real; - _imag = imaginary; + this._real = real; + this._imag = imaginary; } #endregion @@ -146,7 +152,10 @@ namespace MathNet.Numerics /// A value representing the infinity value. public static Complex Infinity { - get { return infinity; } + get + { + return infinity; + } } /// @@ -155,7 +164,10 @@ namespace MathNet.Numerics /// A value representing not-a-number. public static Complex NaN { - get { return nan; } + get + { + return nan; + } } /// @@ -164,7 +176,10 @@ namespace MathNet.Numerics /// A value representing the imaginary unit number. public static Complex I { - get { return i; } + get + { + return i; + } } /// @@ -173,7 +188,10 @@ namespace MathNet.Numerics /// A value representing the zero value. public static Complex Zero { - get { return new Complex(0.0, 0.0); } + get + { + return new Complex(0.0, 0.0); + } } /// @@ -182,7 +200,10 @@ namespace MathNet.Numerics /// A value representing the 1 value. public static Complex One { - get { return one; } + get + { + return one; + } } #endregion Properties @@ -193,7 +214,10 @@ namespace MathNet.Numerics /// The real component of the complex number. public double Real { - get { return _real; } + get + { + return this._real; + } } /// @@ -202,7 +226,10 @@ namespace MathNet.Numerics /// The real imaginary component of the complex number. public double Imaginary { - get { return _imag; } + get + { + return this._imag; + } } /// @@ -211,7 +238,10 @@ namespace MathNet.Numerics /// true if this instance is zero; otherwise, false. public bool IsZero { - get { return _real.AlmostZero() && _imag.AlmostZero(); } + get + { + return this._real.AlmostZero() && this._imag.AlmostZero(); + } } /// @@ -220,7 +250,10 @@ namespace MathNet.Numerics /// true if this instance is one; otherwise, false. public bool IsOne { - get { return _real.AlmostEqual(1.0) && _imag.AlmostZero(); } + get + { + return this._real.AlmostEqual(1.0) && this._imag.AlmostZero(); + } } /// @@ -229,7 +262,10 @@ namespace MathNet.Numerics /// true if this instance is I; otherwise, false. public bool IsI { - get { return _real.AlmostZero() && _imag.AlmostEqual(1.0); } + get + { + return this._real.AlmostZero() && this._imag.AlmostEqual(1.0); + } } /// @@ -239,7 +275,10 @@ namespace MathNet.Numerics /// true if this instance is NaN; otherwise, false. public bool IsNaN { - get { return double.IsNaN(_real) || double.IsNaN(_imag); } + get + { + return double.IsNaN(this._real) || double.IsNaN(this._imag); + } } /// @@ -255,7 +294,10 @@ namespace MathNet.Numerics /// public bool IsInfinity { - get { return double.IsInfinity(_real) || double.IsInfinity(_imag); } + get + { + return double.IsInfinity(this._real) || double.IsInfinity(this._imag); + } } /// @@ -264,7 +306,10 @@ namespace MathNet.Numerics /// true if this instance is a real number; otherwise, false. public bool IsReal { - get { return _imag.AlmostZero(); } + get + { + return this._imag.AlmostZero(); + } } /// @@ -275,7 +320,10 @@ namespace MathNet.Numerics /// public bool IsRealNonNegative { - get { return _imag.AlmostZero() && _real >= 0; } + get + { + return this._imag.AlmostZero() && this._real >= 0; + } } /// @@ -295,7 +343,10 @@ namespace MathNet.Numerics /// public Complex Conjugate { - get { return new Complex(_real, -_imag); } + get + { + return new Complex(this._real, -this._imag); + } } /// @@ -304,7 +355,10 @@ namespace MathNet.Numerics /// public double Modulus { - get { return Math.Sqrt((_real * _real) + (_imag * _imag)); } + get + { + return Math.Sqrt((this._real * this._real) + (this._imag * this._imag)); + } } /// @@ -313,7 +367,10 @@ namespace MathNet.Numerics /// public double ModulusSquared { - get { return (_real * _real) + (_imag * _imag); } + get + { + return (this._real * this._real) + (this._imag * this._imag); + } } /// @@ -328,12 +385,12 @@ namespace MathNet.Numerics { get { - if (IsReal && _real < 0) + if (this.IsReal && this._real < 0) { return Math.PI; } - return IsRealNonNegative ? 0 : Math.Atan2(_imag, _real); + return this.IsRealNonNegative ? 0 : Math.Atan2(this._imag, this._real); } } @@ -344,34 +401,34 @@ namespace MathNet.Numerics { get { - if (double.IsPositiveInfinity(_real) && double.IsPositiveInfinity(_imag)) + if (double.IsPositiveInfinity(this._real) && double.IsPositiveInfinity(this._imag)) { return new Complex(Constants.Sqrt1Over2, Constants.Sqrt1Over2); } - if (double.IsPositiveInfinity(_real) && double.IsNegativeInfinity(_imag)) + if (double.IsPositiveInfinity(this._real) && double.IsNegativeInfinity(this._imag)) { return new Complex(Constants.Sqrt1Over2, -Constants.Sqrt1Over2); } - if (double.IsNegativeInfinity(_real) && double.IsPositiveInfinity(_imag)) + if (double.IsNegativeInfinity(this._real) && double.IsPositiveInfinity(this._imag)) { return new Complex(-Constants.Sqrt1Over2, -Constants.Sqrt1Over2); } - if (double.IsNegativeInfinity(_real) && double.IsNegativeInfinity(_imag)) + if (double.IsNegativeInfinity(this._real) && double.IsNegativeInfinity(this._imag)) { return new Complex(-Constants.Sqrt1Over2, Constants.Sqrt1Over2); } // don't replace this with "Modulus"! - var mod = SpecialFunctions.Hypotenuse(_real, _imag); + var mod = SpecialFunctions.Hypotenuse(this._real, this._imag); if (mod.AlmostZero()) { return Zero; } - return new Complex(_real / mod, _imag / mod); + return new Complex(this._real / mod, this._imag / mod); } } @@ -381,9 +438,15 @@ namespace MathNet.Numerics /// Constructs a Complex from its real /// and imaginary parts. /// - /// The value for the real component. - /// The value for the imaginary component. - /// A new Complex with the given values. + /// + /// The value for the real component. + /// + /// + /// The value for the imaginary component. + /// + /// + /// A new Complex with the given values. + /// public static Complex WithRealImaginary(double real, double imaginary) { return new Complex(real, imaginary); @@ -393,9 +456,15 @@ namespace MathNet.Numerics /// Constructs a Complex from its modulus and /// argument. /// - /// Must be non-negative. - /// Real number. - /// A new Complex from the given values. + /// + /// Must be non-negative. + /// + /// + /// Real number. + /// + /// + /// A new Complex from the given values. + /// public static Complex WithModulusArgument(double modulus, double argument) { if (modulus < 0.0) @@ -410,68 +479,90 @@ namespace MathNet.Numerics #region IFormattable Members - /// A string representation of this complex number. - /// The string representation of this complex number. + /// + /// A string representation of this complex number. + /// + /// + /// The string representation of this complex number. + /// public override string ToString() { - return ToString(null, null); + return this.ToString(null, null); } - /// A string representation of this complex number. + /// + /// A string representation of this complex number. + /// /// /// The string representation of this complex number formatted as specified by the /// format string. /// - /// A format specification. + /// + /// A format specification. + /// public string ToString(string format) { - return ToString(format, null); + return this.ToString(format, null); } - /// A string representation of this complex number. + /// + /// A string representation of this complex number. + /// /// /// The string representation of this complex number formatted as specified by the /// format provider. /// - /// An IFormatProvider that supplies culture-specific formatting information. + /// + /// An IFormatProvider that supplies culture-specific formatting information. + /// public string ToString(IFormatProvider formatProvider) { - return ToString(null, formatProvider); + return this.ToString(null, formatProvider); } - /// A string representation of this complex number. + /// + /// A string representation of this complex number. + /// /// /// The string representation of this complex number formatted as specified by the /// format string and format provider. /// - /// if the n, is not a number. - /// if s, is . - /// A format specification. - /// An IFormatProvider that supplies culture-specific formatting information. + /// + /// if the n, is not a number. + /// + /// + /// if s, is . + /// + /// + /// A format specification. + /// + /// + /// An IFormatProvider that supplies culture-specific formatting information. + /// public string ToString(string format, IFormatProvider formatProvider) { - if (IsNaN) + if (this.IsNaN) { return "NaN"; } - if (IsInfinity) + if (this.IsInfinity) { return "Infinity"; } var ret = new StringBuilder(); - if (!_real.AlmostZero()) + if (!this._real.AlmostZero()) { - ret.Append(_real.ToString(format, formatProvider)); + ret.Append(this._real.ToString(format, formatProvider)); } - if (!_imag.AlmostZero()) + if (!this._imag.AlmostZero()) { - if (!_real.AlmostZero()) + if (!this._real.AlmostZero()) { - if (_imag < 0) + if (this._imag < 0) { ret.Append(" "); } @@ -481,7 +572,7 @@ namespace MathNet.Numerics } } - ret.Append(_imag.ToString(format, formatProvider)).Append("i"); + ret.Append(this._imag.ToString(format, formatProvider)).Append("i"); } return ret.ToString(); @@ -499,29 +590,37 @@ namespace MathNet.Numerics /// Returns true if the two objects are the same object, or if their corresponding /// real and imaginary components are equal, false otherwise. /// - /// The complex number to compare to with. + /// + /// The complex number to compare to with. + /// public bool Equals(Complex other) { - if (IsNaN || other.IsNaN) + if (this.IsNaN || other.IsNaN) { return false; } - if( IsInfinity && other.IsInfinity ) + + if (this.IsInfinity && other.IsInfinity) { return true; } - return _real.AlmostEqual(other._real) && _imag.AlmostEqual(other._imag); + + return this._real.AlmostEqual(other._real) && this._imag.AlmostEqual(other._imag); } - /// The hash code for the complex number. - /// The hash code of the complex number. + /// + /// The hash code for the complex number. + /// + /// + /// The hash code of the complex number. + /// /// /// The hash code is calculated as /// System.Math.Exp(ComplexMath.Absolute(complexNumber)). /// public override int GetHashCode() { - return _real.GetHashCode() ^ (-_imag.GetHashCode()); + return this._real.GetHashCode() ^ (-this._imag.GetHashCode()); } /// @@ -532,10 +631,12 @@ namespace MathNet.Numerics /// Returns true if the two objects are the same object, or if their corresponding /// real and imaginary components are equal, false otherwise. /// - /// The complex number to compare to with. + /// + /// The complex number to compare to with. + /// public override bool Equals(object obj) { - return (obj is Complex) && Equals((Complex) obj); + return (obj is Complex) && this.Equals((Complex)obj); } #endregion @@ -644,7 +745,9 @@ namespace MathNet.Numerics /// The other complex number to multiply. public static Complex operator *(Complex multiplicand, Complex multiplier) { - return new Complex((multiplicand._real * multiplier._real) - (multiplicand._imag * multiplier._imag), (multiplicand._real * multiplier._imag) + (multiplicand._imag * multiplier._real)); + return new Complex( + (multiplicand._real * multiplier._real) - (multiplicand._imag * multiplier._imag), + (multiplicand._real * multiplier._imag) + (multiplicand._imag * multiplier._real)); } /// Multiplication operator. Multiplies a complex number with a double value. @@ -677,7 +780,9 @@ namespace MathNet.Numerics } var modSquared = divisor.ModulusSquared; - return new Complex(((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared); + return new Complex( + ((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, + ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared); } /// Division operator. Divides a double value by a complex number. @@ -722,7 +827,9 @@ namespace MathNet.Numerics /// /// Unary addition. /// - /// Returns the same complex number. + /// + /// Returns the same complex number. + /// public Complex Plus() { return this; @@ -731,44 +838,91 @@ namespace MathNet.Numerics /// /// Unary minus. /// - /// The negated value of this complex number. + /// + /// The negated value of this complex number. + /// public Complex Negate() { return -this; } - /// Adds a complex number to this one. - /// The result of the addition. - /// The other complex number to add. + /// + /// Adds a complex number to this one. + /// + /// + /// The result of the addition. + /// + /// + /// The other complex number to add. + /// public Complex Add(Complex other) { return this + other; } - /// Subtracts a complex number from this one. - /// The result of the subtraction. - /// The other complex number to subtract from this one. + /// + /// Subtracts a complex number from this one. + /// + /// + /// The result of the subtraction. + /// + /// + /// The other complex number to subtract from this one. + /// public Complex Subtract(Complex other) { return this - other; } - /// Multiplies this complex number with this one. - /// The result of the multiplication. - /// The complex number to multiply. + /// + /// Multiplies this complex number with this one. + /// + /// + /// The result of the multiplication. + /// + /// + /// The complex number to multiply. + /// public Complex Multiply(Complex multiplier) { return this * multiplier; } - /// Divides this complex number by another. - /// The result of the division. - /// The divisor. + /// + /// Divides this complex number by another. + /// + /// + /// The result of the division. + /// + /// + /// The divisor. + /// public Complex Divide(Complex divisor) { return this / divisor; } #endregion + + #region Trigonometric Functions + + /// + /// Trigonometric Sine (sin, Sinus) of this Complex. + /// + /// + /// The sine of the complex number. + /// + public Complex Sine() + { + if (this.IsReal) + { + return new Complex(Math.Sin(this._real), 0.0); + } + + return new Complex( + Math.Sin(this._real) * Math.Cosh(this._imag), Math.Cos(this._real) * Math.Sinh(this._imag)); + } + + #endregion } } \ No newline at end of file diff --git a/src/Native.UnitTests/Native.UnitTests.csproj b/src/Native.UnitTests/Native.UnitTests.csproj index 22070fe8..f8e65dde 100644 --- a/src/Native.UnitTests/Native.UnitTests.csproj +++ b/src/Native.UnitTests/Native.UnitTests.csproj @@ -65,8 +65,8 @@ CombinatoricsTests\CombinatoricsCountingTest.cs - - ComplexTest.cs + + ComplexTests\ComplexTest.cs DistributionTests\Continuous\ContinuousUniformTests.cs