// Factorizes terms; a*b+a*c --> a*(b+c) private SimpleExpression factorize() { if (!allow(FACTORIZE) || sop != ADD) return this; SimpleExpression ret = null, gcd = getChild(0); // for normalized form. for (SimpleExpression child : children) { gcd = computeGCD(gcd, child).get(0); if (gcd.equals(sone)) { ret = this; break; } } if (ret == null) { SimpleExpression rhs = szero; for (SimpleExpression child : children) rhs = add(rhs, computeGCD(gcd, child).get(2)); ret = new SimpleExpression(gcd, MUL, rhs); ret.sort(); } // Tools.printlnStatus("[FACT] "+this+" --> "+ret, 1); return ret; }
// Normalizes this simple expression recursively. protected SimpleExpression normalize() { SimpleExpression ret = new SimpleExpression(this); for (SimpleExpression child : children) ret.add(child.normalize()); if (contains_side_effect) return ret; switch (ret.sop) { case ID: case LIT: case LEAF: ret = this; break; case ADD: ret = ret.normalizeADD(); break; case MUL: ret = ret.normalizeMUL(); break; case DIV: ret = ret.normalizeDIV(); break; case MOD: ret = ret.normalizeMOD(); break; case SFTL: case SFTR: case BAND: case BOR: case BXOR: ret = ret.normalizeBitOperation(); break; case BCMP: ret = ret.normalizeBCMP(); break; case AND: case OR: ret = ret.normalizeLogic(); break; case EQ: case NE: case LE: case LT: case GE: case GT: ret = ret.normalizeCompare(); break; case NEG: ret = ret.normalizeNEG(); break; case MIN: case MAX: ret = ret.normalizeMINMAX(); break; default: } if (ret.isCommAssoc()) ret.sort(); // Tools.printlnStatus("[NORM] "+this+" --> "+ret, 1); return ret; }
// Sorts the child terms if they are commutative and associative protected void sort() { for (SimpleExpression child : children) child.sort(); if (isCommAssoc()) Collections.sort(children, this); }