Ejemplo n.º 1
0
        public void TestParse()
        {
            string expectedFirst  = "7*X^2 + 3*X - 2";
            string expectedSecond = "2*X - 2";
            string expectedThird  = "X^4 + 8*X^3 + 21*X^2 + 22*X + 8";
            string expectedFourth = "X^4 + 8*X^3 + 21*X^2 + 22*X + 8";
            string expectedFifth  = "X^3 + 6*X^2 + 11*X + 6";

            IComplexPolynomial firstPoly   = ComplexPolynomial.Parse(expectedFirst);
            IComplexPolynomial secondPoly  = ComplexPolynomial.Parse(expectedSecond);
            IComplexPolynomial thirdPoly   = ComplexPolynomial.Parse(expectedThird);
            IComplexPolynomial firstFourth = ComplexPolynomial.Parse(expectedFourth);
            IComplexPolynomial firstFifth  = ComplexPolynomial.Parse(expectedFifth);

            string actualFirst  = firstPoly.ToString();
            string actualSecond = secondPoly.ToString();
            string actualThird  = thirdPoly.ToString();
            string actualFouth  = firstFourth.ToString();
            string actualFifth  = firstFifth.ToString();

            Assert.AreEqual(expectedFirst, actualFirst, $"Test Parse({expectedFirst})");
            Assert.AreEqual(expectedSecond, actualSecond, $"Test Parse({expectedSecond})");
            Assert.AreEqual(expectedThird, actualThird, $"Test Parse({expectedThird})");
            Assert.AreEqual(expectedFourth, actualFouth, $"Test Parse({expectedFourth})");
            Assert.AreEqual(expectedFifth, actualFifth, $"Test Parse({expectedFifth})");
        }
Ejemplo n.º 2
0
        public void TestDerivative()
        {
            string expected = "576*X + 36";

            IComplexPolynomial first  = ComplexPolynomial.Parse("288*X^2 + 36*X - 2");
            IComplexPolynomial result = ComplexPolynomial.GetDerivativePolynomial(first);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Derivative:");
            TestContext.WriteLine($"f' where f(x) = ({first})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");

            Assert.AreEqual(expected, actual, $"Test Derivative");
        }
Ejemplo n.º 3
0
        public void TestSquare()
        {
            string expected = "144*X^2 + 24*X + 1";

            IComplexPolynomial first = ComplexPolynomial.Parse("12*X + 1");

            IComplexPolynomial result = ComplexPolynomial.Square(first);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Square:");
            TestContext.WriteLine($"({first})^2");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");

            Assert.AreEqual(expected, actual, $"Test Square");
        }
Ejemplo n.º 4
0
        public void TestAddition()
        {
            string expected = "24*X - 1";

            IComplexPolynomial first  = ComplexPolynomial.Parse("12*X + 2");
            IComplexPolynomial second = ComplexPolynomial.Parse("12*X - 3");

            IComplexPolynomial result = ComplexPolynomial.Add(first, second);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Addition:");
            TestContext.WriteLine($"({first}) + ({second})");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual:    {actual}");

            Assert.AreEqual(expected, actual, $"Test Addition");
        }
Ejemplo n.º 5
0
        public void TestDivide()
        {
            string expected = "24*X - 1";

            IComplexPolynomial first  = ComplexPolynomial.Parse("288*X^2 + 36*X - 2");
            IComplexPolynomial second = ComplexPolynomial.Parse("12*X + 2");

            IComplexPolynomial result = ComplexPolynomial.Divide(first, second);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Divide:");
            TestContext.WriteLine($"({first}) / ({second})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");

            Assert.AreEqual(expected, actual, $"Test Divide");
        }
Ejemplo n.º 6
0
        public void TestMultiply()
        {
            string expected = "144*X^2 - 12*X - 6";

            IComplexPolynomial first  = ComplexPolynomial.Parse("12*X + 2");
            IComplexPolynomial second = ComplexPolynomial.Parse("12*X - 3");

            IComplexPolynomial result = ComplexPolynomial.Multiply(first, second);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Multiply:");
            TestContext.WriteLine($"({first}) * ({second})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");


            Assert.AreEqual(expected, actual, $"Test Multiply");
        }
Ejemplo n.º 7
0
        public void TestSubtraction()
        {
            string expected = "7*X^2 + X";

            IComplexPolynomial first  = ComplexPolynomial.Parse("7*X^2 + 3*X - 2");
            IComplexPolynomial second = ComplexPolynomial.Parse("2*X - 2");

            IComplexPolynomial result = ComplexPolynomial.Subtract(first, second);
            string             actual = result.ToString();

            TestContext.WriteLine("Test Subtraction:");
            TestContext.WriteLine($"({first}) - ({second})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");


            Assert.AreEqual(expected, actual, $"Test Subtraction");
        }
Ejemplo n.º 8
0
        public void TestFromRoots()
        {
            Complex oneTwelvth          = new Complex(1d / 12d, 0);
            Complex minusOneTwelvth     = Complex.Negate(oneTwelvth);
            Complex imaginaryOneTwelvth = new Complex(0, 1d / 12d);
            Complex imaginaryTwelve     = new Complex(0, 12);

            IComplexPolynomial poly3 = ComplexPolynomial.FromRoots(oneTwelvth, imaginaryOneTwelvth, 12);

            string expected = "X^3 + (-12.08333 - 0.08333𝐢)*X^2 + (1 + 1.00694444444444𝐢)*X + (0 - 0.08333𝐢)";
            string actual   = poly3.ToString();

            TestContext.WriteLine($"Expecting: {expected}");
            TestContext.WriteLine($"Actual:    {actual}");

            TestContext.WriteLine(actual);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 9
0
        public void TestGCD()
        {
            string polyString1 = "X^2 + 7*X + 6";
            string polyString2 = "X^2 - 5*X - 6";
            string expected    = "X + 1";

            IComplexPolynomial first  = ComplexPolynomial.Parse(polyString1);
            IComplexPolynomial second = ComplexPolynomial.Parse(polyString2);

            IComplexPolynomial result = ComplexPolynomial.GCD(first, second);
            string             actual = result.ToString();

            TestContext.WriteLine("Test GCD:");
            TestContext.WriteLine($"GCD({first}, {second})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Expected: {expected}");
            TestContext.WriteLine($"Actual   = {actual}");

            Assert.AreEqual(expected, actual, $"Test GCD");
        }
 public int GetHashCode(IComplexPolynomial obj)
 {
     return(obj.ToString().GetHashCode());
 }