public static Lst <Monomial> Sum(Monomial monomial, Lst <Monomial> monomials, Style style) { if (monomial.IsZero()) { return(monomials); } if (monomials is Cons <Monomial> cons) { if (cons.head.SameFactors(monomial)) { Flow sumCoeff = OpFlow.Op(cons.head.coefficient, "+", monomial.coefficient).Normalize(style); if (sumCoeff.IsNumber(0.0)) { return(cons.tail); } else { return(Monomial.Cons(new Monomial(sumCoeff, monomial.factors, style), cons.tail)); } } else { return(Monomial.Cons(cons.head, Sum(monomial, cons.tail, style))); } } else { return(Monomial.Singleton(monomial)); } }
public static Lst <Monomial> Substitute(Lst <Factor> factors, Lst <Subst> substs, Style style) { if (factors is Cons <Factor> cons) { return(Monomial.Product(Substitute(cons.head, substs, style), Substitute(cons.tail, substs, style), style)); } else { return(Monomial.Singleton(new Monomial(1.0))); } }
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)); } } }
public static (Lst <ODE> odes, Lst <Equation> eqs) FromCRN(CRN crn) { (SpeciesValue[] vars, Flow[] flows) = crn.MeanFlow(); Lst <ODE> odes = new Nil <ODE>(); for (int i = vars.Length - 1; i >= 0; i--) { odes = new Cons <ODE>(new ODE(new SpeciesFlow(vars[i].symbol), flows[i]), odes); } List <SpeciesValue> species = crn.sample.stateMap.species; Lst <Equation> eqs = new Nil <Equation>(); for (int i = species.Count - 1; i >= 0; i--) { eqs = new Cons <Equation>(new Equation(new SpeciesFlow(species[i].symbol), "id", new Lst <Monomial>[1] { Monomial.Singleton(new Monomial(crn.sample.stateMap.state.Mean(i))) }), eqs); } return(odes, eqs); }
public PolyODE(SpeciesFlow var, Monomial monomial, Split split) { this.var = var; this.poly = new Polynomial(Monomial.Singleton(monomial)); this.split = split; }
public Polynomial(Monomial monomial) { this.monomials = Monomial.Singleton(monomial); }
public static Lst <Monomial> Product(Lst <Monomial> monomials, Lst <Factor> factors, Style style) { return(Product(monomials, Monomial.Singleton(new Monomial(factors)), style)); }
public static Lst <Monomial> Product(Factor factor, Lst <Monomial> monomials, Style style) { return(Product(Monomial.Singleton(new Monomial(factor)), monomials, style)); }
public static Lst <Monomial> Product(Factor factor1, Factor factor2, Style style) { return(Product(factor1, Monomial.Singleton(new Monomial(factor2)), style)); }
public static Lst <Monomial> Product(Flow coefficient, Lst <Monomial> monomials, Style style) { return(Product(Monomial.Singleton(new Monomial(coefficient, style)), monomials, style)); }