public static TermBase Create(Constant coeff, Variables variables) { if (coeff is ConstantZero) { return(new TermZero()); } if (variables.IsEmpty()) { return(TermConstant.Create(coeff)); } if (coeff is ConstantOne) { return(TermVariables.Create(variables)); } return(new Term(coeff, variables)); }
public static TermsBase Create(IEnumerable <TermBase> terms) { if (terms == null) { throw new ArgumentNullException(nameof(terms)); } var(constants, ones, variables, ts) = terms.OfType <TermConstant, TermOne, TermVariables, Term>(); var constant = TermConstant.Create(constants.Aggregate((Constant) new ConstantZero(), (c1, c2) => c1 + c2.Coeff) + ConstantWhole.Create(ones.Count())); var variable = variables.Select(t => (c: (Constant) new ConstantOne(), v: t.Variables)).Concat(ts.Select(t => (c: t.Coeff, v: t.Var))).ToLookup(t => t.v, t => t.c).Select(SumGroup); var lookup = variable.Concat(new[] { constant }); if (lookup.Count() == 1) { return(TermsSingle.Create(lookup.First())); } if (lookup.Count() < 1) { return(new TermsZero()); } return(new Terms(lookup)); }