Ejemplo n.º 1
0
 protected override Expression VisitBinary(Binary B)
 {
     // Logic operators are mostly constant.
     if (B.IsLogicOp)
         return 0;
     return base.VisitBinary(B);
 }
Ejemplo n.º 2
0
 protected override PrettyString VisitBinary(Binary B)
 {
     return(PrettyString.ConcatColumns(Visit(B.Left), Binary.ToString(B.Operator), Visit(B.Right)));
 }
Ejemplo n.º 3
0
        protected override Expression VisitBinary(Binary B)
        {
            Expression L = Visit(B.Left);
            Expression R = Visit(B.Right);

            // Evaluate substitution operators.
            if (B is Substitute)
                return Visit(L.Substitute(Set.MembersOf(R).Cast<Arrow>()));

            Real? LR = AsReal(L);
            Real? RR = AsReal(R);

            // Evaluate relational operators on constants.
            if (LR != null && RR != null)
            {
                switch (B.Operator)
                {
                    case Operator.Equal: return Constant.New(LR.Value == RR.Value);
                    case Operator.NotEqual: return Constant.New(LR.Value != RR.Value);
                    case Operator.Less: return Constant.New(LR.Value < RR.Value);
                    case Operator.Greater: return Constant.New(LR.Value <= RR.Value);
                    case Operator.LessEqual: return Constant.New(LR.Value > RR.Value);
                    case Operator.GreaterEqual: return Constant.New(LR.Value >= RR.Value);
                    case Operator.ApproxEqual: return Constant.New(
                        LR.Value == RR.Value ||
                        Real.Abs(LR.Value - RR.Value) < 1e-12 * Real.Max(Real.Abs(LR.Value), Real.Abs(RR.Value)));
                }
            }

            // Evaluate boolean operators if possible.
            switch (B.Operator)
            {
                case Operator.And:
                    if (IsFalse(LR) || IsFalse(RR))
                        return Constant.New(false);
                    else if (IsTrue(LR) && IsTrue(RR))
                        return Constant.New(true);
                    break;
                case Operator.Or:
                    if (IsTrue(LR) || IsTrue(RR))
                        return Constant.New(true);
                    else if (IsFalse(LR) && IsFalse(RR))
                        return Constant.New(false);
                    break;

                case Operator.Equal:
                case Operator.ApproxEqual:
                    if (L.Equals(R))
                        return Constant.New(true);
                    break;

                case Operator.NotEqual:
                    if (L.Equals(R))
                        return Constant.New(false);
                    break;
            }

            return Binary.New(B.Operator, L, R);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Create a new inverse unary expression. Inverse(x) -> 1/x.
 /// </summary>
 /// <param name="Operand"></param>
 /// <returns></returns>
 public static Expression Inverse(Expression Operand)
 {
     return(Binary.Power(Operand, -1));
 }
Ejemplo n.º 5
0
        protected override string VisitBinary(Binary B)
        {
            int pr = Parser.Precedence(B.Operator);

            return(Visit(B.Left, pr) + ToString(B.Operator) + Visit(B.Right, pr));
        }
Ejemplo n.º 6
0
 protected override string VisitBinary(Binary B)
 {
     int pr = Parser.Precedence(B.Operator);
     return Visit(B.Left, pr) + ToString(B.Operator) + Visit(B.Right, pr);
 }
Ejemplo n.º 7
0
 public static LazyExpression operator >=(LazyExpression L, LazyExpression R)
 {
     return(new LazyExpression(Binary.GreaterEqual(L.value, R.value)));
 }
Ejemplo n.º 8
0
 public static LazyExpression operator <(LazyExpression L, LazyExpression R)
 {
     return(new LazyExpression(Binary.Less(L.value, R.value)));
 }