Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the ComplexRational class,
 /// by directly referencing the two provided polynomial coefficients.
 /// </summary>
 public ComplexRational(
     Complex[] numeratorCoefficients,
     Complex[] denominatorCoefficients)
 {
     _numerator = new ComplexPolynomial(numeratorCoefficients);
     _denominator = new ComplexPolynomial(denominatorCoefficients);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the ComplexRational class,
 /// with the provided numerator and denominator order.
 /// </summary>
 public ComplexRational(
     int numeratorOrder,
     int denominatorOrder)
 {
     _numerator = new ComplexPolynomial(numeratorOrder);
     _denominator = new ComplexPolynomial(denominatorOrder);
 }
Ejemplo n.º 3
0
 // TODO: Implement polynomial factorization to normalize rationals
 /// <summary>
 /// Initializes a new instance of the ComplexRational class,
 /// by directly referencing the two provided polynomials (no deep copy).
 /// </summary>
 public ComplexRational(
     ComplexPolynomial numerator,
     ComplexPolynomial denominator)
 {
     _numerator = numerator.Clone();
     _denominator = denominator.Clone();
 }
Ejemplo n.º 4
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.º 5
0
        public void TestSerializeComplexPolynomial()
        {
            ComplexPolynomial before = new ComplexPolynomial(ComplexVector.Random(10, _random));
            ComplexPolynomial after  = SerializeDeserialize(before);

            Assert.That(after, Is.Not.Null, "Not Null");
            Assert.That(after, Is.EqualTo(before), "Equal");
            Assert.That(after, NumericIs.AlmostEqualTo(before), "Almost Equal");
        }
Ejemplo n.º 6
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.º 7
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.º 8
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.º 9
0
        public void IRID203_PolynomialAddition()
        {
            Polynomial p1 = new Polynomial(new double[] { -1, 0, 2 });
            Polynomial p2 = new Polynomial(new double[] { 4 });

            Polynomial a = p1 + p2;
            Polynomial b = p2 + p1;

            Assert.That(a.ToString(), Is.EqualTo(b.ToString()));

            ComplexPolynomial cp1 = new ComplexPolynomial(new Complex[] { -1, 0, 2 });
            ComplexPolynomial cp2 = new ComplexPolynomial(new Complex[] { 4 });

            ComplexPolynomial ca = cp1 + cp2;
            ComplexPolynomial cb = cp2 + cp1;

            Assert.That(ca.ToString(), Is.EqualTo(cb.ToString()));
        }
Ejemplo n.º 10
0
        public void TestAlmostEquals_ComplexPolynomial()
        {
            ComplexPolynomial a1 = new ComplexPolynomial(ComplexVector.Random(5, new ContinuousUniformDistribution()));
            ComplexPolynomial a2 = a1.Clone();
            ComplexPolynomial b  = -a1;
            ComplexPolynomial c  = a1 * (1.0 + (1e+10 * Number.PositiveEpsilonOf(1.0)));
            ComplexPolynomial d  = a1 * (1.0 + (2 * Number.PositiveEpsilonOf(1.0)));

            Helper_TestAlmostEqualityForGenericType(a1, a2, b, c, d);

            // Wrapper
            Assert.That(ComplexPolynomial.AlmostEqual(a1, c), Is.False);
            Assert.That(ComplexPolynomial.AlmostEqual(a1, c, 1e-10), Is.False);
            Assert.That(ComplexPolynomial.AlmostEqual(a1, c, 1e-2), Is.True);

            // reference type -> no boxing
            Assert.That(a1, Is.SameAs(a1));
        }
Ejemplo n.º 11
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.º 12
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.º 13
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.º 14
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.º 15
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");
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Add a polynomial directly inplace to this rational.
 /// </summary>
 /// <param name="polynomial">The polynomial to add.</param>
 public void AddInplace(ComplexPolynomial polynomial)
 {
     _numerator.AddInplace(_denominator * polynomial);
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Invert this rational directly inplace.
 /// </summary>
 public void InvertInplace()
 {
     ComplexPolynomial temp = _denominator;
     _denominator = _numerator;
     _numerator = temp;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Create a new rational as the result of multiplying a polynomial to this rational.
 /// </summary>
 /// <param name="polynomial">The polynomial to multiply with.</param>
 public ComplexRational Multiply(ComplexPolynomial polynomial)
 {
     return new ComplexRational(
         _numerator * polynomial,
         _denominator.Clone());
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Subtract a polynomial directly inplace from this rational.
 /// </summary>
 /// <param name="polynomial">The polynomial to subtract.</param>
 public void SubtractInplace(ComplexPolynomial polynomial)
 {
     _numerator.SubtractInplace(_denominator * polynomial);
 }
Ejemplo n.º 20
0
        public void IRID203_PolynomialAddition()
        {
            Polynomial p1 = new Polynomial(new double[] { -1, 0, 2 });
            Polynomial p2 = new Polynomial(new double[] { 4 });

            Polynomial a = p1 + p2;
            Polynomial b = p2 + p1;

            Assert.That(a.ToString(), Is.EqualTo(b.ToString()));

            ComplexPolynomial cp1 = new ComplexPolynomial(new Complex[] { -1, 0, 2 });
            ComplexPolynomial cp2 = new ComplexPolynomial(new Complex[] { 4 });

            ComplexPolynomial ca = cp1 + cp2;
            ComplexPolynomial cb = cp2 + cp1;

            Assert.That(ca.ToString(), Is.EqualTo(cb.ToString()));
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Create a new rational as the result of dividing a polynomial from this rational.
 /// </summary>
 /// <param name="polynomial">The polynomial to divide with.</param>
 public ComplexRational Divide(ComplexPolynomial polynomial)
 {
     return new ComplexRational(
         _numerator.Clone(),
         _denominator * polynomial);
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Initializes a new instance of the ComplexRational class,
 /// by deep-copy from an existing complex rational.
 /// </summary>
 /// <param name="copy">A rational to copy from.</param>
 public ComplexRational(ComplexRational copy)
 {
     _numerator = new ComplexPolynomial(copy._numerator);
     _denominator = new ComplexPolynomial(copy._denominator);
 }