public static Lst <Monomial> Substitute(Factor factor, Lst <Subst> substs, Style style) { Subst subst = Lookup(factor.variable, substs); if (subst == null) { return(Monomial.Singleton(new Monomial(factor))); } else { return(Monomial.Power(Monomial.Cons(new Monomial(new Factor(subst.plus)), Monomial.Singleton(new Monomial(Flow.minusOne, new Factor(subst.minus), style))), factor.power, style)); } }
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)); } } }