public override object Evaluate()
        {
            CPolynomial poly  = LeftExpression.EvaluateAsCPolynomial();
            int         order = RightExpression.EvaluateAsInt32();

            return(new CMatrix(poly.NthDerivative(order).ToArray()));
        }
        public override object Evaluate()
        {
            CPolynomial poly1 = LeftExpression.EvaluateAsCPolynomial();
            CPolynomial poly2 = RightExpression.EvaluateAsCPolynomial();

            return(new CMatrix(CPolynomial.Modulus(poly1, poly2).ToArray()));
        }
        public override void Render(IGraphics2DContext context)
        {
            int n;

            double w = context.Viewport.Width;
            double h = context.Viewport.Height;

            double step     = Math.Max(Region.Width / w, Region.Height / h);
            double offset_x = Region.X - (w * step - Region.Width) / 2.0;
            double offset_y = Region.Y - (h * step - Region.Height) / 2.0;

            CPolynomial p  = Polynomial;
            CPolynomial pd = Polynomial.FirstDerivative();

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    Complex Z     = new Complex(offset_x + x * step, offset_y + y * step);
                    Complex Delta = Z;
                    Complex Z1    = Z;

                    for (n = 0; (n < MaxIterations) && (Complex.Abs(Z) < BailOut) && Complex.Abs(Delta) > 1E-15; n++)
                    {
                        Z     = Z - p.Evaluate(Z) / pd.Evaluate(Z);
                        Delta = Z1 - Z;
                        Z1    = Z;
                    }

                    context.PutPixel(x, y, GetColor(Z, n, MaxIterations));
                }
            }
        }
        public override object Evaluate()
        {
            IList <double> xValues = LeftExpression.EvaluateAsRealVector();
            IList <double> yValues = RightExpression.EvaluateAsRealVector();

            return(new CMatrix(CPolynomial.InterpolatingPolynomial(xValues, yValues).ToArray()));
        }
        public NewtonBasins(Rectangle region)
        {
            Region        = region;
            MaxIterations = 32;
            BailOut       = 1E15;

            Polynomial = new CPolynomial(new double[] { -1, 0, 0, 0, 0, 1 });
        }
Esempio n. 6
0
        public void ParseTest_Fail(string s)
        {
            //action
            Action action = () => CPolynomial.Parse(s);

            //assert
            action.ShouldThrow <FormatException>();
        }
Esempio n. 7
0
        public void ParseTest_Success5()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { 3, new Complex(-4, 12.3), 1 });

            //action
            CPolynomial actual = CPolynomial.Parse("x+3 + x^2-(5-12.3i)*x");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 8
0
        public void ParseTest_Success2()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { new Complex(0, -0.3) });

            //action
            CPolynomial actual = CPolynomial.Parse("-0.3i");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 9
0
        public void ParseTest_Success3()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { 0, 2 });

            //action
            CPolynomial actual = CPolynomial.Parse("2x");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 10
0
        public void FirstDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] { 8, -28, 9, 24, -110 });

            //action
            CPolynomial actual = target.FirstDerivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 11
0
        public void SecondDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] { -28, 18, 72, -440 });

            //action
            CPolynomial actual = target.SecondDerivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 12
0
        public void ToStringTest()
        {
            //arrange
            CPolynomial poly     = new CPolynomial(new Complex[] { 3, 1.1, 1, 0, 0, new Complex(-5, 12.3) });
            string      expected = "3 + 1.1*t + t^2 + (-5 + 12.3i)*t^5";

            //action
            var actual = poly.ToString(null, CultureInfo.InvariantCulture, "t");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 13
0
        public void AntiderivativeTest()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 3, 8, 16 });
            CPolynomial expected = new CPolynomial(new Complex[] { 0, 3, 4, 16.0 / 3 });

            //action
            CPolynomial actual = target.Antiderivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 14
0
        public void AntiderivativeTest()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] { 3, 8, 16 });
            CPolynomial expected = new CPolynomial(new Complex[] { 0, 3, 4, 16.0 / 3 });

            //action
            CPolynomial actual = target.Antiderivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 15
0
        public void NthDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] { 18, 144, -1320 });

            //action
            CPolynomial actual = target.NthDerivative(3);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 16
0
        public void CharacteristicPolynomial()
        {
            //arrange
            double  TOL  = 10E-6;
            CMatrix zero = new CMatrix(_m.RowCount, _m.ColumnCount);

            //action
            CPolynomial poly = CMatrix.CharacteristicPolynomial(_m);
            CMatrix     test = poly.Evaluate(_m);

            //assert
            CMatrix.FuzzyEquals(test, zero, TOL).Should().BeTrue();
        }
Esempio n. 17
0
        public void SecondDerivativeTest()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            Complex     c        = 3;
            Complex     expected = -11206;

            //action
            Complex actual = target.SecondDerivative(c);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 18
0
        public void FirstDerivativeTest()
        {
            //arrange
            CPolynomial target   = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            Complex     value    = 3;
            Complex     expected = -8257;

            //action
            Complex actual = target.FirstDerivative(value);

            //assert
            actual.Should().Be(expected);
        }
        /// <summary>
        /// Returns a vector of approximate values of roots of a complex polynomial by companion matrix method.
        /// </summary>
        /// <param name="poly">A complex polynomial.</param>
        /// <returns>The approximate values of roots of poly.</returns>
        /// <exception cref="System.ArgumentException">
        /// Number of elements in coeffs is less than 2 or more than 99.
        /// </exception>
        public static Complex[] CompanionMatrixRootsFinding(this CPolynomial poly)
        {
            // Remove zero elements standing at the end.
            int lidx = 0;
            int ridx = poly.Length - 1;

            while (ridx >= 0 && poly[ridx] == Complex.Zero)
            {
                ridx--;
            }
            while (lidx < poly.Length && poly[lidx] == Complex.Zero)
            {
                lidx++;
            }
            int length = ridx + 1;

            if (length < 2 || length > 99)
            {
                throw new ArgumentException("Number of coefficients must be between 1 and 100.");
            }

            int rootsCount = length - 1;

            Complex[] roots = new Complex[rootsCount];

            int     n = ridx - lidx;
            CMatrix companionMatrix = new CMatrix(n, n);

            for (int i = 1; i < n; i++)
            {
                companionMatrix[i, i - 1] = Complex.One;
            }

            for (int i = 0; i < n; i++)
            {
                companionMatrix[i, n - 1] = -poly[lidx + i] / poly[ridx];
            }

            CMatrix eigenvals = CMatrix.Eigenvalues(companionMatrix);

            for (int i = 0; i < n; i++)
            {
                roots[i] = eigenvals[i];
            }

            Array.Sort <Complex>(roots, new ComplexComparer());
            return(roots);
        }
Esempio n. 20
0
        public void LaguerreRootsFindingTest(params double[] nums)
        {
            //arrange
            double      TOL  = 10E-7;
            CPolynomial poly = FromArray(nums);

            //action
            Complex[] roots = CPolynomial.LaguerreRoots(poly);

            //assert
            for (int i = 0; i < roots.Length; i++)
            {
                Complex p = poly.Evaluate(roots[i]);
                Complex.Abs(p).Should().BeLessOrEqualTo(TOL);
            }
        }
Esempio n. 21
0
        public void FromRootsTest()
        {
            //arrange
            double TOL = 10E-10;

            Complex[] roots = new Complex[] { 3, 0, 158.3, 13, 8 };

            //action
            CPolynomial poly = CPolynomial.FromRoots(roots);

            //assert
            for (int i = 0; i < roots.Length; i++)
            {
                Complex p = poly.Evaluate(roots[i]);
                Complex.Abs(p).Should().BeLessThan(TOL);
            }
        }
Esempio n. 22
0
        public void ReadXmlTest_Deserialize()
        {
            //arrange
            CPolynomial   expected = new CPolynomial(new Complex[] { 3, 10.2, new Complex(3, -0.2), 0, new Complex(2, 35) });
            CPolynomial   actual;
            XmlSerializer serializer = new XmlSerializer(typeof(CPolynomial));
            string        xml        = "<CPolynomial>3 + 10.2*x + (3 - 0.2i)*x^2 + (2 + 35i)*x^4</CPolynomial>";

            //action
            using (StringReader reader = new StringReader(xml))
            {
                actual = (CPolynomial)serializer.Deserialize(reader);
            }

            //assert
            actual.Should().Be(expected);
        }
        public override object Evaluate()
        {
            CPolynomial poly = LeftExpression.EvaluateAsCPolynomial();
            Object      x    = RightExpression.Evaluate();

            if (x is Complex)
            {
                return(poly.Evaluate((Complex)x));
            }
            else if (x is CMatrix)
            {
                return(poly.Evaluate((CMatrix)x));
            }
            else
            {
                throw ExceptionHelper.ThrowWrongArgumentType(x);
            }
        }
Esempio n. 24
0
        public void DivBinomTest()
        {
            //arrange
            double      TOL = 1E-10;
            CPolynomial f = new CPolynomial(new Complex[] { 3, 0.2, new Complex(3, 8.2), -0.16, new Complex(2.3, 2), 0, 18.3466, 2000 });
            Complex     c = 346.34645;
            Complex     r, rtest;

            //action
            CPolynomial q = CPolynomial.DivBinom(f, c, out r);

            f[0] -= r;
            CPolynomial qtest = CPolynomial.DivBinom(f, c, out rtest);

            //assert
            Complex.Abs(rtest).Should().BeLessOrEqualTo(TOL);
            q.Should().Be(qtest);
        }
Esempio n. 25
0
        public void DivBinomTest()
        {
            //arrange
            double TOL = 1E-10;
            CPolynomial f = new CPolynomial(new Complex[] {3, 0.2, new Complex(3, 8.2), -0.16, new Complex(2.3, 2), 0, 18.3466, 2000});
            Complex c = 346.34645;
            Complex r, rtest;

            //action
            CPolynomial q = CPolynomial.DivBinom(f, c, out r);

            f[0] -= r;
            CPolynomial qtest = CPolynomial.DivBinom(f, c, out rtest);

            //assert
            Complex.Abs(rtest).Should().BeLessOrEqualTo(TOL);
            q.Should().Be(qtest);
        }
Esempio n. 26
0
        public void InterpolatingPolynomialTest()
        {
            //arrange
            double TOL = 1E-6;

            double[] xValues = new double[] { 1, 2.5, 6, 7.9, 18 };
            double[] yValues = new double[] { 8.6, 0, -2, 9, 33 };

            //action
            CPolynomial poly = CPolynomial.InterpolatingPolynomial(xValues, yValues);

            //assert
            for (int j = 0; j < xValues.Length; j++)
            {
                Complex val = poly.Evaluate(xValues[j]);
                NumericUtil.FuzzyEquals(yValues[j], val, TOL).Should().BeTrue();
            }
        }
Esempio n. 27
0
        public void WriteXmlTest_Serialize()
        {
            //arrange
            CPolynomial   poly       = new CPolynomial(new Complex[] { 3, 5.2, new Complex(3, -0.2), 0, new Complex(2, 35) });
            string        expected   = "<CPolynomial>3+5.2*x+(3-0.2i)*x^2+(2+35i)*x^4</CPolynomial>";
            XmlSerializer serializer = new XmlSerializer(typeof(CPolynomial));
            StringBuilder sb         = new StringBuilder();

            //action
            using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings {
                OmitXmlDeclaration = true
            }))
            {
                serializer.Serialize(xw, poly);
            }

            //assert
            sb.ToString().Should().Be(expected);
        }
        /// <summary>
        /// Returns the value of the complex polynomial evaluated at a specified value.
        /// </summary>
        /// <param name="poly">The source.</param>
        /// <param name="value">A complex square matrix.</param>
        /// <returns>The evaluated value of the complex polynomial.</returns>
        /// <exception cref="MatrixSizeMismatchException">The matrix value is not square.</exception>
        public static CMatrix Evaluate(this CPolynomial poly, CMatrix value)
        {
            if (!value.IsSquare)
            {
                throw new MatrixSizeMismatchException("The matrix must be square.");
            }

            int len = poly.Degree + 1;
            int n   = value.RowCount;

            CMatrix m      = CMatrix.Identity(n);
            CMatrix result = new CMatrix(n, n);

            for (int i = 0; i < len; i++)
            {
                result += poly[i] * m;
                m      *= value;
            }

            return(result);
        }
Esempio n. 29
0
        public void DivTest()
        {
            //arrange
            double TOL = 1E-12;

            CPolynomial f    = new CPolynomial(new Complex[] { new Complex(3467.2, 456), new Complex(2.225, -0.0123), new Complex(46.2, 4.24), new Complex(2, 2), new Complex(12.8, 16.3), new Complex(0, 0), new Complex(22, 347) });
            CPolynomial g    = new CPolynomial(new Complex[] { new Complex(3, 8.5), new Complex(11, -0.5), new Complex(0, 1), new Complex(1, 2), new Complex(346, 4.365) });
            CPolynomial zero = new CPolynomial(1);

            CPolynomial r;
            CPolynomial q     = CPolynomial.Divide(f, g, out r);
            CPolynomial rtest = new CPolynomial(1);

            //action
            CPolynomial qtest = CPolynomial.Divide(f - r, g, out rtest);

            //assert
            for (int j = 0; j < rtest.Length; j++)
            {
                Complex.Abs(rtest[j]).Should().BeLessOrEqualTo(TOL);
            }
            qtest.Should().Be(q);
        }
Esempio n. 30
0
        public void DivTest()
        {
            //arrange
            double TOL = 1E-12;

            CPolynomial f = new CPolynomial(new Complex[] { new Complex(3467.2, 456), new Complex(2.225, -0.0123), new Complex(46.2 ,4.24), new Complex(2, 2), new Complex(12.8, 16.3), new Complex(0, 0), new Complex(22, 347) });
            CPolynomial g = new CPolynomial(new Complex[] { new Complex(3, 8.5), new Complex(11, -0.5), new Complex(0, 1), new Complex(1, 2), new Complex(346, 4.365) });
            CPolynomial zero = new CPolynomial(1);

            CPolynomial r;
            CPolynomial q = CPolynomial.Divide(f, g, out r);
            CPolynomial rtest = new CPolynomial(1);

            //action
            CPolynomial qtest = CPolynomial.Divide(f - r, g, out rtest);

            //assert
            for (int j = 0; j < rtest.Length; j++)
            {
                Complex.Abs(rtest[j]).Should().BeLessOrEqualTo(TOL);
            }
            qtest.Should().Be(q);
        }
        public void CompanionMatrixRootsFindingTest()
        {
            //arrange
            double TOL = 10E-10;
            CPolynomial poly = new CPolynomial(new Complex[]
            {
                new Complex(122, 14),
                new Complex(0, 2.2),
                new Complex(14.22, -18.44),
                new Complex(1130, 0),
                new Complex(14, 14),
                new Complex(18, 0.2)
            });

            //action
            Complex[] roots = poly.CompanionMatrixRootsFinding();

            //assert
            foreach (Complex root in roots)
            {
                Complex p = poly.Evaluate(root);
                Complex.Abs(p).Should().BeLessThan(TOL);
            }
        }
        public void CompanionMatrixRootsFindingTest()
        {
            //arrange
            double      TOL  = 10E-10;
            CPolynomial poly = new CPolynomial(new Complex[]
            {
                new Complex(122, 14),
                new Complex(0, 2.2),
                new Complex(14.22, -18.44),
                new Complex(1130, 0),
                new Complex(14, 14),
                new Complex(18, 0.2)
            });

            //action
            Complex[] roots = poly.CompanionMatrixRootsFinding();

            //assert
            foreach (Complex root in roots)
            {
                Complex p = poly.Evaluate(root);
                Complex.Abs(p).Should().BeLessThan(TOL);
            }
        }
        public override object Evaluate()
        {
            CPolynomial poly = SubExpression.EvaluateAsCPolynomial();

            return(new CMatrix(poly.Roots()));
        }
Esempio n. 34
0
        public void FirstDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] {8, -28, 9, 24, -110});

            //action
            CPolynomial actual = target.FirstDerivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 35
0
        public void NthDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] { 18, 144, -1320 });

            //action
            CPolynomial actual = target.NthDerivative(3);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 36
0
        public void ParseTest_Success2()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { new Complex(0, -0.3) });

            //action
            CPolynomial actual = CPolynomial.Parse("-0.3i");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 37
0
        public void ParseTest_Success3()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { 0, 2 });

            //action
            CPolynomial actual = CPolynomial.Parse("2x");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 38
0
        public void ReadXmlTest_Deserialize()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { 3, 10.2, new Complex(3, -0.2), 0, new Complex(2, 35) });
            CPolynomial actual;
            XmlSerializer serializer = new XmlSerializer(typeof(CPolynomial));
            string xml = "<CPolynomial>3 + 10.2*x + (3 - 0.2i)*x^2 + (2 + 35i)*x^4</CPolynomial>";

            //action
            using (StringReader reader = new StringReader(xml))
            {
                actual = (CPolynomial)serializer.Deserialize(reader);
            }

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 39
0
        public void SecondDerivativeTest()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            Complex c = 3;
            Complex expected = -11206;

            //action
            Complex actual = target.SecondDerivative(c);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 40
0
        public void SecondDerivativeTest_Analytical()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] { 5, 8, -14, 3, 6, -22 });
            CPolynomial expected = new CPolynomial(new Complex[] { -28, 18, 72, -440 });

            //action
            CPolynomial actual = target.SecondDerivative();

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 41
0
        public void ToStringTest()
        {
            //arrange
            CPolynomial poly = new CPolynomial(new Complex[] { 3, 1.1, 1, 0, 0, new Complex(-5, 12.3) });
            string expected = "3 + 1.1*t + t^2 + (-5 + 12.3i)*t^5";

            //action
            var actual = poly.ToString(null, CultureInfo.InvariantCulture, "t");

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 42
0
        public void WriteXmlTest_Serialize()
        {
            //arrange
            CPolynomial poly = new CPolynomial(new Complex[] { 3, 5.2, new Complex(3, -0.2), 0, new Complex(2, 35) });
            string expected = "<CPolynomial>3+5.2*x+(3-0.2i)*x^2+(2+35i)*x^4</CPolynomial>";
            XmlSerializer serializer = new XmlSerializer(typeof(CPolynomial));
            StringBuilder sb = new StringBuilder();

            //action
            using (XmlWriter xw = XmlWriter.Create(sb, new XmlWriterSettings { OmitXmlDeclaration = true }))
            {
                serializer.Serialize(xw, poly);
            }

            //assert
            sb.ToString().Should().Be(expected);
        }
        public override object Evaluate()
        {
            IList <Complex> roots = SubExpression.EvaluateAsComplexVector();

            return(new CMatrix(CPolynomial.FromRoots(roots).ToArray()));
        }
Esempio n. 44
0
        public void FirstDerivativeTest()
        {
            //arrange
            CPolynomial target = new CPolynomial(new Complex[] {5, 8, -14, 3, 6, -22});
            Complex value = 3;
            Complex expected = -8257;

            //action
            Complex actual = target.FirstDerivative(value);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 45
0
        public void ParseTest_Success5()
        {
            //arrange
            CPolynomial expected = new CPolynomial(new Complex[] { 3, new Complex(-4, 12.3), 1 });

            //action
            CPolynomial actual = CPolynomial.Parse("x+3 + x^2-(5-12.3i)*x");

            //assert
            actual.Should().Be(expected);
        }