/** * Returns if a simple expression is greater than, less than, or equal to the other. * * @param se1 the first simple expression. * @param se2 the second simple expression. * @return the comparison result (-1, 0, 1). */ public int compare(SimpleExpression se1, SimpleExpression se2) { if (se1.order < se2.order) return -1; else if (se1.order > se2.order) return 1; se1 = se1.getTerm(); se2 = se2.getTerm(); if (se1.children.size() < se2.children.size()) return -1; else if (se1.children.size() > se2.children.size()) return 1; else return se1.compareTo(se2); }
// Assumes se1 and se2 have been normalized. // Returns a triplet (gcd, dividend1, dividend2). private static List<SimpleExpression> computeGCD(SimpleExpression se1, SimpleExpression se2) { // Compute symbolic parts. List<SimpleExpression> terms1 = new LinkedList<SimpleExpression>(); List<SimpleExpression> terms2 = new LinkedList<SimpleExpression>(); SimpleExpression term1 = se1.getTerm(), term2 = se2.getTerm(); if (term1.sop == MUL) terms1.addAll(term1.children); else terms1.add(term1); if (term2.sop == MUL) terms2.addAll(term2.children); else terms2.add(term2); TreeSet<SimpleExpression> gcd_terms = new TreeSet<SimpleExpression>(terms1); gcd_terms.retainAll(terms2); for (SimpleExpression gcd_term : gcd_terms) { terms1.remove(gcd_term); // removes the first occurrence terms2.remove(gcd_term); // of the gcd term } // Compute numeric parts. SimpleExpression s1 = se1.getCoef(), s2 = se2.getCoef(), sgcd = null; if (s1.expr instanceof IntegerLiteral && s2.expr instanceof IntegerLiteral) { int n1 = s1.getValue().intValue(), n2 = s2.getValue().intValue(); int gcd = cetus.analysis.GCD.compute(n1, n2); s1 = getInt(n1 / gcd); s2 = getInt(n2 / gcd); sgcd = getInt(gcd); } else sgcd = sone; // Combine two parts. for (SimpleExpression child : gcd_terms) sgcd = multiply(sgcd, child); for (SimpleExpression child : terms1) s1 = multiply(s1, child); for (SimpleExpression child : terms2) s2 = multiply(s2, child); List<SimpleExpression> ret = new LinkedList<SimpleExpression>(); ret.add(sgcd); ret.add(s1); ret.add(s2); // Tools.printlnStatus("[GCD] "+se1+" and "+se2+" --> "+ret, 1); return ret; }
// Normalizes an ADD expression private SimpleExpression normalizeADD() { if (!allow(FOLD)) return this; TreeMap<SimpleExpression, SimpleExpression> terms = new TreeMap<SimpleExpression, SimpleExpression>(); for (SimpleExpression child : children) { SimpleExpression term = child.getTerm(), coef = child.getCoef(); if (terms.containsKey(term)) terms.put(term, add(terms.get(term), coef)); else terms.put(term, coef); } SimpleExpression ret = new SimpleExpression(ADD); for (SimpleExpression term : terms.keySet()) { SimpleExpression coef = terms.get(term); if (!coef.equals(szero)) ret.add((coef.equals(sone)) ? term : multiply(coef, term)); } if (ret.children.size() == 0) ret = szero; else if (ret.children.size() == 1) ret = ret.getChild(0); // Tools.printlnStatus("[ADD] "+this+" --> "+ret, 1); return ret; }