예제 #1
0
        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()
        {
            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);
            }
        }
예제 #3
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();
        }
예제 #4
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);
            }
        }
예제 #5
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);
            }
        }
예제 #6
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();
            }
        }
        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);
            }
        }