diff --git a/src/Numerics/Complex.cs b/src/Numerics/Complex.cs index 895d7420..4456487e 100644 --- a/src/Numerics/Complex.cs +++ b/src/Numerics/Complex.cs @@ -162,7 +162,7 @@ namespace MathNet.Numerics /// Gets a value representing the imaginary unit number. This field is constant. /// /// A value representing the imaginary unit number. - public static Complex I + public static Complex ImaginaryOne { get { return _i; } } @@ -226,8 +226,8 @@ namespace MathNet.Numerics /// /// Gets a value indicating whether the Complex is the imaginary unit. /// - /// true if this instance is I; otherwise, false. - public bool IsI + /// true if this instance is ImaginaryOne; otherwise, false. + public bool IsImaginaryOne { get { return _real == 0.0 && _imag == 1.0; } } @@ -302,32 +302,32 @@ namespace MathNet.Numerics } /// - /// Gets or modulus of this Complex. + /// Gets the magnitude or modulus of this Complex. /// - /// - public double Modulus + /// + public double Magnitude { get { return Math.Sqrt((_real * _real) + (_imag * _imag)); } } /// - /// Gets the squared modulus of this Complex. + /// Gets the squared magnitude of this Complex. /// - /// - public double ModulusSquared + /// + public double MagnitudeSquared { get { return (_real * _real) + (_imag * _imag); } } /// - /// Gets argument of this Complex. + /// Gets phase or argument of this Complex. /// /// - /// Argument always returns a value bigger than negative Pi and + /// Phase always returns a value bigger than negative Pi and /// smaller or equal to Pi. If this Complex is zero, the Complex /// is assumed to be positive real with an argument of zero. /// - public double Argument + public double Phase { get { @@ -367,7 +367,7 @@ namespace MathNet.Numerics return new Complex(-Constants.Sqrt1Over2, Constants.Sqrt1Over2); } - // don't replace this with "Modulus"! + // don't replace this with "Magnitude"! var mod = SpecialFunctions.Hypotenuse(_real, _imag); if (mod == 0.0) { @@ -410,7 +410,7 @@ namespace MathNet.Numerics return new Complex(Math.Log(_real), 0.0); } - return new Complex(0.5 * Math.Log(ModulusSquared), Argument); + return new Complex(0.5 * Math.Log(MagnitudeSquared), Phase); } /// @@ -883,7 +883,7 @@ namespace MathNet.Numerics return Infinity; } - var modSquared = divisor.ModulusSquared; + var modSquared = divisor.MagnitudeSquared; return new Complex( ((dividend._real * divisor._real) + (dividend._imag * divisor._imag)) / modSquared, ((dividend._imag * divisor._real) - (dividend._real * divisor._imag)) / modSquared); @@ -900,7 +900,7 @@ namespace MathNet.Numerics return Infinity; } - var zmod = divisor.ModulusSquared; + var zmod = divisor.MagnitudeSquared; return new Complex(dividend * divisor._real / zmod, -dividend * divisor._imag / zmod); } @@ -918,16 +918,6 @@ namespace MathNet.Numerics return new Complex(dividend._real / divisor, dividend._imag / divisor); } - /// - /// Implicit conversion of a real double to a real Complex. - /// - /// The double value to convert. - /// The result of the conversion. - public static implicit operator Complex(double number) - { - return new Complex(number, 0.0); - } - /// /// Unary addition. /// @@ -1019,7 +1009,7 @@ namespace MathNet.Numerics /// double IPrecisionSupport.Norm() { - return ModulusSquared; + return MagnitudeSquared; } /// @@ -1034,7 +1024,7 @@ namespace MathNet.Numerics /// double IPrecisionSupport.NormOfDifference(Complex otherValue) { - return (this - otherValue).ModulusSquared; + return (this - otherValue).MagnitudeSquared; } #endregion @@ -1286,5 +1276,336 @@ namespace MathNet.Numerics } #endregion + + #region Conversion + /// + /// Explicit conversion of a real decimal to a real Complex. + /// + /// The decimal value to convert. + /// The result of the conversion. + public static explicit operator Complex(decimal value) + { + return new Complex((double)value, 0.0); + } + + /// + /// Implicit conversion of a real byte to a real Complex. + /// + /// The byte value to convert. + /// The result of the conversion. + public static implicit operator Complex(byte value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real short to a real Complex. + /// + /// The short value to convert. + /// The result of the conversion. + public static implicit operator Complex(short value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real int to a real Complex. + /// + /// The int value to convert. + /// The result of the conversion. + public static implicit operator Complex(int value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real long to a real Complex. + /// + /// The long value to convert. + /// The result of the conversion. + public static implicit operator Complex(long value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real uint to a real Complex. + /// + /// The uint value to convert. + /// The result of the conversion. + public static implicit operator Complex(uint value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real ulong to a real Complex. + /// + /// The ulong value to convert. + /// The result of the conversion. + public static implicit operator Complex(ulong value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real float to a real Complex. + /// + /// The float value to convert. + /// The result of the conversion. + public static implicit operator Complex(float value) + { + return new Complex(value, 0.0); + } + + /// + /// Implicit conversion of a real double to a real Complex. + /// + /// The double value to convert. + /// The result of the conversion. + public static implicit operator Complex(double value) + { + return new Complex(value, 0.0); + } + + #endregion + + #region Static methods from .NET 4.0 + /// + /// Gets the absolute value (or magnitude) of a complex number. + /// + /// A complex number. + /// The absolute value (or magnitude) of a complex number. + public static double Abs(Complex value) + { + return value.Magnitude; + } + + /// + /// Trigonometric Arc Cosine of a Complex number. + /// + /// A complex number. + /// + /// The arc cosine of a complex number. + /// + public static Complex Acos(Complex value) + { + return value.InverseCosine(); + } + + /// + /// Trigonometric Arc Sine of a Complex number. + /// + /// A complex number. + /// + /// The arc sine of a complex number. + /// + public static Complex Asin(Complex value) + { + return value.InverseSine(); + } + + /// + /// Trigonometric Arc Tangent of a Complex number. + /// + /// A complex number. + /// + /// The arc tangent of a complex number. + /// + public static Complex Atan(Complex value) + { + return value.InverseTangent(); + } + + /// + /// Trigonometric Cosine of a Complex number. + /// + /// A complex number. + /// + /// The cosine of a complex number. + /// + public static Complex Cos(Complex value) + { + return value.Cosine(); + } + + /// + /// Trigonometric Sine of a Complex number. + /// + /// A complex number. + /// + /// The Sine of a complex number. + /// + public static Complex Sin(Complex value) + { + return value.Sine(); + } + + /// + /// Trigonometric Tangent of a Complex number. + /// + /// A complex number. + /// + /// The tangent of a complex number. + /// + public static Complex Tan(Complex value) + { + return value.Tangent(); + } + + /// + /// Trigonometric Hyperbolic Cosine of a Complex number. + /// + /// A complex number. + /// + /// The hyperbolic cosine of a complex number. + /// + public static Complex Cosh(Complex value) + { + return value.HyperbolicCosine(); + } + + /// + /// Trigonometric Hyperbolic Sine of a Complex number. + /// + /// A complex number. + /// + /// The hyperbolic sine of a complex number. + /// + public static Complex Sinh(Complex value) + { + return value.HyperbolicSine(); + } + + /// + /// Trigonometric Hyperbolic Tangent of a Complex number. + /// + /// A complex number. + /// + /// The hyperbolic tangent of a complex number. + /// + public static Complex Tanh(Complex value) + { + return value.HyperbolicTangent(); + } + + /// + /// Exponential of a Complex number (exp(x), E^x). + /// + /// A complex number. + /// + /// The exponential of a complex number. + /// + public static Complex Exp(Complex value) + { + return value.Exponential(); + } + + /// + /// Constructs a Complex from its magnitude and phase. + /// + /// + /// Must be non-negative. + /// + /// + /// Real number. + /// + /// + /// A new Complex from the given values. + /// + /// + public static Complex FromPolarCoordinates(double magnitude, double phase) + { + return WithModulusArgument(magnitude, phase); + } + + /// + /// Natural Logarithm of a Complex number (exp(x), E^x). + /// + /// A complex number. + /// + /// The natural logarithm of a complex number. + /// + public static Complex Log(Complex value) + { + return value.NaturalLogarithm(); + } + + /// + /// Returns the logarithm of a specified complex number in a specified base + /// + /// A complex number. + /// The base of the logarithm. + /// The logarithm of value in base baseValue. + public static Complex Log(Complex value, double baseValue) + { + throw new NotImplementedException(); + } + + /// + /// Returns the base-10 logarithm of a specified complex number in a specified base + /// + /// A complex number. + /// The base-10 logarithm of the complex number. + public static Complex Log10(Complex value) + { + return Log(value, 10); + } + + /// + /// Raise this a Complexnumber to the given value. + /// + /// A complex number. + /// The exponent. + /// + /// The complex number raised to the given exponent. + /// + public static Complex Pow(Complex value, Complex power) + { + return value.Power(power); + } + + /// + /// Raise this a Complexnumber to the given value. + /// + /// A complex number. + /// The exponent. + /// + /// The complex number raised to the given exponent. + /// + public static Complex Pow(Complex value, double power) + { + return value.Power(power); + } + + /// + /// Returns the multiplicative inverse of a complex number. + /// + /// A complex number. + /// The reciprocal of value. + /// If value is , the method returns . Otherwise, it returns the result of the expression / value. + public static Complex Reciprocal(Complex value) + { + if (value.IsZero) + { + return _zero; + } + + return 1.0 / value; + } + + /// + /// The Square Root (power 1/2) of a Complex number. + /// + /// A complex number. + /// + /// The square root of a complex number. + /// + public static Complex Sqrt(Complex value) + { + return value.SquareRoot(); + } + + #endregion } } \ No newline at end of file diff --git a/src/Numerics/Trigonometry.cs b/src/Numerics/Trigonometry.cs index 8fcecba1..1f80b090 100644 --- a/src/Numerics/Trigonometry.cs +++ b/src/Numerics/Trigonometry.cs @@ -490,7 +490,7 @@ namespace MathNet.Numerics public static Complex InverseCosecant(this Complex value) { var inv = 1 / value; - return -Complex.I * ((Complex.I * inv) + (1 - inv.Square()).SquareRoot()).NaturalLogarithm(); + return -Complex.ImaginaryOne * ((Complex.ImaginaryOne * inv) + (1 - inv.Square()).SquareRoot()).NaturalLogarithm(); } /// @@ -521,7 +521,7 @@ namespace MathNet.Numerics /// public static Complex InverseCosine(this Complex value) { - return -Complex.I * (value + (Complex.I * (1 - value.Square()).SquareRoot())).NaturalLogarithm(); + return -Complex.ImaginaryOne * (value + (Complex.ImaginaryOne * (1 - value.Square()).SquareRoot())).NaturalLogarithm(); } /// @@ -554,8 +554,8 @@ namespace MathNet.Numerics return Math.PI / 2.0; } - var inv = Complex.I / value; - return (Complex.I * 0.5) * ((1.0 - inv).NaturalLogarithm() - (1.0 + inv).NaturalLogarithm()); + var inv = Complex.ImaginaryOne / value; + return (Complex.ImaginaryOne * 0.5) * ((1.0 - inv).NaturalLogarithm() - (1.0 + inv).NaturalLogarithm()); } /// @@ -755,7 +755,7 @@ namespace MathNet.Numerics public static Complex InverseSecant(this Complex value) { var inv = 1 / value; - return -Complex.I * (inv + (Complex.I * (1 - inv.Square()).SquareRoot())).NaturalLogarithm(); + return -Complex.ImaginaryOne * (inv + (Complex.ImaginaryOne * (1 - inv.Square()).SquareRoot())).NaturalLogarithm(); } /// @@ -783,7 +783,7 @@ namespace MathNet.Numerics /// public static Complex InverseSine(this Complex value) { - return -Complex.I * ((1 - value.Square()).SquareRoot() + (Complex.I * value)).NaturalLogarithm(); + return -Complex.ImaginaryOne * ((1 - value.Square()).SquareRoot() + (Complex.ImaginaryOne * value)).NaturalLogarithm(); } /// diff --git a/src/UnitTests/ComplexTests/ComplexTest.cs b/src/UnitTests/ComplexTests/ComplexTest.cs index 89ace103..61259edc 100644 --- a/src/UnitTests/ComplexTests/ComplexTest.cs +++ b/src/UnitTests/ComplexTests/ComplexTest.cs @@ -265,7 +265,7 @@ namespace MathNet.Numerics.UnitTests.ComplexTests public void CanDetermineIfImaginaryUnit() { var complex = new Complex(0, 1); - Assert.IsTrue(complex.IsI, "Imaginary unit"); + Assert.IsTrue(complex.IsImaginaryOne, "Imaginary unit"); } [Test] @@ -458,9 +458,9 @@ namespace MathNet.Numerics.UnitTests.ComplexTests [Row(0.0, 1.0, 1.0)] [Row(-1.0, 1.0, 1.4142135623730951)] [Row(-111.1, 111.1, 157.11912677965086)] - public void CanComputeModulus(double real, double imag, double expected) + public void CanComputeMagnitude(double real, double imag, double expected) { - Assert.AreEqual(expected, new Complex(real, imag).Modulus); + Assert.AreEqual(expected, new Complex(real, imag).Magnitude); } [Test] diff --git a/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs b/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs index 84b70c19..cf2afbc0 100644 --- a/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs +++ b/src/UnitTests/IntegralTransformsTests/ParsevalTheoremTest.cs @@ -48,7 +48,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests { var samples = Sample.Random((u, v) => new Complex(u, v), _uniform, count); - var timeSpaceEnergy = (from s in samples select s.ModulusSquared).Mean(); + var timeSpaceEnergy = (from s in samples select s.MagnitudeSquared).Mean(); var work = new Complex[samples.Length]; samples.CopyTo(work, 0); @@ -56,7 +56,7 @@ namespace MathNet.Numerics.UnitTests.IntegralTransformsTests // Default -> Symmetric Scaling Transform.FourierForward(work); - var frequencySpaceEnergy = (from s in work select s.ModulusSquared).Mean(); + var frequencySpaceEnergy = (from s in work select s.MagnitudeSquared).Mean(); Assert.AreApproximatelyEqual(timeSpaceEnergy, frequencySpaceEnergy, 1e-12); }