示例#1
0
        protected Expression ProductRule(Expression L, IEnumerable <Expression> R)
        {
            if (R.Empty())
            {
                return(Visit(L));
            }

            bool Lx = L.DependsOn(x);
            bool Rx = R.DependsOn(x);

            if (Lx && Rx)
            {
                // Product rule.
                return(Sum.New(
                           Product.New(new Expression[] { Visit(L) }.Concat(R)),
                           Product.New(L, ProductRule(R.First(), R.Skip(1)))).Evaluate());
            }
            else if (!Lx)
            {
                // L is constant w.r.t. x.
                return(Product.New(L, ProductRule(R.First(), R.Skip(1))).Evaluate());
            }
            else
            {
                // R is constant w.r.t. x.
                return(Product.New(R.Append(Visit(L))).Evaluate());
            }
        }
示例#2
0
        public override Expression Visit(Expression E)
        {
            if (!E.DependsOn(t))
            {
                return(E / s);
            }

            return(base.Visit(E));
        }
示例#3
0
 protected override Expression VisitUnknown(Expression E)
 {
     if (E.Equals(x))
     {
         return(1);
     }
     else if (E.DependsOn(x))
     {
         return(rules.Transform(Call.D(E, x)));
     }
     else
     {
         return(0);
     }
 }
示例#4
0
 public override Expression Visit(Expression E)
 {
     if (x.Equals(E))
     {
         return(1);
     }
     else if (!E.DependsOn(x))
     {
         return(0);
     }
     else
     {
         return(base.Visit(E));
     }
 }
示例#5
0
        protected override Expression VisitPower(Power P)
        {
            Expression f = P.Left;
            Expression g = P.Right;

            if (g.DependsOn(x))
            {
                // f(x)^g(x)
                return(Product.New(P,
                                   Sum.New(
                                       Product.New(Visit(f), Binary.Divide(g, f)),
                                       Product.New(Visit(g), Call.Ln(f)))).Evaluate());
            }
            else
            {
                // f(x)^g
                return(Product.New(
                           g,
                           Power.New(f, Binary.Subtract(g, 1)),
                           Visit(f)).Evaluate());
            }
        }
示例#6
0
        private List <Arrow> Solve(IList <Expression> x)
        {
            List <Arrow> solutions = new List <Arrow>();

            for (int i = Math.Min(x.Count, equations.Count) - 1, _j = x.Count - 1; _j >= 0; --_j)
            {
                Expression j = x[_j];

                // While we still haven't reached a pivot row...
                while (i >= 0 && x.Take(_j).All(j2 => equations[i][j2].EqualsZero()))
                {
                    if (!equations[i][j].EqualsZero())
                    {
                        // Solve this row for the pivot.
                        Expression s = equations[i].Solve(j);
                        if (!s.DependsOn(x))
                        {
                            solutions.Add(Arrow.New(j, s));

                            // Remove the equation and unknown.
                            equations.RemoveAt(i--);
                            x.RemoveAt(_j);
                            if (!ReferenceEquals(x, unknowns))
                            {
                                unknowns.Remove(j);
                            }
                        }
                        break;
                    }
                    else
                    {
                        --i;
                    }
                }
            }

            return(solutions);
        }
示例#7
0
 public static Expression DependsOn(Expression f, Expression x)
 {
     return(Constant.New(f.DependsOn(x)));
 }
示例#8
0
 protected override bool IsConstant(Expression x)
 {
     return(!x.DependsOn(t));
 }