Browse Source

fixed style cop errors in the complex files

la-knuth
Marcus Cuda 17 years ago
parent
commit
d8f580b8b8
  1. 214
      src/Numerics/Complex32.cs
  2. 140
      src/Numerics/ComplexExtensions.cs

214
src/Numerics/Complex32.cs

@ -1,9 +1,7 @@
// <copyright file="Complex32.cs" company="Math.NET"> // <copyright file="Complex32.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-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
@ -12,10 +10,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
@ -131,8 +127,8 @@ namespace MathNet.Numerics
#endif #endif
public Complex32(float real, float imaginary) public Complex32(float real, float imaginary)
{ {
_real = real; this._real = real;
_imag = imaginary; this._imag = imaginary;
} }
#endregion #endregion
@ -153,7 +149,10 @@ namespace MathNet.Numerics
/// <value>A value representing the infinity value.</value> /// <value>A value representing the infinity value.</value>
public static Complex32 Infinity public static Complex32 Infinity
{ {
get { return _infinity; } get
{
return _infinity;
}
} }
/// <summary> /// <summary>
@ -162,7 +161,10 @@ namespace MathNet.Numerics
/// <value>A value representing not-a-number.</value> /// <value>A value representing not-a-number.</value>
public static Complex32 NaN public static Complex32 NaN
{ {
get { return _nan; } get
{
return _nan;
}
} }
/// <summary> /// <summary>
@ -171,7 +173,10 @@ namespace MathNet.Numerics
/// <value>A value representing the imaginary unit number.</value> /// <value>A value representing the imaginary unit number.</value>
public static Complex32 ImaginaryOne public static Complex32 ImaginaryOne
{ {
get { return _i; } get
{
return _i;
}
} }
/// <summary> /// <summary>
@ -180,7 +185,10 @@ namespace MathNet.Numerics
/// <value>A value representing the zero value.</value> /// <value>A value representing the zero value.</value>
public static Complex32 Zero public static Complex32 Zero
{ {
get { return new Complex32(0.0f, 0.0f); } get
{
return new Complex32(0.0f, 0.0f);
}
} }
/// <summary> /// <summary>
@ -189,7 +197,10 @@ 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 Complex32 One public static Complex32 One
{ {
get { return _one; } get
{
return _one;
}
} }
#endregion Properties #endregion Properties
@ -203,8 +214,10 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
get
get { return _real; } {
return this._real;
}
} }
/// <summary> /// <summary>
@ -216,84 +229,86 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
get
get { return _imag; } {
return this._imag;
}
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is zero. /// Gets a value indicating whether the <c>Complex32</c> is zero.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is zero; otherwise, <c>false</c>.</value> /// <returns><c>true</c> if this instance is zero; otherwise, <c>false</c>.</returns>
public bool IsZero() public bool IsZero()
{ {
return _real == 0.0f && _imag == 0.0f; return this._real == 0.0f && this._imag == 0.0f;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is one. /// Gets a value indicating whether the <c>Complex32</c> is one.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is one; otherwise, <c>false</c>.</value> /// <returns><c>true</c> if this instance is one; otherwise, <c>false</c>.</returns>
public bool IsOne() public bool IsOne()
{ {
return _real == 1.0f && _imag == 0.0f; return this._real == 1.0f && this._imag == 0.0f;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is the imaginary unit. /// Gets a value indicating whether the <c>Complex32</c> is the imaginary unit.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</value> /// <returns><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</returns>
public bool IsImaginaryOne() public bool IsImaginaryOne()
{ {
return _real == 0.0f && _imag == 1.0f; return this._real == 0.0f && this._imag == 1.0f;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c>evaluates /// Gets a value indicating whether the provided <c>Complex32</c>evaluates
/// to a value that is not a number. /// to a value that is not a number.
/// </summary> /// </summary>
/// <value> /// <returns>
/// <c>true</c> if this instance is <see cref="NaN"/>; otherwise, /// <c>true</c> if this instance is <see cref="NaN"/>; otherwise,
/// <c>false</c>. /// <c>false</c>.
/// </value> /// </returns>
public bool IsNaN() public bool IsNaN()
{ {
return float.IsNaN(_real) || float.IsNaN(_imag); return float.IsNaN(this._real) || float.IsNaN(this._imag);
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> evaluates to an /// Gets a value indicating whether the provided <c>Complex32</c> evaluates to an
/// infinite value. /// infinite value.
/// </summary> /// </summary>
/// <value> /// <returns>
/// <c>true</c> if this instance is infinite; otherwise, <c>false</c>. /// <c>true</c> if this instance is infinite; otherwise, <c>false</c>.
/// </value> /// </returns>
/// <remarks> /// <remarks>
/// True if it either evaluates to a complex infinity /// True if it either evaluates to a complex infinity
/// or to a directed infinity. /// or to a directed infinity.
/// </remarks> /// </remarks>
public bool IsInfinity() public bool IsInfinity()
{ {
return float.IsInfinity(_real) || float.IsInfinity(_imag); return float.IsInfinity(this._real) || float.IsInfinity(this._imag);
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> is real. /// Gets a value indicating whether the provided <c>Complex32</c> is real.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</value> /// <returns><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</returns>
public bool IsReal() public bool IsReal()
{ {
return _imag == 0.0f; return this._imag == 0.0f;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> is real and not negative, that is &gt;= 0. /// Gets a value indicating whether the provided <c>Complex32</c> is real and not negative, that is &gt;= 0.
/// </summary> /// </summary>
/// <value> /// <returns>
/// <c>true</c> if this instance is real nonnegative number; otherwise, <c>false</c>. /// <c>true</c> if this instance is real nonnegative number; otherwise, <c>false</c>.
/// </value> /// </returns>
public bool IsRealNonNegative() public bool IsRealNonNegative()
{ {
return _imag == 0.0f && _real >= 0; return this._imag == 0.0f && this._real >= 0;
} }
/// <summary> /// <summary>
@ -311,85 +326,95 @@ namespace MathNet.Numerics
/// a = b.Conjugate /// a = b.Conjugate
/// </code> /// </code>
/// </remarks> /// </remarks>
/// <returns>The conjugate of this <c>Complex32</c></returns>
public Complex32 Conjugate() public Complex32 Conjugate()
{ {
return new Complex32(_real, -_imag); return new Complex32(this._real, -this._imag);
} }
/// <summary> /// <summary>
/// Gets the magnitude or modulus of this <c>Complex32</c>. /// Gets the magnitude or modulus of this <c>Complex32</c>.
/// </summary> /// </summary>
/// <returns>The magnitude or modulus of this <c>Complex32</c></returns>
/// <seealso cref="Phase"/> /// <seealso cref="Phase"/>
public float Magnitude public float Magnitude
{ {
get { return (float)Math.Sqrt((_real * _real) + (_imag * _imag)); } get
{
return (float)Math.Sqrt((this._real * this._real) + (this._imag * this._imag));
}
} }
/// <summary> /// <summary>
/// Gets the squared magnitude of this <c>Complex32</c>. /// Gets the squared magnitude of this <c>Complex32</c>.
/// </summary> /// </summary>
/// <seealso cref="Phase"/> /// <returns>The squared magnitude of this <c>Complex32</c></returns>
public float MagnitudeSquared public float MagnitudeSquared
{ {
get { return (_real * _real) + (_imag * _imag); } get
{
return (this._real * this._real) + (this._imag * this._imag);
}
} }
/// <summary> /// <summary>
/// Gets phase or argument of this <c>Complex32</c>. /// Gets the phase or argument of this <c>Complex32</c>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Phase 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 <c>Complex32</c> is zero, the Complex32 /// smaller or equal to Pi. If this <c>Complex32</c> is zero, the Complex32
/// is assumed to be positive real with an argument of zero. /// is assumed to be positive real with an argument of zero.
/// </remarks> /// </remarks>
/// <returns>The phase or argument of this <c>Complex32</c></returns>
public float Phase public float Phase
{ {
get get
{ {
if (IsReal() && _real < 0) if (this.IsReal() && this._real < 0)
{ {
return (float)Math.PI; return (float)Math.PI;
} }
return IsRealNonNegative() ? 0.0f : (float)Math.Atan2(_imag, _real); return this.IsRealNonNegative() ? 0.0f : (float)Math.Atan2(this._imag, this._real);
} }
} }
/// <summary> /// <summary>
/// Gets the unity of this complex (same argument, but on the unit circle; exp(I*arg)) /// Gets the unity of this complex (same argument, but on the unit circle; exp(I*arg))
/// </summary> /// </summary>
/// <returns>The unity of this <c>Complex32</c>.</returns>
public Complex32 Sign public Complex32 Sign
{ {
get get
{ {
if (float.IsPositiveInfinity(_real) && float.IsPositiveInfinity(_imag)) if (float.IsPositiveInfinity(this._real) && float.IsPositiveInfinity(this._imag))
{ {
return new Complex32((float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2); return new Complex32((float)Constants.Sqrt1Over2, (float)Constants.Sqrt1Over2);
} }
if (float.IsPositiveInfinity(_real) && float.IsNegativeInfinity(_imag)) if (float.IsPositiveInfinity(this._real) && float.IsNegativeInfinity(this._imag))
{ {
return new Complex32((float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2); return new Complex32((float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2);
} }
if (float.IsNegativeInfinity(_real) && float.IsPositiveInfinity(_imag)) if (float.IsNegativeInfinity(this._real) && float.IsPositiveInfinity(this._imag))
{ {
return new Complex32(-(float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2); return new Complex32(-(float)Constants.Sqrt1Over2, -(float)Constants.Sqrt1Over2);
} }
if (float.IsNegativeInfinity(_real) && float.IsNegativeInfinity(_imag)) if (float.IsNegativeInfinity(this._real) && float.IsNegativeInfinity(this._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(_real, _imag); var mod = SpecialFunctions.Hypotenuse(this._real, this._imag);
if (mod == 0.0f) if (mod == 0.0f)
{ {
return Zero; return Zero;
} }
return new Complex32((float)(_real / mod), (float)(_imag / mod)); return new Complex32((float)(this._real / mod), (float)(this._imag / mod));
} }
} }
@ -403,13 +428,13 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Exponential() public Complex32 Exponential()
{ {
var exp = (float)Math.Exp(_real); var exp = (float)Math.Exp(this._real);
if (IsReal()) if (this.IsReal())
{ {
return new Complex32(exp, 0.0f); return new Complex32(exp, 0.0f);
} }
return new Complex32(exp * (float)Trig.Cosine(_imag), exp * (float)Trig.Sine(_imag)); return new Complex32(exp * (float)Trig.Cosine(this._imag), exp * (float)Trig.Sine(this._imag));
} }
/// <summary> /// <summary>
@ -420,12 +445,12 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 NaturalLogarithm() public Complex32 NaturalLogarithm()
{ {
if (IsRealNonNegative()) if (this.IsRealNonNegative())
{ {
return new Complex32((float)Math.Log(_real), 0.0f); return new Complex32((float)Math.Log(this._real), 0.0f);
} }
return new Complex32(0.5f * (float)Math.Log(MagnitudeSquared), Phase); return new Complex32(0.5f * (float)Math.Log(this.MagnitudeSquared), this.Phase);
} }
/// <summary> /// <summary>
@ -439,7 +464,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Power(Complex32 exponent) public Complex32 Power(Complex32 exponent)
{ {
if (IsZero()) if (this.IsZero())
{ {
if (exponent.IsZero()) if (exponent.IsZero())
{ {
@ -464,7 +489,7 @@ namespace MathNet.Numerics
return NaN; return NaN;
} }
return (exponent * NaturalLogarithm()).Exponential(); return (exponent * this.NaturalLogarithm()).Exponential();
} }
/// <summary> /// <summary>
@ -478,7 +503,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Root(Complex32 rootExponent) public Complex32 Root(Complex32 rootExponent)
{ {
return Power(1 / rootExponent); return this.Power(1 / rootExponent);
} }
/// <summary> /// <summary>
@ -489,12 +514,12 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 Square() public Complex32 Square()
{ {
if (IsReal()) if (this.IsReal())
{ {
return new Complex32(_real * _real, 0.0f); return new Complex32(this._real * this._real, 0.0f);
} }
return new Complex32((_real * _real) - (_imag * _imag), 2 * _real * _imag); return new Complex32((this._real * this._real) - (this._imag * this._imag), 2 * this._real * this._imag);
} }
/// <summary> /// <summary>
@ -505,32 +530,32 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public Complex32 SquareRoot() public Complex32 SquareRoot()
{ {
if (IsRealNonNegative()) if (this.IsRealNonNegative())
{ {
return new Complex32((float)Math.Sqrt(_real), 0.0f); return new Complex32((float)Math.Sqrt(this._real), 0.0f);
} }
Complex32 result; Complex32 result;
var absReal = Math.Abs(Real); var absReal = Math.Abs(this.Real);
var absImag = Math.Abs(Imaginary); var absImag = Math.Abs(this.Imaginary);
double w; double w;
if (absReal >= absImag) if (absReal >= absImag)
{ {
var ratio = Imaginary / Real; var ratio = this.Imaginary / this.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 = Real / Imaginary; var ratio = this.Real / this.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 (Real >= 0.0f) if (this.Real >= 0.0f)
{ {
result = new Complex32((float)w, (float)(Imaginary / (2.0f * w))); result = new Complex32((float)w, (float)(this.Imaginary / (2.0f * w)));
} }
else if (Imaginary >= 0.0f) else if (this.Imaginary >= 0.0f)
{ {
result = new Complex32((float)(absImag / (2.0 * w)), (float)w); result = new Complex32((float)(absImag / (2.0 * w)), (float)w);
} }
@ -599,7 +624,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
public override string ToString() public override string ToString()
{ {
return ToString(null, null); return this.ToString(null, null);
} }
/// <summary> /// <summary>
@ -614,7 +639,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public string ToString(string format) public string ToString(string format)
{ {
return ToString(format, null); return this.ToString(format, null);
} }
/// <summary> /// <summary>
@ -629,7 +654,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public string ToString(IFormatProvider formatProvider) public string ToString(IFormatProvider formatProvider)
{ {
return ToString(null, formatProvider); return this.ToString(null, formatProvider);
} }
/// <summary> /// <summary>
@ -655,28 +680,28 @@ namespace MathNet.Numerics
{ {
var numberFormatInfo = formatProvider.GetNumberFormatInfo(); var numberFormatInfo = formatProvider.GetNumberFormatInfo();
if (IsNaN()) if (this.IsNaN())
{ {
return numberFormatInfo.NaNSymbol; return numberFormatInfo.NaNSymbol;
} }
if (IsInfinity()) if (this.IsInfinity())
{ {
return numberFormatInfo.PositiveInfinitySymbol; return numberFormatInfo.PositiveInfinitySymbol;
} }
var ret = new StringBuilder(); var ret = new StringBuilder();
if (_real != 0.0f) if (this._real != 0.0f)
{ {
ret.Append(_real.ToString(format, formatProvider)); ret.Append(this._real.ToString(format, formatProvider));
} }
if (_imag != 0.0f) if (this._imag != 0.0f)
{ {
if (_real != 0.0f) if (this._real != 0.0f)
{ {
if (_imag < 0) if (this._imag < 0)
{ {
ret.Append(" "); ret.Append(" ");
} }
@ -686,7 +711,7 @@ namespace MathNet.Numerics
} }
} }
ret.Append(_imag.ToString(format, formatProvider)).Append("i"); ret.Append(this._imag.ToString(format, formatProvider)).Append("i");
} }
if (ret.Length == 0) if (ret.Length == 0)
@ -714,17 +739,17 @@ namespace MathNet.Numerics
/// </param> /// </param>
public bool Equals(Complex32 other) public bool Equals(Complex32 other)
{ {
if (IsNaN() || other.IsNaN()) if (this.IsNaN() || other.IsNaN())
{ {
return false; return false;
} }
if (IsInfinity() && other.IsInfinity()) if (this.IsInfinity() && other.IsInfinity())
{ {
return true; return true;
} }
return _real.AlmostEqual(other._real) && _imag.AlmostEqual(other._imag); return this._real.AlmostEqual(other._real) && this._imag.AlmostEqual(other._imag);
} }
/// <summary> /// <summary>
@ -739,7 +764,7 @@ namespace MathNet.Numerics
/// </remarks> /// </remarks>
public override int GetHashCode() public override int GetHashCode()
{ {
return _real.GetHashCode() ^ (-_imag.GetHashCode()); return this._real.GetHashCode() ^ (-this._imag.GetHashCode());
} }
/// <summary> /// <summary>
@ -755,7 +780,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return (obj is Complex32) && Equals((Complex32)obj); return (obj is Complex32) && this.Equals((Complex32)obj);
} }
#endregion #endregion
@ -865,7 +890,7 @@ namespace MathNet.Numerics
public static Complex32 operator *(Complex32 multiplicand, Complex32 multiplier) public static Complex32 operator *(Complex32 multiplicand, Complex32 multiplier)
{ {
return new Complex32( return new Complex32(
(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));
} }
@ -900,7 +925,7 @@ namespace MathNet.Numerics
var modSquared = divisor.MagnitudeSquared; var modSquared = divisor.MagnitudeSquared;
return new Complex32( return new Complex32(
((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);
} }
@ -942,7 +967,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Plus() public Complex32 Plus()
{ {
return this; return this;
@ -957,7 +981,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Negate() public Complex32 Negate()
{ {
return -this; return -this;
@ -975,7 +998,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Add(Complex32 other) public Complex32 Add(Complex32 other)
{ {
return this + other; return this + other;
@ -993,7 +1015,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Subtract(Complex32 other) public Complex32 Subtract(Complex32 other)
{ {
return this - other; return this - other;
@ -1011,7 +1032,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Multiply(Complex32 multiplier) public Complex32 Multiply(Complex32 multiplier)
{ {
return this * multiplier; return this * multiplier;
@ -1029,7 +1049,6 @@ namespace MathNet.Numerics
#if !SILVERLIGHT #if !SILVERLIGHT
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
#endif #endif
public Complex32 Divide(Complex32 divisor) public Complex32 Divide(Complex32 divisor)
{ {
return this / divisor; return this / divisor;
@ -1048,7 +1067,7 @@ namespace MathNet.Numerics
/// </returns> /// </returns>
double IPrecisionSupport<Complex32>.Norm() double IPrecisionSupport<Complex32>.Norm()
{ {
return MagnitudeSquared; return this.MagnitudeSquared;
} }
/// <summary> /// <summary>
@ -1131,8 +1150,8 @@ namespace MathNet.Numerics
var keywords = var keywords =
new[] new[]
{ {
textInfo.ListSeparator, numberFormatInfo.NaNSymbol, textInfo.ListSeparator, numberFormatInfo.NaNSymbol,
numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol,
"+", "-", "i", "j" "+", "-", "i", "j"
}; };
@ -1212,7 +1231,7 @@ namespace MathNet.Numerics
} }
} }
bool negative = false; var negative = false;
if (token.Value == "-") if (token.Value == "-")
{ {
negative = true; negative = true;
@ -1321,6 +1340,7 @@ namespace MathNet.Numerics
#endregion #endregion
#region Conversion #region Conversion
/// <summary> /// <summary>
/// Explicit conversion of a real decimal to a <c>Complex32</c>. /// Explicit conversion of a real decimal to a <c>Complex32</c>.
/// </summary> /// </summary>
@ -1444,7 +1464,7 @@ namespace MathNet.Numerics
{ {
return new Complex32(value, 0.0f); return new Complex32(value, 0.0f);
} }
/// <summary> /// <summary>
/// Implicit conversion of a real double to a <c>Complex32</c>. /// Implicit conversion of a real double to a <c>Complex32</c>.
/// </summary> /// </summary>
@ -1455,6 +1475,10 @@ namespace MathNet.Numerics
return new Complex32((float)value, 0.0f); return new Complex32((float)value, 0.0f);
} }
/// <summary>
/// Converts this <c>Complex32</c> to a <see cref="Complex"/>.
/// </summary>
/// <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(this._real, this._imag);

140
src/Numerics/ComplexExtensions.cs

@ -1,9 +1,7 @@
// <copyright file="ComplexExtensions.cs" company="Math.NET"> // <copyright file="ComplexExtensions.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-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
@ -12,10 +10,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
@ -40,63 +36,69 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is zero. /// Gets a value indicating whether the <c>Complex32</c> is zero.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is zero; otherwise, <c>false</c>.</value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns><c>true</c> if this instance is zero; otherwise, <c>false</c>.</returns>
public static bool IsZero(this Complex complex) public static bool IsZero(this Complex complex)
{ {
return complex.Real == 0.0 && complex.Imaginary == 0.0; return complex.Real == 0.0 && complex.Imaginary == 0.0;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is one. /// Gets a value indicating whether the <c>Complex32</c> is one.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is one; otherwise, <c>false</c>.</value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns><c>true</c> if this instance is one; otherwise, <c>false</c>.</returns>
public static bool IsOne(this Complex complex) public static bool IsOne(this Complex complex)
{ {
return complex.Real == 1.0 && complex.Imaginary == 0.0; return complex.Real == 1.0 && complex.Imaginary == 0.0;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the <c>Complex32</c> is the imaginary unit. /// Gets a value indicating whether the <c>Complex32</c> is the imaginary unit.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</value> /// <returns><c>true</c> if this instance is ImaginaryOne; otherwise, <c>false</c>.</returns>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
public static bool IsImaginaryOne(this Complex complex) public static bool IsImaginaryOne(this Complex complex)
{ {
return complex.Real == 0.0 && complex.Imaginary == 1.0; return complex.Real == 0.0 && complex.Imaginary == 1.0;
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c>evaluates /// Gets a value indicating whether the provided <c>Complex32</c>evaluates
/// to a value that is not a number. /// to a value that is not a number.
/// </summary> /// </summary>
/// <value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <c>true</c> if this instance is <see cref="NaN"/>; otherwise, /// <returns>
/// <c>true</c> if this instance is <c>NaN</c>; otherwise,
/// <c>false</c>. /// <c>false</c>.
/// </value> /// </returns>
public static bool IsNaN(this Complex complex) public static bool IsNaN(this Complex complex)
{ {
return double.IsNaN(complex.Real) || double.IsNaN(complex.Imaginary); return double.IsNaN(complex.Real) || double.IsNaN(complex.Imaginary);
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> evaluates to an /// Gets a value indicating whether the provided <c>Complex32</c> evaluates to an
/// infinite value. /// infinite value.
/// </summary> /// </summary>
/// <value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns>
/// <c>true</c> if this instance is infinite; otherwise, <c>false</c>. /// <c>true</c> if this instance is infinite; otherwise, <c>false</c>.
/// </value> /// </returns>
/// <remarks> /// <remarks>
/// True if it either evaluates to a complex infinity /// True if it either evaluates to a complex infinity
/// or to a directed infinity. /// or to a directed infinity.
/// </remarks> /// </remarks>
public static bool IsInfinity(this Complex complex) public static bool IsInfinity(this Complex complex)
{ {
return double.IsInfinity(complex.Real) || double.IsInfinity(complex.Imaginary); return double.IsInfinity(complex.Real) || double.IsInfinity(complex.Imaginary);
} }
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> is real. /// Gets a value indicating whether the provided <c>Complex32</c> is real.
/// </summary> /// </summary>
/// <value><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns><c>true</c> if this instance is a real number; otherwise, <c>false</c>.</returns>
public static bool IsReal(this Complex complex) public static bool IsReal(this Complex complex)
{ {
return complex.Imaginary == 0.0; return complex.Imaginary == 0.0;
@ -105,17 +107,19 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Gets a value indicating whether the provided <c>Complex32</c> is real and not negative, that is &gt;= 0. /// Gets a value indicating whether the provided <c>Complex32</c> is real and not negative, that is &gt;= 0.
/// </summary> /// </summary>
/// <value> /// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns>
/// <c>true</c> if this instance is real nonnegative number; otherwise, <c>false</c>. /// <c>true</c> if this instance is real nonnegative number; otherwise, <c>false</c>.
/// </value> /// </returns>
public static bool IsRealNonNegative(this Complex complex) public static bool IsRealNonNegative(this Complex complex)
{ {
return complex.Imaginary == 0.0f && complex.Real >= 0; return complex.Imaginary == 0.0f && complex.Real >= 0;
} }
/// <summary> /// <summary>
/// Gets the conjugate of this <c>Complex32</c>. /// Gets the conjugate of the <c>Complex</c> number.
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <remarks> /// <remarks>
/// The semantic of <i>setting the conjugate</i> is such that /// The semantic of <i>setting the conjugate</i> is such that
/// <code> /// <code>
@ -128,22 +132,26 @@ namespace MathNet.Numerics
/// a = b.Conjugate /// a = b.Conjugate
/// </code> /// </code>
/// </remarks> /// </remarks>
/// <returns>The conjugate of the <see cref="Complex"/> number.</returns>
public static Complex Conjugate(this Complex complex) public static Complex Conjugate(this Complex complex)
{ {
return new Complex(complex.Real, -complex.Imaginary); return new Complex(complex.Real, -complex.Imaginary);
} }
/// <summary> /// <summary>
/// Gets the squared magnitude of this <c>Complex</c>. /// Gets the squared magnitude of the <c>Complex</c> number.
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns>The squared magnitude of the <c>Complex</c> number.</returns>
public static double MagnitudeSquared(this Complex complex) public static double MagnitudeSquared(this Complex complex)
{ {
return (complex.Real * complex.Real) + (complex.Imaginary * complex.Imaginary); return (complex.Real * complex.Real) + (complex.Imaginary * complex.Imaginary);
} }
/// <summary> /// <summary>
/// Exponential of this <c>Complex</c> (exp(x), E^x). /// Exponential of this <c>Complex</c> (exp(x), E^x).
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns> /// <returns>
/// The exponential of this complex number. /// The exponential of this complex number.
/// </returns> /// </returns>
@ -161,6 +169,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Natural Logarithm of this <c>Complex</c> (Base E). /// Natural Logarithm of this <c>Complex</c> (Base E).
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns> /// <returns>
/// The natural logarithm of this complex number. /// The natural logarithm of this complex number.
/// </returns> /// </returns>
@ -177,6 +186,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Raise this <c>Complex</c> to the given value. /// Raise this <c>Complex</c> to the given value.
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <param name="exponent"> /// <param name="exponent">
/// The exponent. /// The exponent.
/// </param> /// </param>
@ -216,6 +226,7 @@ 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="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <param name="rootExponent"> /// <param name="rootExponent">
/// The root exponent. /// The root exponent.
/// </param> /// </param>
@ -230,6 +241,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// The Square (power 2) of this <c>Complex</c> /// The Square (power 2) of this <c>Complex</c>
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns> /// <returns>
/// The square of this complex number. /// The square of this complex number.
/// </returns> /// </returns>
@ -246,6 +258,7 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// The Square Root (power 1/2) of this <c>Complex</c> /// The Square Root (power 1/2) of this <c>Complex</c>
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns> /// <returns>
/// The square root of this complex number. /// The square root of this complex number.
/// </returns> /// </returns>
@ -292,6 +305,7 @@ namespace MathNet.Numerics
/// Returns a Norm of a value of this type, which is appropriate for measuring how /// Returns a Norm of a value of this type, which is appropriate for measuring how
/// close this value is to zero. /// close this value is to zero.
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <returns>A norm of this value.</returns> /// <returns>A norm of this value.</returns>
public static double Norm(this Complex complex) public static double Norm(this Complex complex)
{ {
@ -302,11 +316,12 @@ namespace MathNet.Numerics
/// Returns a Norm of the difference of two values of this type, which is /// Returns a Norm of the difference of two values of this type, which is
/// appropriate for measuring how close together these two values are. /// appropriate for measuring how close together these two values are.
/// </summary> /// </summary>
/// <param name="complex">The <see cref="Complex"/> number to perfom this operation on.</param>
/// <param name="otherValue">The value to compare with.</param> /// <param name="otherValue">The value to compare with.</param>
/// <returns>A norm of the difference between this and the other value.</returns> /// <returns>A norm of the difference between this and the other value.</returns>
public static double NormOfDifference(this Complex complex, Complex otherValue) public static double NormOfDifference(this Complex complex, Complex otherValue)
{ {
return (complex- otherValue).MagnitudeSquared(); return (complex - otherValue).MagnitudeSquared();
} }
/// <summary> /// <summary>
@ -370,8 +385,8 @@ namespace MathNet.Numerics
var keywords = var keywords =
new[] new[]
{ {
textInfo.ListSeparator, numberFormatInfo.NaNSymbol, textInfo.ListSeparator, numberFormatInfo.NaNSymbol,
numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol, numberFormatInfo.NegativeInfinitySymbol, numberFormatInfo.PositiveInfinitySymbol,
"+", "-", "i", "j" "+", "-", "i", "j"
}; };
@ -382,7 +397,7 @@ namespace MathNet.Numerics
// parse the left part // parse the left part
bool isLeftPartImaginary; bool isLeftPartImaginary;
double leftPart = ParsePart(ref token, out isLeftPartImaginary, formatProvider); var leftPart = ParsePart(ref token, out isLeftPartImaginary, formatProvider);
if (token == null) if (token == null)
{ {
return isLeftPartImaginary ? new Complex(0, leftPart) : new Complex(leftPart, 0); return isLeftPartImaginary ? new Complex(0, leftPart) : new Complex(leftPart, 0);
@ -401,7 +416,7 @@ namespace MathNet.Numerics
} }
bool isRightPartImaginary; bool isRightPartImaginary;
double rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider); var rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider);
return new Complex(leftPart, rightPart); return new Complex(leftPart, rightPart);
} }
@ -409,7 +424,7 @@ namespace MathNet.Numerics
{ {
// format: real + imag // format: real + imag
bool isRightPartImaginary; bool isRightPartImaginary;
double rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider); var rightPart = ParsePart(ref token, out isRightPartImaginary, formatProvider);
if (!(isLeftPartImaginary ^ isRightPartImaginary)) if (!(isLeftPartImaginary ^ isRightPartImaginary))
{ {
@ -451,7 +466,7 @@ namespace MathNet.Numerics
} }
} }
bool negative = false; var negative = false;
if (token.Value == "-") if (token.Value == "-")
{ {
negative = true; negative = true;
@ -511,7 +526,7 @@ namespace MathNet.Numerics
/// </param> /// </param>
/// <returns> /// <returns>
/// If the conversion succeeds, the result will contain a complex number equivalent to value. /// If the conversion succeeds, the result will contain a complex number equivalent to value.
/// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized /// Otherwise the result will contain Complex.Zero. This parameter is passed uninitialized.
/// </returns> /// </returns>
public static bool TryToComplex(this string value, out Complex result) public static bool TryToComplex(this string value, out Complex result)
{ {
@ -557,24 +572,81 @@ namespace MathNet.Numerics
return ret; return ret;
} }
/// <summary>
/// Creates a <c>Complex32</c> number based on a string. The string can be in the
/// following formats (without the quotes): 'n', 'ni', 'n +/- ni',
/// 'ni +/- n', 'n,n', 'n,ni,' '(n,n)', or '(n,ni)', where n is a double.
/// </summary>
/// <returns>
/// A complex number containing the value specified by the given string.
/// </returns>
/// <param name="value">
/// the string to parse.
/// </param>
public static Complex32 ToComplex32(this string value) public static Complex32 ToComplex32(this string value)
{ {
return Complex32.Parse(value); return Complex32.Parse(value);
} }
/// <summary>
/// Creates a <c>Complex32</c> number based on a string. The string can be in the
/// following formats (without the quotes): 'n', 'ni', 'n +/- ni',
/// 'ni +/- n', 'n,n', 'n,ni,' '(n,n)', or '(n,ni)', where n is a double.
/// </summary>
/// <returns>
/// A complex number containing the value specified by the given string.
/// </returns>
/// <param name="value">
/// the string to parse.
/// </param>
/// <param name="formatProvider">
/// An <see cref="IFormatProvider"/> that supplies culture-specific
/// formatting information.
/// </param>
public static Complex32 ToComplex32(this string value, IFormatProvider formatProvider) public static Complex32 ToComplex32(this string value, IFormatProvider formatProvider)
{ {
return Complex32.Parse(value, formatProvider); return Complex32.Parse(value, formatProvider);
} }
/// <summary>
/// Converts the string representation of a complex number to a single-precision complex number equivalent.
/// A return value indicates whether the conversion succeeded or failed.
/// </summary>
/// <param name="value">
/// A string containing a complex number to convert.
/// </param>
/// <param name="result">
/// The parsed value.
/// </param>
/// <returns>
/// If the conversion succeeds, the result will contain a complex number equivalent to value.
/// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized.
/// </returns>
public static bool TryToComplex32(this string value, out Complex32 result) public static bool TryToComplex32(this string value, out Complex32 result)
{ {
return Complex32.TryParse(value, out result); return Complex32.TryParse(value, out result);
} }
/// <summary>
/// Converts the string representation of a complex number to single-precision complex number equivalent.
/// A return value indicates whether the conversion succeeded or failed.
/// </summary>
/// <param name="value">
/// A string containing a complex number to convert.
/// </param>
/// <param name="formatProvider">
/// An <see cref="IFormatProvider"/> that supplies culture-specific formatting information about value.
/// </param>
/// <param name="result">
/// The parsed value.
/// </param>
/// <returns>
/// If the conversion succeeds, the result will contain a complex number equivalent to value.
/// Otherwise the result will contain Complex.Zero. This parameter is passed uninitialized.
/// </returns>
public static bool TryToComplex32(this string value, IFormatProvider formatProvider, out Complex32 result) public static bool TryToComplex32(this string value, IFormatProvider formatProvider, out Complex32 result)
{ {
return Complex32.TryParse(value, formatProvider, out result); return Complex32.TryParse(value, formatProvider, out result);
} }
} }
} }
Loading…
Cancel
Save