/// <summary>
        /// Наибольший общий делитель 3 и более полиномов
        /// </summary>
        /// <param name="polynoms"> Список полиномов </param>
        /// <returns></returns>
        public static Polynom GetGCD(params Polynom[] polynoms)
        {
            Polynom        h = new Polynom();
            List <Polynom> sortedByDescending = polynoms.OrderBy(p => p.Degree).ToList();

            for (int i = sortedByDescending.Count - 2; i >= 0; i--)
            {
                sortedByDescending[i] = GetGCD(sortedByDescending[i], sortedByDescending[i + 1]);
                sortedByDescending.RemoveAt(i + 1);
            }

            return(sortedByDescending.First());
        }
        /// <summary>
        /// Упростить рациональную функцию
        /// </summary>
        /// <param name="divisionResult"></param>
        /// <returns></returns>
        public Function Simplify(out List <Monom> divisionResult)
        {
            divisionResult = new List <Monom>();

            if (numerator.LT.CompareTo(denominator.LT) >= 0) //сравниваем степени числителя и знаменателя
            {
                var reminder = new Polynom();
                Polynom.DividePolynoms(numerator, denominator, out divisionResult, out reminder);
                return(new RationalFunction(reminder, denominator));
            }
            else
            {
                return(new RationalFunction(numerator, denominator));
            }
        }
        /// <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;
                }
            }
        }
Example #4
0
        /// <summary>
        /// Перестановка мономов в полиноме
        /// </summary>
        /// <param name="poly"> Анализируемый полином </param>
        /// <returns> Готовый полином </returns>
        public static Polynom CreateOrderedPolynom(Polynom poly)
        {
            Polynom tempPolynom = poly, orderedPolynom;

            Dictionary <Monom, int> monomRepeats = tempPolynom.monoms.ToDictionary(m => m, m => 1);

            foreach (Monom t in tempPolynom.monoms)
            {
                foreach (Monom t1 in tempPolynom.monoms)
                {
                    if (t.CompareTo(t1) > 0)
                    {
                        monomRepeats[t]++;
                    }
                }
            }
            monomRepeats = monomRepeats.OrderByDescending(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value);

            //Составим упорядоченный полином
            orderedPolynom = new Polynom(monomRepeats.Keys.ToList());

            return(orderedPolynom.SimplifyPolynom());
        }
        /// <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);
        }
 public RationalFunction(Polynom num, Polynom denom)
 {
     numerator   = num;
     denominator = denom;
 }