Beispiel #1
0
        public static Tuple <IntegerType, IntegerType, IntegerType> ExtendedEuclideanAlgorithm <IntegerType>(IAlgebraInteger <IntegerType> algebra, IntegerType a, IntegerType b)
        {
            if (algebra.Compare(algebra.Abs(a), algebra.Abs(b)) == -1)
            {
                Tuple <IntegerType, IntegerType, IntegerType> xyd = ExtendedEuclideanAlgorithm(algebra, b, a);
                return(new Tuple <IntegerType, IntegerType, IntegerType>(xyd.Item2, xyd.Item1, xyd.Item3));
            }

            if (algebra.Abs(b).Equals(algebra.AddIdentity))
            {
                return(new Tuple <IntegerType, IntegerType, IntegerType>(algebra.MultiplyIdentity, algebra.AddIdentity, a));
            }

            IntegerType x1 = algebra.AddIdentity;
            IntegerType x2 = algebra.MultiplyIdentity;
            IntegerType y1 = algebra.MultiplyIdentity;
            IntegerType y2 = algebra.AddIdentity;

            while (algebra.Compare(algebra.AddIdentity, algebra.Abs(b)) == -1)
            {
                IntegerType q = algebra.Divide(a, b);
                IntegerType r = algebra.Modulo(a, b);
                IntegerType x = algebra.Subtract(x2, algebra.Multiply(q, x1));
                IntegerType y = algebra.Subtract(y2, algebra.Multiply(q, y1));
                a  = b;
                b  = r;
                x2 = x1;
                x1 = x;
                y2 = y1;
                y1 = y;
            }
            return(new Tuple <IntegerType, IntegerType, IntegerType>(x2, y2, a));
        }
Beispiel #2
0
        public Tuple <Polynomial <DomainType>, Polynomial <DomainType> > DivideRemainder(Polynomial <DomainType> divisor_poly)
        {
            Debug.Assert(algebra.Equals(divisor_poly.algebra));
            // if divisor is too big
            if (coeffecients.Length < divisor_poly.coeffecients.Length)
            {
                return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra), new Polynomial <DomainType>(algebra, coeffecients)));
            }
            // if divisor is too big
            if ((coeffecients.Length == divisor_poly.coeffecients.Length) && (algebra.Compare(coeffecients[coeffecients.Length - 1], coeffecients[coeffecients.Length - 1]) == -1))
            {
                return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra), new Polynomial <DomainType>(algebra, coeffecients)));
            }

            //Else do long division
            DomainType[] remainder = ToolsCollection.Copy(coeffecients);
            DomainType[] results   = new DomainType[coeffecients.Length];
            DomainType[] divisor   = divisor_poly.coeffecients;
            for (int index = 0; index < results.Length; index++)
            {
                results[index] = algebra.AddIdentity;
            }
            int max_shift = remainder.Length - divisor.Length;


            for (int shift = max_shift; 0 <= shift; shift--)
            {
                DomainType multiplier = algebra.Divide(remainder[shift], divisor[divisor.Length - 1]);
                for (int index = shift; index < coeffecients.Length; index++)
                {
                    remainder[index] = algebra.Subtract(remainder[index], algebra.Multiply(divisor[index + shift], multiplier));
                }
                results[shift] = multiplier;
            }

            return(new Tuple <Polynomial <DomainType>, Polynomial <DomainType> >(new Polynomial <DomainType>(algebra, results), new Polynomial <DomainType>(algebra, remainder)));
        }