Exemplo n.º 1
0
 Expression DifferentiateSubtraction(Subtraction node)
 {
     return(new Subtraction
     {
         Left = Differentiate(node.Left),
         Right = Differentiate(node.Right)
     });
 }
Exemplo n.º 2
0
 void CompileSubtraction(Subtraction node)
 {
     CompileVisitor(node.Left);
     CompileVisitor(node.Right);
     rpnStack.Add(new SubtractionOperator());
 }
Exemplo n.º 3
0
        private static Expression SimplifySubtraction(Subtraction node)
        {
            /*Expression left = Simplify(node.Left);
             * Expression right = Simplify(node.Right);
             *
             * if (left.Type == ExpressionType.Float)
             * {
             *  Float nl = (Float)left;
             *  if (right.Type == ExpressionType.Float)
             *  {
             *      Float nr = (Float)right;
             *      return nl - nr;
             *  }
             *  if (nl.isZero())
             *  {
             *      return new Negation { InnerNode = right };
             *  }
             * }
             * if (right.Type == ExpressionType.Float)
             * {
             *  Float nr = (Float)right;
             *
             *  if (nr.isZero())
             *  {
             *      return left;
             *  }
             * }
             * return new Subtraction { Left = left, Right = right };*/
            Stack <Expression> stack = new Stack <Expression>();

            stack.Push(node);
            List <Expression> operands = new List <Expression>();
            List <Float>      floats   = new List <Float>();

            while (stack.Count != 0)
            {
                Expression current = stack.Pop();
                if (current.Type == ExpressionType.Addition)
                {
                    stack.Push(Simplify((current as Addition).Right));
                    stack.Push(Simplify((current as Addition).Left));
                }
                else if (current.Type == ExpressionType.Subtraction)
                {
                    stack.Push(Simplify(new Negation {
                        InnerNode = (current as Subtraction).Right
                    }));
                    stack.Push(Simplify((current as Subtraction).Left));
                }
                else if (current.Type == ExpressionType.Float)
                {
                    floats.Add(current as Float);
                }
                else
                {
                    operands.Add(current);
                }
            }
            Expression root;

            if (floats.Count > 0)
            {
                Float result = new Float {
                    Value = 0.0f
                };
                foreach (var fNode in floats)
                {
                    result += fNode;
                }
                root = result;
                if (result.IsZero())
                {
                    if (operands.Count > 0)
                    {
                        root = operands[0];
                        operands.RemoveAt(0);
                    }
                }
            }
            else
            {
                root = operands[0];
                operands.RemoveAt(0);
            }
            foreach (var value in operands)
            {
                if (value is Negation)
                {
                    root = new Subtraction {
                        Left = root, Right = (value as Negation).InnerNode
                    };
                }
                else
                {
                    root = new Addition {
                        Left = root, Right = value
                    };
                }
            }
            return(root);
        }