Browse Source

Complex: added missing tests

Signed-off-by: Marcus Cuda <marcus@cuda.net>
pull/36/head
Marcus Cuda 17 years ago
parent
commit
ba2910eee2
  1. 352
      src/Managed.UnitTests/ComplexTests/ComplexTest.cs
  2. 136
      src/Managed/Complex.cs

352
src/Managed.UnitTests/ComplexTests/ComplexTest.cs

@ -5,16 +5,9 @@
using MbUnit.Framework;
/// <summary>
/// The complex test.
/// </summary>
[TestFixture]
public class ComplexTest
{
/// <summary>
/// The can add complex number and double using operartor.
/// </summary>
[Test]
[MultipleAsserts]
public void CanAddComplexNumberAndDoubleUsingOperartor()
@ -29,9 +22,6 @@
AssertEx.That(() => -2.2 + new Complex(-1.1, 2.2) == new Complex(-3.3, 2.2));
}
/// <summary>
/// The can add subtract complex numbers using operartor.
/// </summary>
[Test]
[MultipleAsserts]
public void CanAddSubtractComplexNumbersUsingOperartor()
@ -42,9 +32,6 @@
AssertEx.That(() => (new Complex(1.1, -2.2) - new Complex(1.1, -2.2)) == Complex.Zero);
}
/// <summary>
/// The can add two complex numbers.
/// </summary>
[Test]
[MultipleAsserts]
public void CanAddTwoComplexNumbers()
@ -55,9 +42,6 @@
AssertEx.That(() => new Complex(1.1, -2.2).Add(new Complex(-1.1, 2.2)) == Complex.Zero);
}
/// <summary>
/// The can add two complex numbers using operartor.
/// </summary>
[Test]
[MultipleAsserts]
public void CanAddTwoComplexNumbersUsingOperartor()
@ -68,9 +52,6 @@
AssertEx.That(() => (new Complex(1.1, -2.2) + new Complex(-1.1, 2.2)) == Complex.Zero);
}
/// <summary>
/// The can calculate hash code.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCalculateHashCode()
@ -83,21 +64,6 @@
Assert.AreEqual(-2097152, complex.GetHashCode());
}
/// <summary>
/// The can compute exponential.
/// </summary>
/// <param name="real">
/// The real.
/// </param>
/// <param name="imag">
/// The imag.
/// </param>
/// <param name="expectedReal">
/// The expected real.
/// </param>
/// <param name="expectedImag">
/// The expected imag.
/// </param>
[Test]
[Row(0.0, 0.0, 1.0, 0.0)]
[Row(0.0, 1.0, 0.54030230586813977, 0.8414709848078965)]
@ -110,21 +76,6 @@
AssertHelpers.AlmostEqual(expected, value.Exponential(), 15);
}
/// <summary>
/// The can compute natural logarithm.
/// </summary>
/// <param name="real">
/// The real.
/// </param>
/// <param name="imag">
/// The imag.
/// </param>
/// <param name="expectedReal">
/// The expected real.
/// </param>
/// <param name="expectedImag">
/// The expected imag.
/// </param>
[Test]
[Row(0.0, 0.0, double.NegativeInfinity, 0.0)]
[Row(0.0, 1.0, 0.0, 1.5707963267948966)]
@ -138,9 +89,6 @@
AssertHelpers.AlmostEqual(expected, value.NaturalLogarithm(), 15);
}
/// <summary>
/// The can compute power.
/// </summary>
[Test]
[MultipleAsserts]
public void CanComputePower()
@ -167,11 +115,24 @@
a = new Complex(0.0, -8.388608e6);
b = new Complex(1.19209289550780998537e-7, 0.0);
AssertHelpers.AlmostEqual(new Complex(1.00000190048219620166, -1.87253870018168043834e-7), a.Power(b), 15);
a = new Complex(0.0, 0.0);
b = new Complex(0.0, 0.0);
AssertHelpers.AlmostEqual(new Complex(1.0, 0.0), a.Power(b), 15);
a = new Complex(0.0, 0.0);
b = new Complex(1.0, 0.0);
AssertHelpers.AlmostEqual(new Complex(0.0, 0.0), a.Power(b), 15);
a = new Complex(0.0, 0.0);
b = new Complex(-1.0, 0.0);
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, 0.0), a.Power(b), 15);
a = new Complex(0.0, 0.0);
b = new Complex(-1.0, 1.0);
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.PositiveInfinity), a.Power(b), 15);
a = new Complex(0.0, 0.0);
b = new Complex(0.0, 1.0);
AssertEx.That(()=>a.Power(b).IsNaN);
}
/// <summary>
/// The can compute root.
/// </summary>
[Test]
[MultipleAsserts]
public void CanComputeRoot()
@ -196,9 +157,6 @@
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.NegativeInfinity), a.Root(b), 15);
}
/// <summary>
/// The can compute square.
/// </summary>
[Test]
[MultipleAsserts]
public void CanComputeSquare()
@ -217,9 +175,6 @@
AssertHelpers.AlmostEqual(new Complex(-70368744177664.0, 0.0), complex.Square(), 15);
}
/// <summary>
/// The can compute square root.
/// </summary>
[Test]
[MultipleAsserts]
public void CanComputeSquareRoot()
@ -241,11 +196,22 @@
AssertHelpers.AlmostEqual(new Complex(2048.0, -2048.0), complex.SquareRoot(), 15);
complex = new Complex(8.388608e6, 1.19209289550780998537e-7);
AssertHelpers.AlmostEqual(new Complex(2896.3093757400989, 2.0579515874459933e-11), complex.SquareRoot(), 15);
complex = new Complex(0.0, 0.0);
AssertHelpers.AlmostEqual(Complex.Zero, complex.SquareRoot(), 15);
}
[Test]
[Row(1, -2, "1 -2i")]
[Row(1, 2, "1 + 2i")]
[Row(1, 0, "1")]
[Row(0, -2, "-2i")]
[Row(0, 2, "2i")]
public void CanConvertComplexToString(double real, double imag, string expected)
{
var a = new Complex(real, imag);
Assert.AreEqual(expected, a.ToString());
}
/// <summary>
/// The can convert double to complex.
/// </summary>
[Test]
[MultipleAsserts]
public void CanConvertDoubleToComplex()
@ -255,9 +221,68 @@
Assert.AreEqual(1.1, new Complex(1.1, 0));
}
/// <summary>
/// The can create complex number using the constructor.
/// </summary>
[Test]
[Row("-1", -1, 0)]
[Row("-i", 0, -1)]
[Row("i", 0, 1)]
[Row("2i", 0, 2)]
[Row("1 + 2i", 1, 2)]
[Row("1+2i", 1, 2)]
[Row("1 - 2i", 1, -2)]
[Row("1-2i", 1, -2)]
[Row("1,2", 1, 2)]
[Row("1 , 2", 1, 2)]
[Row("1,2i", 1, 2)]
[Row("-1, -2i", -1, -2)]
[Row("(+1,2i)", 1, 2)]
[Row("(-1 , -2)", -1, -2)]
[Row("(-1 , -2i)", -1, -2)]
[Row("(+1e1 , -2e-2i)", 10, -0.02)]
[Row("(-1E1 -2e2i)", -10, -200)]
[Row("(-1e+1 -2e2i)", -10, -200)]
[Row("(-1e1 -2e+2i)", -10, -200)]
[Row("(-1e-1 -2E2i)", -0.1, -200)]
[Row("(-1e1 -2e-2i)", -10, -0.02)]
[Row("(-1E+1 -2e+2i)", -10, -200)]
[Row("(-1e-1,-2e-2i)", -0.1, -0.02)]
[Row("(+1 +2i)", 1, 2)]
public void CanConvertStringToComplexUsingTryParse(string str, double expectedReal, double expectedImag)
{
Complex z;
var ret = Complex.TryParse(str, out z);
Assert.IsTrue(ret);
Assert.AreEqual(expectedReal, z.Real);
Assert.AreEqual(expectedImag, z.Imaginary);
ret = Complex.TryParse("(-1E+1 -2e+2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(-10, z.Real);
Assert.AreEqual(-200, z.Imaginary);
ret = Complex.TryParse("(-1e-1,-2e-2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(-.1, z.Real);
Assert.AreEqual(-.02, z.Imaginary);
ret = Complex.TryParse("(+1 +2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(1, z.Real);
Assert.AreEqual(2, z.Imaginary);
}
[Test]
public void CanParseStringToComplex()
{
var actual = Complex.Parse("-1 -2i");
Assert.AreEqual(new Complex(-1,-2), actual);
}
[Test]
public void ParseThrowsFormatExceptionIfMissingClosingParen()
{
Assert.Throws<FormatException>(() => Complex.Parse("(1,2"));
}
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberUsingTheConstructor()
@ -267,9 +292,6 @@
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2.");
}
/// <summary>
/// The can create complex number with modulus argument.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberWithModulusArgument()
@ -279,9 +301,6 @@
Assert.AreApproximatelyEqual(-1, complex.Imaginary, 1e-15, "Imaginary part is -1.");
}
/// <summary>
/// The can create complex number with real imaginary intializer.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberWithRealImaginaryIntializer()
@ -291,9 +310,6 @@
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2.");
}
/// <summary>
/// The can create string from complex number.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCreateStringFromComplexNumber()
@ -306,9 +322,6 @@
Assert.AreEqual("1.1 + 1.1i", new Complex(1.1, 1.1).ToString());
}
/// <summary>
/// The can create string using format provider.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCreateStringUsingFormatProvider()
@ -322,9 +335,6 @@
Assert.AreEqual("1,1 + 1,1i", new Complex(1.1, 1.1).ToString(provider));
}
/// <summary>
/// The can create string using number format.
/// </summary>
[Test]
[MultipleAsserts]
public void CanCreateStringUsingNumberFormat()
@ -337,9 +347,6 @@
Assert.AreEqual("1.100 + 1.100i", new Complex(1.1, 1.1).ToString("#.000"));
}
/// <summary>
/// The can determine if imaginary unit.
/// </summary>
[Test]
public void CanDetermineIfImaginaryUnit()
{
@ -347,9 +354,6 @@
Assert.IsTrue(complex.IsI, "Imaginary unit");
}
/// <summary>
/// The can determine if infinity.
/// </summary>
[Test]
[MultipleAsserts]
public void CanDetermineIfInfinity()
@ -362,9 +366,6 @@
Assert.IsTrue(complex.IsInfinity, "Both parts are infinity.");
}
/// <summary>
/// The can determine if na n.
/// </summary>
[Test]
[MultipleAsserts]
public void CanDetermineIfNaN()
@ -377,9 +378,6 @@
Assert.IsTrue(complex.IsNaN, "Both parts are NaN.");
}
/// <summary>
/// The can determine if one value complex number.
/// </summary>
[Test]
public void CanDetermineIfOneValueComplexNumber()
{
@ -387,9 +385,6 @@
Assert.IsTrue(complex.IsOne, "Complex number with a value of one.");
}
/// <summary>
/// The can determine if real non negative number.
/// </summary>
[Test]
public void CanDetermineIfRealNonNegativeNumber()
{
@ -397,9 +392,6 @@
Assert.IsTrue(complex.IsReal, "Is a real non-negative number.");
}
/// <summary>
/// The can determine if real number.
/// </summary>
[Test]
public void CanDetermineIfRealNumber()
{
@ -407,9 +399,6 @@
Assert.IsTrue(complex.IsReal, "Is a real number.");
}
/// <summary>
/// The can determine if zero value complex number.
/// </summary>
[Test]
public void CanDetermineIfZeroValueComplexNumber()
{
@ -417,9 +406,6 @@
Assert.IsTrue(complex.IsZero, "Zero complex number.");
}
/// <summary>
/// The can divide complex number and double using operators.
/// </summary>
[Test]
[MultipleAsserts]
public void CanDivideComplexNumberAndDoubleUsingOperators()
@ -427,12 +413,10 @@
AssertEx.That(() => (Complex.NaN * 1.0).IsNaN);
Assert.AreEqual(new Complex(-2, 2), new Complex(4, -4) / -2);
Assert.AreEqual(new Complex(0.25, 0.25), 2 / new Complex(4, -4));
Assert.AreEqual(Complex.Infinity, 2.0 / Complex.Zero);
Assert.AreEqual(Complex.Infinity, Complex.One / 0);
}
/// <summary>
/// The can divide two complex numbers.
/// </summary>
[Test]
[MultipleAsserts]
public void CanDivideTwoComplexNumbers()
@ -442,9 +426,6 @@
Assert.AreEqual(Complex.Infinity, Complex.One.Divide(Complex.Zero));
}
/// <summary>
/// The can divide two complex numbers using operators.
/// </summary>
[Test]
[MultipleAsserts]
public void CanDivideTwoComplexNumbersUsingOperators()
@ -454,9 +435,6 @@
Assert.AreEqual(Complex.Infinity, Complex.One / Complex.Zero);
}
/// <summary>
/// The can multiple complex number and double using operators.
/// </summary>
[Test]
[MultipleAsserts]
public void CanMultipleComplexNumberAndDoubleUsingOperators()
@ -466,9 +444,6 @@
Assert.AreEqual(new Complex(8, -8), 2 * new Complex(4, -4));
}
/// <summary>
/// The can multiple two complex numbers.
/// </summary>
[Test]
[MultipleAsserts]
public void CanMultipleTwoComplexNumbers()
@ -477,9 +452,6 @@
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4).Multiply(new Complex(-2, 2)));
}
/// <summary>
/// The can multiple two complex numbers using operators.
/// </summary>
[Test]
[MultipleAsserts]
public void CanMultipleTwoComplexNumbersUsingOperators()
@ -488,9 +460,6 @@
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4) * new Complex(-2, 2));
}
/// <summary>
/// The can negate value.
/// </summary>
[Test]
public void CanNegateValue()
{
@ -498,9 +467,6 @@
Assert.AreEqual(new Complex(-1.1, 2.2), complex.Negate());
}
/// <summary>
/// The can negate value using operator.
/// </summary>
[Test]
public void CanNegateValueUsingOperator()
{
@ -508,9 +474,6 @@
Assert.AreEqual(new Complex(-1.1, 2.2), -complex);
}
/// <summary>
/// The can subtract complex number and double using operartor.
/// </summary>
[Test]
[MultipleAsserts]
public void CanSubtractComplexNumberAndDoubleUsingOperartor()
@ -525,9 +488,6 @@
AssertEx.That(() => -2.2 - new Complex(-1.1, 2.2) == new Complex(-1.1, -2.2));
}
/// <summary>
/// The can subtract two complex numbers.
/// </summary>
[Test]
[MultipleAsserts]
public void CanSubtractTwoComplexNumbers()
@ -538,9 +498,6 @@
AssertEx.That(() => new Complex(1.1, -2.2).Subtract(new Complex(1.1, -2.2)) == Complex.Zero);
}
/// <summary>
/// The can test for equality.
/// </summary>
[Test]
[MultipleAsserts]
public void CanTestForEquality()
@ -551,9 +508,6 @@
Assert.AreNotEqual(new Complex(-1.1, 2.2), new Complex(1.1, -2.2));
}
/// <summary>
/// The can test for equality using operators.
/// </summary>
[Test]
[MultipleAsserts]
public void CanTestForEqualityUsingOperators()
@ -564,9 +518,6 @@
AssertEx.That(() => new Complex(-1.1, 2.2) != new Complex(1.1, -2.2));
}
/// <summary>
/// The can use plus.
/// </summary>
[Test]
public void CanUsePlus()
{
@ -574,9 +525,6 @@
Assert.AreEqual(complex, complex.Plus());
}
/// <summary>
/// The can use plus operator.
/// </summary>
[Test]
public void CanUsePlusOperator()
{
@ -584,43 +532,6 @@
Assert.AreEqual(complex, +complex);
}
/// <summary>
/// The with modulus argument throws argument out of range exception.
/// </summary>
[Test]
public void WithModulusArgumentThrowsArgumentOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>(
() => Complex.WithModulusArgument(-1, 1), "Throws exception because modulus is negative.");
}
[Test]
[Row(1,-2,"1 -2i")]
[Row(1, 2, "1 + 2i")]
[Row(1, 0, "1")]
[Row(0, -2, "-2i")]
[Row(0, 2, "2i")]
public void CanConvertComplexToString(double real, double imag, string expected)
{
var a = new Complex(real, imag);
Assert.AreEqual<string>(expected, a.ToString());
}
[Test]
[Row("")]
[Row("+")]
[Row("1i+2")]
[Row(null)]
public void TryParseReturnsFalseWhenGiveBadValue(string str)
{
Complex z;
bool ret = Complex.TryParse(str, out z);
Assert.IsFalse(ret);
Assert.AreEqual(0, z.Real);
Assert.AreEqual(0, z.Imaginary);
}
[Test]
public void TryParseCanHandleSymbols()
{
@ -650,57 +561,50 @@
Assert.IsTrue(ret);
Assert.AreEqual(double.MaxValue, z.Real);
Assert.AreEqual(double.MinValue, z.Imaginary);
}
[Test]
[Row("-1", -1, 0)]
[Row("-i", 0, -1)]
[Row("i", 0, 1)]
[Row("2i", 0, 2)]
[Row("1 + 2i", 1, 2)]
[Row("1+2i", 1, 2)]
[Row("1 - 2i", 1, -2)]
[Row("1-2i", 1, -2)]
[Row("1,2", 1, 2)]
[Row("1 , 2", 1, 2)]
[Row("1,2i", 1, 2)]
[Row("-1, -2i", -1, -2)]
[Row("(+1,2i)", 1, 2)]
[Row("(-1 , -2)", -1, -2)]
[Row("(-1 , -2i)", -1, -2)]
[Row("(+1e1 , -2e-2i)", 10, -0.02)]
[Row("(-1E1 -2e2i)", -10, -200)]
[Row("(-1e+1 -2e2i)", -10, -200)]
[Row("(-1e1 -2e+2i)", -10, -200)]
[Row("(-1e-1 -2E2i)", -0.1, -200)]
[Row("(-1e1 -2e-2i)", -10, -0.02)]
[Row("(-1E+1 -2e+2i)", -10, -200)]
[Row("(-1e-1,-2e-2i)", -0.1, -0.02)]
[Row("(+1 +2i)", 1, 2)]
public void CanConvertStringToComplexUsingTryParse(string str, double expectedReal, double expectedImag)
[Row("")]
[Row("+")]
[Row("1i+2")]
[Row(null)]
public void TryParseReturnsFalseWhenGiveBadValue(string str)
{
Complex z;
var ret = Complex.TryParse(str, out z);
Assert.IsTrue(ret);
Assert.AreEqual(expectedReal, z.Real);
Assert.AreEqual(expectedImag, z.Imaginary);
ret = Complex.TryParse("(-1E+1 -2e+2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(-10, z.Real);
Assert.AreEqual(-200, z.Imaginary);
Assert.IsFalse(ret);
Assert.AreEqual(0, z.Real);
Assert.AreEqual(0, z.Imaginary);
}
ret = Complex.TryParse("(-1e-1,-2e-2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(-.1, z.Real);
Assert.AreEqual(-.02, z.Imaginary);
[Test]
public void WithModulusArgumentThrowsArgumentOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>(
() => Complex.WithModulusArgument(-1, 1), "Throws exception because modulus is negative.");
}
ret = Complex.TryParse("(+1 +2i)", out z);
Assert.IsTrue(ret);
Assert.AreEqual(1, z.Real);
Assert.AreEqual(2, z.Imaginary);
[Test]
[Row(0.0, 0.0, 0.0)]
[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)
{
Assert.AreEqual(expected, new Complex(real, imag).Modulus);
}
[Test]
[Row(double.PositiveInfinity, double.PositiveInfinity, Constants.Sqrt1Over2, Constants.Sqrt1Over2)]
[Row(double.PositiveInfinity, double.NegativeInfinity, Constants.Sqrt1Over2, -Constants.Sqrt1Over2)]
[Row(double.NegativeInfinity, double.PositiveInfinity, -Constants.Sqrt1Over2, -Constants.Sqrt1Over2)]
[Row(double.NegativeInfinity, double.NegativeInfinity, -Constants.Sqrt1Over2, Constants.Sqrt1Over2)]
[Row(0.0, 0.0, 0.0, 0.0)]
[Row(-1.0, 1.0, -0.70710678118654746, 0.70710678118654746)]
[Row(-111.1, 111.1, -0.70710678118654746, 0.70710678118654746)]
public void CanComputeSign(double real, double imag, double expectedReal, double expectedImag)
{
Assert.AreEqual(new Complex(expectedReal, expectedImag), new Complex(real, imag).Sign);
}
}
}

136
src/Managed/Complex.cs

@ -1,26 +1,11 @@
// <copyright file="Complex.cs" company="Math.NET">
// 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
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// 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
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Complex.cs" company="">
//
// </copyright>
// <summary>
// Complex numbers class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MathNet.Numerics
{
@ -551,38 +536,31 @@ namespace MathNet.Numerics
Complex result;
if (Real.AlmostZero() && Imaginary.AlmostZero())
var absReal = Math.Abs(Real);
var absImag = Math.Abs(Imaginary);
double w;
if (absReal >= absImag)
{
result = Zero;
var ratio = Imaginary / Real;
w = Math.Sqrt(absReal) * Math.Sqrt(0.5 * (1.0 + Math.Sqrt(1.0 + (ratio * ratio))));
}
else
{
var absReal = Math.Abs(Real);
var absImag = Math.Abs(Imaginary);
double w;
if (absReal >= absImag)
{
var ratio = Imaginary / Real;
w = Math.Sqrt(absReal) * Math.Sqrt(0.5 * (1.0 + Math.Sqrt(1.0 + (ratio * ratio))));
}
else
{
var ratio = Real / Imaginary;
w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0 + (ratio * ratio))));
}
var ratio = Real / Imaginary;
w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0 + (ratio * ratio))));
}
if (Real >= 0.0)
{
result = new Complex(w, Imaginary / (2.0 * w));
}
else if (Imaginary >= 0.0)
{
result = new Complex(absImag / (2.0 * w), w);
}
else
{
result = new Complex(absImag / (2.0 * w), -w);
}
if (Real >= 0.0)
{
result = new Complex(w, Imaginary / (2.0 * w));
}
else if (Imaginary >= 0.0)
{
result = new Complex(absImag / (2.0 * w), w);
}
else
{
result = new Complex(absImag / (2.0 * w), -w);
}
return result;
@ -1068,7 +1046,9 @@ namespace MathNet.Numerics
/// Returns a Norm of a value of this type, which is appropriate for measuring how
/// close this value is to zero.
/// </summary>
/// <returns>A norm of this value.</returns>
/// <returns>
/// A norm of this value.
/// </returns>
double IPrecisionSupport<Complex>.Norm()
{
return ModulusSquared;
@ -1078,8 +1058,12 @@ namespace MathNet.Numerics
/// Returns a Norm of the difference of two values of this type, which is
/// appropriate for measuring how close together these two values are.
/// </summary>
/// <param name="otherValue">The value to compare with.</param>
/// <returns>A norm of the difference between this and the other value.</returns>
/// <param name="otherValue">
/// The value to compare with.
/// </param>
/// <returns>
/// A norm of the difference between this and the other value.
/// </returns>
double IPrecisionSupport<Complex>.NormOfDifference(Complex otherValue)
{
return (this - otherValue).ModulusSquared;
@ -1088,13 +1072,18 @@ namespace MathNet.Numerics
#endregion
#region Parse Functions
/// <summary>
/// Creates a complex number based on a string. The string can be in the following
/// formats(without the quotes): 'n', 'ni', 'n +/- ni', 'n,n', 'n,ni,' '(n,n)', or
/// '(n,ni)', where n is a real number.
/// </summary>
/// <returns>A complex number containing the value specified by the given string.</returns>
/// <param name="value">The string to parse.</param>
/// <returns>
/// A complex number containing the value specified by the given string.
/// </returns>
/// <param name="value">
/// The string to parse.
/// </param>
public static Complex Parse(string value)
{
return Parse(value, null);
@ -1105,9 +1094,15 @@ namespace MathNet.Numerics
/// formats(without the quotes): 'n', 'ni', 'n +/- ni', '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 IFormatProvider that supplies culture-specific formatting information.</param>
/// <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 IFormatProvider that supplies culture-specific formatting information.
/// </param>
public static Complex Parse(string value, IFormatProvider formatProvider)
{
if (value == null)
@ -1208,10 +1203,16 @@ namespace MathNet.Numerics
/// Converts the string representation of a complex number to a double-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>
/// <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 TryParse(string value, out Complex result)
{
return TryParse(value, null, out result);
@ -1221,9 +1222,15 @@ namespace MathNet.Numerics
/// Converts the string representation of a complex number to double-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 IFormatProvider that supplies culture-specific formatting information about value.</param>
/// <param name="result">The parsed value.</param>
/// <param name="value">
/// A string containing a complex number to convert.
/// </param>
/// <param name="formatProvider">
/// An 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 complex32.Zero. This parameter is passed uninitialized
@ -1249,6 +1256,7 @@ namespace MathNet.Numerics
return ret;
}
#endregion
}
}
Loading…
Cancel
Save