Example #1
0
            /// <summary>
            /// Возвращает результат умножения многочленов.
            /// </summary>
            /// <param name="First"></param>
            /// <param name="Second"></param>
            /// <returns></returns>
            public static Polynom operator *(Polynom First, Polynom Second)
            {
                List <Polynom> summaryarray = new List <Polynom>();

                for (int i = 0; i <= First.Degree; i++)
                {
                    int FirstDegree = First.GetCoefDegree(i);
                    if (First[i] == 0)
                    {
                        continue;
                    }
                    summaryarray.Add(new Polynom(FirstDegree + Second.Degree));
                    for (int j = 0; j <= Second.Degree; j++)
                    {
                        int SecondDegree = Second.GetCoefDegree(j);
                        summaryarray[summaryarray.Count - 1][summaryarray[summaryarray.Count - 1].GetCoefIndex(FirstDegree + SecondDegree)] = First[i] * Second[j];
                    }
                }
                Polynom Result = new Polynom(summaryarray.Select(pol => pol.Degree).Max());

                Result = summaryarray.Aggregate(Result, (Current, part) => Current + part);
                return(Result);
            }
Example #2
0
            /// <summary>
            /// Возвращает результат умножения многочленов по модулю.
            /// </summary>
            /// <param name="First"></param>
            /// <param name="Second"></param>
            /// <returns></returns>
            public static ModularPolynom operator *(ModularPolynom First, ModularPolynom Second)
            {
                First  = GetNormalized(First);
                Second = GetNormalized(Second);
                if (First.Module != Second.Module)
                {
                    throw new InvalidOperationException("Модули многочленов должны совпадать.");
                }
                if (First.Degree == 0)
                {
                    return(Second * First[0]);
                }
                if (Second.Degree == 0)
                {
                    return(First * Second[0]);
                }
                List <ModularPolynom> summaryarray = new List <ModularPolynom>();

                for (int i = 0; i <= First.Degree; i++)
                {
                    int FirstDegree = First.GetCoefDegree(i);
                    if (First[i] == 0)
                    {
                        continue;
                    }
                    summaryarray.Add(new ModularPolynom(FirstDegree + Second.Degree, First.Module));
                    for (int j = 0; j <= Second.Degree; j++)
                    {
                        int SecondDegree = Second.GetCoefDegree(j);
                        summaryarray[summaryarray.Count - 1][summaryarray[summaryarray.Count - 1].GetCoefIndex(FirstDegree + SecondDegree)] = First[i] * Second[j];
                    }
                }
                ModularPolynom Result = new ModularPolynom(summaryarray.Select(pol => pol.Degree).Max(), First.Module);

                Result = summaryarray.Aggregate(Result, (Current, part) => Current + part);
                return(Result);
            }