public static Lst <Monomial> Substitute(Lst <Monomial> monomials, Lst <Subst> substs, Style style) { if (monomials is Cons <Monomial> cons) { return(Monomial.Sum(Substitute(cons.head, substs, style), Substitute(cons.tail, substs, style), style)); } else { return(Monomial.nil); } }
public static Lst <Monomial> ToMonomials(Flow flow, Style style) { if (flow is NumberFlow num) { return(Monomial.Singleton(new Monomial(new NumberFlow(num.value), style))); } else if (flow is SpeciesFlow species) { return(Monomial.Singleton(new Monomial(new Factor(species)))); } else if (flow is OpFlow op) { if (op.arity == 1) { if (op.op == "-") { return(ToMonomials(op.args[0], style).Map((Monomial m) => m.Product(Flow.minusOne, style))); } } else if (op.arity == 2) { if (op.op == "+") { return(Monomial.Sum(ToMonomials(op.args[0], style), ToMonomials(op.args[1], style), style)); } else if (op.op == "-") { return(Monomial.Sum(ToMonomials(op.args[0], style), ToMonomials(op.args[1], style).Map((Monomial m) => m.Product(Flow.minusOne, style)), style)); } else if (op.op == "*") { return(Monomial.Product(ToMonomials(op.args[0], style), ToMonomials(op.args[1], style), style)); } else if (op.op == "^") { if (op.args[1] is NumberFlow exp && IsInt(exp.value) && (int)exp.value >= 0) { return(Monomial.Power(ToMonomials(op.args[0], style), (int)exp.value, style)); } else { throw new Error("Polynomial.ToMonomials: " + flow.Format(style)); } } else { throw new Error("Polynomial.ToMonomials: " + flow.Format(style)); } } }