Exemplo n.º 1
0
        public void Simple()
        {
            var complexNumber1 = new ComplexNumber(1, 2);
            var complexNumber2 = new ComplexNumber(3, 4);
            var multiplyComplexNumber = complexNumber1.Multiply(complexNumber2);

            Assert.AreEqual(1 * 3 - 2 * 4, multiplyComplexNumber.Real);
            Assert.AreEqual(2 * 3 + 1 * 4, multiplyComplexNumber.Imaginary);

            multiplyComplexNumber = complexNumber1.Multiply(5);
            Assert.AreEqual(5 * 1, multiplyComplexNumber.Real);
            Assert.AreEqual(5 * 2, multiplyComplexNumber.Imaginary);

            multiplyComplexNumber = 6 * complexNumber1;
            Assert.AreEqual(6 * 1, multiplyComplexNumber.Real);
            Assert.AreEqual(6 * 2, multiplyComplexNumber.Imaginary);

            multiplyComplexNumber = complexNumber1 * 7;
            Assert.AreEqual(7 * 1, multiplyComplexNumber.Real);
            Assert.AreEqual(7 * 2, multiplyComplexNumber.Imaginary);

            complexNumber1 *= 1;
            Assert.AreEqual(1, complexNumber1.Real);
            Assert.AreEqual(2, complexNumber1.Imaginary);

            complexNumber1 *= 5;
            Assert.AreEqual(5, complexNumber1.Real);
            Assert.AreEqual(10, complexNumber1.Imaginary);

            complexNumber1 *= complexNumber2;
            Assert.AreEqual(5 * 3 - 10 * 4, complexNumber1.Real);
            Assert.AreEqual(10 * 3 + 5 * 4, complexNumber1.Imaginary);
        }
Exemplo n.º 2
0
        public void Simple2()
        {
            var complexNumber1 = new ComplexNumber(4, 3);
            var complexNumber2 = new ComplexNumber(6, 2);
            var complexNumber3 = new ComplexNumber(-2, -4);
            var complexNumber4 = new ComplexNumber(3, -8);
            var complexNumber5 = new ComplexNumber(8, 3);

            var resultComplexNumber = complexNumber1 + complexNumber2;

            Assert.AreEqual(10, resultComplexNumber.Real);
            Assert.AreEqual(5, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber1 + complexNumber3;

            Assert.AreEqual(2, resultComplexNumber.Real);
            Assert.AreEqual(-1, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber1 + complexNumber4;

            Assert.AreEqual(7, resultComplexNumber.Real);
            Assert.AreEqual(-5, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber1 + complexNumber5;

            Assert.AreEqual(12, resultComplexNumber.Real);
            Assert.AreEqual(6, resultComplexNumber.Imaginary);
        }
Exemplo n.º 3
0
        public void ExecuteWithParamsTest()
        {
            var complex = new Complex(5, 2);
            var exp = new ComplexNumber(complex);

            Assert.Equal(complex, exp.Execute(null));
        }
Exemplo n.º 4
0
        public void Simple1()
        {
            var complexNumber1 = new ComplexNumber(4, 3);
            var complexNumber2 = new ComplexNumber(6, 2);
            var complexNumber3 = new ComplexNumber(-2, -4);
            var complexNumber4 = new ComplexNumber(3, -8);
            var complexNumber5 = new ComplexNumber(8, 3);

            var resultComplexNumber = complexNumber1.Subtract(complexNumber2);

            Assert.AreEqual(1, resultComplexNumber.Imaginary);
            Assert.AreEqual(-2, resultComplexNumber.Real);

            resultComplexNumber = complexNumber1.Subtract(complexNumber3);

            Assert.AreEqual(7, resultComplexNumber.Imaginary);
            Assert.AreEqual(6, resultComplexNumber.Real);

            resultComplexNumber = complexNumber1.Subtract(complexNumber4);

            Assert.AreEqual(11, resultComplexNumber.Imaginary);
            Assert.AreEqual(1, resultComplexNumber.Real);

            resultComplexNumber = complexNumber1.Subtract(complexNumber5);

            Assert.AreEqual(0, resultComplexNumber.Imaginary);
            Assert.AreEqual(-4, resultComplexNumber.Real);
        }
Exemplo n.º 5
0
        public void EqualsTest()
        {
            var exp1 = new ComplexNumber(new Complex(5, 2));
            var exp2 = new ComplexNumber(new Complex(5, 2));

            Assert.True(exp1.Equals(exp2));
        }
Exemplo n.º 6
0
        public void ExecuteTest()
        {
            var complex = new Complex(5, 2);
            var exp = new ComplexNumber(complex);

            Assert.Equal(complex, exp.Execute());
        }
Exemplo n.º 7
0
        public void Simple()
        {
            var complexNumber = new ComplexNumber(5, 6);
            var copy = (ComplexNumber) complexNumber.Clone();

            Assert.AreEqual(copy.Real, complexNumber.Real);
            Assert.AreEqual(copy.Imaginary, complexNumber.Imaginary);

            complexNumber = new ComplexNumber(-2, 3);
            copy = (ComplexNumber) complexNumber.Clone();

            Assert.AreEqual(copy.Real, complexNumber.Real);
            Assert.AreEqual(copy.Imaginary, complexNumber.Imaginary);

            complexNumber = new ComplexNumber(5, -3);
            copy = (ComplexNumber) complexNumber.Clone();

            Assert.AreEqual(copy.Real, complexNumber.Real);
            Assert.AreEqual(copy.Imaginary, complexNumber.Imaginary);

            complexNumber = new ComplexNumber(-9, -4);
            copy = (ComplexNumber) complexNumber.Clone();

            Assert.AreEqual(copy.Real, complexNumber.Real);
            Assert.AreEqual(copy.Imaginary, complexNumber.Imaginary);
        }
Exemplo n.º 8
0
        public void Simple()
        {
            var complexNumber1 = new ComplexNumber(4, 5);
            var complexNumber2 = new ComplexNumber(-2, 4);
            var complexNumber3 = new ComplexNumber(1, -3);
            var complexNumber4 = new ComplexNumber(-6, -12);

            var resultComplexNumber = complexNumber1.Reciprocal;

            Assert.AreEqual(4d / 41d, resultComplexNumber.Real);
            Assert.AreEqual(-5d / 41d, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber2.Reciprocal;

            Assert.AreEqual(-2d / 20d, resultComplexNumber.Real);
            Assert.AreEqual(-4d / 20d, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber3.Reciprocal;

            Assert.AreEqual(1d / 10d, resultComplexNumber.Real);
            Assert.AreEqual(3d / 10d, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber4.Reciprocal;

            Assert.AreEqual(-6d / 180d, resultComplexNumber.Real);
            Assert.AreEqual(12d / 180d, resultComplexNumber.Imaginary);
        }
Exemplo n.º 9
0
 public ComplexNumber CanAdd(ComplexNumber x, ComplexNumber y)
 {
     ComplexNumber result = x + y;
     Assert.AreEqual(x.Real + y.Real, result.Real);
     Assert.AreEqual(x.Imaginary + y.Imaginary, result.Imaginary);
     return result;
 }
Exemplo n.º 10
0
    public static void Main( string[] args )
    {
        // declare two variables to store complex numbers
          // to be entered by user
          ComplexNumber x, y;

          // prompt the user to enter the first complex number
          Console.Write( "Enter the real part of complex number x: " );
          double realPart = Convert.ToDouble( Console.ReadLine() );
          Console.Write(
         "Enter the imaginary part of complex number x: " );
          double imaginaryPart = Convert.ToDouble( Console.ReadLine() );
          x = new ComplexNumber( realPart, imaginaryPart );

          // prompt the user to enter the second complex number
          Console.Write( "\nEnter the real part of complex number y: " );
          realPart = Convert.ToDouble( Console.ReadLine() );
          Console.Write(
         "Enter the imaginary part of complex number y: " );
          imaginaryPart = Convert.ToDouble( Console.ReadLine() );
          y = new ComplexNumber( realPart, imaginaryPart );

          // display the results of calculations with x and y
          Console.WriteLine();
          Console.WriteLine( "{0} + {1} = {2}", x, y, x + y );
          Console.WriteLine( "{0} - {1} = {2}", x, y, x - y );
          Console.WriteLine( "{0} * {1} = {2}", x, y, x * y );
    }
 public void SumNumbers()
 {
     ComplexNumber n1 = new ComplexNumber(1,1);
     ComplexNumber n2 = new ComplexNumber(1,1);
     ComplexNumber result = n1 + n2;
     Assert.AreEqual(2, result.Real);
     Assert.AreEqual(2, result.Imaginary);
 }
 public void MulNumbers()
 {
     ComplexNumber n1 = new ComplexNumber(3, 5);
     ComplexNumber n2 = new ComplexNumber(2, 3);
     ComplexNumber result = n1 * n2;
     Assert.AreEqual(6, result.Real);
     Assert.AreEqual(15, result.Imaginary);
 }
 public void DivNumbers()
 {
     ComplexNumber n1 = new ComplexNumber(3, 15);
     ComplexNumber n2 = new ComplexNumber(2, 3);
     ComplexNumber result = n1 / n2;
     Assert.AreEqual(1.5, result.Real);
     Assert.AreEqual(5, result.Imaginary);
 }
 public void DiffNumbers()
 {
     ComplexNumber n1 = new ComplexNumber(1, 1);
     ComplexNumber n2 = new ComplexNumber(1, 1);
     ComplexNumber result = n1 - n2;
     Assert.AreEqual(0, result.Real);
     Assert.AreEqual(0, result.Imaginary);
 }
Exemplo n.º 15
0
 private static int CalculateMandelbrotIterations(ComplexNumber current, ComplexNumber start, int iterations,int maxiterations)
 {
     if (iterations >= maxiterations)
         return maxiterations;
     if (current.LengthSquared() > 4f)
         return iterations;
     return CalculateMandelbrotIterations(current * current + start, start, ++iterations,maxiterations);
 }
Exemplo n.º 16
0
        public void ConjugateExample()
        {
            var complexNumber = new ComplexNumber(1, 2);
            var conjugateComplexNumber = complexNumber.Conjugate;

            Assert.AreEqual(conjugateComplexNumber.Real, 1);
            Assert.AreEqual(conjugateComplexNumber.Imaginary, -2);
        }
Exemplo n.º 17
0
        public void Simple3()
        {
            object complexNumber1 = new ComplexNumber(1, 2);
            var complexNumber2 = new object();

            Assert.IsFalse(complexNumber1.Equals(complexNumber2));
            Assert.IsFalse(complexNumber1.Equals(null));
        }
Exemplo n.º 18
0
        public void AbsoluteValueExample()
        {
            var complexNumber = new ComplexNumber(4, -3);

            var result = complexNumber.AbsoluteValue;

            Assert.AreEqual(result, 5);
        }
Exemplo n.º 19
0
        public void AdditiveInverseExample()
        {
            var complexNumber1 = new ComplexNumber(4, -2);

            var result = complexNumber1.AdditiveInverse;

            Assert.AreEqual(result.Real, -4);
            Assert.AreEqual(result.Imaginary, 2);
        }
Exemplo n.º 20
0
        public void CloneExample()
        {
            var complexNumber = new ComplexNumber(5, 6);
            var clonedComplexNumber = (ComplexNumber)complexNumber.Clone();

            // The clone will have the same Real and Imaginary numbers
            Assert.AreEqual(clonedComplexNumber.Real, 5);
            Assert.AreEqual(clonedComplexNumber.Imaginary, 6);
        }
Exemplo n.º 21
0
        public void AddExample()
        {
            var complexNumber1 = new ComplexNumber(4, 3);
            var complexNumber2 = new ComplexNumber(6, 2);

            var result = complexNumber1.Add(complexNumber2);

            Assert.AreEqual(result.Real, 10);
            Assert.AreEqual(result.Imaginary, 5);
        }
Exemplo n.º 22
0
        public void CanCompare()
        {
            const double real = 5.2;
            const double imaginary = -10.3;
            var x = new ComplexNumber(real, imaginary);
            var y = new ComplexNumber(real, imaginary);

            Assert.IsTrue(x == y);
            Assert.IsFalse(x != y);
        }
 public void SumWithIntegers()
 {
     ComplexNumber n1 = new ComplexNumber(3, 15);
     ComplexNumber result = n1 + 5;
     ComplexNumber n2 = new ComplexNumber(2, 3);
     ComplexNumber result2 = 5 + n2;
     Assert.AreEqual(8, result.Real);
     Assert.AreEqual(15, result.Imaginary);
     Assert.AreEqual(7, result2.Real);
     Assert.AreEqual(3, result2.Imaginary);
 }
Exemplo n.º 24
0
        public void Simple()
        {
            var complexNumber1 = new ComplexNumber(-3, -4);
            var complexNumber2 = new ComplexNumber(-3, 4);
            var complexNumber3 = new ComplexNumber(3, -4);
            var complexNumber4 = new ComplexNumber(3, 4);

            Assert.AreEqual(5, complexNumber1.AbsoluteValue);
            Assert.AreEqual(5, complexNumber2.AbsoluteValue);
            Assert.AreEqual(5, complexNumber3.AbsoluteValue);
            Assert.AreEqual(5, complexNumber4.AbsoluteValue);
        }
Exemplo n.º 25
0
        public void Set()
        {
            var complexNumber = new ComplexNumber(5, 10);

            Assert.AreEqual(5, complexNumber.Real);
            Assert.AreEqual(10, complexNumber.Imaginary);

            complexNumber.Real = 6;
            complexNumber.Imaginary = 11;

            Assert.AreEqual(6, complexNumber.Real);
            Assert.AreEqual(11, complexNumber.Imaginary);
        }
Exemplo n.º 26
0
        public void Simple()
        {
            var complexNumber1 = new ComplexNumber(1, 2);
            var conjugateComplexNumber = complexNumber1.Conjugate;

            Assert.AreEqual(1, conjugateComplexNumber.Real);
            Assert.AreEqual(-2, conjugateComplexNumber.Imaginary);

            complexNumber1.Imaginary = -1;
            conjugateComplexNumber = complexNumber1.Conjugate;
            Assert.AreEqual(1, conjugateComplexNumber.Real);
            Assert.AreEqual(1, conjugateComplexNumber.Imaginary);
        }
Exemplo n.º 27
0
        public void Simple1()
        {
            object complexNumber1 = new ComplexNumber(1, 2);
            object complexNumber2 = new ComplexNumber(1, 2);
            object complexNumber3 = new ComplexNumber(1, 6);
            object complexNumber4 = new ComplexNumber(2, 2);
            object complexNumber5 = new ComplexNumber(3, 3);

            Assert.IsTrue(complexNumber1.Equals(complexNumber1));
            Assert.IsTrue(complexNumber1.Equals(complexNumber2));
            Assert.IsFalse(complexNumber1.Equals(complexNumber3));
            Assert.IsFalse(complexNumber1.Equals(complexNumber4));
            Assert.IsFalse(complexNumber1.Equals(complexNumber5));
        }
Exemplo n.º 28
0
 /*public override MathEvaluator.MathOperations.Number Calculate(float real, float imaginary,float iterations)
 {
     ComplexNumber start = new ComplexNumber(real, imaginary);
     return CalculateMandelbrot(start,start, (int)iterations).Abs();
 }*/
 public override MathEvaluator.MathOperations.Number Calculate(float real, float imaginary, float maxiterations)
 {
     ComplexNumber start = new ComplexNumber(real, imaginary);
     ComplexNumber current = start;
     int iteration = 0;
     for (; iteration < (int)maxiterations; iteration++)
     {
         if (current.LengthSquared() > 4.0f)
             break;
         current = current * current + start;
     }
     return 3 - iteration / (float)((int)maxiterations);
     //return 3 - CalculateMandelbrotIterations(start, start, 0,(int)maxiterations)/(float)((int)maxiterations) ;
 }
Exemplo n.º 29
0
        public void Simple()
        {
            var complexNumber1 = new ComplexNumber(4, -2);
            var complexNumber2 = new ComplexNumber(-2, 6);

            var resultComplexNumber = complexNumber1.AdditiveInverse;

            Assert.AreEqual(-4, resultComplexNumber.Real);
            Assert.AreEqual(2, resultComplexNumber.Imaginary);

            resultComplexNumber = complexNumber2.AdditiveInverse;

            Assert.AreEqual(2, resultComplexNumber.Real);
            Assert.AreEqual(-6, resultComplexNumber.Imaginary);
        }
        public IterationValue ComputeIterationDepthFor(ComplexNumber z)
        {
            ComplexNumber c = z;

            for (int n = 0; n < _maxIterationDepth; n++)
            {
                z.MultiplyWith(z);
                z.Add(c);

                if (z.AbsoluteValueSquared >= _thresholdSquared)
                    return new IterationValue(n + 1, z.AbsoluteValue);
            }

            return new IterationValue(_maxIterationDepth, double.MinValue);
        }