Exemplo n.º 1
0
 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));
     }
 }
Exemplo n.º 2
0
 public static Lst <Monomial> Cons(Monomial monomial, Lst <Monomial> monomials)
 {
     if (monomial.IsZero())
     {
         return(monomials);
     }
     else
     {
         return(new Cons <Monomial>(monomial, monomials));
     }
 }
Exemplo n.º 3
0
 public static Lst <ReactionValue> ToReactions(SpeciesFlow variable, Monomial monomial, Lst <ReactionValue> rest, Style style)
 {
     if (monomial.IsZero())
     {
         return(rest);
     }
     else
     {
         int decide = Polynomial.DecideNonnegative(monomial.coefficient, style);
         if (decide == 1)   // positive monomials
         {
             RateValue rate;
             if (monomial.coefficient is NumberFlow num)
             {
                 rate = new MassActionNumericalRate(num.value);
             }
             else
             {
                 rate = new MassActionFlowRate(monomial.coefficient);
             }
             return(new Cons <ReactionValue>(
                        new ReactionValue(
                            Factor.ToList(monomial.factors),
                            Factor.ToList(Factor.Product(new Factor(variable, 1), monomial.factors)),
                            rate),
                        rest));
         }
         else if (decide == -1)     // negative monomials
         {
             RateValue rate;
             if (monomial.coefficient is NumberFlow num)
             {
                 rate = new MassActionNumericalRate(-num.value);
             }
             else
             {
                 rate = new MassActionFlowRate(OpFlow.Op("-", monomial.coefficient).Normalize(style));
             }
             return(new Cons <ReactionValue>(
                        new ReactionValue(
                            Factor.ToList(monomial.factors),
                            Factor.ToList(Factor.Quotient(monomial.factors, new Factor(variable, 1), style)),
                            rate),
                        rest));
         }
         else
         {
             throw new Error("MassActionCompiler: aborted because it cannot determine the sign of this expression: " + monomial.coefficient.Format(style));
         }
     }
 }