public Term Clone() { Term f = new Term (); f.coefficient = coefficient; foreach (var variable in Variables) { f.Variables.Add (variable.Key, variable.Value); } return f; }
/// <summary> /// Adds a term. /// eg. {2, x^2}, {4} + {-3, y} /// </summary> /// <param name="f">the term to be added.</param> public void Add(Term term) { foreach (var t in Terms) { if (t.VariablesToString ().Equals (term.VariablesToString ())) { t.Coefficient += term.Coefficient; return; } } Terms.Add (term); }
/// <summary> /// Divides by a Term result is stored in current term. /// eg (2x / 3y) /// </summary> /// <param name="f">The term to be devided.</param> public void Divide(Term f) { Coefficient /= f.Coefficient; if (Coefficient == 0) { Variables = new SortedDictionary<string, decimal> (); return; } foreach (var variable in f.Variables) AddVariable (variable.Key, variable.Value * -1); }
public Term(Term f) { Multiply (f); }
public Factor(Term term) { Terms.Add (term); }
/// <summary> /// Substracts a term. /// eg. {2, x^2}, {4} - {-3, y} /// </summary> /// <param name="term">The term to be substracted.</param> public void Subtract(Term term) { term.Coefficient *= -1; Add (term); }
/// <summary> /// Multiply a Factor by a Term. /// eg. (2x) * (2x + 3y) /// </summary> /// <param name="term">The term to be multiplied.</param> public void Multiply(Term term) { foreach (var factor in Terms) { factor.Multiply (term); } }
/// <summary> /// Divide by a term. /// eg 2x / (2x + 1) /// </summary> /// <param name="term">The term to be devided by.</param> public void Divide(Term term) { Term temp; foreach (var factor in Terms) { temp = term.Clone (); temp.Divide (factor); factor.Coefficient = temp.Coefficient; factor.Variables = temp.Variables; } }