示例#1
0
        /// <summary>
        /// get the eigenvalue matrix A of this Polynomial such that eig(A) = roots of this Polynomial.
        /// </summary>
        /// <returns>Eigenvalue matrix A</returns>
        /// <note>this matrix is similar to the companion matrix of this polynomial, in such a way, that it's transpose is the columnflip of the companion matrix</note>
        public DenseMatrix GetEigValMatrix()
        {
            Polynomial pLoc = new Polynomial(this.Coeffs);

            pLoc.Trim();

            int n = pLoc.Coeffs.Length - 1;

            if (n < 2)
            {
                return(null);
            }

            double[] p = new double[n];

            double a0 = pLoc.Coeffs[n];

            for (int ii = n - 1; ii >= 0; ii--)
            {
                p[ii] = -pLoc.Coeffs[ii] / a0;
            }

            DenseMatrix A0 = DenseMatrix.CreateDiagonal(n - 1, n - 1, 1.0);
            DenseMatrix A  = new DenseMatrix(n);

            A.SetSubMatrix(1, 0, A0);

            A.SetRow(0, p.Reverse().ToArray());
            return(A);
        }
示例#2
0
        public Polynomial Integrate()
        {
            var t = this.Clone() as Polynomial;

            t.Trim();
            var cNew = new double[t.Coeffs.Length + 1];

            for (int i = 1; i < cNew.Length; i++)
            {
                cNew[i] = t.Coeffs[i - 1] / i;
            }
            var p = new Polynomial(cNew);

            p.Trim();
            return(p);
        }
示例#3
0
        public Polynomial Differentiate()
        {
            if (Coeffs.Length == 0)
            {
                return(null);
            }

            var t = this.Clone() as Polynomial;

            t.Trim();
            var cNew = new double[t.Coeffs.Length - 1];

            for (int i = 1; i < t.Coeffs.Length; i++)
            {
                cNew[i - 1] = t.Coeffs[i] * i;
            }
            var p = new Polynomial(cNew);

            p.Trim();
            return(p);
        }
示例#4
0
        /// <summary>
        /// Division of two polynomials returning the quotient-with-remainder of the two polynomials given
        /// </summary>
        /// <param name="a">left polynomial</param>
        /// <param name="b">right polynomial</param>
        /// <returns>a tuple holding quotient in first and remainder in second</returns>
        public static Tuple <Polynomial, Polynomial> DivideLong(Polynomial a, Polynomial b)
        {
            if (a == null)
            {
                throw new ArgumentNullException("a");
            }
            if (b == null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Degree <= 0)
            {
                throw new ArgumentOutOfRangeException("a Degree must be greater than zero");
            }
            if (b.Degree <= 0)
            {
                throw new ArgumentOutOfRangeException("b Degree must be greater than zero");
            }

            if (b.Coeffs[b.Degree - 1] == 0)
            {
                throw new DivideByZeroException("b polynomial ends with zero");
            }

            var c1 = a.Coeffs.ToArray();
            var c2 = b.Coeffs.ToArray();

            var n1 = c1.Length;
            var n2 = c2.Length;

            double[] quo = null;
            double[] rem = null;

            if (n2 == 1) // division by scalar
            {
                var fact = c2[0];
                quo = new double[n1];
                for (int i = 0; i < n1; i++)
                {
                    quo[i] = c1[i] / fact;
                }
                rem = new double[] { 0 };
            }
            else if (n1 < n2) // denominator degree higher than nominator degree
            {
                // quotient always be 0 and return c1 as remainder
                quo = new double[] { 0 };
                rem = c1.ToArray();
            }
            else
            {
                var dn  = n1 - n2;
                var scl = c2[n2 - 1];
                var c22 = new double[n2 - 1];
                for (int ii = 0; ii < c22.Length; ii++)
                {
                    c22[ii] = c2[ii] / scl;
                }

                int i = dn;
                int j = n1 - 1;
                while (i >= 0)
                {
                    var v    = c1[j];
                    var vals = new double[j - i];
                    for (int k = i; k < j; k++)
                    {
                        c1[k] -= c22[k - i] * v;
                    }
                    i--;
                    j--;
                }

                var j1 = j + 1;
                var l1 = n1 - j1;

                rem = new double[j1];
                quo = new double[l1];

                for (int k = 0; k < l1; k++)
                {
                    quo[k] = c1[k + j1] / scl;
                }

                for (int k = 0; k < j1; k++)
                {
                    rem[k] = c1[k];
                }
            }

            if (rem == null)
            {
                throw new NullReferenceException("resulting remainder was null");
            }

            if (quo == null)
            {
                throw new NullReferenceException("resulting quotient was null");
            }


            // output mapping
            var pQuo = new Polynomial(quo);
            var pRem = new Polynomial(rem);

            pRem.Trim();
            pQuo.Trim();
            return(new Tuple <Polynomial, Polynomial>(pQuo, pRem));
        }