def factor(expression, variable = None, ensemble = None, decomposer_entiers = True): if isinstance(expression, (int, long, Integer)): if decomposer_entiers: return ProduitEntiers(*factorint(expression).iteritems()) else: return expression elif isinstance(expression, Basic) and expression.is_polynomial(): if variable is None: variable = extract_var(expression) if variable is None: # polynôme à plusieurs variables return factor_(expression) else: try: return poly_factor(expression, variable, ensemble) except NotImplementedError: if param.debug: print_error() return expression resultat = together_(expression) if resultat.is_rational_function(): num, den = resultat.as_numer_denom() if den != 1: return factor(num, variable, ensemble, decomposer_entiers)/factor(den, variable, ensemble, decomposer_entiers) else: resultat = auto_collect(resultat) if resultat.is_Mul: produit = 1 for facteur in resultat.args: if facteur != 1: produit *= factor(facteur, variable, ensemble, decomposer_entiers) return produit return resultat
def factor(expression, variable=None, ensemble=None, decomposer_entiers=True): if isinstance(expression, (int, long, Integer)): if decomposer_entiers: return ProduitEntiers(*factorint(expression).iteritems()) else: return expression elif isinstance(expression, Basic) and expression.is_polynomial(): if variable is None: variable = extract_var(expression) if variable is None: # polynôme à plusieurs variables return factor_(expression) else: try: return poly_factor(expression, variable, ensemble) except NotImplementedError: if param.debug: print_error() return expression resultat = together_(expression) if resultat.is_rational_function(): num, den = resultat.as_numer_denom() if den != 1: return factor(num, variable, ensemble, decomposer_entiers) / factor( den, variable, ensemble, decomposer_entiers) else: resultat = auto_collect(resultat) if resultat.is_Mul: produit = 1 for facteur in resultat.args: if facteur != 1: produit *= factor(facteur, variable, ensemble, decomposer_entiers) return produit return resultat
def together(expression): return cancel(together_(expression))