/// <summary>
        /// Adds a factor.
        /// eg. {2, x^2}, {4} + {-3, y}, {2}
        /// </summary>
        /// <param name="factor"></param>
        public void Add(Factor factor)
        {
            Factor rFactor = this;
            Factor lFactor = factor;
            Factor result = new Factor ();

            if (lFactor.Terms.Count > 0)
                for (int i = lFactor.Terms.Count - 1; i >= 0; i--) {
                    bool done = false;

                    if (rFactor.Terms.Count > 0)
                        for (int j = rFactor.Terms.Count - 1; j >= 0; j--) {
                            if (lFactor.Terms [i].VariablesToString ().Equals (rFactor.Terms [j].VariablesToString ())) {
                                lFactor.Terms [i].Coefficient += rFactor.Terms [j].Coefficient;
                                if (lFactor.Terms [i].Coefficient != 0)
                                    result.Add (lFactor.Terms [i]);
                                lFactor.Terms.RemoveAt (i);
                                rFactor.Terms.RemoveAt (j);
                                done = true;
                                break;
                            }
                        }

                    if (!done) {
                        result.Add (lFactor.Terms [i]);
                        lFactor.Terms.RemoveAt (i);
                    }
                }

            if (rFactor.Terms.Count > 0)
                foreach (var rTerm in rFactor.Terms) {
                    result.Add (rTerm);
                }

            this.Terms = result.Terms;
        }
 // TermOp -> * SignedFactor TermOp
 // TermOp -> / SignedFactor TermOp
 // TermOp -> EPSILON
 Factor TermOp(Factor factor)
 {
     switch (lookahead.type) {
     case TokenType.MULTI:
         GetNextToken ();
         factor.Multiply (SignedFactor ());
         return TermOp (factor);
     case TokenType.DEV:
         GetNextToken ();
         factor.Divide (SignedFactor ());
         return TermOp (factor);
     default:
         return factor;
     }
 }
 //SumOp -> + Term SumOp
 //SumOp -> - Term SumOp
 //SumOp -> EPSILON
 Factor SumOp(Factor t)
 {
     Factor t2;
     switch (lookahead.type) {
     case TokenType.ADD:
         GetNextToken ();
         t2 = Term ();
         t.Add (t2);
         return SumOp (t);
     case TokenType.SUB:
         GetNextToken ();
         t2 = Term ();
         t.Subtract (t2);
         return SumOp (t);
     default:
         return t;
     }
 }
 public Factor(Factor factor)
 {
     Terms = factor.Terms;
 }
        /// <summary>
        /// Subtracts a factor.
        /// eg. {2, x^2}, {4} - {-3, y}, {2}
        /// </summary>
        /// <param name="factor"></param>
        public void Subtract(Factor factor)
        {
            foreach (var f in factor.Terms)
                f.Coefficient *= -1;

            Add (factor);
        }
        /// <summary>
        /// Multiply a factor by a factor.
        /// (2x + 1) * (x + 3)
        /// </summary>
        /// <param name="factor"></param>
        public void Multiply(Factor factor)
        {
            Factor result = new Factor ();
            Factor rTerm;

            foreach (var f in Terms) {
                rTerm = factor.Clone ();
                rTerm.Multiply (f);
                result.Add (rTerm);
            }

            Terms = result.Terms;
        }
        /// <summary>
        /// Divide by a factor.
        /// eg. (2x + 1) / (3x + 2)
        /// </summary>
        /// <param name="t">The factor to be devided by.</param>
        public void Divide(Factor t)
        {
            Factor result = new Factor ();
            Factor rTerm;

            foreach (var factor in Terms) {
                rTerm = t.Clone ();
                rTerm.Divide (factor);
                result.Add (rTerm);
            }

            Terms = result.Terms;
        }
 public Factor Clone()
 {
     Factor t = new Factor ();
     foreach (var factor in Terms) {
         t.Terms.Add (factor.Clone ());
     }
     return t;
 }