예제 #1
0
 // Computes the least common multiple of the two simple expression.
 // Assumes se1 and se2 have been normalized.
 private static SimpleExpression computeLCM(SimpleExpression se1, SimpleExpression se2) {
   se1 = se1.factorize();
   se2 = se2.factorize();
   List<SimpleExpression> gcd = computeGCD(se1, se2);
   SimpleExpression ret = multiply(gcd.get(0), gcd.get(1));
   ret = multiply(ret, gcd.get(2));
   return ret;
 }
예제 #2
0
 // Normalizes a MOD expression
 private SimpleExpression normalizeMOD() {
   if (!allow(DIVIDE)) return this;
   SimpleExpression lhs = getChild(0), rhs = getChild(1), ret = null;
   if (rhs.equals(szero)) ret = this;
   else if (lhs.sop == LIT && rhs.sop == LIT) ret = mod(lhs, rhs);
   else if (rhs.equals(sone) || rhs.equals(getInt(-1))) ret = szero;
   if (ret == null) {
     lhs = lhs.factorize();
     rhs = rhs.factorize();
     List<SimpleExpression> gcd = computeGCD(lhs, rhs);
     if (gcd.get(0).equals(sone)) ret = this;
     else // (a*x)%(b*x) = (a%b)*x
     ret = multiply(gcd.get(0), new SimpleExpression(gcd.get(1), MOD, gcd.get(2)));
   }
   // Tools.printlnStatus("[MOD] "+this+" --> "+ret, 1);
   return ret;
 }
예제 #3
0
 // Normalizes a DIV expression
 private SimpleExpression normalizeDIV() {
   if (!allow(DIVIDE)) return this;
   SimpleExpression lhs = getChild(0), rhs = getChild(1), ret = null;
   if (rhs.equals(szero)) ret = this; // Don't do anything with division by zero
   else if (lhs.equals(szero)) ret = szero; // 0/<expr>: expr=0 is an exception anyhow
   else if (lhs.sop == LIT && rhs.sop == LIT) ret = divide(lhs, rhs); // Call compute method
   else if (rhs.equals(sone) || rhs.equals(getInt(-1)))
     ret = multiply(rhs, lhs); // Division by one -> multiplication by one
   if (ret == null) {
     lhs = lhs.factorize();
     rhs = rhs.factorize();
     List<SimpleExpression> gcd = computeGCD(lhs, rhs);
     if (gcd.get(0).equals(sone)) ret = this;
     else ret = divide(gcd.get(1), gcd.get(2));
   }
   // Tools.printlnStatus("[DIV] "+this+" --> "+ret, 1);
   return ret;
 }