

def clear_coefficients(expr, rhs=S.Zero):
    "Return `p, r` where `p` is the expression obtained when Rational\n    additive and multiplicative coefficients of `expr` have been stripped\n    away in a naive fashion (i.e. without simplification). The operations\n    needed to remove the coefficients will be applied to `rhs` and returned\n    as `r`.\n\n    Examples\n    ========\n\n    >>> from sympy.simplify.simplify import clear_coefficients\n    >>> from sympy.abc import x, y\n    >>> from sympy import Dummy\n    >>> expr = 4*y*(6*x + 3)\n    >>> clear_coefficients(expr - 2)\n    (y*(2*x + 1), 1/6)\n\n    When solving 2 or more expressions like `expr = a`,\n    `expr = b`, etc..., it is advantageous to provide a Dummy symbol\n    for `rhs` and  simply replace it with `a`, `b`, etc... in `r`.\n\n    >>> rhs = Dummy('rhs')\n    >>> clear_coefficients(expr, rhs)\n    (y*(2*x + 1), _rhs/12)\n    >>> _[1].subs(rhs, 2)\n    1/6\n    "
    was = None
    free = expr.free_symbols
    if expr.is_Rational:
        return (S.Zero, (rhs - expr))
    while (expr and (was != expr)):
        was = expr
        (m, expr) = (expr.as_content_primitive() if free else factor_terms(expr).as_coeff_Mul(rational=True))
        rhs /= m
        (c, expr) = expr.as_coeff_Add(rational=True)
        rhs -= c
    expr = signsimp(expr, evaluate=False)
    if _coeff_isneg(expr):
        expr = (- expr)
        rhs = (- rhs)
    return (expr, rhs)

dataset/ETHPy150Open sympy/sympy/sympy/simplify/simplify.py clear_coefficients/original