コード例 #1
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.Add(complexNumber2);

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

            resultComplexNumber = complexNumber1.Add(complexNumber3);

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

            resultComplexNumber = complexNumber1.Add(complexNumber4);

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


            resultComplexNumber = complexNumber1.Add(complexNumber5);

            Assert.AreEqual(12, resultComplexNumber.Real);
            Assert.AreEqual(6, resultComplexNumber.Imaginary);
        }
コード例 #2
0
ファイル: Plus.cs プロジェクト: havok/ngenerics
        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.Add(complexNumber2);

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

            resultComplexNumber = complexNumber1.Add(complexNumber3);

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

            resultComplexNumber = complexNumber1.Add(complexNumber4);

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

            resultComplexNumber = complexNumber1.Add(complexNumber5);

            Assert.AreEqual(12, resultComplexNumber.Real);
            Assert.AreEqual(6, resultComplexNumber.Imaginary);
        }
コード例 #3
0
        public void Add_purely_real_numbers()
        {
            var sut      = new ComplexNumber(1, 0);
            var expected = new ComplexNumber(3, 0);

            Assert.Equal(expected.Real, sut.Add(new ComplexNumber(2, 0)).Real, precision: 7);
            Assert.Equal(expected.Imaginary, sut.Add(new ComplexNumber(2, 0)).Imaginary, precision: 7);
        }
コード例 #4
0
        public void Add_numbers_with_real_and_imaginary_part()
        {
            var sut      = new ComplexNumber(1, 2);
            var expected = new ComplexNumber(4, 6);

            Assert.Equal(expected.Real, sut.Add(new ComplexNumber(3, 4)).Real, precision: 7);
            Assert.Equal(expected.Imaginary, sut.Add(new ComplexNumber(3, 4)).Imaginary, precision: 7);
        }
コード例 #5
0
        public void Add_purely_imaginary_numbers()
        {
            var sut      = new ComplexNumber(0, 1);
            var expected = new ComplexNumber(0, 3);

            Assert.Equal(expected.Real, sut.Add(new ComplexNumber(0, 2)).Real, precision: 7);
            Assert.Equal(expected.Imaginary, sut.Add(new ComplexNumber(0, 2)).Imaginary, precision: 7);
        }
コード例 #6
0
    public static void Main()
    {
        ComplexNumber myNumber =
            new ComplexNumber(2, 3);

        Console.WriteLine("Real: " + myNumber.GetReal());
        Console.WriteLine("Imag: " + myNumber.GetImag());

        myNumber.SetReal(-2);
        myNumber.SetImag(4.5);
        Console.WriteLine("Real: " + myNumber.GetReal());
        Console.WriteLine("Imag: " + myNumber.GetImag());

        Console.WriteLine(myNumber.ToString());

        ComplexNumber n2 =
            new ComplexNumber(3, 4);

        Console.WriteLine(n2.ToString());
        Console.WriteLine("Magn: " + n2.GetMagnitude());

        myNumber.Add(n2);
        Console.WriteLine("Sum: {0}",
                          myNumber.ToString());
    }
コード例 #7
0
    public static ComplexNumber operator +(ComplexNumber c1,
                                           ComplexNumber c2)
    {
        ComplexNumber result = new ComplexNumber(c1.real, c1.imag);

        result.Add(c2);
        return(result);
    }
コード例 #8
0
    public static void Main()
    {
        ComplexNumber c = new ComplexNumber(2, -3);

        Console.WriteLine(c);
        c.Add(new ComplexNumber(2, -3));
        Console.WriteLine(c);
        Console.WriteLine(c.GetMagnitude());
    }
コード例 #9
0
        public void AddTest(double number1Real, double number1Imag, double number2Real, double number2Imag, double soulutionReal, double solutionImag)
        {
            ComplexNumber number1  = new ComplexNumber(number1Real, number1Imag);
            ComplexNumber number2  = new ComplexNumber(number2Real, number2Imag);
            ComplexNumber solution = ComplexNumber.Add(number1, number2);

            Assert.AreEqual(soulutionReal, solution.Real);
            Assert.AreEqual(solutionImag, solution.Imag);
        }
コード例 #10
0
        public void TestComplexNumberAdd()
        {
            ComplexNumber a = new ComplexNumber(5, 2);
            ComplexNumber b = new ComplexNumber(15, 3);
            ComplexNumber c = new ComplexNumber(20, 5);

            a = a.Add(b) as ComplexNumber;
            Assert.True(a.CompareTo(c) == 0);
        }
コード例 #11
0
    public static ComplexNumber operator +(
        ComplexNumber c1, ComplexNumber c2)
    {
        ComplexNumber result = new ComplexNumber(
            c1.GetReal(), c1.GetImag());

        result.Add(c2);
        return(result);
    }
コード例 #12
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);
        }
コード例 #13
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);
        }
コード例 #14
0
        public void AddTest()
        {
            var testVar2 = new ComplexNumber(2, 2);

            _testVar = new ComplexNumber(1, 1);

            _testVar = _testVar.Add(testVar2);
            Assert.Equal(3, _testVar.RealPart);
            Assert.Equal(3, _testVar.ImagPart);
        }
コード例 #15
0
    static void Main()
    {
        ComplexNumber n1 = new ComplexNumber(4, 6);
        ComplexNumber n2 = new ComplexNumber(1, 9);

        Console.WriteLine(n1);
        Console.WriteLine(n1.GetMagnitude());
        n1.Add(n2);
        Console.WriteLine(n1);
    }
        public void TestAdd()
        {
            ComplexNumber left     = new ComplexNumber(-4, 7);
            ComplexNumber right    = new ComplexNumber(5, -10);
            ComplexNumber expected = new ComplexNumber(1, -3);
            ComplexNumber actual   = left.Add(right);

            Assert.Equal(expected.Real(), actual.Real(), 2);
            Assert.Equal(expected.Imaginary(), actual.Imaginary(), 2);
        }
コード例 #17
0
        public void AddTest()
        {
            ComplexNumber actual   = firstComplexNumber.Add(secondComplexNumber);
            ComplexNumber shouldBe = new ComplexNumber()
            {
                Re = 9,
                Im = 22
            };

            Assert.AreEqual(shouldBe, actual);
        }
コード例 #18
0
    static void Main()
    {
        ComplexNumber c1 = new ComplexNumber(3.4, 6.7);

        Console.WriteLine("c1 = " + c1);
        Console.WriteLine("|c1| = " + c1.GetMagnitude());
        ComplexNumber c2 = new ComplexNumber(10, 4.78);

        Console.WriteLine("c2 = " + c2);
        Console.WriteLine("|c2| = " + c2.GetMagnitude());
        c1.Add(c2);
        Console.WriteLine("c1 + c2 = " + c1);
    }
コード例 #19
0
    static void Main(string[] args)
    {
        ComplexNumber x = new ComplexNumber(10, 20);
        ComplexNumber y = new ComplexNumber(30, 40);

        Console.WriteLine(x + " + " + y + " = " + (x + y));
        Console.WriteLine(x + " - " + y + " = " + (x - y));
        Console.WriteLine(x + " * " + y + " = " + (x * y));

        Console.WriteLine(x + " + " + y + " = " + ComplexNumber.Add(x, y));
        Console.WriteLine(x + " - " + y + " = " + ComplexNumber.Subtract(x, y));
        Console.WriteLine(x + " * " + y + " = " + ComplexNumber.Multiply(x, y));
    }
コード例 #20
0
        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);
        }
コード例 #21
0
    public static void Main()
    {
        ComplexNumber c1 = new ComplexNumber(2, -3);
        ComplexNumber c2 = new ComplexNumber(4, 6);

        Console.Write("{0} + {1} = ", c1, c2);
        c1.Add(c2);
        Console.WriteLine(c1.ToString());

        ComplexNumber c3 = ComplexNumber.Add(c2, c2);

        Console.WriteLine("c3 is " + c3.ToString());

        ComplexNumber c4 = c2 + c3;

        Console.WriteLine("c4 is " + c4.ToString());
    }
コード例 #22
0
        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));
        }
コード例 #23
0
        public void AddTest()
        {
            ComplexNumber a = new ComplexNumber()
            {
                RealPart      = 10,
                ImaginaryPart = 20
            };
            ComplexNumber b = new ComplexNumber()
            {
                RealPart      = 1,
                ImaginaryPart = 2
            };

            ComplexNumber actualAnswer   = a.Add(b);
            ComplexNumber expectedAnswer = new ComplexNumber()
            {
                RealPart      = 11,
                ImaginaryPart = 22
            };

            Assert.AreEqual(expectedAnswer, actualAnswer);
        }
コード例 #24
0
        static void Main(string[] args)
        {
            Console.WriteLine("=== Complex Numbers ===");

            Console.Write("Primo Numero - Parte Reale: ");
            var realOneString = Console.ReadLine();

            double.TryParse(realOneString, out double realOne);

            Console.Write("Primo Numero - Parte Immaginaria: ");
            var immagOneString = Console.ReadLine();

            double.TryParse(immagOneString, out double immagOne);

            Console.Write("Secondo Numero - Parte Reale: ");
            var realTwoString = Console.ReadLine();

            double.TryParse(realTwoString, out double realTwo);

            Console.Write("Secondo Numero - Parte Immaginaria: ");
            var immagTwoString = Console.ReadLine();

            double.TryParse(immagTwoString, out double immagTwo);

            Console.Write("Operazione (+, -, *, /): ");
            var op = Console.ReadLine();

            var           cnOne = new ComplexNumber(realOne, immagOne);
            var           cnTwo = new ComplexNumber(realTwo, immagTwo);
            ComplexNumber result;

            switch (op)
            {
            case "+":
                result = cnOne.Add(cnTwo);
                Console.WriteLine($"ADD: ({result.Real}, {result.Immag})");
                break;

            case "-":
                result = cnOne.Subtract(cnTwo);
                Console.WriteLine($"SUBTRACT: ({result.Real}, {result.Immag})");
                break;

            case "*":
                result = cnOne.Multiply(cnTwo);
                Console.WriteLine($"MULTIPLY: ({result.Real}, {result.Immag})");
                break;

            case "/":
                result = cnOne.Divide(cnTwo);
                Console.WriteLine($"DIVIDE: ({result.Real}, {result.Immag})");
                break;

            default:
                Console.WriteLine("Operazione invalida");
                break;
            }

            Console.WriteLine($"Modulo di ({cnOne.Real}, {cnOne.Immag}): {cnOne.Modulo}");
            Console.WriteLine($"Coniugato di ({cnTwo.Real}, {cnTwo.Immag}): ({cnTwo.Coniugato.Real}, {cnTwo.Coniugato.Immag})");
        }