Пример #1
0
        /// <summary>
        /// Наибольший общий делитель 2-х полиномов
        /// </summary>
        /// <param name="f"> Полином </param>
        /// <param name="g"> Полином </param>
        /// <returns> НОД - полином </returns>
        public static Polynom GetGCD(Polynom f, Polynom g)
        {
            Polynom h = f.Degree > g.Degree ? (Polynom)f.Clone() : (Polynom)g.Clone();
            Polynom s = f.Degree < g.Degree ? (Polynom)f.Clone() : (Polynom)g.Clone();

            while (!s.IsNull)
            {
                if (h.Degree >= s.Degree)
                {
                    Polynom      rem = new Polynom();
                    List <Monom> q   = new List <Monom>();
                    DividePolynoms(h, s, out q, out rem);
                    h = s;
                    s = rem;
                }
                else
                {
                    break;
                }
            }

            return(h);
        }
Пример #2
0
        /// <summary>
        /// Деление полиномов с остатком
        /// </summary>
        /// <param name="f"> Делимое </param>
        /// <param name="g"> Делитель </param>
        /// <param name="q"> Частное </param>
        /// <param name="r"> Остаток </param>
        public static void DividePolynoms(Polynom f, Polynom g, out List <Monom> q, out Polynom r)
        {
            q = new List <Monom>();
            r = (Polynom)f.Clone();

            while (r.Degree >= g.Degree)
            {
                if (!r.IsNull && Monom.CanDivide(r.LT, g.LT))
                {
                    Monom divLT = r.LT / g.LT;
                    q.Add(divLT);
                    var temp = g * (divLT);
                    r = r - temp;
                    r = LexOrder.CreateOrderedPolynom(r);
                    r = r.SimplifyPolynom();
                }
                else
                {
                    break;
                }
            }
        }