Пример #1
0
        }                                  //invokes Polynomial(100)

        //clone the polynomial
        public Object Clone()
        {
            int        i = 0;
            Polynomial c = new Polynomial();
            Term       t;

            //Polynomial c = (Polynomial)this.MemberwiseClone();//makes a copy of all of the references and sets it up to be copied properly

            //goes through all of the elements in the array to clone each term induvidually, making a deep copy
            while (i <= count - 1)
            {
                t = (Term)P[i].Clone(); //takes the term at position i in the current polynomial and
                c.AddTerm(t);           //adds said term to the new polynomial
                i++;
            }

            return(c); //spits out the cloned Polynomial
        }
Пример #2
0
    // Multiplies the given polynomials p and q to yield a new polynomial
    public static Polynomial operator *(Polynomial p, Polynomial q)
    {
        //create the new empty (to be filled) returning polynomial
        Polynomial ret = new Polynomial();

        //Reference to 2 new nodes
        Node <Term> n, m;

        //Node 1 points to front of polynomial p
        n = p.front.next;

        //create nested loop so for each term in p, multiply but each term in q
        while (n != null)
        {
            //Node 2 points to front of polynomial q
            m = q.front.next;
            while (m != null)
            {
                //Multiply Coefficients
                double c = n.item.Coefficient * m.item.Coefficient;
                //Add Exponents
                int e = n.item.Exponent + m.item.Exponent;

                //Defaults exponent to 255 if it becomes greater
                if (e > 255)
                {
                    System.Console.WriteLine("Exponent Above Range (255), setting to 255.");
                    e = 255;
                }
                //create new term and add it to the return polynomial
                ret.AddTerm(new Term(c, (byte)e));

                //Move to next node
                m = m.next;
            }
            //Mov to next node
            n = n.next;
        }

        ret.RemoveZeros();

        //Return new Polynomial
        return(ret);
    }
Пример #3
0
    // Multiplies the given polynomials p and q to yield a new polynomial
    public static Polynomial operator *(Polynomial p, Polynomial q)
    {
        Polynomial  z            = new Polynomial();
        Node <Term> currentPNode = p.front; //needs two pointers to multiply the two polynomials together
        Node <Term> currentQNode = q.front;

        while (currentPNode != null)
        {
            while (currentQNode != null)
            {
                double newCoefficient = currentPNode.Item.Coefficient * currentQNode.Item.Coefficient;   //Multiplying every combination of coefficients of p & q together.
                byte   newExponent    = (byte)(currentPNode.Item.Exponent + currentQNode.Item.Exponent); //Adding every combination of exponents of p & q together. Adding bytes together becomes an int, therefore you must explicitly cast to a byte.
                Term   newTerm        = new Term(newCoefficient, newExponent);                           // Creates new Term with previous values
                z.AddTerm(newTerm);                                                                      // Adds newTerm to Polynomial z
                currentQNode = currentQNode.Next;                                                        // Iterates over to next Node in Polynomial q
            }
            currentPNode = currentPNode.Next;                                                            // Iterates over to next Node in Polynomial p
        }
        return(z);
    }
Пример #4
0
    //Multiplies the given polynomials p and q to yield a new polynomial
    public static Polynomial operator *(Polynomial p, Polynomial q)
    {
        Polynomial  result = new Polynomial();
        Node <Term> currentP = p.front.Next, currentQ = q.front.Next;

        while (currentP != null)
        {
            Polynomial temporary = new Polynomial();
            while (CurrentQ != null)
            {
                Term newTerm = new Term(currentP.Item.Coefficient * currentQ.Item.Coefficient, (byte)(currentP.Item.Exponent + currentQ.Item.Exponent));
                temporary.AddTerm(newTerm);

                currentQ = currentQ.Next;
            }

            result = result + temporary;

            currentP = currentP.Next;
            currentQ = q.front.Next;
        }
        return(result);
    }
Пример #5
0
        //Multiplies polynomials p and q together to produce a new polynomial
        public static Polynomial operator *(Polynomial p, Polynomial q)
        {
            Polynomial r = new Polynomial(); //makes the new resultant polynomial
            Term       h;
            Polynomial tempP, tempQ;         //used to make copies of p and q

            tempP = (Polynomial)p.Clone();   //clones p
            tempQ = (Polynomial)q.Clone();   //clones q
            double tempCoeff;
            int    tempExpo;
            int    i = 0, j = 0;

            //makes sure that both of the chosen polynomials are valid polynomials with terms in them
            if (tempP.count == 0 || tempQ.count == 0)
            {
                return(null);
            }

            //loops through all of the terms in both polynomials being multiplied, first starting with the first term in polynomial p and multiplying it with each
            while (i <= (tempP.count - 1))
            {
                while (j <= (tempQ.count - 1))
                {
                    tempCoeff = (tempP.P[i].Coefficient) * (tempQ.P[j].Coefficient);
                    tempExpo  = (tempP.P[i].Exponent) + (tempQ.P[j].Exponent);
                    h         = new Term(tempCoeff, tempExpo); // creates a new term h that is the product of of two terms of the polynomial multiplied together
                    r.AddTerm(h);                              //adds the term to the resultant polynomial
                    j++;                                       //goes to the next term in Q
                }
                j = 0;                                         //restarts to the start of Q so that each of P's terms are multiplied by each of Q's terms
                i++;                                           //moves to the next term in P
            }


            return(r);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the coefficent for the first term in the polynomial");
            double coeff = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Please enter the exponent for the first term in the polynmial");
            int exp = Convert.ToInt32(Console.ReadLine());

            Term Term1 = new Term(coeff, exp);


            Console.WriteLine("Please enter the value at which you would like your polynomail evaluated at");
            double y = Convert.ToDouble(Console.ReadLine());



            Console.WriteLine("The current term in the polynomial is: " + Term1.ToString());


            Console.WriteLine("This term evaluates to: " + Term1.Evaluate(y));

            Term test2 = new Term(-2.2, 2);
            Term test1 = new Term(1.1, 1);
            Term test3 = new Term(3.3, 3);

/*
 *          Node<Term> p;
 *
 *          p = new Node<Term>();
 *
 *          Polynomial<Term> L1;
 *
 *          L1 = new Polynomial<Term>();
 *
 *          L1.AddTerm(test1, 1);
 *
 *          Console.WriteLine("I will now try to print out L1");
 *          L1.Print();
 *
 */



            Polynomial Poly1 = new Polynomial(); // Create a new polynmial from the array version

            Poly1.AddTerm(test2);                // Add three terms to poly 1
            Poly1.AddTerm(test1);
            Poly1.AddTerm(test3);

            Term test4 = new Term(4.4, 4); // create a 4th term to add to poly 1

            Poly1.AddTerm(test4);          // Add the 4th term to poly1

            Poly1.Print();                 // Print out poly1 to show that poly 1 was created and the terms were added to it


            Console.WriteLine("Please enter the value at which you would like to be evaluate with"); //  Get user input for the polynomial to be evaluated at
            double z = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("This term evaluates to: " + Poly1.Evaluate(z)); // Evalute Poly1 at the user inputted value



            Term addtest1 = new Term(5.5, 5); // create new terms to be added to a new polynomial for the operation override testing
            Term addtest2 = new Term(6.6, 6);
            Term addtest3 = new Term(1.5, 2);

            Polynomial Poly2 = new Polynomial(); // create the second polynomail

            Poly2.AddTerm(addtest2);             // add terms to poly 2
            Poly2.AddTerm(addtest1);
            Poly2.AddTerm(addtest3);



            Poly2.Print(); // print out poly2
            Console.WriteLine("\n");

            Polynomial Poly3 = new Polynomial(); // create a polynomail to hold the new polynomail that will occur from + operations

            Poly3 = Poly1 + Poly2;               // add poly 1 and 2 and store the new polynomial in poly 3

            Poly3.Print();                       // print out poly 3


            Console.WriteLine("\n");
            Polynomial Poly4 = new Polynomial(); // create a new polynomial to hold the multiplication of poly1 and poly 2

            Poly4 = Poly1 * Poly2;
            Poly4.Print();
        }
Пример #7
0
    public static Polynomials CreatePolynomial(Polynomials P)
    {
        string coefficientInput, exponentInput = null;
        string userInput  = null;
        char   userOutput = 'F';
        double newCoefficient;
        byte   newExponent;

        Polynomial newPoly = new Polynomial();

        do
        {
            Console.WriteLine("'A'dd a term to the polynomial. \n 'F'inish the polynomial.");
            Console.Write("Please enter an action. >> ");
            userInput = Console.ReadLine();

            while (!char.TryParse(userInput, out userOutput))
            {
                Console.Write("Invalid input. Please enter a single character. >> ");
                userInput = Console.ReadLine();
            }

            switch (char.ToUpper(userOutput))
            {
            case 'A':
            {
                Console.Write("Enter a value for the coefficient. >> ");
                coefficientInput = Console.ReadLine();

                while (!double.TryParse(coefficientInput, out newCoefficient))
                {
                    Console.Write("Input invalid. Please enter a double. >> ");
                    coefficientInput = Console.ReadLine();
                }

                Console.Write("Enter a value for the exponent. >> ");
                exponentInput = Console.ReadLine();

                while (!byte.TryParse(exponentInput, out newExponent))
                {
                    Console.Write("Input invalid. Please enter a number between 0 and 255. >> ");
                    exponentInput = Console.ReadLine();
                }

                Term newTerm = new Term(newCoefficient, newExponent);
                newPoly.AddTerm(newTerm);
                break;
            }

            case 'F':
            {
                P.Insert(newPoly);
                return(P);
            }

            default:
            {
                Console.WriteLine("Error, please input a valid command.");
                Console.WriteLine();
                break;
            }
            }
        } while (char.ToUpper(userOutput) != 'F');
        return(P);
    }
Пример #8
0
        static void Main(string[] args)
        {
            Test17();
            return;

            Console.WriteLine("_________________TEST 0__________________________");
            Test0();
            Console.WriteLine("_________________TEST 1__________________________");
            Test1();
            Console.WriteLine("_________________TEST 3__________________________");
            Test3();
            Console.WriteLine("_________________TEST 4__________________________");
            Test4();
            return;

            Ring r = new Ring(Field.Real, new string[] { "x", "y", "z" });

            r.FixOrder(new string[] { "x", "y", "z" });

            Polynomial f1 = new Polynomial(r);

            f1.AddTerm(1, new int[] { 0, 1, 2 });
            f1.AddTerm(1, new int[] { 0, 0, 2 });

            Polynomial f2 = new Polynomial(r);

            f2.AddTerm(1, new int[] { 3, 1, 0 });
            f2.AddTerm(1, new int[] { 1, 0, 0 });
            f2.AddTerm(1, new int[] { 0, 1, 0 });
            f2.AddTerm(1, new int[] { 0, 0, 0 });

            Polynomial f3 = new Polynomial(r);

            f3.AddTerm(1, new int[] { 0, 0, 1 });
            f3.AddTerm(1, new int[] { 2, 0, 0 });
            f3.AddTerm(1, new int[] { 0, 3, 0 });

            Ideal I = new Ideal(new Polynomial[] { f1, f2, f3 }, r);


            Console.WriteLine("__________Gröbner Basis__________________");
            var gb = I.GröbnerBasis();

            foreach (var p in gb)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("_________Minimal Gröbner Basis___________________");

            var minimal = I.MinimalGröbnerBasis();

            foreach (var p in minimal)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("______________Reduced Gröbner Basis______________");


            var reduced = I.ReducedGrobnerBasis();

            foreach (var p in reduced)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("__________Membership__________________");
            Console.WriteLine(I.Member(f1));
            Console.Read();
        }
Пример #9
0
        static void Test9()
        {
            Ring ring = new Ring(Field.Real, new string[] { "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8" });

            ring.FixOrder(new string[] { "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8" });


            Polynomial f1 = new Polynomial(ring);

            f1.AddTerm(1, new int[] { 3, 0, 0, 0, 0, 0, 0, 0 });
            f1.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f2 = new Polynomial(ring);

            f2.AddTerm(1, new int[] { 0, 3, 0, 0, 0, 0, 0, 0 });
            f2.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f3 = new Polynomial(ring);

            f3.AddTerm(1, new int[] { 0, 0, 3, 0, 0, 0, 0, 0 });
            f3.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f4 = new Polynomial(ring);

            f4.AddTerm(1, new int[] { 0, 0, 0, 3, 0, 0, 0, 0 });
            f4.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f5 = new Polynomial(ring);

            f5.AddTerm(1, new int[] { 0, 0, 0, 0, 3, 0, 0, 0 });
            f5.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f6 = new Polynomial(ring);

            f6.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 3, 0, 0 });
            f6.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f7 = new Polynomial(ring);

            f7.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 3, 0 });
            f7.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f8 = new Polynomial(ring);

            f8.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 0, 3 });
            f8.AddTerm(-1, new int[] { 0, 0, 0, 0, 0, 0, 0, 0 });

            Polynomial f9 = new Polynomial(ring);

            f9.AddTerm(1, new int[] { 2, 0, 0, 0, 0, 0, 0, 0 });
            f9.AddTerm(1, new int[] { 1, 1, 0, 0, 0, 0, 0, 0 });
            f9.AddTerm(1, new int[] { 0, 2, 0, 0, 0, 0, 0, 0 });


            Polynomial f10 = new Polynomial(ring);

            f10.AddTerm(1, new int[] { 0, 2, 0, 0, 0, 0, 0, 0 });
            f10.AddTerm(1, new int[] { 0, 1, 1, 0, 0, 0, 0, 0 });
            f10.AddTerm(1, new int[] { 0, 0, 2, 0, 0, 0, 0, 0 });

            Polynomial f11 = new Polynomial(ring);

            f11.AddTerm(1, new int[] { 0, 0, 2, 0, 0, 0, 0, 0 });
            f11.AddTerm(1, new int[] { 0, 0, 1, 1, 0, 0, 0, 0 });
            f11.AddTerm(1, new int[] { 0, 0, 0, 2, 0, 0, 0, 0 });


            Polynomial f12 = new Polynomial(ring);

            f12.AddTerm(1, new int[] { 0, 0, 0, 2, 0, 0, 0, 0 });
            f12.AddTerm(1, new int[] { 0, 0, 0, 1, 1, 0, 0, 0 });
            f12.AddTerm(1, new int[] { 0, 0, 0, 0, 2, 0, 0, 0 });

            Polynomial f13 = new Polynomial(ring);

            f13.AddTerm(1, new int[] { 0, 0, 0, 0, 2, 0, 0, 0 });
            f13.AddTerm(1, new int[] { 0, 0, 0, 0, 1, 1, 0, 0 });
            f13.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 2, 0, 0 });

            Polynomial f14 = new Polynomial(ring);

            f14.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 2, 0, 0 });
            f14.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 1, 1, 0 });
            f14.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 2, 0 });

            Polynomial f15 = new Polynomial(ring);

            f15.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 2, 0 });
            f15.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 1, 1 });
            f15.AddTerm(1, new int[] { 0, 0, 0, 0, 0, 0, 0, 2 });

            Ideal I = new Ideal(new Polynomial[] { f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15 }, ring);

            Console.WriteLine("__________Gröbner Basis__________________");
            var gb = I.GröbnerBasis();

            foreach (var p in gb)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("_________Minimal Gröbner Basis___________________");

            var minimal = I.MinimalGröbnerBasis();

            foreach (var p in minimal)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("______________Reduced Gröbner Basis______________");


            var reduced = I.ReducedGrobnerBasis();

            foreach (var p in reduced)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("__________Membership__________________");
            Console.WriteLine(I.Member(f1));
        }
Пример #10
0
        //Graph with 4 nodes and not 3-colorable
        static void Test8()
        {
            Ring r = new Ring(Field.Real, new string[] { "x", "y", "z", "t" });

            r.FixOrder(new string[] { "x", "y", "z", "t" });


            Polynomial f1 = new Polynomial(r);

            f1.AddTerm(1, new int[] { 3, 0, 0, 0 });
            f1.AddTerm(-1, new int[] { 0, 0, 0, 0 });


            Polynomial f2 = new Polynomial(r);

            f2.AddTerm(1, new int[] { 0, 3, 0, 0 });
            f2.AddTerm(-1, new int[] { 0, 0, 0, 0 });

            Polynomial f3 = new Polynomial(r);

            f3.AddTerm(1, new int[] { 0, 0, 3, 0 });
            f3.AddTerm(-1, new int[] { 0, 0, 0, 0 });

            Polynomial f4 = new Polynomial(r);

            f4.AddTerm(1, new int[] { 0, 2, 0, 0 });
            f4.AddTerm(1, new int[] { 0, 1, 1, 0 });
            f4.AddTerm(1, new int[] { 0, 0, 2, 0 });

            Polynomial f5 = new Polynomial(r);

            f5.AddTerm(1, new int[] { 2, 0, 0, 0 });
            f5.AddTerm(1, new int[] { 1, 0, 1, 0 });
            f5.AddTerm(1, new int[] { 0, 0, 2, 0 });

            Polynomial f6 = new Polynomial(r);

            f6.AddTerm(1, new int[] { 2, 0, 0, 0 });
            f6.AddTerm(1, new int[] { 1, 1, 0, 0 });
            f6.AddTerm(1, new int[] { 0, 2, 0, 0 });



            Polynomial f7 = new Polynomial(r);

            f7.AddTerm(1, new int[] { 0, 0, 0, 3 });
            f7.AddTerm(-1, new int[] { 0, 0, 0, 0 });

            Polynomial f8 = new Polynomial(r);

            f8.AddTerm(1, new int[] { 2, 0, 0, 0 });
            f8.AddTerm(1, new int[] { 1, 0, 0, 1 });
            f8.AddTerm(1, new int[] { 0, 0, 0, 2 });

            Polynomial f9 = new Polynomial(r);

            f9.AddTerm(1, new int[] { 0, 0, 2, 0 });
            f9.AddTerm(1, new int[] { 0, 0, 1, 1 });
            f9.AddTerm(1, new int[] { 0, 0, 0, 2 });


            Polynomial f10 = new Polynomial(r);

            f10.AddTerm(1, new int[] { 0, 2, 0, 0 });
            f10.AddTerm(1, new int[] { 0, 1, 0, 1 });
            f10.AddTerm(1, new int[] { 0, 0, 0, 2 });


            Ideal I = new Ideal(new Polynomial[] { f1, f2, f6, f3, f9, f4, f5, f7, f10, f8 }, r);

            Console.WriteLine("__________Generator_________________");
            var sys = I.GeneratorSet;

            foreach (var p in sys)
            {
                Console.WriteLine(p);
            }


            Console.WriteLine("__________Gröbner Basis__________________");
            var gb = I.GröbnerBasis();

            foreach (var p in gb)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("_________Minimal Gröbner Basis___________________");

            var minimal = I.MinimalGröbnerBasis();

            foreach (var p in minimal)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("______________Reduced Gröbner Basis______________");


            var reduced = I.ReducedGrobnerBasis();

            foreach (var p in reduced)
            {
                Console.WriteLine(p);
            }

            Console.WriteLine("__________Membership__________________");
            Console.WriteLine(I.Member(f1));
        }
Пример #11
0
        static void Main(string[] args)
        {
            try
            {
                //Creates a new list, S, of Polynomials
                Polynomials S = new Polynomials();

                //Data members
                double coef;
                int    count = 0;

                //Data members for deciding what the program will do the make sure it's a number
                int pick;
                int exp;

                //Will continue the program until the user inputs "6"
                do
                {
                    //Detailing all the options the user has
                    Console.WriteLine("Welcome to the Polynomial program, please read all the options before entering an input");
                    Console.WriteLine("Here is the list of polynomials");
                    S.Print(); // prints the list so the user knows what is already in the list
                    Console.WriteLine("Press 1 if you would like to insert a polynomial into the list printed above");
                    Console.WriteLine("Press 2 if you would like to add two polynomials together and insert them into the list");
                    Console.WriteLine("Press 3 if you would like to multiply two polynomials together and insert it into the list");
                    Console.WriteLine("Press 4 if you would like to delete any polynomials from the list above");
                    Console.WriteLine("Press 5 if you would like to evaluate a polynomial from the list, provided that you enter x");
                    Console.WriteLine("Press 6 if you would like to close this console");
                    pick = Convert.ToInt32(Console.ReadLine());

                    //If the user tries to enter a letter instead of a number, it gets an error
                    if (pick < 1 || pick > 6)
                    {
                        Console.WriteLine("Sorry, you must pick an option between 1 and 6");
                        Console.WriteLine("");
                    }


                    //If the  user picks the 1st option, it does this
                    if (pick == 1)
                    {
                        //creates a new list to keep track of what the user has entered
                        Polynomial b = new Polynomial();

                        int hMany;
                        int i = 0;

                        //asks the user how many terms they want to enter
                        Console.WriteLine("How many terms do you want to add to?");
                        hMany = Convert.ToInt32(Console.ReadLine());

                        //Asks the user what they want the coefficient and exponent to be until i reaches hMany
                        while (i < hMany)
                        {
                            Console.WriteLine("Please enter the coefficient");
                            coef = Convert.ToDouble(Console.ReadLine());

                            Console.WriteLine("Please enther the exponent");
                            exp = Convert.ToInt32(Console.ReadLine());

                            //takes the coefficient and exponents, adds them to the term
                            Term t = new Term(coef, exp);
                            b.AddTerm(t);

                            i++;
                            count++;
                        }
                        //Inserts the newly added polynomials into S
                        S.Insert(b);
                    }
                    //Tells the program what to do if the user enters 2
                    else if (pick == 2)
                    {
                        //Checks to see if there are any polynomials to add together in the first place
                        if (count < 2)
                        {
                            Console.WriteLine("There are no polynomials in the list");
                            Console.WriteLine("");
                        }
                        //Asks the user which polynomails he wants to use, then it adds them together and inserts it in to the list
                        else
                        {
                            Console.WriteLine("Here is the list of Polynomials");
                            S.Print();

                            int Ret1, Ret2;


                            Console.WriteLine("Please enter the index of the first polynomial you would like to use ");
                            Ret1 = Convert.ToInt32(Console.ReadLine());

                            Console.WriteLine("Please enter the index of the second polynomial you would like to use");
                            Ret2 = Convert.ToInt32(Console.ReadLine());


                            Polynomial answer = S.Retrieve(Ret1) + S.Retrieve(Ret2);


                            Console.WriteLine("Your new polynomial is:");
                            answer.Print();
                            Console.WriteLine("");


                            S.Insert(answer);
                        }
                    }
                    //Tells the program what to do if the user wants to multiply
                    else if (pick == 3)
                    {
                        //Checks to see if there are polynomials to multiply together
                        if (count < 2)
                        {
                            Console.WriteLine("There are no polynomials in the list");
                            Console.WriteLine("");
                        }
                        //Asks the user for which polynomials it wants to multiply together and then inserts it
                        else
                        {
                            Console.WriteLine("Here is the list of Polynomials");
                            S.Print();

                            int Mul1, Mul2;

                            Console.WriteLine("Please enter the index of the first polynomial you would like to use ");
                            Mul1 = Convert.ToInt32(Console.ReadLine());

                            Console.WriteLine("Please enter the index of the second polynomial you would like to use");
                            Mul2 = Convert.ToInt32(Console.ReadLine());


                            //inserts the newly multiplied together polynomial
                            Polynomial result = S.Retrieve(Mul1) * S.Retrieve(Mul2);
                            Console.WriteLine("Your new polynomial is:");
                            result.Print();
                            Console.WriteLine("");
                            S.Insert(result);
                        }
                    }
                    //Tells the program what to do if the user wants to delete polynomials
                    else if (pick == 4)
                    {
                        //checks to see there are any polynomials to add together
                        if (count == 0)
                        {
                            Console.WriteLine("There are no polynomials in the list");
                            Console.WriteLine("");
                        }
                        //Asks the user which polynomial they want to delete and then deletes it
                        else
                        {
                            int del;

                            Console.WriteLine("Here is the list of Polynomials");
                            S.Print();

                            Console.WriteLine("Please enter the index of the polynomial you would like to delete");
                            del = Convert.ToInt32(Console.ReadLine());

                            S.Delete(del);

                            Console.WriteLine("Deleted");
                            Console.WriteLine("");
                        }
                    }
                    //Tells the program what to do if the user wants to evaluate a term
                    else if (pick == 5)
                    {
                        //Checks to see if there are any polynomials to evaluate
                        if (count == 0)
                        {
                            Console.WriteLine("There are no polynomials in the list");
                            Console.WriteLine();
                        }
                        //Asks the user which polynomial they want to evaluate, what value for x and then answers them
                        else
                        {
                            int    Ret;
                            double x;

                            Console.WriteLine("Please enter the index of the polynomial you would like to evaluate");
                            Ret = Convert.ToInt32(Console.ReadLine());

                            Console.WriteLine("Please enter the x value you would like to use");
                            x = Convert.ToInt32(Console.ReadLine());

                            double answer;
                            answer = S.Retrieve(Ret).Evaluate(x);

                            Console.WriteLine("Your answer is:");
                            Console.WriteLine(answer);
                            Console.WriteLine("");
                        }
                    }
                } while (pick != 6);
            }
            catch (ArgumentException e) { Console.WriteLine(e.Message); }
        }
Пример #12
0
        private Tuple <Polynomial[], Polynomial [], bool> Check(int k, CancellationToken token, IProgress <int> progress)
        {
            var graph     = graphEditor.Graph;
            var variables = graph.Nodes.Select(x => x.Label.Text).ToArray();

            foreach (String s in variables)
            {
                Console.WriteLine(s);
            }
            Ring ring = new Ring(Field.Real, variables);

            ring.FixOrder(variables);

            List <Polynomial> generator = new List <Polynomial>();
            List <Node>       nodes     = graph.Nodes.ToList();

            float currentProgress = 0;

            for (int i = 0; i < nodes.Count; i++)
            {
                int[] powerProduct = new int[graph.NodeCount];
                powerProduct[i] = k;
                Polynomial first = new Polynomial(ring);
                first.AddTerm(1, powerProduct);
                first.AddTerm(-1, new int[nodes.Count]);

                generator.Add(first);

                var node = nodes[i];
                foreach (Edge edge in node.Edges)
                {
                    Polynomial second = new Polynomial(ring);

                    int index = nodes.IndexOf(edge.TargetNode);
                    if (index == i)
                    {
                        continue;
                    }
                    powerProduct    = new int[nodes.Count];
                    powerProduct[i] = k - 1;
                    second.AddTerm(1, powerProduct);
                    // xi^k−1 + xj^k−2 * xj +· · ·+xi* xj^k−2 + xj^k−1
                    for (int j = k - 2; j > 0; j--)
                    {
                        powerProduct        = new int[nodes.Count];
                        powerProduct[i]     = j;
                        powerProduct[index] = k - 1 - j;
                        second.AddTerm(1, powerProduct);
                    }
                    powerProduct        = new int[nodes.Count];
                    powerProduct[i]     = 0;
                    powerProduct[index] = k - 1;
                    second.AddTerm(1, powerProduct);

                    generator.Add(second);
                }
                currentProgress = i / nodes.Count * 100;
                progress.Report((int)currentProgress);
            }


            Ideal      ideal = new Ideal(generator.ToArray(), ring);
            Polynomial one   = new Polynomial(ring);

            one.AddTerm(1, new int[nodes.Count]);
            var reducedBasis = ideal.ReducedGrobnerBasis(token, progress);

            return(new Tuple <Polynomial[], Polynomial[], bool> (ideal.GeneratorSet, reducedBasis, !reducedBasis.Any(i => i.Equals(one))));
        }