예제 #1
0
        public static KeyValuePair <Polynome, Polynome> operator /(Polynome p1, Polynome p2)
        {
            var result = new Polynome("0");

            if (p1.coeffs.Count < p2.coeffs.Count)
            {
                return(new KeyValuePair <Polynome, Polynome>(new Polynome("0"), p1));
            }

            var index = p1.coeffs.Count - 1;

            int pow = p1.coeffs.Count - p2.coeffs.Count;

            var divided = new Polynome(p1.coeffs);

            var divider = new Polynome("0");

            do
            {
                divider = new Polynome("0");

                Coeff c = divided.coeffs[index] / p2.coeffs[p2.coeffs.Count - 1];

                result = result + new Polynome(c + "x^" + pow);

                divider = p2 * new Polynome(c + "x^" + pow);

                divided -= divider;

                index--;
                pow--;
            } while (pow >= 0);

            return(new KeyValuePair <Polynome, Polynome>(result, divided));
        }
예제 #2
0
        public static Polynome GCF(Polynome p1, Polynome p2)
        {
            Polynome copy1 = new Polynome(p1.coeffs);
            Polynome copy2 = new Polynome(p2.coeffs);

            var  CoeffGCF = new BigInteger();
            bool order    = false;

            while (copy1.coeffs.Count != 1 && copy2.coeffs.Count != 1)
            {
                if (copy1.coeffs.Count > copy2.coeffs.Count)
                {
                    copy1 = (copy1 / copy2).Value;
                    order = true;
                }
                else
                {
                    copy2 = (copy2 / copy1).Value;
                    order = false;
                }
                Console.WriteLine(copy1);
                Console.WriteLine(copy2);
            }

            if (order)
            {
                return((copy1 / new Polynome(copy1.coeffs[0].ToString())).Key);
            }
            else
            {
                return((copy2 / new Polynome(copy2.coeffs[0].ToString())).Key);
            }

            return(copy2 + copy1);
        }
예제 #3
0
 public void TestPolSub()
 {
     polS = pol1 - pol1;
     Assert.AreEqual("0", polS.ToString());
     polS = pol1 - pol2 - pol4;
     Assert.AreEqual("6x^2+4x-2", polS.ToString());
 }
예제 #4
0
 public void Init()
 {
     pol1 = new Polynome(coef1);
     pol2 = new Polynome(coef2);
     pol3 = new Polynome(coef3);
     pol4 = new Polynome(coef4);
 }
예제 #5
0
        public Polynome Derivative()
        {
            Polynome polynome = new Polynome("0");

            for (int i = 1; i < coeffs.Count; i++)
            {
                polynome.addInCoeffs(i - 1, coeffs[i] * new Coeff(i, 1));
            }
            return(polynome);
        }
예제 #6
0
 private bool CheckPloynome(Polynome p)
 {
     for (int i = 1; i < p.coeffs.Count; i++)
     {
         if (p.coeffs[i].up != 0)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #7
0
 public void TestPolDeriv()
 {
     polD = pol1.Derivate();
     Assert.AreEqual("6x+2", polD.ToString());
     Assert.AreEqual(1, polD.Degree);
     polD.Derivate();
     Assert.AreEqual("6x+2", polD.ToString());
     polD = polD.Derivate();
     Assert.AreEqual("6", polD.ToString());
     polD = polD.Derivate();
     Assert.AreEqual("0", polD.ToString());
 }
예제 #8
0
        //override - operator
        public static Polynome operator -(Polynome left, Polynome right)
        {
            double[] invcoefs = new double[right.Degree + 1];
            int      i        = 0;

            while (i <= right.Degree)
            {
                invcoefs[i] = -right.Coeffs[i];
                i++;
            }
            Polynome invright = new Polynome(invcoefs);

            return(left + invright);
        }
예제 #9
0
        public static Polynome Lagrange(List <int> values)
        {
            List <Polynome> parts = new List <Polynome>();

            for (int i = 0; i < values.Count; ++i)
            {
                var coef = 1;
                var tek  = new List <Polynome>();
                for (int j = 0; j < values.Count; ++j)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    var newTemp = new Polynome(new List <Coeff>()
                    {
                        new Coeff(-j, 1),
                        new Coeff(1, 1)
                    });
                    //var newTemp = new Coeff(-j, 1);new Fraction[]
                    //{
                    //    new Fraction(-j, 1),
                    //    new Fraction(1, 1)
                    //});
                    tek.Add(newTemp);
                    coef *= (i - j);
                }
                if (tek.Count != 0)
                {
                    var t    = new Coeff(values[i], coef);
                    var temp = tek.Aggregate((x, y) => x * y);
                    temp.coeffs = temp.coeffs.Select(x => x * t).ToList();
                    //temp.coeffs.Clear();
                    if (temp.coeffs.Count != 0)
                    {
                        parts.Add(temp);
                    }
                }
            }

            if (parts.Count == 0)
            {
                return(new Polynome("0"));
            }

            return(parts.Aggregate((i, k) => i + k));
            //return null;
        }
예제 #10
0
        //return the root of the derivate of degree 1
        public double FindRootsLine()
        {
            double result;

            if (degree == 1)
            {
                result = -coeffs[0] / coeffs[1];
            }
            else if (degree == 0)
            {
                throw new Exception("Polynome cannot be of degree 0");
            }
            else
            {
                Polynome polD = Derivate();
                while (polD.Degree > 1)
                {
                    polD = polD.Derivate();
                }
                result = -polD.Coeffs[0] / polD.Coeffs[1];
            }
            return(result);
        }
예제 #11
0
            static void Main()
            {
                Polynome a = new Polynome();
                Polynome b = new Polynome();
                a.Input();
                //b.Input();

                //Polynome c = a + b;

                //c.Print();
                a.Integriate();
                //a.Differentiate();
                Console.ReadKey();
            }
예제 #12
0
        static void Main(string[] args)
        {
            //Polynome p = new Polynome("3x^2 + 2 - 1/2x^3 - 6x");

            //Polynome p3 = new Polynome("3/2x + 5x^4 - 3/6x^3 - 1");

            //Polynome p2 = new Polynome("3x^2 + 6x - 7");
            //Polynome p4 = new Polynome("16x^1+5");

            //Console.WriteLine(p2);
            //Console.WriteLine(p4);

            //Polynome p5 = p2 * p4;

            //var p6 = p3 / p;

            //Console.WriteLine(p5);


            // НУЖГЫЙ КОД
            var p1 = new Polynome("-16x^6 + 20x^5 - 4x^3 - 12x^2 + 1");
            var p2 = new Polynome("2x^4 - 3x^3 - 5x^2 - 14x - 1");

            //var p1 = new Polynome("8x^6 + 32x^3 - 16x^2 + 40");
            //var p2 = new Polynome("-2x^4 - 3x^3 + 5x^2 - x - 1");

            Console.WriteLine("Polynome 1: " + p1);
            Console.WriteLine("Polynome 2: " + p2);

            var a = (p1 / p2).Key;
            var b = (p1 / p2).Value;

            Console.WriteLine("Full : " + a);
            Console.WriteLine("Remains : " + b);

            //var num = MathUtils.Solve(new List<int>() { 5, 7, 11, 13 }, new List<int>() { 2, 1, 3, 8 });
            var num = MathUtils.Solve(new[] { 3, 7, 13 }, new[] { 1, 3, 5 });

            //var num = MathUtils.Solve(new[] { 5, 7, 11 }, new[] { 1, 3, 5 });

            Console.WriteLine($"CHINA RESULT!: {num}");

            var res = a.AtPoint(num);

            Console.WriteLine($"full part of polymonopolynom at {num} is {res}");

            res = b.AtPoint(num);
            Console.WriteLine($"remains of monononomopolynom at {num} is {res}");


            Console.WriteLine("GCF = " + Polynome.GCF(p1, p2));

            Console.WriteLine("Kroneker's diveders of polynome 2: ");
            var krek = p2.Krek();
            var pol  = krek[0];

            Console.WriteLine(pol);
            Console.WriteLine((p2 / pol).Key + " + " + (p2 / pol).Value);
            Console.WriteLine("RES: ");
            Console.WriteLine((p2 / pol).Key * pol);
            Console.WriteLine("Derivative: ");
            Console.WriteLine(p2.Derivative());
            //Console.WriteLine(krek);

            Console.ReadKey();
        }
예제 #13
0
 public void TestPolAdd()
 {
     polP = pol1 + pol2;
     Assert.AreEqual("2", polP.ToString());
 }