/// <summary>
        /// Static method multiplies 2 polynomials.
        /// </summary>
        /// <param name="firstPolynom">
        /// first polynomial.
        /// </param>
        /// <param name="secondPolynom">
        /// second polynomial.
        /// </param>
        /// <returns>
        /// Result of multiplication.
        /// </returns>
        private static Polynom Multiply(Polynom firstPolynom, Polynom secondPolynom)
        {
            double[] result_array = new double[firstPolynom.ArrayOfFactors.Length + secondPolynom.ArrayOfFactors.Length];
            for (int i = secondPolynom.ArrayOfFactors.Length - 1; i >= 0; i--)
            {
                for (int j = 0; j < firstPolynom.ArrayOfFactors.Length; j++)
                {
                    result_array[i + j] += firstPolynom.ArrayOfFactors[j] * secondPolynom.ArrayOfFactors[i];
                }
            }

            return(new Polynom(result_array));
        }
Beispiel #2
0
 public PolynomialBuilding(params double[] coefficient)
 {
     polynom = new Polynom[coefficient.Length];
     for (int i = 0; i < coefficient.Length; i++)
     {
         if (coefficient[i] > Eps)
         {
             polynom[i] = new Polynom(coefficient[i], i);
         }
         else
         {
             polynom[i] = new Polynom();
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Entry point.
        /// </summary>
        public static void Main()
        {
            Polynom x1  = new Polynom();
            Polynom x2  = new Polynom();
            Polynom x3  = x1 * x2;
            string  str = x3.ToString();

            for (int i = 0; i < x3.ArrayOfFactors.Length; i++)
            {
                Console.Write($"{x3.ArrayOfFactors[i]} ");
            }

            Console.WriteLine();
            Console.WriteLine(str);
            Console.ReadKey();
        }
Beispiel #4
0
        private void pbNewPolynom_Click(object sender, EventArgs e)
        {
            Label destination = null;

            if (sender.Equals(pbNewPolynom1))
            {
                destination = lbPolynom1;
            }
            if (sender.Equals(pbNewPolynom2))
            {
                destination = lbPolynom2;
            }

            if (destination == null)
            {
                return;
            }

            List <double> coeficents = new List <double>();
            int           count      = 0;
            string        str;

            do
            {
                str = Interaction.InputBox(string.Format("Введите коэфицент a{0}", count++), "Коефицент");
                if (str != "")
                {
                    coeficents.Add(double.Parse(str));
                }
            } while (str != "");

            Polynom polynom = new Polynom(coeficents.ToArray());

            if (sender.Equals(pbNewPolynom1))
            {
                lbVariableName.Text = string.Format(variableNamePatern, polynom.Variable);
                p1 = polynom;
            }
            if (sender.Equals(pbNewPolynom2))
            {
                p2 = polynom;
            }

            destination.Text = polynom.ToString();
        }
        private static bool Compare(Polynom p1, Polynom p2)
        {
            if (p1.ArrayOfFactors.Length == p2.ArrayOfFactors.Length)
            {
                for (int i = 0; i < p1.ArrayOfFactors.Length; i++)
                {
                    if (p1.ArrayOfFactors[i] != p2.ArrayOfFactors[i])
                    {
                        return(false);
                    }
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            // Polynom polynom = new Polynom("-x^2g-3x-4");
            Polynom polynom  = new Polynom("x^2-3x-8");
            Polynom polynom1 = new Polynom("3x+5x");

            // Console.WriteLine(polynom.Max);
            // Console.WriteLine(polynom1.Max);
            Console.WriteLine("dvd");
            Operations.Sum(polynom, polynom1);

            //polynom.Show();

            // polynom.Show();
            Console.WriteLine();
            //  polynom1.Show();
            //polynom1.Show();
            //  Console.WriteLine(Operations.Sum(polynom, polynom1));

            Console.ReadKey();
        }
Beispiel #7
0
        public static PolynomialBuilding operator *(PolynomialBuilding first, PolynomialBuilding second)
        {
            if (first == null || second == null)
            {
                throw new NullReferenceException();
            }
            Polynom[] resultPolynom = new Polynom[first.polynom.Length + second.polynom.Length - 1];
            for (int i = 0; i < first.polynom.Length; i++)
            {
                for (int j = 0; j < second.polynom.Length; j++)
                {
                    resultPolynom[i + j].Coefficient += first.polynom[i].Coefficient * second.polynom[j].Coefficient;
                }
            }

            for (int i = 0; i < resultPolynom.Length; i++)
            {
                if (!(resultPolynom[i].Coefficient < 0 + Eps && resultPolynom[i].Coefficient > 0 - Eps))
                {
                    resultPolynom[i].Power = i;
                }
            }
            return(new PolynomialBuilding(resultPolynom));
        }
Beispiel #8
0
        private void button7_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                f = new Polynom(p.GetNodes());
            }

            if (radioButton2.Checked)
            {
                p = new Polynom(f.GetNodes());
            }

            if (radioButton3.Checked)
            {
                var temp = new Polynom(f.GetNodes());
                f = new Polynom(p.GetNodes());
                p = new Polynom(temp.GetNodes());
            }

            prepareLetters();

            textBox3.Text = p.GetRepresentation();
            textBox4.Text = f.GetRepresentation();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Polynomial 1");
            Polynom p1 = new Polynom();

            p1.CreatePolynom();
            p1.Print();
            Console.WriteLine();

            Console.WriteLine("Polynomial 2");
            Polynom p2 = new Polynom();

            p2.CreatePolynom();
            p2.Print();
            Console.WriteLine();

            Polynom p3;

            while (true)
            {
                Console.WriteLine("1. Add polynomials.");
                Console.WriteLine("2. Subtract polynomials.");
                Console.WriteLine("3. Multiply polynomials.");
                Console.WriteLine("4. Increment polynomial.");
                Console.WriteLine("5. Recreate polynomials.");
                Console.Write("Select menu item: ");
                int key = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine();
                switch (key)
                {
                case 1:
                    p3 = p1 + p2;
                    p3.Print();
                    Console.WriteLine();
                    break;

                case 2:
                    p3 = p1 - p2;
                    p3.Print();
                    Console.WriteLine();
                    break;

                case 3:
                    p3 = p1 - p2;
                    p3.Print();
                    Console.WriteLine();
                    break;

                case 4:
                    p3 = p1++;
                    p3.Print();
                    Console.WriteLine();
                    break;

                case 5:
                    Console.WriteLine("Polynomial 1");
                    p1.RecreatePolynom();
                    p1.Print();
                    Console.WriteLine();
                    Console.WriteLine("Polynomial 2");
                    p2.RecreatePolynom();
                    p2.Print();
                    Console.WriteLine();
                    break;

                default:
                    Console.WriteLine("\nThere are no such menu item!\nPlease try again!\n");
                    break;
                }
            }
        }
        public void RecreatePolynom()
        {
            Polynom p1 = new Polynom();

            CreatePolynom();
        }
Beispiel #11
0
        //public static int i=0;

        public static void Sum(Polynom one, Polynom two)
        {
            if (one.Variable == two.Variable)
            {
                int i        = one.CompareTo(two);
                var variable = one.Variable;
                Dictionary <int, int>    Coefficient = new Dictionary <int, int>();
                Dictionary <int, string> Variables   = new Dictionary <int, string>();
                Dictionary <int, int>    Exponents   = new Dictionary <int, int>();



                int  j     = 0;
                int  k     = 0;
                bool check = false;
                while (i > 0)
                {
                    foreach (var item in one.Exponents)
                    {
                        k = 0;
                        if (item.Value == i)
                        {
                            Coefficient[j] = item.Value;
                            Variables[j]   = variable;
                            Exponents[j]   = i;
                            check          = true;
                        }
                        if (one.Variables[k] == null && i == 1)
                        {
                            Console.WriteLine(item.Value);
                        }
                        k++;
                    }

                    /* foreach (var item in two.Exponents)
                     * {
                     *   if (item.Value == i)
                     *   {
                     *       Coefficient[j] += item.Value;
                     *       Variables[j] = variable;
                     *       Exponents[j] = i;
                     *       check = true;
                     *   }
                     * }*/

                    if (check == true)
                    {
                        j++;
                        check = false;
                    }


                    // Console.WriteLine(Coefficient[0]);
                    // Console.WriteLine(Variables[0]);



                    --i;
                }
            }
            else
            {
                throw new Exception("Different variables");
            }
        }