def analyze(self, expr): """Rewrite an expression as sorted list of terms. """ gens, terms = set([]), [] for term in expr.as_Add(): coeff, cpart, ncpart = [], {}, [] for factor in term.as_Mul(): if not factor.is_commutative: ncpart.append(factor) else: if factor.is_Number: coeff.append(factor) else: base, exp = _analyze_power(*factor.as_Pow()) cpart[base] = exp gens.add(base) terms.append((coeff, cpart, ncpart, term)) gens = sorted(gens, Basic._compare_pretty) k, indices = len(gens), {} for i, g in enumerate(gens): indices[g] = i result = [] for coeff, cpart, ncpart, term in terms: monom = [0] * k for base, exp in cpart.iteritems(): monom[indices[base]] = exp result.append((coeff, monom, ncpart, term)) if self.order is None: return sorted(result, Basic._compare_pretty) else: return sorted(result, self._compare_terms)
def test__analyze_power(): assert _analyze_power(x, S(1)) == (x, S(1)) assert _analyze_power(x, S(2)) == (x, S(2)) assert _analyze_power(x, -S(1)) == (x**(-1), S(1)) assert _analyze_power(x, -S(2)) == (x**(-1), S(2)) assert _analyze_power(x, S(1) / 3) == (x**(S(1) / 3), S(1)) assert _analyze_power(x, S(2) / 3) == (x**(S(1) / 3), S(2)) assert _analyze_power(x, -S(1) / 3) == (x**(-S(1) / 3), S(1)) assert _analyze_power(x, -S(2) / 3) == (x**(-S(1) / 3), S(2)) assert _analyze_power(x, y) == (x**y, S(1)) assert _analyze_power(x, -y) == (x**(-y), S(1)) assert _analyze_power(x, 2 * y) == (x**y, S(2)) assert _analyze_power(x, -2 * y) == (x**(-y), S(2)) assert _analyze_power(x, y / 3) == (x**(y / 3), S(1)) assert _analyze_power(x, -y / 3) == (x**(-y / 3), S(1)) assert _analyze_power(x, 2 * y / 3) == (x**(y / 3), S(2)) assert _analyze_power(x, -2 * y / 3) == (x**(-y / 3), S(2)) assert _analyze_power(x, S(1.0)) == (x**S(1.0), S(1)) assert _analyze_power(x, S(2.0)) == (x**S(2.0), S(1)) assert _analyze_power(x, -S(1.0)) == (x**(-S(1.0)), S(1)) assert _analyze_power(x, -S(2.0)) == (x**(-S(2.0)), S(1)) assert _analyze_power(x, S(1.0) * y) == (x**(S(1.0) * y), S(1)) assert _analyze_power(x, S(2.0) * y) == (x**(S(2.0) * y), S(1)) assert _analyze_power(x, -S(1.0) * y) == (x**(-S(1.0) * y), S(1)) assert _analyze_power(x, -S(2.0) * y) == (x**(-S(2.0) * y), S(1))
def test__analyze_power(): assert _analyze_power(x, S(1)) == (x, S(1)) assert _analyze_power(x, S(2)) == (x, S(2)) assert _analyze_power(x, -S(1)) == (x**(-1), S(1)) assert _analyze_power(x, -S(2)) == (x**(-1), S(2)) assert _analyze_power(x, S(1)/3) == (x**(S(1)/3), S(1)) assert _analyze_power(x, S(2)/3) == (x**(S(1)/3), S(2)) assert _analyze_power(x, -S(1)/3) == (x**(-S(1)/3), S(1)) assert _analyze_power(x, -S(2)/3) == (x**(-S(1)/3), S(2)) assert _analyze_power(x, y) == (x**y, S(1)) assert _analyze_power(x, -y) == (x**(-y), S(1)) assert _analyze_power(x, 2*y) == (x**y, S(2)) assert _analyze_power(x, -2*y) == (x**(-y), S(2)) assert _analyze_power(x, y/3) == (x**(y/3), S(1)) assert _analyze_power(x, -y/3) == (x**(-y/3), S(1)) assert _analyze_power(x, 2*y/3) == (x**(y/3), S(2)) assert _analyze_power(x, -2*y/3) == (x**(-y/3), S(2)) assert _analyze_power(x, S(1.0)) == (x**S(1.0), S(1)) assert _analyze_power(x, S(2.0)) == (x**S(2.0), S(1)) assert _analyze_power(x, -S(1.0)) == (x**(-S(1.0)), S(1)) assert _analyze_power(x, -S(2.0)) == (x**(-S(2.0)), S(1)) assert _analyze_power(x, S(1.0)*y) == (x**(S(1.0)*y), S(1)) assert _analyze_power(x, S(2.0)*y) == (x**(S(2.0)*y), S(1)) assert _analyze_power(x, -S(1.0)*y) == (x**(-S(1.0)*y), S(1)) assert _analyze_power(x, -S(2.0)*y) == (x**(-S(2.0)*y), S(1))