public static Term Multiply(Term a, Term b) { List <Multiplier> multipliers = new List <Multiplier>(a.multipliers); foreach (Multiplier mul_b in b.multipliers) { int index = multipliers.FindIndex(delegate(Multiplier item){ return(item.name == mul_b.name); }); if (index == -1) { multipliers.Add(mul_b); } else { Multiplier mul_a = multipliers[index]; mul_a.power += mul_b.power; multipliers[index] = mul_a; } } return(new Term(a.count * b.count, multipliers, a.dimension + b.dimension)); }
public static Term Parse(string s) { int sign = 1; List <Multiplier> multipliers = new List <Multiplier>(); string dimension = ""; int i = 0; while (i < s.Length) { char c = s[i]; if (c == '-') { sign = -1; } else { bool is_dimension = (c == 'x' || c == 'y' || c == 'z'); if (is_dimension) { dimension = s.Substring(i, s.Length - i); break; } else { Multiplier multiplier = new Multiplier(); multiplier.power = 1; multiplier.name = c.ToString(); multipliers.Add(multiplier); } } ++i; } return(new Term(sign, multipliers, dimension)); }