public static Polynomial operator *(Polynomial a, Polynomial b) { uint maxPower = a.MaxPower + b.MaxPower; List<Mononomial> mononomials = new List<Mononomial>(); foreach (var mononomialA in a.Mononomials) { foreach (var mononomialB in b.Mononomials) { decimal tempCoeff = mononomialA.Coefficient; tempCoeff *= mononomialB.Coefficient; uint tempPow = mononomialA.Power + mononomialB.Power; mononomials.Add(new Mononomial(tempCoeff, tempPow)); } } mononomials = mononomials.Where(x => x != null).ToList(); string literal = (a.Literal == b.Literal) ? b.Literal : a.Literal + b.Literal; Polynomial result = new Polynomial(literal); result.AddMononomials(mononomials); return result; }
public static Polynomial operator +(Polynomial a, Polynomial b) { if (!a.HasAnyMononomials() || !b.HasAnyMononomials()) { throw new ArgumentNullException("polynomial", "Both polynomials must have at least one mononomial."); } uint maxPower = Math.Max(a.MaxPower, b.MaxPower); List <Mononomial> mononomials = new List <Mononomial>(); for (uint pow = 0; pow <= maxPower; pow++) { decimal tempCoeff = 0.0m; tempCoeff += a.Mononomials.Where(x => x.Power == pow).Select(x => x.Coefficient).ToArray().Sum(); tempCoeff += b.Mononomials.Where(x => x.Power == pow).Select(x => x.Coefficient).ToArray().Sum(); mononomials.Add(new Mononomial(tempCoeff, pow)); } mononomials = mononomials.Where(x => x != null).ToList(); string literal = (a.Literal == b.Literal) ? b.Literal : a.Literal + b.Literal; Polynomial result = new Polynomial(literal); result.AddMononomials(mononomials); return(result); }
public static Polynomial operator *(Polynomial a, Polynomial b) { uint maxPower = a.MaxPower + b.MaxPower; List <Mononomial> mononomials = new List <Mononomial>(); foreach (var mononomialA in a.Mononomials) { foreach (var mononomialB in b.Mononomials) { decimal tempCoeff = mononomialA.Coefficient; tempCoeff *= mononomialB.Coefficient; uint tempPow = mononomialA.Power + mononomialB.Power; mononomials.Add(new Mononomial(tempCoeff, tempPow)); } } mononomials = mononomials.Where(x => x != null).ToList(); string literal = (a.Literal == b.Literal) ? b.Literal : a.Literal + b.Literal; Polynomial result = new Polynomial(literal); result.AddMononomials(mononomials); return(result); }
public static Polynomial operator +(Polynomial a, Polynomial b) { if (!a.HasAnyMononomials() || !b.HasAnyMononomials()) { throw new ArgumentNullException("polynomial", "Both polynomials must have at least one mononomial."); } uint maxPower = Math.Max(a.MaxPower, b.MaxPower); List<Mononomial> mononomials = new List<Mononomial>(); for (uint pow = 0; pow <= maxPower; pow++) { decimal tempCoeff = 0.0m; tempCoeff += a.Mononomials.Where(x => x.Power == pow).Select(x => x.Coefficient).ToArray().Sum(); tempCoeff += b.Mononomials.Where(x => x.Power == pow).Select(x => x.Coefficient).ToArray().Sum(); mononomials.Add(new Mononomial(tempCoeff, pow)); } mononomials = mononomials.Where(x => x != null).ToList(); string literal = (a.Literal == b.Literal) ? b.Literal : a.Literal + b.Literal; Polynomial result = new Polynomial(literal); result.AddMononomials(mononomials); return result; }