private Rational(BigNum num, BigNum den) { Contract.Assert(den.Signum > 0); Contract.Assert(num == BigNum.ZERO || num.Gcd(den) == BigNum.ONE); numerator = num; denominator = den; }
public static bool TryParse(string v, out BigNum res) { try { res = BigNum.FromString(v); return true; } catch (FormatException) { res = ZERO; return false; } }
public static bool TryParse(string v, out BigNum res) { try { res = BigNum.FromString(v); return(true); } catch (FormatException) { res = ZERO; return(false); } }
public int CompareTo(BigNum that) { if (this.val == that.val) { return(0); } if (this.val < that.val) { return(-1); } return(1); }
public static Rational FromBignums(BigNum num, BigNum den) { Contract.Assert(!den.IsZero); if (num == BigNum.ZERO) return ZERO; if (den.Signum < 0) { den = -den; num = -num; } if (den == BigNum.ONE) return new Rational(num, den); var gcd = num.Gcd(den); if (gcd == BigNum.ONE) return new Rational(num, den); return new Rational(num / gcd, den / gcd); }
public override bool Equals(object obj) { if (obj == null) { return(false); } if (!(obj is BigNum)) { return(false); } BigNum other = (BigNum)obj; return(this.val == other.val); }
public static Rational FromBignums(BigNum num, BigNum den) { Contract.Assert(!den.IsZero); if (num == BigNum.ZERO) { return(ZERO); } if (den.Signum < 0) { den = -den; num = -num; } if (den == BigNum.ONE) { return(new Rational(num, den)); } var gcd = num.Gcd(den); if (gcd == BigNum.ONE) { return(new Rational(num, den)); } return(new Rational(num / gcd, den / gcd)); }
public PureInteger(BigNum i) { this.val = i; }
public static IntervalElement/*!*/ Factory(BigNum inf, BigNum sup) { Contract.Ensures(Contract.Result<IntervalElement>() != null); ExtendedInt/*!*/ i = ExtendedInt.Factory(inf); ExtendedInt/*!*/ s = ExtendedInt.Factory(sup); return Factory(i, s); }
// Construct the interval [inf, sup] protected IntervalElement(BigNum infInt, BigNum supInt) { this.inf = ExtendedInt.Factory(infInt); this.sup = ExtendedInt.Factory(supInt); // base(); // to please the compiler... }
void BvLit(out BigNum n, out int m) { Expect(2); int pos = t.val.IndexOf("bv"); string a = t.val.Substring(0, pos); string b = t.val.Substring(pos + 2); try { n = BigNum.FromString(a); m = Convert.ToInt32(b); } catch (FormatException) { this.SemErr("incorrectly formatted bitvector"); n = BigNum.ZERO; m = 0; } }
public BvBounds(IToken/*!*/ tok, BigNum lower, BigNum upper) : base(tok, /*immutable=*/ false) { Contract.Requires(tok != null); this.Lower = lower; this.Upper = upper; }
// int numerator; // int denominator; // invariant: 0 < denominator || (numerator == 0 && denominator == 0); // invariant: numerator != 0 ==> gcd(abs(numerator),denominator) == 1; // invariant: numerator == 0 ==> denominator == 1 || denominator == 0; public static Rational FromInt(int x) { return(FromBignum(BigNum.FromInt(x))); }
IFunApp IIntExprFactory.Const(BigNum i) { throw new System.NotImplementedException(); }
IFunApp IIntExprFactory.Const(BigNum i) { Contract.Ensures(Contract.Result<IFunApp>() != null); throw new System.NotImplementedException(); }
public Elt(BigNum i) { this.domainValue = Value.Constant; this.constantValue = i; }
/// <summary> /// Returns true if "expr" represents a constant integer expressions, in which case /// "z" returns as that integer. Otherwise, returns false, in which case "z" should /// not be used by the caller. /// /// This method throws an System.ArithmeticException in the event that folding the /// constant expression results in an arithmetic overflow or division by zero. /// </summary> private bool Fold(IExpr/*!*/ expr, out BigNum z) { Contract.Requires(expr != null); IFunApp e = expr as IFunApp; if (e == null) { z = BigNum.ZERO; return false; } if (e.FunctionSymbol is IntSymbol) { z = ((IntSymbol)e.FunctionSymbol).Value; return true; } else if (e.FunctionSymbol.Equals(Int.Negate)) { IList/*<IExpr!>*//*!*/ args = e.Arguments; Contract.Assert(args != null); Contract.Assert(args.Count == 1); IExpr/*!*/ arg0 = (IExpr/*!*/)cce.NonNull(args[0]); if (Fold(arg0, out z)) { z = z.Neg; return true; } } else if (e.Arguments.Count == 2) { IExpr/*!*/ arg0 = (IExpr/*!*/)cce.NonNull(e.Arguments[0]); IExpr/*!*/ arg1 = (IExpr/*!*/)cce.NonNull(e.Arguments[1]); BigNum z0, z1; if (Fold(arg0, out z0) && Fold(arg1, out z1)) { if (e.FunctionSymbol.Equals(Int.Add)) { z = z0 + z1; } else if (e.FunctionSymbol.Equals(Int.Sub)) { z = z0 - z1; } else if (e.FunctionSymbol.Equals(Int.Mul)) { z = z0 * z1; } else if (e.FunctionSymbol.Equals(Int.Div)) { z = z0 / z1; } else if (e.FunctionSymbol.Equals(Int.Mod)) { z = z0 % z1; } else { z = BigNum.ZERO; return false; } return true; } } z = BigNum.ZERO; return false; }
public bool AsConstant(out BigNum x) { if (terms == null) { x = constant; return true; } else { x = BigNum.FromInt(-70022); // to please complier return false; } }
public void Multiply(BigNum x) /* throws ArithmeticException */ { if (x.IsZero) { constant = BigNum.ZERO; terms = null; } else { for (Term t = terms; t != null; t = t.next) { checked { t.coeff *= x; } } checked { constant *= x; } } }
/// <summary> /// Adds "le" to "this". Afterwards, "le" should not be used, because it will have been destroyed. /// </summary> /// <param name="le"></param> public void Add(LinearExpr/*!*/ le) /* throws ArithmeticException */ { Contract.Requires(le != null); Contract.Requires(le != this); checked { constant += le.constant; } le.constant = BigNum.FromInt(-70029); // "le" should no longer be used; assign it a strange value so that misuse is perhaps more easily detected // optimization: if (le.terms == null) { return; } else if (terms == null) { terms = le.terms; le.terms = null; return; } // merge the two term lists // Use a nested loop, which is quadratic in time complexity, but we hope the lists will be small Term newTerms = null; while (le.terms != null) { // take off next term from "le" Term t = le.terms; le.terms = t.next; t.next = null; for (Term u = terms; u != null; u = u.next) { if (u.var == t.var) { checked { u.coeff += t.coeff; } goto NextOuter; } } t.next = newTerms; newTerms = t; NextOuter: ; } // finally, include all non-0 terms while (terms != null) { // take off next term from "this" Term t = terms; terms = t.next; if (!t.coeff.IsZero) { t.next = newTerms; newTerms = t; } } terms = newTerms; }
public LinearExpr(BigNum x) { constant = x; }
/// <summary> /// Adds "x" to "this". /// </summary> /// <param name="x"></param> public void AddConstant(BigNum x) /* throws ArithmeticException */ { checked { constant += x; } }
public static IntSymbol/*!*/ Const(BigNum x) { Contract.Ensures(Contract.Result<IntSymbol>() != null); // We could cache things here, but for now we don't. return new IntSymbol(x); }
public static Rational FromBignum(BigNum n) { return(new Rational(n, BigNum.ONE)); }
public static BvSymbol/*!*/ Const(BigNum x, int y) { Contract.Ensures(Contract.Result<BvSymbol>() != null); // We could cache things here, but for now we don't. return new BvSymbol(x, y); }
public static Rational FromInts(int num, int den) { return(FromBignums(BigNum.FromInt(num), BigNum.FromInt(den))); }
/// <summary> /// The intention is that this constructor be called only from the Int.Const method. /// </summary> internal IntSymbol(BigNum x) : base(cce.NonNull(x.ToString()), Int.Type) { this.Value = x; }
void Nat(out BigNum n) { Expect(3); try { n = BigNum.FromString(t.val); } catch (FormatException) { this.SemErr("incorrectly formatted number"); n = BigNum.ZERO; } }
/// <summary> /// The intention is that this constructor be called only from the Int.Const method. /// </summary> internal BvSymbol(BigNum x, int y) : base(x + "bv" + y, Bv.Type) { this.Value = x; this.Bits = y; }
// Construct the inteval [val, val] protected IntervalElement(BigNum val) { this.inf = this.sup = ExtendedInt.Factory(val); // base(); }
public BvBounds(IToken! tok, BigNum lower, BigNum upper) { base(tok); this.Lower = lower; this.Upper = upper; }
public static IntervalElement/*!*/ Factory(BigNum i) { Contract.Ensures(Contract.Result<IntervalElement>() != null); return new IntervalElement(i); }
public Term(BigNum coeff, IVariable/*!*/ var) { Contract.Requires(var != null); this.coeff = coeff; this.var = var; // base(); }
// Return the ExtendedInt corresponding to the value public static ExtendedInt/*!*/ Factory(BigNum val) { Contract.Ensures(Contract.Result<ExtendedInt>() != null); return new PureInteger(val); }
public BigNum Min(BigNum that) { return(new BigNum(this.val <= that.val ? this.val : that.val)); }
public static Rational FromBignum(BigNum n) { return new Rational(n, BigNum.ONE); }
public BigNum Max(BigNum that) { return(new BigNum(this.val >= that.val ? this.val : that.val)); }
public LinearExpr(IVariable/*!*/ var) { Contract.Requires(var != null); constant = BigNum.ZERO; terms = new Term(BigNum.ONE, var); }
public void Negate() /* throws ArithmeticException */ { checked { constant = -constant; } for (Term t = terms; t != null; t = t.next) { checked { t.coeff = -t.coeff; } } }