Example #1
0
        private static Expression SimplifyPower(Power node)
        {
            Expression left  = Simplify(node.Left);
            Expression right = Simplify(node.Right);

            if (left.Type == ExpressionType.Float)
            {
                Float nl = (Float)left;
                if (nl.IsZero() || nl.IsOne())
                {
                    return(left);
                }
                if (right.Type == ExpressionType.Float)
                {
                    Float nr = (Float)right;
                    return(Float.Pow(nl, nr));
                }
            }
            if (right.Type == ExpressionType.Float)
            {
                Float nr = (Float)right;
                if (nr.IsZero())
                {
                    return(new Float {
                        Value = 1.0
                    });
                }
                if (nr.IsOne())
                {
                    return(left);
                }
            }
            return(new Power {
                Left = left, Right = right
            });
        }
Example #2
0
 void CompileConstant(Float node)
 {
     rpnStack.Add(new Operand(node.Value));
 }
Example #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);
        }
Example #4
0
        private static Expression SimplifyMultiplication(Multiplication node)
        {
            /*Expression left = Simplify(node.Left);
             * Expression right = Simplify(node.Right);
             * if (left.Type == ExpressionType.Float)
             * {
             *  Float nl = (Float)left;
             *  if (nl.isZero())
             *  {
             *      return left;
             *  }
             *  if (nl.isOne())
             *  {
             *      return right;
             *  }
             *  if (right.Type == ExpressionType.Float)
             *  {
             *      Float nr = (Float)right;
             *      return nl * nr;
             *  }
             * }
             * if (right.Type == ExpressionType.Float)
             * {
             *  Float nr = (Float)right;
             *  if (nr.isOne())
             *  {
             *      return left;
             *  }
             *  if (nr.isZero())
             *  {
             *      return right;
             *  }
             * }
             * return new Multiplication { 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.Multiplication)
                {
                    stack.Push(Simplify((current as Multiplication).Right));
                    stack.Push(Simplify((current as Multiplication).Left));
                }
                else if (current.Type == ExpressionType.Division)
                {
                    stack.Push(Simplify(new Inverse {
                        InnerNode = (current as Division).Right
                    }));
                    stack.Push(Simplify((current as Division).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 = 1.0f
                };
                foreach (var fNode in floats)
                {
                    result *= fNode;
                }
                root = result;
                if (result.IsOne())
                {
                    if (operands.Count > 0)
                    {
                        root = operands[0];
                        if (root is Inverse)
                        {
                            root = new Division {
                                Left = result, Right = (root as Inverse).InnerNode
                            };
                        }
                        operands.RemoveAt(0);
                    }
                    else
                    {
                        return(result);
                    }
                }
                else if (result.IsZero())
                {
                    return(result);
                }
            }
            else
            {
                root = operands[0];
                operands.RemoveAt(0);
            }
            foreach (var value in operands)
            {
                if (value is Inverse)
                {
                    root = new Division {
                        Left = root, Right = (value as Inverse).InnerNode
                    };
                }
                else
                {
                    root = new Multiplication {
                        Left = root, Right = value
                    };
                }
            }
            return(root);
        }
Example #5
0
 static public Float Pow(Float a, Float b)
 {
     return(new Float {
         Value = Math.Pow(a.Value, b.Value)
     });
 }