Arithmetic expressions (int/real)
Наследование: Expr
Пример #1
0
        /// <summary>
        /// Translates an ASTVector into a ArithExpr[]
        /// </summary>
        public ArithExpr[] ToArithExprArray()
        {
            uint n = Size;

            ArithExpr[] res = new ArithExpr[n];
            for (uint i = 0; i < n; i++)
            {
                res[i] = (ArithExpr)Expr.Create(this.Context, this[i].NativeObject);
            }
            return(res);
        }
Пример #2
0
 /// <summary>
 /// Declare an arithmetical minimization objective. 
 /// Similar to MkMaximize.
 /// </summary>        	
 public Handle MkMinimize(ArithExpr e)
 {
     return new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject));
 }
Пример #3
0
 public IntExpr(Microsoft.Z3.ArithExpr s)
 {
     expr = s;
 }
Пример #4
0
 /// <summary>
 /// Translates an ASTVector into a ArithExpr[]
 /// </summary>    
 public ArithExpr[] ToArithExprArray()
 {
     uint n = Size;
     ArithExpr[] res = new ArithExpr[n];
     for (uint i = 0; i < n; i++)
         res[i] = (ArithExpr)Expr.Create(this.Context, this[i].NativeObject);
     return res;
 }
Пример #5
0
        private BoolExpr ReduceComparison(TermModelOperation type, ArithExpr[] operands)
        {
            var context = _solver.Context;
            Debug.Assert(operands.Length >= 2);            
            Func<ArithExpr, ArithExpr, BoolExpr> mkComparison;
            switch (type)
            {
                case TermModelOperation.Greater:
                    mkComparison = (x, y) => context.MkGt(x, y);
                    break;
                case TermModelOperation.Less:
                    mkComparison = (x, y) => context.MkLt(x, y);
                    break;
                case TermModelOperation.GreaterEqual:
                    mkComparison = (x, y) => context.MkGe(x, y);
                    break;
                case TermModelOperation.LessEqual:
                    mkComparison = (x, y) => context.MkLe(x, y);
                    break;
                case TermModelOperation.Equal:
                    mkComparison = (x, y) => context.MkEq(x, y);
                    break;
                default:
                    throw new NotSupportedException();
            }

            BoolExpr current = mkComparison(operands[0], operands[1]);
            for (int i = 1; i < operands.Length - 1; ++i)
                current = context.MkAnd(current, mkComparison(operands[i], operands[i + 1]));
            return current;
        }
Пример #6
0
 private static ArithExpr MkNum(ArithExpr e, int i)
 {
     using var sort = e.Context.MkIntSort();
     return((ArithExpr)e.Context.MkNumeral(i, sort));
 }
Пример #7
0
        /// <summary>
        /// Create an expression representing <c>t1 ^ t2</c>.
        /// </summary>    
        public ArithExpr MkPower(ArithExpr t1, ArithExpr t2)
        {
            Contract.Requires(t1 != null);
            Contract.Requires(t2 != null);
            Contract.Ensures(Contract.Result<ArithExpr>() != null);

            CheckContextMatch(t1);
            CheckContextMatch(t2);
            return (ArithExpr)Expr.Create(this, Native.Z3_mk_power(nCtx, t1.NativeObject, t2.NativeObject));
        }
Пример #8
0
 /// <summary>
 /// Extract index of sub-string starting at offset.
 /// </summary>
 public IntExpr MkIndexOf(SeqExpr s, SeqExpr substr, ArithExpr offset)
 {
     Contract.Requires(s != null);
     Contract.Requires(offset != null);
     Contract.Requires(substr != null);
     Contract.Ensures(Contract.Result<IntExpr>() != null);
     CheckContextMatch(s, substr, offset);
     return new IntExpr(this, Native.Z3_mk_seq_index(nCtx, s.NativeObject, substr.NativeObject, offset.NativeObject));
 }
Пример #9
0
 private static ArithExpr MkNum(ArithExpr e, int i)
 {
     return((ArithExpr)e.Context.MkNumeral(i, e.Context.MkIntSort()));
 }
Пример #10
0
        private static void CalcZ3CurrentAndTargetValue(
            Z3Point3D currentPoint,
            Z3Point3D startPoint,
            int degrees,
            Direction direction,
            out ArithExpr currentValue,
            out double targetValue,
            out double directionSign)
        {
            // once it is rotating towards a direction there is a limit for the rotation
            // in this case the limit is imposed to a single component (X, Y or Z) relative to the direction
            var limitValue = Math.Sin(75 * Math.PI / 180.0);
            var radians = degrees * Math.PI / 180.0; // assigning a double for angle in radians

            // determining if the direction sign is negative
            directionSign = 1.0;
            switch (direction)
            {
                case Direction.Down:
                case Direction.Back:
                case Direction.Left:
                    directionSign = -1.0;
                    break;
            }

            // update limit based on the direction sign
            limitValue *= directionSign;

            // start value stores the component (X, Y or Z) from the startPoint
            // determining the current and start value
            currentValue = currentPoint.X;
            var startValue = 0.0;
            switch (direction)
            {
                case Direction.Right:
                case Direction.Left:
                    startValue = startPoint.GetXValue();
                    currentValue = currentPoint.X;
                    break;
                case Direction.Up:
                case Direction.Down:
                    startValue = startPoint.GetYValue();
                    currentValue = currentPoint.Y;
                    break;
                case Direction.Front:
                case Direction.Back:
                    startValue = startPoint.GetZValue();
                    currentValue = currentPoint.Z;
                    break;
            }

            double startRadians = Math.Asin(startValue);
            double targetRadians = startRadians + (directionSign * radians);
            targetValue = Math.Sin(targetRadians);

            // this first case tells that the rotation is bigger than the desired and
            // is moving the vector (targetValue) on the opposite direction
            // this rotation is not desired here because we are rotating towards a direction
            // the rotation is not a pure transform
            var targetIsLowerThanStart =
                directionSign * targetValue <
                directionSign * startValue;

            // this second case tells that the rotation exceeded the limitValue
            var targetExceedsLimit = Math.Abs(targetValue) > Math.Abs(limitValue);

            // on both cases the targetValue should be the limitValue
            if (targetIsLowerThanStart || targetExceedsLimit)
            {
                targetValue = limitValue;
            }
        }
Пример #11
0
            static ArithExpr CalcApproximateCoordFromManhattanToEuclidianSystem(
            ArithExpr firstCoord,
            ArithExpr secondCoord,
            ArithExpr thirdCoord)
            {
                // Work only with values length
                // Values sign will be assigned again in the end
                ArithExpr firstCoordLength = Z3Math.Abs(firstCoord);
                ArithExpr secondCoordLength = Z3Math.Abs(secondCoord);
                ArithExpr thirdCoordLength = Z3Math.Abs(thirdCoord);

                // The all common length will be weighted by this
                // This way for example a (1, 1, 1) vector will become
                // A (0.57, 0.57, 0.57) with norm near to 1
                ArithExpr sqrt1div3 = Z3Math.Real(0.57735026918962576450914878050196);

                // The remaining common length will be weighted by this
                // This way for example a (1, 1, 0) vector will become
                // A (0.7, 0.7, 0.7) with norm near to 1
                ArithExpr sin45 = Z3Math.Real(0.70710678118654752440084436210485);

                // Calc common length between x, y, z
                ArithExpr allCommonLength =
                    Z3Math.Min(
                        firstCoordLength,
                        secondCoordLength,
                        thirdCoordLength);

                // Calc the common length between the target coord (firstCoord)
                // and the higher coord between the second and third coords
                ArithExpr lastTwoCommonLength =
                    Z3Math.Max(
                    Z3Math.Min(secondCoordLength, firstCoordLength),
                    Z3Math.Min(thirdCoordLength, firstCoordLength));

                // Calc exclusevely common length with the remaining coordinate
                ArithExpr lastTwoExclusiveCommonLength =
                    Z3Math.Sub(
                        lastTwoCommonLength,
                        allCommonLength);

                // Calc remaining length
                ArithExpr especificLength =
                    Z3Math.Sub(firstCoordLength,
                    Z3Math.Add(lastTwoExclusiveCommonLength, allCommonLength));

                // Calc weighted lengths
                ArithExpr weigthedLength1 = Z3Math.Mul(lastTwoExclusiveCommonLength, sin45);
                ArithExpr weigthedLength2 = Z3Math.Mul(allCommonLength, sqrt1div3);

                // Calc weighted result length
                ArithExpr resultLength =
                    Z3Math.Add(
                    especificLength,
                    weigthedLength1,
                    weigthedLength2);

                // The transform doesn't change the sign of the coordinate
                // Recover it from original data
                Expr result =
                    Z3.Context.MkITE(
                    Z3.Context.MkGe(firstCoord, Z3Math.Zero),
                    resultLength,
                    Z3Math.Neg(resultLength));

                return result as ArithExpr;
            }
Пример #12
0
 public static ArithExpr Min(ArithExpr expr1, ArithExpr expr2, ArithExpr expr3)
 {
     return Z3Math.Min(Z3Math.Min(expr1, expr2), expr3);
 }
Пример #13
0
            public static ArithExpr Min(ArithExpr expr1, ArithExpr expr2)
            {
                Expr result = Z3.Context.MkITE(
                    Z3.Context.MkLt(expr1, expr2),
                    expr1,
                    expr2);

                return result as ArithExpr;
            }
Пример #14
0
            public static ArithExpr Abs(ArithExpr expr)
            {
                Expr result = Z3.Context.MkITE(
                    Z3.Context.MkGe(expr, Z3Math.Zero),
                    expr,
                    Z3Math.Neg(expr));

                return result as ArithExpr;
            }
Пример #15
0
        internal void AssertArith(int vid, ArithExpr variable)
        {
            // Get the bounds on the row
            Rational lower, upper;
            _model.GetBounds(vid, out lower, out upper);

            // Case of equality
            if (lower == upper)
            {
                // Create the equality term
                Expr eqConst = GetNumeral(lower, variable.Sort);
                BoolExpr constraint = _context.MkEq(eqConst, variable);
                // Assert the constraint
                _optSolver.Assert(constraint);
            }
            else
            {
                // If upper bound is finite assert the upper bound constraint
                if (lower.IsFinite)
                {
                    // Create the lower Bound constraint
                    ArithExpr lowerTerm = GetNumeral(lower, variable.Sort);
                    BoolExpr constraint = _context.MkLe(lowerTerm, variable);
                    // Assert the constraint
                    _optSolver.Assert(constraint);
                }
                // If lower bound is finite assert the lower bound constraint
                if (upper.IsFinite)
                {
                    // Create the upper bound constraint
                    ArithExpr upperTerm = GetNumeral(upper, variable.Sort);
                    BoolExpr constraint = _context.MkGe(upperTerm, variable);
                    // Assert the constraint
                    _optSolver.Assert(constraint);
                }
            }
        }
Пример #16
0
 /// <summary>
 /// Declare an arithmetical minimization objective.
 /// Similar to MkMaximize.
 /// </summary>
 public Handle MkMinimize(ArithExpr e)
 {
     return(new Handle(this, Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject)));
 }
Пример #17
0
 private static ArithExpr MkNum(ArithExpr e, double d)
 {
     return((ArithExpr)e.Context.MkNumeral(d.ToString(), e.Context.MkRealSort()));
 }
Пример #18
0
        /// <summary>
        /// Create an expression representing <c>t1 &lt; t2</c>
        /// </summary>    
        public BoolExpr MkLt(ArithExpr t1, ArithExpr t2)
        {
            Contract.Requires(t1 != null);
            Contract.Requires(t2 != null);
            Contract.Ensures(Contract.Result<BoolExpr>() != null);

            CheckContextMatch(t1);
            CheckContextMatch(t2);
            return new BoolExpr(this, Native.Z3_mk_lt(nCtx, t1.NativeObject, t2.NativeObject));
        }
Пример #19
0
 public uint MkMinimize(ArithExpr e)
 {
     return(Native.Z3_optimize_minimize(Context.nCtx, NativeObject, e.NativeObject));
 }
Пример #20
0
        /// <summary>
        /// Create an expression representing <c>-t</c>.
        /// </summary>    
        public ArithExpr MkUnaryMinus(ArithExpr t)
        {
            Contract.Requires(t != null);
            Contract.Ensures(Contract.Result<ArithExpr>() != null);

            CheckContextMatch(t);
            return (ArithExpr)Expr.Create(this, Native.Z3_mk_unary_minus(nCtx, t.NativeObject));
        }
Пример #21
0
 private static ArithExpr MkNum(ArithExpr e, int i) { return (ArithExpr)e.Context.MkNumeral(i, e.Context.MkIntSort()); }        
Пример #22
0
 private static ArithExpr MkNum(ArithExpr e, double d)
 {
     using var sort = e.Context.MkRealSort();
     return((ArithExpr)e.Context.MkNumeral(d.ToString(), sort));
 }
Пример #23
0
 private static ArithExpr MkNum(ArithExpr e, double d) { return (ArithExpr)e.Context.MkNumeral(d.ToString(), e.Context.MkRealSort()); }