Example #1
0
        protected bool Equals(SingleVariablePolynomial other)
        {
            if (ReferenceEquals(Coefficients, null) && ReferenceEquals(other.Coefficients, null))
            {
                return(true);
            }

            if (ReferenceEquals(Coefficients, null) || ReferenceEquals(other.Coefficients, null))
            {
                return(false);
            }


            if (Coefficients.Length != other.Coefficients.Length)
            {
                return(false);
            }

            for (int i = 0; i < Coefficients.Length; i++)
            {
                var d = Math.Abs(Coefficients[i] - other.Coefficients[i]);

                if (d > 1e-10 * Math.Abs(Coefficients[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #2
0
        public SingleVariablePolynomial Clone()
        {
            var c2 = Coefficients == null ? (double[])null : (double[])Coefficients.Clone();

            var buf = new SingleVariablePolynomial(c2);

            return(buf);
        }
Example #3
0
        /// <summary>
        /// Gets the derivative of polynomial as another polynomial
        /// </summary>
        /// <param name="deg">derivation degree</param>
        /// <returns>derivation</returns>
        public SingleVariablePolynomial GetDerivative(int deg)
        {
            if (deg == 0)
            {
                return((SingleVariablePolynomial)this.MemberwiseClone());
            }

            if (Coefficients.Length == 0)
            {
                return(this.Clone());
            }

            if (deg >= Coefficients.Length)
            {
                return(new SingleVariablePolynomial());
            }

            var dic = new Dictionary <int, double>();

            var buf = new double[Coefficients.Length - deg];

            for (var i = 0; i < Coefficients.Length; i++)
            {
                var origPow = Coefficients.Length - 1 - i;

                var n = origPow;
                var m = deg;

                if (n - m >= 0)
                {
                    var newPow = n - m;

                    //dic[newPow] = Factorial(n)/Factorial(n - m) * this.Coefficients[i];
                    dic[newPow] = Factorial(n, n - m) * this.Coefficients[i];

                    //var d = Factorial(n) / Factorial(n - m) - Factorial(n, n - m);
                }
            }

            for (var i = 0; i < buf.Length; i++)
            {
                var origPow = buf.Length - 1 - i;

                buf[i] = dic[origPow];
            }

            var pBuf = new SingleVariablePolynomial(buf);

            return(pBuf);