public static SMTAffineTerm create(Rational factor, Term subterm) { Sort sort = subterm.getSort(); Map<Term, Rational> summands; Rational constant; if (factor.equals(Rational.ZERO)) { summands = Collections.emptyMap(); constant = Rational.ZERO; } else if (subterm instanceof SMTAffineTerm) { SMTAffineTerm a = (SMTAffineTerm) subterm; constant = a.mConstant.mul(factor); summands = new HashMap<Term, Rational>(); for (Map.Entry<Term, Rational> me : a.mSummands.entrySet()) { summands.put(me.getKey(), me.getValue().mul(factor)); } } else if (subterm instanceof ConstantTerm) { Object value = ((ConstantTerm) subterm).getValue(); if (value instanceof BigInteger) { constant = Rational.valueOf((BigInteger) value, BigInteger.ONE).mul(factor); summands = Collections.emptyMap(); } else if (value instanceof BigDecimal) { BigDecimal decimal = (BigDecimal) value; if (decimal.scale() <= 0) { BigInteger num = decimal.toBigInteger(); constant = Rational.valueOf(num, BigInteger.ONE).mul(factor); } else { BigInteger num = decimal.unscaledValue(); BigInteger denom = BigInteger.TEN.pow(decimal.scale()); constant = Rational.valueOf(num, denom).mul(factor); } summands = Collections.emptyMap(); } else if (value instanceof Rational) { constant = (Rational) value; summands = Collections.emptyMap(); } else { summands = Collections.singletonMap(subterm, factor); constant = Rational.ZERO; } } else { summands = Collections.singletonMap(subterm, factor); constant = Rational.ZERO; } return create(summands, constant, sort); }
/** * Add a sub-annotation to the current LAAnnotation and explain it. * * @param reason The reason for which a sub-annotation is created. * @param coeff The Farkas coefficient of the sub-annotation. */ public void addAnnotation(LAReason reason, Rational coeff) { assert ((coeff.signum() > 0) == reason.isUpper()); Rational sign = Rational.valueOf(coeff.signum(), 1); LAAnnotation annot = mSubReasons.get(reason); if (annot == null) { annot = new LAAnnotation(reason); mSubReasons.put(reason, annot); if (mAnnotationStack != null) mAnnotationStack.addLast(annot); reason.explain(this, reason.getVar().getEpsilon(), sign); if (mAnnotationStack != null) mAnnotationStack.removeLast(); } if (mAnnotationStack != null) mAnnotationStack.getLast().addFarkas(annot, coeff); }
@Override protected void convert(Term term) { if (term instanceof ConstantTerm) { ConstantTerm ct = (ConstantTerm) term; if (ct.getValue() instanceof BigInteger) { Rational rat = Rational.valueOf((BigInteger) ct.getValue(), BigInteger.ONE); setResult(rat.toTerm(term.getSort())); } else if (ct.getValue() instanceof BigDecimal) { BigDecimal decimal = (BigDecimal) ct.getValue(); Rational rat; if (decimal.scale() <= 0) { BigInteger num = decimal.toBigInteger(); rat = Rational.valueOf(num, BigInteger.ONE); } else { BigInteger num = decimal.unscaledValue(); BigInteger denom = BigInteger.TEN.pow(decimal.scale()); rat = Rational.valueOf(num, denom); } setResult(rat.toTerm(term.getSort())); } else if (ct.getValue() instanceof Rational) setResult(ct); else setResult(term); } else super.convert(term); }
public void addEQAnnotation(LiteralReason reason, Rational coeff) { // FIXME: make a special annotation for disequalities assert ((coeff.signum() > 0) == reason.isUpper()); Rational sign = Rational.valueOf(coeff.signum(), 1); LAAnnotation annot = mSubReasons.get(reason); if (annot == null) { annot = new LAAnnotation(reason); mSubReasons.put(reason, annot); mAnnotationStack.addLast(annot); if (reason.getOldReason() instanceof LiteralReason) reason.getOldReason().explain(this, reason.getVar().getEpsilon(), sign); else addAnnotation(reason.getOldReason(), sign); addLiteral(reason.getLiteral().negate(), sign); mAnnotationStack.removeLast(); } mAnnotationStack.getLast().addFarkas(annot, coeff); }