Browse Source

added complex32 type

la-knuth
Marcus Cuda 17 years ago
parent
commit
511b872aed
  1. 28
      src/Numerics/Complex.cs
  2. 1383
      src/Numerics/Complex32.cs
  3. 34
      src/Numerics/GlobalizationHelper.cs
  4. 1
      src/Numerics/Numerics.csproj
  5. 77
      src/Numerics/Precision.cs
  6. 22
      src/UnitTests/AssertHelpers.cs
  7. 284
      src/UnitTests/ComplexTests/Complex32Test.TextHandling.cs
  8. 572
      src/UnitTests/ComplexTests/Complex32Test.cs
  9. 10
      src/UnitTests/ComplexTests/ComplexTest.cs
  10. 2
      src/UnitTests/PrecisionTest.cs
  11. 2
      src/UnitTests/UnitTests.csproj

28
src/Numerics/Complex.cs

@ -1279,7 +1279,17 @@ namespace MathNet.Numerics
#region Conversion
/// <summary>
/// Explicit conversion of a real decimal to a real <c>Complex</c>.
/// Explicit conversion of a <c>Complex32</c> to a <c>Complex</c>.
/// </summary>
/// <param name="value">The decimal value to convert.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Complex(Complex32 value)
{
return new Complex(value.Real, value.Imaginary);
}
/// <summary>
/// Explicit conversion of a real decimal to a <c>Complex</c>.
/// </summary>
/// <param name="value">The decimal value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1289,7 +1299,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real byte to a real <c>Complex</c>.
/// Implicit conversion of a real byte to a <c>Complex</c>.
/// </summary>
/// <param name="value">The byte value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1299,7 +1309,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real short to a real <c>Complex</c>.
/// Implicit conversion of a real short to a <c>Complex</c>.
/// </summary>
/// <param name="value">The short value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1309,7 +1319,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real int to a real <c>Complex</c>.
/// Implicit conversion of a real int to a <c>Complex</c>.
/// </summary>
/// <param name="value">The int value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1319,7 +1329,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real long to a real <c>Complex</c>.
/// Implicit conversion of a real long to a <c>Complex</c>.
/// </summary>
/// <param name="value">The long value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1329,7 +1339,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real uint to a real <c>Complex</c>.
/// Implicit conversion of a real uint to a <c>Complex</c>.
/// </summary>
/// <param name="value">The uint value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1339,7 +1349,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real ulong to a real <c>Complex</c>.
/// Implicit conversion of a real ulong to a <c>Complex</c>.
/// </summary>
/// <param name="value">The ulong value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1349,7 +1359,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real float to a real <c>Complex</c>.
/// Implicit conversion of a real float to a <c>Complex</c>.
/// </summary>
/// <param name="value">The float value to convert.</param>
/// <returns>The result of the conversion.</returns>
@ -1359,7 +1369,7 @@ namespace MathNet.Numerics
}
/// <summary>
/// Implicit conversion of a real double to a real <c>Complex</c>.
/// Implicit conversion of a real double to a <c>Complex</c>.
/// </summary>
/// <param name="value">The double value to convert.</param>
/// <returns>The result of the conversion.</returns>

1383
src/Numerics/Complex32.cs

File diff suppressed because it is too large

34
src/Numerics/GlobalizationHelper.cs

@ -158,5 +158,39 @@ namespace MathNet.Numerics
token = token.Next;
return value;
}
/// <summary>
/// Globalized Parsing: Parse a float number
/// </summary>
/// <param name="token">First token of the number.</param>
/// <param name="culture">Culture Info.</param>
/// <returns>The parsed float number using the given culture information.</returns>
/// <exception cref="FormatException" />
internal static float ParseSingle(ref LinkedListNode<string> token, CultureInfo culture)
{
// in case the + and - in scientific notation are separated, join them back together.
if (token.Value.EndsWith("e", true, culture))
{
if (token.Next == null || token.Next.Next == null)
{
throw new FormatException();
}
token.Value = token.Value + token.Next.Value + token.Next.Next.Value;
var list = token.List;
list.Remove(token.Next.Next);
list.Remove(token.Next);
}
float value;
if (!Single.TryParse(token.Value, NumberStyles.Any, culture, out value))
{
throw new FormatException();
}
token = token.Next;
return value;
}
}
}

1
src/Numerics/Numerics.csproj

@ -72,6 +72,7 @@
<Compile Include="Complex.cs" />
<Compile Include="Constants.cs" />
<Compile Include="Control.cs" />
<Compile Include="Complex32.cs" />
<Compile Include="Distributions\Continuous\Beta.cs" />
<Compile Include="Distributions\Continuous\ContinuousUniform.cs" />
<Compile Include="Distributions\Continuous\LogNormal.cs" />

77
src/Numerics/Precision.cs

@ -95,7 +95,10 @@ namespace MathNet.Numerics
private static readonly int _numberOfDecimalPlacesForFloats;
/// <summary>Value representing 10 * 2^(-52)</summary>
private static readonly double _defaultRelativeAccuracy = _doubleMachinePrecision * 10;
private static readonly double _defaultDoubleRelativeAccuracy = _doubleMachinePrecision * 10;
/// <summary>Value representing 10 * 2^(-52)</summary>
private static readonly float _defaultSingleRelativeAccuracy = (float)(_singleMachinePrecision * 10);
#endregion
@ -691,7 +694,19 @@ namespace MathNet.Numerics
public static bool AlmostEqual(this double a, double b)
{
double diff = a - b;
return AlmostEqualWithError(a, b, diff, _defaultRelativeAccuracy);
return AlmostEqualWithError(a, b, diff, _defaultDoubleRelativeAccuracy);
}
/// <summary>
/// Checks whether two real numbers are almost equal.
/// </summary>
/// <param name="a">The first number</param>
/// <param name="b">The second number</param>
/// <returns>true if the two values differ by no more than 10 * 2^(-52); false otherwise.</returns>
public static bool AlmostEqual(this float a, float b)
{
double diff = a - b;
return AlmostEqualWithError(a, b, diff, _defaultSingleRelativeAccuracy);
}
/// <summary>
@ -705,7 +720,7 @@ namespace MathNet.Numerics
where T : IPrecisionSupport<T>
{
double diff = a.NormOfDifference(b);
return AlmostEqualWithError(a.Norm(), b.Norm(), diff, _defaultRelativeAccuracy);
return AlmostEqualWithError(a.Norm(), b.Norm(), diff, _defaultDoubleRelativeAccuracy);
}
/// <summary>
@ -995,6 +1010,62 @@ namespace MathNet.Numerics
return AlmostEqualWithRelativeDecimalPlaces(a, b, decimalPlaces);
}
/// <summary>
/// Compares two floats and determines if they are equal to within the specified number of decimal places or not. If the numbers
/// are very close to zero an absolute difference is compared, otherwise the relative difference is compared.
/// </summary>
/// <remarks>
/// <para>
/// The values are equal if the difference between the two numbers is smaller than 10^(-numberOfDecimalPlaces). We divide by
/// two so that we have half the range on each side of the numbers, e.g. if <paramref name="decimalPlaces"/> == 2, then 0.01 will equal between
/// 0.005 and 0.015, but not 0.02 and not 0.00
/// </para>
/// </remarks>
/// <param name="a">The first value.</param>
/// <param name="b">The second value.</param>
/// <param name="decimalPlaces">The number of decimal places.</param>
/// <returns><see langword="true" /> if both doubles are equal to each other within the specified number of decimal places; otherwise <see langword="false" />.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="decimalPlaces"/> is smaller than zero.
/// </exception>
public static bool AlmostEqualInDecimalPlaces(this float a, float b, int decimalPlaces)
{
if (decimalPlaces <= 0)
{
// Can't have a negative number of decimal places
throw new ArgumentOutOfRangeException("decimalPlaces");
}
// If A or B are a NAN, return false. NANs are equal to nothing,
// not even themselves.
if (double.IsNaN(a) || double.IsNaN(b))
{
return false;
}
// If A or B are infinity (positive or negative) then
// only return true if they are exactly equal to each other -
// that is, if they are both infinities of the same sign.
if (double.IsInfinity(a) || double.IsInfinity(b))
{
return a == b;
}
if (Math.Abs(a) < _doubleMachinePrecision || Math.Abs(b) < _doubleMachinePrecision)
{
return AlmostEqualWithAbsoluteDecimalPlaces(a, b, decimalPlaces);
}
// If both numbers are equal, get out now. This should remove the possibility of both numbers being zero
// and any problems associated with that.
if (a.Equals(b))
{
return true;
}
return AlmostEqualWithRelativeDecimalPlaces(a, b, decimalPlaces);
}
/// <summary>
/// Compares two doubles and determines if they are equal to within the specified number of decimal places or not.
/// </summary>

22
src/UnitTests/AssertHelpers.cs

@ -80,6 +80,28 @@ namespace MathNet.Numerics.UnitTests
}
}
/// <summary>
/// Asserts that the expected value and the actual value are equal up to a certain number of decimal places.
/// </summary>
/// <param name="expected">The expected value.</param>
/// <param name="actual">The actual value.</param>
/// <param name="decimalPlaces">The number of decimal places to agree on.</param>
public static void AlmostEqual(Complex32 expected, Complex32 actual, int decimalPlaces)
{
bool pass = expected.Real.AlmostEqualInDecimalPlaces(actual.Real, decimalPlaces);
if (!pass)
{
Assert.Fail("Real components are not equal within {0} places. Expected:{1}; Actual:{2}", decimalPlaces, expected.Real, actual.Real);
}
pass = expected.Imaginary.AlmostEqualInDecimalPlaces(actual.Imaginary, decimalPlaces);
if (!pass)
{
Assert.Fail("Imaginary components are not equal within {0} places. Expected:{1}; Actual:{2}", decimalPlaces, expected.Imaginary, actual.Imaginary);
}
}
/// <summary>
/// Asserts that the expected value and the actual value are equal up to a certain
/// maximum error.

284
src/UnitTests/ComplexTests/Complex32Test.TextHandling.cs

@ -0,0 +1,284 @@
// <copyright file="ComplexTest.TextHandling.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>
namespace MathNet.Numerics.UnitTests.ComplexTests
{
using System;
using System.Globalization;
using MbUnit.Framework;
[TestFixture]
public class Complex32TextHandlingTest
{
[Test]
[Row(1, -2, "1 -2i")]
[Row(1, 2, "1 + 2i")]
[Row(1, 0, "1")]
[Row(0, -2, "-2i")]
[Row(0, 2, "2i")]
[Row(0, 2, "2i")]
[Row(0, 0, "0")]
[Row(Double.NaN, Double.NaN, "{1}")]
[Row(Double.NaN, 0, "{1}")]
[Row(0, Double.NaN, "{1}")]
[Row(Double.PositiveInfinity, Double.PositiveInfinity, "{2}")]
[Row(1.1, 0, "1{0}1")]
[Row(-1.1, 0, "-1{0}1")]
[Row(0, 1.1, "1{0}1i")]
[Row(0, -1.1, "-1{0}1i")]
[Row(1.1, 1.1, "1{0}1 + 1{0}1i")]
public void CanFormatComplexToString(float real, float imag, string expected)
{
var numberFormat = NumberFormatInfo.CurrentInfo;
var a = new Complex32(real, imag);
Assert.AreEqual(
String.Format(
expected,
numberFormat.NumberDecimalSeparator,
numberFormat.NaNSymbol,
numberFormat.PositiveInfinitySymbol),
a.ToString());
}
[Test]
[MultipleAsserts]
[Row("en-US", "NaN", "Infinity", "1.1")]
[Row("tr-TR", "NaN", "Infinity", "1,1")]
[Row("de-DE", "n. def.", "+unendlich", "1,1")]
[Row("de-CH", "n. def.", "+unendlich", "1.1")]
[Row("he-IL", "לא מספר", "אינסוף חיובי", "1.1")]
public void CanFormatComplexToStringWithCulture(
string cultureName, string nan, string infinity, string number)
{
var provider = CultureInfo.GetCultureInfo(cultureName);
Assert.AreEqual(nan, Complex32.NaN.ToString(provider));
Assert.AreEqual(infinity, Complex32.Infinity.ToString(provider));
Assert.AreEqual("0", Complex32.Zero.ToString(provider));
Assert.AreEqual(String.Format("{0}", number), new Complex32(1.1f, 0.0f).ToString(provider));
Assert.AreEqual(String.Format("-{0}", number), new Complex32(-1.1f, 0f).ToString(provider));
Assert.AreEqual(String.Format("-{0}i", number), new Complex32(0.0f, -1.1f).ToString(provider));
Assert.AreEqual(String.Format("{0}i", number), new Complex32(0.0f, 1.1f).ToString(provider));
Assert.AreEqual(String.Format("{0} + {0}i", number), new Complex32(1.1f, 1.1f).ToString(provider));
}
[Test]
[MultipleAsserts]
public void CanFormatComplexToStringWithFormat()
{
Assert.AreEqual("0", String.Format("{0:G}", Complex32.Zero));
Assert.AreEqual("1 + 2i", String.Format("{0:G}", new Complex32(1, 2)));
Assert.AreEqual("001 + 002i", String.Format("{0:000;minus 000;zero}", new Complex32(1, 2)));
Assert.AreEqual("minus 002i", String.Format("{0:000;minus 000;zero}", new Complex32(0, -2)));
Assert.AreEqual("zero", String.Format("{0:000;minus 000;zero}", Complex32.Zero));
Assert.AreEqual("0", Complex32.Zero.ToString("G"));
Assert.AreEqual("1 + 2i", new Complex32(1, 2).ToString("G"));
Assert.AreEqual("001 + 002i", new Complex32(1, 2).ToString("#000;minus 000;zero"));
Assert.AreEqual("minus 002i", new Complex32(0, -2).ToString("#000;minus 000;zero"));
Assert.AreEqual("zero", Complex32.Zero.ToString("#000;minus 000;zero"));
}
[Test]
[MultipleAsserts]
public void CanFormatComplexToStringWithFormatInvariant()
{
var culture = CultureInfo.InvariantCulture;
Assert.AreEqual("NaN", String.Format(culture, "{0:.000}", Complex32.NaN));
Assert.AreEqual(".000", String.Format(culture, "{0:.000}", Complex32.Zero));
Assert.AreEqual("1.100", String.Format(culture, "{0:.000}", new Complex32(1.1f, 0.0f)));
Assert.AreEqual("1.100 + 1.100i", String.Format(culture, "{0:.000}", new Complex32(1.1f, 1.1f)));
Assert.AreEqual("NaN", Complex32.NaN.ToString("#.000", culture));
Assert.AreEqual("Infinity", Complex32.Infinity.ToString("#.000", culture));
Assert.AreEqual(".000", Complex32.Zero.ToString("#.000", culture));
Assert.AreEqual("1.100", new Complex32(1.1f, 0.0f).ToString("#.000", culture));
Assert.AreEqual("-1.100i", new Complex32(0.0f, -1.1f).ToString("#.000", culture));
Assert.AreEqual("1.100i", new Complex32(0.0f, 1.1f).ToString("#.000", culture));
Assert.AreEqual("1.100 + 1.100i", new Complex32(1.1f, 1.1f).ToString("#.000", culture));
}
[Test]
[Row("-1 -2i", -1, -2, "en-US")]
[Row("-1 - 2i ", -1, -2, "de-CH")]
public void CanParseStringToComplexWithCulture(
string text, float expectedReal, float expectedImaginary, string cultureName)
{
Complex32 parsed = Complex32.Parse(text, CultureInfo.GetCultureInfo(cultureName));
Assert.AreEqual(expectedReal, parsed.Real);
Assert.AreEqual(expectedImaginary, parsed.Imaginary);
}
[Test]
[Row("1", 1, 0)]
[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 , - 2 i ", -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)]
[Row("(-1E+1 -2e+2i)", -10, -200)]
[Row("(-1e-1,-2e-2i)", -0.1, -0.02)]
public void CanTryParseStringToComplexWithInvariant(string str, float expectedReal, float expectedImaginary)
{
var invariantCulture = CultureInfo.InvariantCulture;
Complex32 z;
var ret = Complex32.TryParse(str, invariantCulture, out z);
Assert.IsTrue(ret);
Assert.AreEqual(expectedReal, z.Real);
Assert.AreEqual(expectedImaginary, z.Imaginary);
}
[Test]
public void ParseThrowsFormatExceptionIfMissingClosingParen()
{
Assert.Throws<FormatException>(() => Complex32.Parse("(1,2"));
}
[Test]
public void TryParseCanHandleSymbols()
{
Complex32 z;
var ni = NumberFormatInfo.CurrentInfo;
var separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
var ret = Complex32.TryParse(
ni.NegativeInfinitySymbol + separator + ni.PositiveInfinitySymbol, out z);
Assert.IsTrue(ret, "A1");
Assert.AreEqual(float.NegativeInfinity, z.Real, "A2");
Assert.AreEqual(float.PositiveInfinity, z.Imaginary, "A3");
ret = Complex32.TryParse(ni.NaNSymbol + separator + ni.NaNSymbol, out z);
Assert.IsTrue(ret, "B1");
Assert.AreEqual(float.NaN, z.Real, "B2");
Assert.AreEqual(float.NaN, z.Imaginary, "B3");
ret = Complex32.TryParse(ni.NegativeInfinitySymbol + "+" + ni.PositiveInfinitySymbol + "i", out z);
Assert.IsTrue(ret, "C1");
Assert.AreEqual(float.NegativeInfinity, z.Real, "C2");
Assert.AreEqual(float.PositiveInfinity, z.Imaginary, "C3");
ret = Complex32.TryParse(ni.NaNSymbol + "+" + ni.NaNSymbol + "i", out z);
Assert.IsTrue(ret, "D1");
Assert.AreEqual(float.NaN, z.Real, "D2");
Assert.AreEqual(float.NaN, z.Imaginary, "D3");
ret = Complex32.TryParse(
float.MaxValue.ToString("R") + " " + float.MinValue.ToString("R") + "i",
out z);
Assert.IsTrue(ret, "E1");
Assert.AreEqual(float.MaxValue, z.Real, "E2");
Assert.AreEqual(float.MinValue, z.Imaginary, "E3");
}
[Test]
[Row("en-US")]
[Row("tr-TR")]
[Row("de-DE")]
[Row("de-CH")]
[Row("he-IL")]
public void TryParseCanHandleSymbolsWithCulture(string cultureName)
{
Complex32 z;
var culture = CultureInfo.GetCultureInfo(cultureName);
var ni = culture.NumberFormat;
var separator = culture.TextInfo.ListSeparator;
var ret = Complex32.TryParse(
ni.NegativeInfinitySymbol + separator + ni.PositiveInfinitySymbol, culture, out z);
Assert.IsTrue(ret, "A1");
Assert.AreEqual(float.NegativeInfinity, z.Real, "A2");
Assert.AreEqual(float.PositiveInfinity, z.Imaginary, "A3");
ret = Complex32.TryParse(ni.NaNSymbol + separator + ni.NaNSymbol, culture, out z);
Assert.IsTrue(ret, "B1");
Assert.AreEqual(float.NaN, z.Real, "B2");
Assert.AreEqual(float.NaN, z.Imaginary, "B3");
ret = Complex32.TryParse(ni.NegativeInfinitySymbol + "+" + ni.PositiveInfinitySymbol + "i", culture, out z);
Assert.IsTrue(ret, "C1");
Assert.AreEqual(float.NegativeInfinity, z.Real, "C2");
Assert.AreEqual(float.PositiveInfinity, z.Imaginary, "C3");
ret = Complex32.TryParse(ni.NaNSymbol + "+" + ni.NaNSymbol + "i", culture, out z);
Assert.IsTrue(ret, "D1");
Assert.AreEqual(float.NaN, z.Real, "D2");
Assert.AreEqual(float.NaN, z.Imaginary, "D3");
ret = Complex32.TryParse(
float.MaxValue.ToString("R", culture) + " " + float.MinValue.ToString("R", culture) + "i",
culture,
out z);
Assert.IsTrue(ret, "E1");
Assert.AreEqual(float.MaxValue, z.Real, "E2");
Assert.AreEqual(float.MinValue, z.Imaginary, "E3");
}
[Test]
[Row("")]
[Row("+")]
[Row("1-")]
[Row("i+")]
[Row("1/2i")]
[Row("1i+2i")]
[Row("i1i")]
[Row("(1i,2)")]
[Row("1e+")]
[Row("1e")]
[Row("1,")]
[Row(",1")]
[Row(null)]
[Row("()")]
[Row("( )")]
public void TryParseReturnsFalseWhenGivenBadValueWithInvariant(string str)
{
Complex32 z;
var ret = Complex32.TryParse(str, CultureInfo.InvariantCulture, out z);
Assert.IsFalse(ret);
Assert.AreEqual(0, z.Real);
Assert.AreEqual(0, z.Imaginary);
}
}
}

572
src/UnitTests/ComplexTests/Complex32Test.cs

@ -0,0 +1,572 @@
// <copyright file="ComplexTest.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>
namespace MathNet.Numerics.UnitTests.ComplexTests
{
using System;
using MbUnit.Framework;
[TestFixture]
public class Complex32Test
{
[Test]
[MultipleAsserts]
public void CanAddComplexNumberAndDoubleUsingOperartor()
{
AssertEx.That(() => (Complex32.NaN + float.NaN).IsNaN);
AssertEx.That(() => (float.NaN + Complex32.NaN).IsNaN);
AssertEx.That(() => (float.PositiveInfinity + Complex32.One).IsInfinity);
AssertEx.That(() => (Complex32.Infinity + 1.0f).IsInfinity);
AssertEx.That(() => (Complex32.One + 0.0f) == Complex32.One);
AssertEx.That(() => (0.0f + Complex32.One) == Complex32.One);
AssertEx.That(() => (new Complex32(1.1f, -2.2f) + 1.1f == new Complex32(2.2f, -2.2f)));
AssertEx.That(() => -2.2f + new Complex32(-1.1f, 2.2f) == new Complex32(-3.3f, 2.2f));
}
[Test]
[MultipleAsserts]
public void CanAddSubtractComplexNumbersUsingOperartor()
{
AssertEx.That(() => (Complex32.NaN - Complex32.NaN).IsNaN);
AssertEx.That(() => (Complex32.Infinity - Complex32.One).IsInfinity);
AssertEx.That(() => (Complex32.One - Complex32.Zero) == Complex32.One);
AssertEx.That(() => (new Complex32(1.1f, -2.2f) - new Complex32(1.1f, -2.2f)) == Complex32.Zero);
}
[Test]
[MultipleAsserts]
public void CanAddTwoComplexNumbers()
{
AssertEx.That(() => Complex32.NaN.Add(Complex32.NaN).IsNaN);
AssertEx.That(() => Complex32.Infinity.Add(Complex32.One).IsInfinity);
AssertEx.That(() => Complex32.One.Add(Complex32.Zero) == Complex32.One);
AssertEx.That(() => new Complex32(1.1f, -2.2f).Add(new Complex32(-1.1f, 2.2f)) == Complex32.Zero);
}
[Test]
[MultipleAsserts]
public void CanAddTwoComplexNumbersUsingOperartor()
{
AssertEx.That(() => (Complex32.NaN + Complex32.NaN).IsNaN);
AssertEx.That(() => (Complex32.Infinity + Complex32.One).IsInfinity);
AssertEx.That(() => (Complex32.One + Complex32.Zero) == Complex32.One);
AssertEx.That(() => (new Complex32(1.1f, -2.2f) + new Complex32(-1.1f, 2.2f)) == Complex32.Zero);
}
[Test]
[MultipleAsserts]
public void CanCalculateHashCode()
{
var complex = new Complex32(1, 0);
Assert.AreEqual(1065353216, complex.GetHashCode());
complex = new Complex32(0, 1);
Assert.AreEqual(-1065353216, complex.GetHashCode());
complex = new Complex32(1, 1);
Assert.AreEqual(-16777216, complex.GetHashCode());
}
[Test]
[Row(0.0f, 0.0f, 1.0f, 0.0f)]
[Row(0.0f, 1.0f, 0.54030230586813977, 0.8414709848078965)]
[Row(-1.0f, 1.0f, 0.19876611034641295, 0.30955987565311222)]
[Row(-111.1, 111.1, -2.3259065941590448e-49, -5.1181940185795617e-49)]
public void CanComputeExponential(float real, float imag, float expectedReal, float expectedImag)
{
var value = new Complex32(real, imag);
var expected = new Complex32(expectedReal, expectedImag);
AssertHelpers.AlmostEqual(expected, value.Exponential(), 7);
}
[Test]
[Row(0.0f, 0.0f, float.NegativeInfinity, 0.0f)]
[Row(0.0f, 1.0f, 0.0f, 1.5707963267948966)]
[Row(-1.0f, 1.0f, 0.34657359027997264, 2.3561944901923448)]
[Row(-111.1, 111.1, 5.0570042869255571, 2.3561944901923448)]
[Row(111.1, -111.1, 5.0570042869255571, -0.78539816339744828)]
public void CanComputeNaturalLogarithm(float real, float imag, float expectedReal, float expectedImag)
{
var value = new Complex32(real, imag);
var expected = new Complex32(expectedReal, expectedImag);
AssertHelpers.AlmostEqual(expected, value.NaturalLogarithm(), 7);
}
[Test]
[MultipleAsserts]
public void CanComputePower()
{
var a = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
var b = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(
new Complex32(9.99998047207974718744e-1f, -1.76553541154378695012e-6f), a.Power(b), 7);
a = new Complex32(0.0f, 1.19209289550780998537e-7f);
b = new Complex32(0.0f, -1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(1.00000018725172576491f, 1.90048076369011843105e-6f), a.Power(b), 7);
a = new Complex32(0.0f, -1.19209289550780998537e-7f);
b = new Complex32(0.0f, 0.5f);
AssertHelpers.AlmostEqual(new Complex32(-2.56488189382693049636e-1f, -2.17823120666116144959f), a.Power(b), 6);
a = new Complex32(0.0f, 0.5f);
b = new Complex32(0.0f, -0.5f);
AssertHelpers.AlmostEqual(new Complex32(2.06287223508090495171f, 7.45007062179724087859e-1f), a.Power(b), 7);
a = new Complex32(0.0f, -0.5f);
b = new Complex32(0.0f, 1.0f);
AssertHelpers.AlmostEqual(new Complex32(3.70040633557002510874f, -3.07370876701949232239f), a.Power(b), 7);
a = new Complex32(0.0f, 2.0f);
b = new Complex32(0.0f, -2.0f);
AssertHelpers.AlmostEqual(new Complex32(4.24532146387429353891f, -2.27479427903521192648e1f), a.Power(b), 7);
a = new Complex32(0.0f, -8.388608e6f);
b = new Complex32(1.19209289550780998537e-7f, 0.0f);
AssertHelpers.AlmostEqual(new Complex32(1.00000190048219620166f, -1.87253870018168043834e-7f), a.Power(b), 7);
a = new Complex32(0.0f, 0.0f);
b = new Complex32(0.0f, 0.0f);
AssertHelpers.AlmostEqual(new Complex32(1.0f, 0.0f), a.Power(b), 7);
a = new Complex32(0.0f, 0.0f);
b = new Complex32(1.0f, 0.0f);
AssertHelpers.AlmostEqual(new Complex32(0.0f, 0.0f), a.Power(b), 7);
a = new Complex32(0.0f, 0.0f);
b = new Complex32(-1.0f, 0.0f);
AssertHelpers.AlmostEqual(new Complex32(float.PositiveInfinity, 0.0f), a.Power(b), 7);
a = new Complex32(0.0f, 0.0f);
b = new Complex32(-1.0f, 1.0f);
AssertHelpers.AlmostEqual(new Complex32(float.PositiveInfinity, float.PositiveInfinity), a.Power(b), 7);
a = new Complex32(0.0f, 0.0f);
b = new Complex32(0.0f, 1.0f);
AssertEx.That(() => a.Power(b).IsNaN);
}
[Test]
[MultipleAsserts]
public void CanComputeRoot()
{
var a = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
var b = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(0.0f, 0.0f), a.Root(b), 7);
a = new Complex32(0.0f, -1.19209289550780998537e-7f);
b = new Complex32(0.0f, 0.5f);
AssertHelpers.AlmostEqual(new Complex32(0.038550761943650161f, 0.019526430428319544f), a.Root(b), 6);
a = new Complex32(0.0f, 0.5f);
b = new Complex32(0.0f, -0.5f);
AssertHelpers.AlmostEqual(new Complex32(0.007927894711475968f, -0.042480480425152213f), a.Root(b), 6);
a = new Complex32(0.0f, -0.5f);
b = new Complex32(0.0f, 1.0f);
AssertHelpers.AlmostEqual(new Complex32(0.15990905692806806f, 0.13282699942462053f), a.Root(b), 7);
a = new Complex32(0.0f, 2.0f);
b = new Complex32(0.0f, -2.0f);
AssertHelpers.AlmostEqual(new Complex32(0.42882900629436788f, 0.15487175246424678f), a.Root(b), 7);
a = new Complex32(0.0f, -8.388608e6f);
b = new Complex32(1.19209289550780998537e-7f, 0.0f);
AssertHelpers.AlmostEqual(new Complex32(float.PositiveInfinity, float.NegativeInfinity), a.Root(b), 7);
}
[Test]
[MultipleAsserts]
public void CanComputeSquare()
{
var complex = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(0, 2.8421709430403888e-14f), complex.Square(), 7);
complex = new Complex32(0.0f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(-1.4210854715201944e-14f, 0.0f), complex.Square(), 7);
complex = new Complex32(0.0f, -1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(-1.4210854715201944e-14f, 0.0f), complex.Square(), 7);
complex = new Complex32(0.0f, 0.5f);
AssertHelpers.AlmostEqual(new Complex32(-0.25f, 0.0f), complex.Square(), 7);
complex = new Complex32(0.0f, -0.5f);
AssertHelpers.AlmostEqual(new Complex32(-0.25f, 0.0f), complex.Square(), 7);
complex = new Complex32(0.0f, -8.388608e6f);
AssertHelpers.AlmostEqual(new Complex32(-70368744177664.0f, 0.0f), complex.Square(), 7);
}
[Test]
[MultipleAsserts]
public void CanComputeSquareRoot()
{
var complex = new Complex32(1.19209289550780998537e-7f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(
new Complex32(0.00037933934912842666f, 0.00015712750315077684f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(
new Complex32(0.00024414062499999973f, 0.00024414062499999976f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, -1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(
new Complex32(0.00024414062499999973f, -0.00024414062499999976f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, 0.5f);
AssertHelpers.AlmostEqual(new Complex32(0.5f, 0.5f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, -0.5f);
AssertHelpers.AlmostEqual(new Complex32(0.5f, -0.5f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, -8.388608e6f);
AssertHelpers.AlmostEqual(new Complex32(2048.0f, -2048.0f), complex.SquareRoot(), 7);
complex = new Complex32(8.388608e6f, 1.19209289550780998537e-7f);
AssertHelpers.AlmostEqual(new Complex32(2896.3093757400989f, 2.0579515874459933e-11f), complex.SquareRoot(), 7);
complex = new Complex32(0.0f, 0.0f);
AssertHelpers.AlmostEqual(Complex32.Zero, complex.SquareRoot(), 7);
}
[Test]
[MultipleAsserts]
public void CanConvertDoubleToComplex()
{
AssertEx.That(() => ((Complex32)float.NaN).IsNaN);
AssertEx.That(() => ((Complex32)float.NegativeInfinity).IsInfinity);
Assert.AreEqual(1.1f, new Complex32(1.1f, 0));
}
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberUsingTheConstructor()
{
var complex = new Complex32(1.1f, -2.2f);
Assert.AreEqual(1.1f, complex.Real, "Real part is 1.1f.");
Assert.AreEqual(-2.2f, complex.Imaginary, "Imaginary part is -2.2f.");
}
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberWithModulusArgument()
{
var complex = Complex32.WithModulusArgument(2, (float)-Math.PI / 6);
Assert.AreApproximatelyEqual((float)Math.Sqrt(3), complex.Real, 1e-7f, "Real part is Sqrt(3).");
Assert.AreApproximatelyEqual(-1.0f, complex.Imaginary, 1e-7f, "Imaginary part is -1.");
}
[Test]
[MultipleAsserts]
public void CanCreateComplexNumberWithRealImaginaryIntializer()
{
var complex = Complex32.WithRealImaginary(1.1f, -2.2f);
Assert.AreEqual(1.1f, complex.Real, "Real part is 1.1f.");
Assert.AreEqual(-2.2f, complex.Imaginary, "Imaginary part is -2.2f.");
}
[Test]
public void CanDetermineIfImaginaryUnit()
{
var complex = new Complex32(0, 1);
Assert.IsTrue(complex.IsImaginaryOne, "Imaginary unit");
}
[Test]
[MultipleAsserts]
public void CanDetermineIfInfinity()
{
var complex = new Complex32(float.PositiveInfinity, 1);
Assert.IsTrue(complex.IsInfinity, "Real part is infinity.");
complex = new Complex32(1, float.NegativeInfinity);
Assert.IsTrue(complex.IsInfinity, "Imaginary part is infinity.");
complex = new Complex32(float.NegativeInfinity, float.PositiveInfinity);
Assert.IsTrue(complex.IsInfinity, "Both parts are infinity.");
}
[Test]
[MultipleAsserts]
public void CanDetermineIfNaN()
{
var complex = new Complex32(float.NaN, 1);
Assert.IsTrue(complex.IsNaN, "Real part is NaN.");
complex = new Complex32(1, float.NaN);
Assert.IsTrue(complex.IsNaN, "Imaginary part is NaN.");
complex = new Complex32(float.NaN, float.NaN);
Assert.IsTrue(complex.IsNaN, "Both parts are NaN.");
}
[Test]
public void CanDetermineIfOneValueComplexNumber()
{
var complex = new Complex32(1, 0);
Assert.IsTrue(complex.IsOne, "Complex32 number with a value of one.");
}
[Test]
public void CanDetermineIfRealNonNegativeNumber()
{
var complex = new Complex32(1, 0);
Assert.IsTrue(complex.IsReal, "Is a real non-negative number.");
}
[Test]
public void CanDetermineIfRealNumber()
{
var complex = new Complex32(-1, 0);
Assert.IsTrue(complex.IsReal, "Is a real number.");
}
[Test]
public void CanDetermineIfZeroValueComplexNumber()
{
var complex = new Complex32(0, 0);
Assert.IsTrue(complex.IsZero, "Zero complex number.");
}
[Test]
[MultipleAsserts]
public void CanDivideComplexNumberAndDoubleUsingOperators()
{
AssertEx.That(() => (Complex32.NaN * 1.0f).IsNaN);
Assert.AreEqual(new Complex32(-2, 2), new Complex32(4, -4) / -2);
Assert.AreEqual(new Complex32(0.25f, 0.25f), 2 / new Complex32(4, -4));
Assert.AreEqual(Complex32.Infinity, 2.0f / Complex32.Zero);
Assert.AreEqual(Complex32.Infinity, Complex32.One / 0);
}
[Test]
[MultipleAsserts]
public void CanDivideTwoComplexNumbers()
{
AssertEx.That(() => Complex32.NaN.Multiply(Complex32.One).IsNaN);
Assert.AreEqual(new Complex32(-2, 0), new Complex32(4, -4).Divide(new Complex32(-2, 2)));
Assert.AreEqual(Complex32.Infinity, Complex32.One.Divide(Complex32.Zero));
}
[Test]
[MultipleAsserts]
public void CanDivideTwoComplexNumbersUsingOperators()
{
AssertEx.That(() => (Complex32.NaN / Complex32.One).IsNaN);
Assert.AreEqual(new Complex32(-2, 0), new Complex32(4, -4) / new Complex32(-2, 2));
Assert.AreEqual(Complex32.Infinity, Complex32.One / Complex32.Zero);
}
[Test]
[MultipleAsserts]
public void CanMultipleComplexNumberAndDoubleUsingOperators()
{
AssertEx.That(() => (Complex32.NaN * 1.0f).IsNaN);
Assert.AreEqual(new Complex32(8, -8), new Complex32(4, -4) * 2);
Assert.AreEqual(new Complex32(8, -8), 2 * new Complex32(4, -4));
}
[Test]
[MultipleAsserts]
public void CanMultipleTwoComplexNumbers()
{
AssertEx.That(() => Complex32.NaN.Multiply(Complex32.One).IsNaN);
Assert.AreEqual(new Complex32(0, 16), new Complex32(4, -4).Multiply(new Complex32(-2, 2)));
}
[Test]
[MultipleAsserts]
public void CanMultipleTwoComplexNumbersUsingOperators()
{
AssertEx.That(() => (Complex32.NaN * Complex32.One).IsNaN);
Assert.AreEqual(new Complex32(0, 16), new Complex32(4, -4) * new Complex32(-2, 2));
}
[Test]
public void CanNegateValue()
{
var complex = new Complex32(1.1f, -2.2f);
Assert.AreEqual(new Complex32(-1.1f, 2.2f), complex.Negate());
}
[Test]
public void CanNegateValueUsingOperator()
{
var complex = new Complex32(1.1f, -2.2f);
Assert.AreEqual(new Complex32(-1.1f, 2.2f), -complex);
}
[Test]
[MultipleAsserts]
public void CanSubtractComplexNumberAndDoubleUsingOperartor()
{
AssertEx.That(() => (Complex32.NaN - float.NaN).IsNaN);
AssertEx.That(() => (float.NaN - Complex32.NaN).IsNaN);
AssertEx.That(() => (float.PositiveInfinity - Complex32.One).IsInfinity);
AssertEx.That(() => (Complex32.Infinity - 1.0f).IsInfinity);
AssertEx.That(() => (Complex32.One - 0.0f) == Complex32.One);
AssertEx.That(() => (0.0f - Complex32.One) == -Complex32.One);
AssertEx.That(() => (new Complex32(1.1f, -2.2f) - 1.1f == new Complex32(0.0f, -2.2f)));
AssertEx.That(() => -2.2f - new Complex32(-1.1f, 2.2f) == new Complex32(-1.1f, -2.2f));
}
[Test]
[MultipleAsserts]
public void CanSubtractTwoComplexNumbers()
{
AssertEx.That(() => Complex32.NaN.Subtract(Complex32.NaN).IsNaN);
AssertEx.That(() => Complex32.Infinity.Subtract(Complex32.One).IsInfinity);
AssertEx.That(() => Complex32.One.Subtract(Complex32.Zero) == Complex32.One);
AssertEx.That(() => new Complex32(1.1f, -2.2f).Subtract(new Complex32(1.1f, -2.2f)) == Complex32.Zero);
}
[Test]
[MultipleAsserts]
public void CanTestForEquality()
{
Assert.AreNotEqual(Complex32.NaN, Complex32.NaN);
Assert.AreEqual(Complex32.Infinity, Complex32.Infinity);
Assert.AreEqual(new Complex32(1.1f, -2.2f), new Complex32(1.1f, -2.2f));
Assert.AreNotEqual(new Complex32(-1.1f, 2.2f), new Complex32(1.1f, -2.2f));
}
[Test]
[MultipleAsserts]
public void CanTestForEqualityUsingOperators()
{
AssertEx.That(() => Complex32.NaN != Complex32.NaN);
AssertEx.That(() => Complex32.Infinity == Complex32.Infinity);
AssertEx.That(() => new Complex32(1.1f, -2.2f) == new Complex32(1.1f, -2.2f));
AssertEx.That(() => new Complex32(-1.1f, 2.2f) != new Complex32(1.1f, -2.2f));
}
[Test]
public void CanUsePlus()
{
var complex = new Complex32(1.1f, -2.2f);
Assert.AreEqual(complex, complex.Plus());
}
[Test]
public void CanUsePlusOperator()
{
var complex = new Complex32(1.1f, -2.2f);
Assert.AreEqual(complex, +complex);
}
[Test]
public void WithModulusArgumentThrowsArgumentOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>(
() => Complex32.WithModulusArgument(-1, 1), "Throws exception because modulus is negative.");
}
[Test]
[Row(0.0f, 0.0f, 0.0f)]
[Row(0.0f, 1.0f, 1.0f)]
[Row(-1.0f, 1.0f, 1.4142135623730951)]
[Row(-111.1, 111.1, 157.11912677965086)]
public void CanComputeMagnitude(float real, float imag, float expected)
{
Assert.AreEqual(expected, new Complex32(real, imag).Magnitude);
}
[Test]
[Row(float.PositiveInfinity, float.PositiveInfinity, Constants.Sqrt1Over2, Constants.Sqrt1Over2)]
[Row(float.PositiveInfinity, float.NegativeInfinity, Constants.Sqrt1Over2, -Constants.Sqrt1Over2)]
[Row(float.NegativeInfinity, float.PositiveInfinity, -Constants.Sqrt1Over2, -Constants.Sqrt1Over2)]
[Row(float.NegativeInfinity, float.NegativeInfinity, -Constants.Sqrt1Over2, Constants.Sqrt1Over2)]
[Row(0.0f, 0.0f, 0.0f, 0.0f)]
[Row(-1.0f, 1.0f, -0.70710678118654746, 0.70710678118654746)]
[Row(-111.1, 111.1, -0.70710678118654746, 0.70710678118654746)]
public void CanComputeSign(float real, float imag, float expectedReal, float expectedImag)
{
Assert.AreEqual(new Complex32(expectedReal, expectedImag), new Complex32(real, imag).Sign);
}
[Test]
public void CanConvertDecimalToComplex()
{
var orginal = new decimal(1.234567890);
var complex = (Complex32)orginal;
Assert.AreEqual((float)1.234567890, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertByteToComplex()
{
const byte orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertShortToComplex()
{
const short orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertIntToComplex()
{
const int orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertLongToComplex()
{
const long orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertUIntToComplex()
{
const uint orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertULongToComplex()
{
const ulong orginal = 123;
var complex = (Complex32)orginal;
Assert.AreEqual(123, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertFloatToComplex()
{
const float orginal = 123.456789f;
var complex = (Complex32)orginal;
Assert.AreEqual(123.456789f, complex.Real);
Assert.AreEqual(0.0f, complex.Imaginary);
}
[Test]
public void CanConvertComplexToComplex32()
{
var complex32 = new Complex(123.456, -78.9);
var complex = (Complex32)complex32;
Assert.AreEqual(123.456f, complex.Real);
Assert.AreEqual(-78.9f, complex.Imaginary);
}
[Test]
public void CanGetConjugate()
{
var complex = new Complex(123.456, -78.9);
var conjugate = complex.Conjugate;
Assert.AreEqual(complex.Real, conjugate.Real);
Assert.AreEqual(-complex.Imaginary, conjugate.Imaginary);
}
}
}

10
src/UnitTests/ComplexTests/ComplexTest.cs

@ -794,12 +794,12 @@ namespace MathNet.Numerics.UnitTests.ComplexTests
}
[Test]
public void CanConvertDoulbeComplex()
public void CanConvertComplex32ToComplex()
{
const double orginal = 123.456789;
var complex = (Complex)orginal;
Assert.AreEqual(123.456789, complex.Real);
Assert.AreEqual(0.0, complex.Imaginary);
var complex32 = new Complex32(123.456f, -78.9f);
var complex = (Complex)complex32;
Assert.AreEqual(123.456f, (float)complex.Real);
Assert.AreEqual(-78.9f, (float)complex.Imaginary);
}
}
}

2
src/UnitTests/PrecisionTest.cs

@ -35,7 +35,7 @@ namespace MathNet.Numerics.UnitTests
public sealed class PrecisionTest
{
private const double _acceptableError = 1e-12;
private readonly double _doublePrecision = System.Math.Pow(2, -53);
private readonly double _doublePrecision = Math.Pow(2, -53);
[Test]
public void Magnitude()

2
src/UnitTests/UnitTests.csproj

@ -64,6 +64,8 @@
<Compile Include="CombinatoricsTests\CombinatoricsCountingTest.cs" />
<Compile Include="ComplexTests\ComplexTest.TextHandling.cs" />
<Compile Include="ComplexTests\ComplexTest.cs" />
<Compile Include="ComplexTests\Complex32Test.TextHandling.cs" />
<Compile Include="ComplexTests\Complex32Test.cs" />
<Compile Include="DistributionTests\CommonDistributionTests.cs" />
<Compile Include="DistributionTests\Continuous\BetaTests.cs" />
<Compile Include="DistributionTests\Continuous\ContinuousUniformTests.cs" />

Loading…
Cancel
Save