Browse Source

Complex: added missing tests

Signed-off-by: Marcus Cuda <marcus@cuda.net>
pull/2/head
Marcus Cuda 17 years ago
parent
commit
d4e4483d86
  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; using MbUnit.Framework;
/// <summary>
/// The complex test.
/// </summary>
[TestFixture] [TestFixture]
public class ComplexTest public class ComplexTest
{ {
/// <summary>
/// The can add complex number and double using operartor.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanAddComplexNumberAndDoubleUsingOperartor() public void CanAddComplexNumberAndDoubleUsingOperartor()
@ -29,9 +22,6 @@
AssertEx.That(() => -2.2 + new Complex(-1.1, 2.2) == new Complex(-3.3, 2.2)); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanAddSubtractComplexNumbersUsingOperartor() public void CanAddSubtractComplexNumbersUsingOperartor()
@ -42,9 +32,6 @@
AssertEx.That(() => (new Complex(1.1, -2.2) - new Complex(1.1, -2.2)) == Complex.Zero); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoComplexNumbers() public void CanAddTwoComplexNumbers()
@ -55,9 +42,6 @@
AssertEx.That(() => new Complex(1.1, -2.2).Add(new Complex(-1.1, 2.2)) == Complex.Zero); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanAddTwoComplexNumbersUsingOperartor() public void CanAddTwoComplexNumbersUsingOperartor()
@ -68,9 +52,6 @@
AssertEx.That(() => (new Complex(1.1, -2.2) + new Complex(-1.1, 2.2)) == Complex.Zero); AssertEx.That(() => (new Complex(1.1, -2.2) + new Complex(-1.1, 2.2)) == Complex.Zero);
} }
/// <summary>
/// The can calculate hash code.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCalculateHashCode() public void CanCalculateHashCode()
@ -83,21 +64,6 @@
Assert.AreEqual(-2097152, complex.GetHashCode()); 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] [Test]
[Row(0.0, 0.0, 1.0, 0.0)] [Row(0.0, 0.0, 1.0, 0.0)]
[Row(0.0, 1.0, 0.54030230586813977, 0.8414709848078965)] [Row(0.0, 1.0, 0.54030230586813977, 0.8414709848078965)]
@ -110,21 +76,6 @@
AssertHelpers.AlmostEqual(expected, value.Exponential(), 15); 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] [Test]
[Row(0.0, 0.0, double.NegativeInfinity, 0.0)] [Row(0.0, 0.0, double.NegativeInfinity, 0.0)]
[Row(0.0, 1.0, 0.0, 1.5707963267948966)] [Row(0.0, 1.0, 0.0, 1.5707963267948966)]
@ -138,9 +89,6 @@
AssertHelpers.AlmostEqual(expected, value.NaturalLogarithm(), 15); AssertHelpers.AlmostEqual(expected, value.NaturalLogarithm(), 15);
} }
/// <summary>
/// The can compute power.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanComputePower() public void CanComputePower()
@ -167,11 +115,24 @@
a = new Complex(0.0, -8.388608e6); a = new Complex(0.0, -8.388608e6);
b = new Complex(1.19209289550780998537e-7, 0.0); b = new Complex(1.19209289550780998537e-7, 0.0);
AssertHelpers.AlmostEqual(new Complex(1.00000190048219620166, -1.87253870018168043834e-7), a.Power(b), 15); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanComputeRoot() public void CanComputeRoot()
@ -196,9 +157,6 @@
AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.NegativeInfinity), a.Root(b), 15); AssertHelpers.AlmostEqual(new Complex(double.PositiveInfinity, double.NegativeInfinity), a.Root(b), 15);
} }
/// <summary>
/// The can compute square.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanComputeSquare() public void CanComputeSquare()
@ -217,9 +175,6 @@
AssertHelpers.AlmostEqual(new Complex(-70368744177664.0, 0.0), complex.Square(), 15); AssertHelpers.AlmostEqual(new Complex(-70368744177664.0, 0.0), complex.Square(), 15);
} }
/// <summary>
/// The can compute square root.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanComputeSquareRoot() public void CanComputeSquareRoot()
@ -241,11 +196,22 @@
AssertHelpers.AlmostEqual(new Complex(2048.0, -2048.0), complex.SquareRoot(), 15); AssertHelpers.AlmostEqual(new Complex(2048.0, -2048.0), complex.SquareRoot(), 15);
complex = new Complex(8.388608e6, 1.19209289550780998537e-7); complex = new Complex(8.388608e6, 1.19209289550780998537e-7);
AssertHelpers.AlmostEqual(new Complex(2896.3093757400989, 2.0579515874459933e-11), complex.SquareRoot(), 15); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanConvertDoubleToComplex() public void CanConvertDoubleToComplex()
@ -255,9 +221,68 @@
Assert.AreEqual(1.1, new Complex(1.1, 0)); Assert.AreEqual(1.1, new Complex(1.1, 0));
} }
/// <summary> [Test]
/// The can create complex number using the constructor. [Row("-1", -1, 0)]
/// </summary> [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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateComplexNumberUsingTheConstructor() public void CanCreateComplexNumberUsingTheConstructor()
@ -267,9 +292,6 @@
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2."); Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2.");
} }
/// <summary>
/// The can create complex number with modulus argument.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateComplexNumberWithModulusArgument() public void CanCreateComplexNumberWithModulusArgument()
@ -279,9 +301,6 @@
Assert.AreApproximatelyEqual(-1, complex.Imaginary, 1e-15, "Imaginary part is -1."); Assert.AreApproximatelyEqual(-1, complex.Imaginary, 1e-15, "Imaginary part is -1.");
} }
/// <summary>
/// The can create complex number with real imaginary intializer.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateComplexNumberWithRealImaginaryIntializer() public void CanCreateComplexNumberWithRealImaginaryIntializer()
@ -291,9 +310,6 @@
Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2."); Assert.AreEqual(-2.2, complex.Imaginary, "Imaginary part is -2.2.");
} }
/// <summary>
/// The can create string from complex number.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateStringFromComplexNumber() public void CanCreateStringFromComplexNumber()
@ -306,9 +322,6 @@
Assert.AreEqual("1.1 + 1.1i", new Complex(1.1, 1.1).ToString()); Assert.AreEqual("1.1 + 1.1i", new Complex(1.1, 1.1).ToString());
} }
/// <summary>
/// The can create string using format provider.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateStringUsingFormatProvider() public void CanCreateStringUsingFormatProvider()
@ -322,9 +335,6 @@
Assert.AreEqual("1,1 + 1,1i", new Complex(1.1, 1.1).ToString(provider)); Assert.AreEqual("1,1 + 1,1i", new Complex(1.1, 1.1).ToString(provider));
} }
/// <summary>
/// The can create string using number format.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanCreateStringUsingNumberFormat() public void CanCreateStringUsingNumberFormat()
@ -337,9 +347,6 @@
Assert.AreEqual("1.100 + 1.100i", new Complex(1.1, 1.1).ToString("#.000")); Assert.AreEqual("1.100 + 1.100i", new Complex(1.1, 1.1).ToString("#.000"));
} }
/// <summary>
/// The can determine if imaginary unit.
/// </summary>
[Test] [Test]
public void CanDetermineIfImaginaryUnit() public void CanDetermineIfImaginaryUnit()
{ {
@ -347,9 +354,6 @@
Assert.IsTrue(complex.IsI, "Imaginary unit"); Assert.IsTrue(complex.IsI, "Imaginary unit");
} }
/// <summary>
/// The can determine if infinity.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanDetermineIfInfinity() public void CanDetermineIfInfinity()
@ -362,9 +366,6 @@
Assert.IsTrue(complex.IsInfinity, "Both parts are infinity."); Assert.IsTrue(complex.IsInfinity, "Both parts are infinity.");
} }
/// <summary>
/// The can determine if na n.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanDetermineIfNaN() public void CanDetermineIfNaN()
@ -377,9 +378,6 @@
Assert.IsTrue(complex.IsNaN, "Both parts are NaN."); Assert.IsTrue(complex.IsNaN, "Both parts are NaN.");
} }
/// <summary>
/// The can determine if one value complex number.
/// </summary>
[Test] [Test]
public void CanDetermineIfOneValueComplexNumber() public void CanDetermineIfOneValueComplexNumber()
{ {
@ -387,9 +385,6 @@
Assert.IsTrue(complex.IsOne, "Complex number with a value of one."); Assert.IsTrue(complex.IsOne, "Complex number with a value of one.");
} }
/// <summary>
/// The can determine if real non negative number.
/// </summary>
[Test] [Test]
public void CanDetermineIfRealNonNegativeNumber() public void CanDetermineIfRealNonNegativeNumber()
{ {
@ -397,9 +392,6 @@
Assert.IsTrue(complex.IsReal, "Is a real non-negative number."); Assert.IsTrue(complex.IsReal, "Is a real non-negative number.");
} }
/// <summary>
/// The can determine if real number.
/// </summary>
[Test] [Test]
public void CanDetermineIfRealNumber() public void CanDetermineIfRealNumber()
{ {
@ -407,9 +399,6 @@
Assert.IsTrue(complex.IsReal, "Is a real number."); Assert.IsTrue(complex.IsReal, "Is a real number.");
} }
/// <summary>
/// The can determine if zero value complex number.
/// </summary>
[Test] [Test]
public void CanDetermineIfZeroValueComplexNumber() public void CanDetermineIfZeroValueComplexNumber()
{ {
@ -417,9 +406,6 @@
Assert.IsTrue(complex.IsZero, "Zero complex number."); Assert.IsTrue(complex.IsZero, "Zero complex number.");
} }
/// <summary>
/// The can divide complex number and double using operators.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanDivideComplexNumberAndDoubleUsingOperators() public void CanDivideComplexNumberAndDoubleUsingOperators()
@ -427,12 +413,10 @@
AssertEx.That(() => (Complex.NaN * 1.0).IsNaN); AssertEx.That(() => (Complex.NaN * 1.0).IsNaN);
Assert.AreEqual(new Complex(-2, 2), new Complex(4, -4) / -2); 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(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); Assert.AreEqual(Complex.Infinity, Complex.One / 0);
} }
/// <summary>
/// The can divide two complex numbers.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanDivideTwoComplexNumbers() public void CanDivideTwoComplexNumbers()
@ -442,9 +426,6 @@
Assert.AreEqual(Complex.Infinity, Complex.One.Divide(Complex.Zero)); Assert.AreEqual(Complex.Infinity, Complex.One.Divide(Complex.Zero));
} }
/// <summary>
/// The can divide two complex numbers using operators.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanDivideTwoComplexNumbersUsingOperators() public void CanDivideTwoComplexNumbersUsingOperators()
@ -454,9 +435,6 @@
Assert.AreEqual(Complex.Infinity, Complex.One / Complex.Zero); Assert.AreEqual(Complex.Infinity, Complex.One / Complex.Zero);
} }
/// <summary>
/// The can multiple complex number and double using operators.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanMultipleComplexNumberAndDoubleUsingOperators() public void CanMultipleComplexNumberAndDoubleUsingOperators()
@ -466,9 +444,6 @@
Assert.AreEqual(new Complex(8, -8), 2 * new Complex(4, -4)); Assert.AreEqual(new Complex(8, -8), 2 * new Complex(4, -4));
} }
/// <summary>
/// The can multiple two complex numbers.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanMultipleTwoComplexNumbers() public void CanMultipleTwoComplexNumbers()
@ -477,9 +452,6 @@
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4).Multiply(new Complex(-2, 2))); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanMultipleTwoComplexNumbersUsingOperators() public void CanMultipleTwoComplexNumbersUsingOperators()
@ -488,9 +460,6 @@
Assert.AreEqual(new Complex(0, 16), new Complex(4, -4) * new Complex(-2, 2)); Assert.AreEqual(new Complex(0, 16), new Complex(4, -4) * new Complex(-2, 2));
} }
/// <summary>
/// The can negate value.
/// </summary>
[Test] [Test]
public void CanNegateValue() public void CanNegateValue()
{ {
@ -498,9 +467,6 @@
Assert.AreEqual(new Complex(-1.1, 2.2), complex.Negate()); Assert.AreEqual(new Complex(-1.1, 2.2), complex.Negate());
} }
/// <summary>
/// The can negate value using operator.
/// </summary>
[Test] [Test]
public void CanNegateValueUsingOperator() public void CanNegateValueUsingOperator()
{ {
@ -508,9 +474,6 @@
Assert.AreEqual(new Complex(-1.1, 2.2), -complex); Assert.AreEqual(new Complex(-1.1, 2.2), -complex);
} }
/// <summary>
/// The can subtract complex number and double using operartor.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractComplexNumberAndDoubleUsingOperartor() public void CanSubtractComplexNumberAndDoubleUsingOperartor()
@ -525,9 +488,6 @@
AssertEx.That(() => -2.2 - new Complex(-1.1, 2.2) == new Complex(-1.1, -2.2)); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanSubtractTwoComplexNumbers() public void CanSubtractTwoComplexNumbers()
@ -538,9 +498,6 @@
AssertEx.That(() => new Complex(1.1, -2.2).Subtract(new Complex(1.1, -2.2)) == Complex.Zero); 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] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanTestForEquality() public void CanTestForEquality()
@ -551,9 +508,6 @@
Assert.AreNotEqual(new Complex(-1.1, 2.2), new Complex(1.1, -2.2)); Assert.AreNotEqual(new Complex(-1.1, 2.2), new Complex(1.1, -2.2));
} }
/// <summary>
/// The can test for equality using operators.
/// </summary>
[Test] [Test]
[MultipleAsserts] [MultipleAsserts]
public void CanTestForEqualityUsingOperators() public void CanTestForEqualityUsingOperators()
@ -564,9 +518,6 @@
AssertEx.That(() => new Complex(-1.1, 2.2) != new Complex(1.1, -2.2)); AssertEx.That(() => new Complex(-1.1, 2.2) != new Complex(1.1, -2.2));
} }
/// <summary>
/// The can use plus.
/// </summary>
[Test] [Test]
public void CanUsePlus() public void CanUsePlus()
{ {
@ -574,9 +525,6 @@
Assert.AreEqual(complex, complex.Plus()); Assert.AreEqual(complex, complex.Plus());
} }
/// <summary>
/// The can use plus operator.
/// </summary>
[Test] [Test]
public void CanUsePlusOperator() public void CanUsePlusOperator()
{ {
@ -584,43 +532,6 @@
Assert.AreEqual(complex, +complex); 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] [Test]
public void TryParseCanHandleSymbols() public void TryParseCanHandleSymbols()
{ {
@ -650,57 +561,50 @@
Assert.IsTrue(ret); Assert.IsTrue(ret);
Assert.AreEqual(double.MaxValue, z.Real); Assert.AreEqual(double.MaxValue, z.Real);
Assert.AreEqual(double.MinValue, z.Imaginary); Assert.AreEqual(double.MinValue, z.Imaginary);
} }
[Test] [Test]
[Row("-1", -1, 0)] [Row("")]
[Row("-i", 0, -1)] [Row("+")]
[Row("i", 0, 1)] [Row("1i+2")]
[Row("2i", 0, 2)] [Row(null)]
[Row("1 + 2i", 1, 2)] public void TryParseReturnsFalseWhenGiveBadValue(string str)
[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; Complex z;
var ret = Complex.TryParse(str, out z); var ret = Complex.TryParse(str, out z);
Assert.IsTrue(ret); Assert.IsFalse(ret);
Assert.AreEqual(expectedReal, z.Real); Assert.AreEqual(0, z.Real);
Assert.AreEqual(expectedImag, z.Imaginary); Assert.AreEqual(0, 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); [Test]
Assert.IsTrue(ret); public void WithModulusArgumentThrowsArgumentOutOfRangeException()
Assert.AreEqual(-.1, z.Real); {
Assert.AreEqual(-.02, z.Imaginary); Assert.Throws<ArgumentOutOfRangeException>(
() => Complex.WithModulusArgument(-1, 1), "Throws exception because modulus is negative.");
}
ret = Complex.TryParse("(+1 +2i)", out z); [Test]
Assert.IsTrue(ret); [Row(0.0, 0.0, 0.0)]
Assert.AreEqual(1, z.Real); [Row(0.0, 1.0, 1.0)]
Assert.AreEqual(2, z.Imaginary); [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 // <copyright file="Complex.cs" company="">
// 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> // </copyright>
// <summary>
// Complex numbers class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace MathNet.Numerics namespace MathNet.Numerics
{ {
@ -551,38 +536,31 @@ namespace MathNet.Numerics
Complex result; 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 else
{ {
var absReal = Math.Abs(Real); var ratio = Real / Imaginary;
var absImag = Math.Abs(Imaginary); w = Math.Sqrt(absImag) * Math.Sqrt(0.5 * (Math.Abs(ratio) + Math.Sqrt(1.0 + (ratio * ratio))));
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))));
}
if (Real >= 0.0) if (Real >= 0.0)
{ {
result = new Complex(w, Imaginary / (2.0 * w)); result = new Complex(w, Imaginary / (2.0 * w));
} }
else if (Imaginary >= 0.0) else if (Imaginary >= 0.0)
{ {
result = new Complex(absImag / (2.0 * w), w); result = new Complex(absImag / (2.0 * w), w);
} }
else else
{ {
result = new Complex(absImag / (2.0 * w), -w); result = new Complex(absImag / (2.0 * w), -w);
}
} }
return result; return result;
@ -1068,7 +1046,9 @@ 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>
/// <returns>A norm of this value.</returns> /// <returns>
/// A norm of this value.
/// </returns>
double IPrecisionSupport<Complex>.Norm() double IPrecisionSupport<Complex>.Norm()
{ {
return ModulusSquared; return ModulusSquared;
@ -1078,8 +1058,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="otherValue">The value to compare with.</param> /// <param name="otherValue">
/// <returns>A norm of the difference between this and the other value.</returns> /// 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) double IPrecisionSupport<Complex>.NormOfDifference(Complex otherValue)
{ {
return (this - otherValue).ModulusSquared; return (this - otherValue).ModulusSquared;
@ -1088,13 +1072,18 @@ namespace MathNet.Numerics
#endregion #endregion
#region Parse Functions #region Parse Functions
/// <summary> /// <summary>
/// Creates a complex number based on a string. The string can be in the following /// 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 /// formats(without the quotes): 'n', 'ni', 'n +/- ni', 'n,n', 'n,ni,' '(n,n)', or
/// '(n,ni)', where n is a real number. /// '(n,ni)', where n is a real number.
/// </summary> /// </summary>
/// <returns>A complex number containing the value specified by the given string.</returns> /// <returns>
/// <param name="value">The string to parse.</param> /// 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) public static Complex Parse(string value)
{ {
return Parse(value, null); 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 /// formats(without the quotes): 'n', 'ni', 'n +/- ni', 'n,n', 'n,ni,' '(n,n)', or
/// '(n,ni)', where n is a double. /// '(n,ni)', where n is a double.
/// </summary> /// </summary>
/// <returns>A complex number containing the value specified by the given string.</returns> /// <returns>
/// <param name="value">the string to parse.</param> /// A complex number containing the value specified by the given string.
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information.</param> /// </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) public static Complex Parse(string value, IFormatProvider formatProvider)
{ {
if (value == null) 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. /// 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. /// A return value indicates whether the conversion succeeded or failed.
/// </summary> /// </summary>
/// <param name="value">A string containing a complex number to convert. </param> /// <param name="value">
/// <param name="result">The parsed value.</param> /// A string containing a complex number to convert.
/// <returns>If the conversion succeeds, the result will contain a complex number equivalent to value. /// </param>
/// Otherwise the result will contain complex32.Zero. This parameter is passed uninitialized</returns> /// <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) public static bool TryParse(string value, out Complex result)
{ {
return TryParse(value, null, out 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. /// Converts the string representation of a complex number to double-precision complex number equivalent.
/// A return value indicates whether the conversion succeeded or failed. /// A return value indicates whether the conversion succeeded or failed.
/// </summary> /// </summary>
/// <param name="value">A string containing a complex number to convert.</param> /// <param name="value">
/// <param name="formatProvider">An IFormatProvider that supplies culture-specific formatting information about value.</param> /// A string containing a complex number to convert.
/// <param name="result">The parsed value.</param> /// </param>
/// <param name="formatProvider">
/// An IFormatProvider that supplies culture-specific formatting information about value.
/// </param>
/// <param name="result">
/// The parsed value.
/// </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 complex32.Zero. This parameter is passed uninitialized
@ -1249,6 +1256,7 @@ namespace MathNet.Numerics
return ret; return ret;
} }
#endregion #endregion
} }
} }
Loading…
Cancel
Save