private static void VerifyComplexComparison(Complex c1, Complex c2, bool expectedResult, bool expectedResultEqual) { Assert.True(expectedResult == (c1 == c2), string.Format("c1:{0} == c2{1} is not '{2}' as expected", c1, c2, expectedResult)); Assert.True(expectedResult == (c2 == c1), string.Format("c2:{0} == c1{1} is not '{2}' as expected", c2, c1, expectedResult)); Assert.True(expectedResult != (c1 != c2), string.Format("c1:{0} != c2{1} is not '{2}' as expected", c1, c2, !expectedResult)); Assert.True(expectedResult != (c2 != c1), string.Format("c2:{0} != c1{1} is not '{2}' as expected", c2, c1, !expectedResult)); bool result = c1.Equals(c2); Assert.True(expectedResultEqual == result, string.Format("c1:{0}.Equals(c2{1}) is not '{2}' as expected", c1, c2, expectedResultEqual)); if (result) // then verify Hash Code equality { Assert.True(c1.GetHashCode() == c2.GetHashCode(), string.Format("c1:{0}.GetHashCode() == c2:{1}.GetHashCode() is 'true' as expected", c1, c2)); } result = c2.Equals(c1); Assert.True(expectedResultEqual == result, string.Format("c2:{0}.Equals(c1{1}) is not '{2}' as expected", c2, c1, expectedResultEqual)); if (result) // then verify Hash Code equality { Assert.True(c2.GetHashCode() == c1.GetHashCode(), string.Format("Obj c2:{0}.GetHashCode() == c1:{1}.GetHashCode() is 'true' as expected", c2, c1)); } Assert.True(expectedResult == c2.Equals((Object)c1), string.Format("c2:{0}.Equals((object) c1{1}) is not '{2}' as expected", c2, c1, expectedResult)); Assert.True(expectedResult == c1.Equals((Object)c2), string.Format("c1:{0}.Equals((object) c2{1}) is not '{2}' as expected", c1, c2, expectedResult)); }
public void EqualsWithComplexTest() { var target = new Complex(1, 3); Assert.IsTrue(target.Equals(new Complex(1, 3))); Assert.IsFalse(target.Equals(Rnd)); }
public void EqualsTest() { Assert.AreEqual(false, _left.Equals(_right)); Complex complex = new Complex(_left.RealNumbers, _left.ImaginaryUnit); Assert.AreEqual(true, _left.Equals(complex)); }
/// <summary>Gets the value of the confluent hypergeometric function of the first kind (Kummer's function) 1_F_1(a,b,z) = M(a,b,z) = \sum_{k=0}^\infty (a)_k / (b)_k * z^k /k!. /// </summary> /// <param name="a">The first argument.</param> /// <param name="b">The second argument.</param> /// <param name="z">The third argument.</param> /// <returns>The value of the confluent hypergeometric function 1_F_1(a,b,z) = M(a,b,z) = \sum_{k=0}^\infty (a)_k / (b)_k * z^k /k!.</returns> ///<remarks>The implementation is based on 'Algorithms for the Computation of Mathematical Functions" p.182-191, Luke, Y.L (1977), Academic Press and ///"Computing the confluent hypergeometric function, M(a,b,x)", K. E. Muller, Numer. Math. 90, p.179-196 (2001). The later reference has several typos.</remarks> public Complex GetValue(Complex a, Complex b, Complex z) { /* remark: This implementation is almost identical to the real-argument implementation above. */ if (a.Equals(b)) // without tolerance { return(Complex.Exp(z)); } else if (a.Equals(0.0) || z.Equals(0.0)) // without tolerance { return(1.0); } else if ((b == 0) || ((b.Imaginary == 0.0) && (b.Real < 0.0) && (b.Real == Math.Round(b.Real)))) // if b is a non-positive integer or 0, the function is not defined { throw new ArgumentException(String.Format(ExceptionMessages.ArgumentIsInvalidForObject, z, "1F1"), nameof(z)); } /* We replace 'x' by '-x' and '\delta' by 'a' in "Computing the confluent hypergeometric function M(a,b,x)", K. E. Muller */ Complex QnMinus3 = 1.0; var QnMinus2 = 1 - z * (a + 1) / (2.0 * b); var QnMinus1 = 1 - z * (a + 2) / (2 * (b + 1)) + z * z * (a + 1) * (a + 2) / (12 * b * (b + 1)); Complex PnMinus3 = 1.0; var PnMinus2 = QnMinus2 + z * a / b; var PnMinus1 = QnMinus1 + a * z / b * (1 - z * (a + 2) / (2 * (b + 1))) + z * z * a * (a + 1) / (2 * b * (b + 1)); // the first '(b+1)' was '(b+2) in the paper of K. E. Muller Complex Qn, Pn; Qn = Pn = 0.0; Complex previousValue = 0.0; for (int n = 3; n < m_MaxIteration; n++) { var fi1 = (n - a - 2) / (2 * (2 * n - 3) * (n + b - 1)); var fi2 = (n + a) * (n + a - 1) / (4 * (2 * n - 1) * (2 * n - 3) * (n + b - 2) * (n + b - 1)); // additional term in paper of K. E. Muller var fi3 = -(n + a - 2) * (n + a - 1) * (n - a - 2) / (8 * (2 * n - 3) * (2 * n - 3) * (2 * n - 5) * (n + b - 3) * (n + b - 2) * (n + b - 1)); // different sign in the 3. product in the paper of K. E. Muller var fi4 = -(n + a - 1) * (n - b - 1) / (2 * (2 * n - 3) * (n + b - 2) * (n + b - 1)); Pn = (1 - fi1 * z) * PnMinus1 - (fi4 - fi2 * z) * z * PnMinus2 - fi3 * z * z * z * PnMinus3; // an additional '-x' was missing in paper of K. e. Muller Qn = (1 - fi1 * z) * QnMinus1 - (fi4 - fi2 * z) * z * QnMinus2 - fi3 * z * z * z * QnMinus3; if ((n >= 4) && (Complex.Abs(Pn / Qn - previousValue) < MachineConsts.ExtremeTinyEpsilon * (2.0 + Complex.Abs(previousValue)))) // abort condition { return(Pn / Qn); } previousValue = Pn / Qn; PnMinus3 = PnMinus2; PnMinus2 = PnMinus1; PnMinus1 = Pn; QnMinus3 = QnMinus2; QnMinus2 = QnMinus1; QnMinus1 = Qn; } return(Pn / Qn); }
public void Equals() { Complex <double> actual = new Complex <double>(1, 2); Complex <double> expected = new Complex <double>(2, 3); Assert.True(actual.Equals(actual)); Assert.AreEqual(actual.Equals(expected), expected.Equals(actual)); }
public void EqualsIEquatableTest() { var Re = Random; var Im = Random; IEquatable <Complex> target = new Complex(Re, Im); Assert.IsTrue(target.Equals(new Complex(Re, Im))); Assert.IsFalse(target.Equals(Rnd)); }
public void Divide() { Complex actual = number1.Divide(number2); Complex expected = new Complex((number1.re * number2.re + number1.im * number2.im) / (number2.re * number2.re + number2.im * number2.im), (number1.im * number2.re - number1.re * number2.im) / (number2.re * number2.re + number2.im * number2.im)); Assert.True(expected.Equals(actual)); }
public void Multiply() { Complex actual = number1.Multiply(number2); Complex expected = new Complex(number1.re * number2.re - number1.im * number2.im, number1.re * number2.im + number1.im * number2.re); Assert.True(actual.Equals(expected)); }
private static void VerifyExpWithAddition(Double real, Double imaginary) { // verify with e(x+y) = e(x)*e(y) if xy == yx Complex realComplex = new Complex(real, 0.0); Complex imgComplex = new Complex(0.0, imaginary); Complex ri = realComplex * imgComplex; Complex ir = imgComplex * realComplex; if (!ri.Equals(ir)) { return; } Complex realExp = Complex.Exp(realComplex); Complex imgExp = Complex.Exp(imgComplex); Complex expectedExp = realExp * imgExp; Complex complex = new Complex(real, imaginary); Complex complexExp = Complex.Exp(complex); if (false == Support.VerifyRealImaginaryProperties(complexExp, expectedExp.Real, expectedExp.Imaginary)) { Console.WriteLine("Error eXp-Err3521! Exp({0}):{1} != {2})", complex, complexExp, expectedExp); Assert.True(false, "Verification Failed"); } }
private static void testRandom() { Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000000; i++) { Complex a = new Complex(r(), r()); if (a.Equals(Complex.ZERO)) { a = Complex.ONE; } Poly4 poly = new Poly4( a, new Complex(r(), r()), new Complex(r(), r()), new Complex(r(), r()), new Complex(r(), r())); //poly.print(); try { poly.solve(); //poly.check(); } catch { Console.WriteLine("A={0} B={1} C={2} D={3} E={4}", poly.A, poly.B, poly.C, poly.D, poly.E); } } sw.Stop(); Console.WriteLine("Time={0}", sw.Elapsed); }
static void Main(string[] args) { Complex c1 = new Complex(1, 2); Complex c2 = new Complex(1, 2); Complex c3 = new Complex(); Console.WriteLine(c1); Console.WriteLine(c2); Console.WriteLine(c3); Complex c4 = c1.Add(c2); Console.WriteLine(c4); Complex c5 = c2.Subtract(c1); Console.WriteLine(c5); Complex c6 = c1.Multiply(c2); Console.WriteLine(c6); if(c1.Equals(c2)) Console.WriteLine("c1 este egal cu c2"); else Console.WriteLine("c1 nu este egal cu c2"); }
public static void Main() { float n1 = 1.430718e-12f; Complex c1 = new Complex(1.430718e-12, 0); Console.WriteLine("{0} = {1}: {2}", c1, n1, c1.Equals(n1)); }
public void Subtract() { Complex actual = number1.Subtract(number2); Complex expected = new Complex(number1.re - number2.re, number1.im - number2.im); Assert.True(actual.Equals(expected)); }
public void ComplexEquals() { Complex <double> complex1 = new Complex <double>(1, 0); bool result = complex1.Equals(complex1); Assert.IsTrue(result); }
static void Main() { Complex c1 = new Complex(3, 4); Complex c2 = new Complex(1, 2); //c2 += c1; // c2 = c2 + c1; Complex sum = c1 + c2; Complex diff = c1 - c2; Complex product = c1 * c2; Complex quotation = c1 / c2; Console.WriteLine("c1 = {0}", c1); Console.WriteLine("c2 = {0}", c2); Console.WriteLine("sum = {0}", sum); Console.WriteLine("diff = {0}", diff); Console.WriteLine("product = {0}", product); Console.WriteLine("quotation = {0}", quotation); Console.WriteLine("c1 == c2: {0}", c1 == c2); Console.WriteLine("c1 != c2: {0}", c1 != c2); Complex c3 = new Complex(3, 4); Console.WriteLine("c1 == c3: {0}", c1 == c3); Console.WriteLine("c1 != c3: {0}", c1 != c3); Console.WriteLine("c1.Equals(c3): {0}", c1.Equals(c3)); }
public void AddTest() { calculator = new ComplexNumbers.Calculator(); Complex <double> actual = calculator.Add(new Complex <double>(4, 5), new Complex <double>(6, 7)); Complex <double> expected = new Complex <double>(4 + 6, 5 + 7); Assert.True(actual.Equals(expected)); }
public void DivideTest() { calculator = new ComplexNumbers.Calculator(); Complex <double> actual = calculator.Divide(new Complex <double>(4, 5), new Complex <double>(6, 7)); Complex <double> expected = new Complex <double>((4.0 * 6.0 + 5.0 * 7.0) / (6.0 * 6.0 + 7.0 * 7.0), (5.0 * 6.0 - 4.0 * 7.0) / (6.0 * 6.0 + 7.0 * 7.0)); Assert.True(actual.Equals(expected)); }
public void SubtrachtTest() { calculator = new ComplexNumbers.Calculator(); Complex <double> actual = calculator.Subtracht(new Complex <double>(4, 5), new Complex <double>(6, 7)); Complex <double> expected = new Complex <double>(4 - 6, 5 - 7); Assert.True(actual.Equals(expected)); }
public override bool Equals(object obj) { if (obj is Number num) { return(value.Equals(num.value)); } return(false); }
public void MultiplyTest() { calculator = new ComplexNumbers.Calculator(); Complex <double> actual = calculator.Multiply(new Complex <double>(4, 5), new Complex <double>(6, 7)); Complex <double> expected = new Complex <double>(4 * 6 - 5 * 7, 5 * 6 + 4 * 7); Assert.True(actual.Equals(expected)); }
public bool Equals(Vector2D other) { if (other == null) { return(false); } return(position.Equals(other.position)); }
public static void RunTests_WithNonComplexObject() { // local variables double real = Support.GetSmallRandomDoubleValue(false); Complex randomComplex = new Complex(real, 0.0); // verify with same double value Assert.False(randomComplex.Equals((Object)real), string.Format("Obj randomComplex:{0}.Equals((object) real) is not 'false' as expected", randomComplex, real)); // verify with null Assert.False(randomComplex.Equals((Object)null), string.Format("Obj randomComplex:{0}.Equals((object) null) is not 'false' as expected", randomComplex)); // verify with 0 Assert.False(randomComplex.Equals((Object)0), string.Format("Obj randomComplex:{0}.Equals((object) 0) is not 'false' as expected", randomComplex)); // verify with string Assert.False(randomComplex.Equals((Object)"0"), string.Format("Obj randomComplex:{0}.Equals((object) \"0\") is not 'false' as expected", randomComplex)); }
/* * Complex exponential */ public static Complex ComplexExp(Complex value) { if (value.Equals(Complex.ImaginaryOne * Math.PI / 2)) { return(Complex.ImaginaryOne); } else if (value.Equals(Complex.ImaginaryOne * Math.PI)) { return(-Complex.One); } else if (value.Equals(Complex.ImaginaryOne * 3 * Math.PI / 2)) { return(-Complex.ImaginaryOne); } else { return(Complex.Exp(value)); } }
/** * Create a polynomial given coefficinents. */ public Poly2(Complex a, Complex b, Complex c) { this.A = a; this.B = b; this.C = c; if (a.Equals(Complex.ZERO)) { throw new Exception(); } }
public override bool Equals(object obj) { Number num = obj as Number; if (num == null) { return(false); } return(value.Equals(num.value)); }
public void TestEqualsAndGetHashCode() { Complex TestComplex1 = new Complex(real1, real2); Complex TestComplex2 = new Complex(real1, real2); Complex TestComplex3 = new Complex(real1, real2); //Test GetHashCode Assert.AreEqual(TestComplex1.GetHashCode(), TestComplex2.GetHashCode()); Assert.AreEqual(TestComplex1.GetHashCode(), TestComplex1.GetHashCode()); //Test Equals Assert.IsTrue(TestComplex1.Equals(TestComplex1)); Assert.AreEqual(TestComplex1.Equals(TestComplex2), TestComplex2.Equals(TestComplex1)); bool firstResult = TestComplex1.Equals(TestComplex2); bool secondResult = TestComplex2.Equals(TestComplex3); bool thirdResult = TestComplex1.Equals(TestComplex3); Assert.IsTrue(firstResult && secondResult && thirdResult); }
public void EqualsTest() { Complex cd1 = new Complex(-1.1, 2.2); Complex cd2 = new Complex(-1.1, 2.2); Complex cd3 = new Complex(-1, 2); ComplexFloat cf = new ComplexFloat(-1, 2); Assert.IsTrue(cd1 == cd2); Assert.IsTrue(cd1.Equals(cd2)); Assert.IsTrue(cd3 == cf); Assert.IsTrue(cd3.Equals(cf)); }
public void Equals() { Complex firstComplex = new Complex(1, 4); Complex relinkComplex = firstComplex; Complex secondComplex = new Complex(1, 4); firstComplex.Equals(secondComplex).Should().BeTrue(); (firstComplex == relinkComplex).Should().BeTrue(); (firstComplex == secondComplex).Should().BeTrue(); (firstComplex != secondComplex).Should().BeFalse(); }
/// <summary> /// Determines whether the specified <see cref="System.Object" />, is equal to this instance. /// </summary> /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> /// <returns> /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>. /// </returns> public override bool Equals(object obj) { var num = obj as ComplexNumber; if (num == null) { return(false); } return(complex.Equals(num.complex)); }
public static void Main() { Complex value = Complex.One; Console.WriteLine(value.ToString()); // Instantiate a complex number with real part 1 and imaginary part 0. Complex value1 = new Complex(1, 0); Console.WriteLine(value.Equals(value1)); }
/** * Create a polynomial from given coefficients. * */ public Poly4(Complex a, Complex b, Complex c, Complex d, Complex h) { this.A = a; this.B = b; this.C = c; this.D = d; this.E = h; if (a.Equals(Complex.ZERO)) { throw new Exception(); } }
public void EqualsTest() { var cd1 = new Complex(-1.1, 2.2); var cd2 = new Complex(-1.1, 2.2); var cd3 = new Complex(-1, 2); var cf = new ComplexFloat(-1, 2); Assert.IsTrue(cd1 == cd2); Assert.IsTrue(cd1.Equals(cd2)); Assert.IsTrue(cd3 == cf); Assert.IsTrue(cd3.Equals(cf)); }
static void Main(string[] args) { Complex c1 = new Complex(1, 3); Complex c2 = new Complex(1.3, 3.5); Complex c3 = c1; Console.WriteLine("c1 + c2: {0}", (c1 + c2)); Console.WriteLine("c3 += c2: {0}", (c3 += c2)); Console.WriteLine("c1 - c2: {0}", (c1 - c2)); Console.WriteLine(c1.Equals(c2)); Complex c4 = new Complex(1, 3); Console.WriteLine(c1.Equals(c4)); Console.WriteLine(c1 == c4); Complex c = new Complex(1, 1); Console.WriteLine("C++: {0} (should be: 1,1)", c++); Console.WriteLine("C: {0} (should be: 2,2)", c); Console.WriteLine("++C: {0} (should be: 3,3)", ++c); Console.WriteLine("Press any Key to continue"); Console.ReadKey(); }
private static void Equals(Complex complex1, object obj, bool expected, bool expectedEquals) { if (obj is Complex) { Complex complex2 = (Complex)obj; Assert.True(expected == (complex1 == complex2), string.Format("c1:{0} == c2{1} is not '{2}' as expected", complex1, complex2, expected)); Assert.True(expected == (complex2 == complex1), string.Format("c2:{0} == c1{1} is not '{2}' as expected", complex2, complex1, expected)); Assert.False(expected == (complex1 != complex2), string.Format("c1:{0} != c2{1} is not '{2}' as expected", complex1, complex2, !expected)); Assert.False(expected == (complex2 != complex1), string.Format("c2:{0} != c1{1} is not '{2}' as expected", complex2, complex1, !expected)); Assert.True(expectedEquals == complex1.Equals(complex2), string.Format("{0}.Equals({1}) is not '{2}' as expected", complex1, complex2, expectedEquals)); Assert.True(expectedEquals == complex2.Equals(complex1), string.Format("{0}.Equals({1}) is not '{2}' as expected", complex2, complex1, expectedEquals)); Assert.True(expectedEquals == complex1.GetHashCode().Equals(complex2.GetHashCode()), string.Format("{0}.GetHashCode().Equals({1}.GetHashCode()) is not '{2}' as expected", complex1, complex2, expectedEquals)); } Assert.True(expectedEquals == complex1.Equals(obj), string.Format("{0}.Equals({1}) is not '{2}' as expected", complex1, obj, expectedEquals)); }
private static void VerifyComplexComparison(Complex c1, Complex c2, bool expectedResult, bool expectedResultEqual) { if (expectedResult != (c1 == c2)) { Console.WriteLine("Error-8592 c1:{0} == c2{1} is not '{2}' as expected", c1, c2, expectedResult); Assert.True(false, "Verification Failed"); } if (expectedResult != (c2 == c1)) { Console.WriteLine("Error-8592-1 c2:{0} == c1{1} is not '{2}' as expected", c2, c1, expectedResult); Assert.True(false, "Verification Failed"); } if (expectedResult == (c1 != c2)) { Console.WriteLine("Error-81792 c1:{0} != c2{1} is not '{2}' as expected", c1, c2, !expectedResult); Assert.True(false, "Verification Failed"); } if (expectedResult == (c2 != c1)) { Console.WriteLine("Error-81792-1 c2:{0} != c1{1} is not '{2}' as expected", c2, c1, !expectedResult); Assert.True(false, "Verification Failed"); } bool result = c1.Equals(c2); if (expectedResultEqual != result) { Console.WriteLine("Error-6172 c1:{0}.Equals(c2{1}) is not '{2}' as expected", c1, c2, expectedResultEqual); Assert.True(false, "Verification Failed"); } if (result) // then verify Hash Code equality { if (c1.GetHashCode() != c2.GetHashCode()) { Console.WriteLine("Error-9HaSh72 c1:{0}.GetHashCode() == c2:{1}.GetHashCode() is 'true' as expected", c1, c2); Assert.True(false, "Verification Failed"); } } result = c2.Equals(c1); if (expectedResultEqual != result) { Console.WriteLine("Error-6172-1 c2:{0}.Equals(c1{1}) is not '{2}' as expected", c2, c1, expectedResultEqual); Assert.True(false, "Verification Failed"); } if (result) // then verify Hash Code equality { if (c2.GetHashCode() != c1.GetHashCode()) { Console.WriteLine("Error-9HaSh72-1 c2:{0}.GetHashCode() == c1:{1}.GetHashCode() is 'true' as expected", c2, c1); Assert.True(false, "Verification Failed"); } } if (expectedResult != c2.Equals((Object)c1)) { Console.WriteLine("Error-6172Obj c2:{0}.Equals((object) c1{1}) is not '{2}' as expected", c2, c1, expectedResult); Assert.True(false, "Verification Failed"); } if (expectedResult != c1.Equals((Object)c2)) { Console.WriteLine("Error-6172Obj-1 c1:{0}.Equals((object) c2{1}) is not '{2}' as expected", c1, c2, expectedResult); Assert.True(false, "Verification Failed"); } }
public static void RunTests_WithNonComplexObject() { // local variables Double real = Support.GetSmallRandomDoubleValue(false); Complex randomComplex = new Complex(real, 0.0); // verify with same double value if (randomComplex.Equals((Object)real)) { Console.WriteLine("Error-9674Obj randomComplex:{0}.Equals((object) real) is not 'false' as expected", randomComplex, real); Assert.True(false, "Verification Failed"); } // verify with null if (randomComplex.Equals((Object)null)) { Console.WriteLine("Error-5441Obj randomComplex:{0}.Equals((object) null) is not 'false' as expected", randomComplex); Assert.True(false, "Verification Failed"); } // verify with 0 if (randomComplex.Equals((Object)0)) { Console.WriteLine("Error-5441Obj randomComplex:{0}.Equals((object) 0) is not 'false' as expected", randomComplex); Assert.True(false, "Verification Failed"); } // verify with string if (randomComplex.Equals((Object)"0")) { Console.WriteLine("Error-5441Obj randomComplex:{0}.Equals((object) \"0\") is not 'false' as expected", randomComplex); Assert.True(false, "Verification Failed"); } }