|
| 1 | +"""Tools for manipulation of rational expressions. """ |
| 2 | + |
| 3 | +import importlib |
| 4 | + |
| 5 | +import sympy |
| 6 | +from sympy.core import Basic, Add, sympify |
| 7 | +from sympy.core.exprtools import gcd_terms |
| 8 | +from sympy.utilities import public |
| 9 | +from sympy.utilities.iterables import iterable |
| 10 | + |
| 11 | +__all__ = [] |
| 12 | + |
| 13 | + |
| 14 | +@public |
| 15 | +def together(expr, deep=False, fraction=True): |
| 16 | + """ |
| 17 | + Denest and combine rational expressions using symbolic methods. |
| 18 | +
|
| 19 | + This function takes an expression or a container of expressions |
| 20 | + and puts it (them) together by denesting and combining rational |
| 21 | + subexpressions. No heroic measures are taken to minimize degree |
| 22 | + of the resulting numerator and denominator. To obtain completely |
| 23 | + reduced expression use :func:`~.cancel`. However, :func:`~.together` |
| 24 | + can preserve as much as possible of the structure of the input |
| 25 | + expression in the output (no expansion is performed). |
| 26 | +
|
| 27 | + A wide variety of objects can be put together including lists, |
| 28 | + tuples, sets, relational objects, integrals and others. It is |
| 29 | + also possible to transform interior of function applications, |
| 30 | + by setting ``deep`` flag to ``True``. |
| 31 | +
|
| 32 | + By definition, :func:`~.together` is a complement to :func:`~.apart`, |
| 33 | + so ``apart(together(expr))`` should return expr unchanged. Note |
| 34 | + however, that :func:`~.together` uses only symbolic methods, so |
| 35 | + it might be necessary to use :func:`~.cancel` to perform algebraic |
| 36 | + simplification and minimize degree of the numerator and denominator. |
| 37 | +
|
| 38 | + Examples |
| 39 | + ======== |
| 40 | +
|
| 41 | + >>> from sympy import together, exp |
| 42 | + >>> from sympy.abc import x, y, z |
| 43 | +
|
| 44 | + >>> together(1/x + 1/y) |
| 45 | + (x + y)/(x*y) |
| 46 | + >>> together(1/x + 1/y + 1/z) |
| 47 | + (x*y + x*z + y*z)/(x*y*z) |
| 48 | +
|
| 49 | + >>> together(1/(x*y) + 1/y**2) |
| 50 | + (x + y)/(x*y**2) |
| 51 | +
|
| 52 | + >>> together(1/(1 + 1/x) + 1/(1 + 1/y)) |
| 53 | + (x*(y + 1) + y*(x + 1))/((x + 1)*(y + 1)) |
| 54 | +
|
| 55 | + >>> together(exp(1/x + 1/y)) |
| 56 | + exp(1/y + 1/x) |
| 57 | + >>> together(exp(1/x + 1/y), deep=True) |
| 58 | + exp((x + y)/(x*y)) |
| 59 | +
|
| 60 | + >>> together(1/exp(x) + 1/(x*exp(x))) |
| 61 | + (x + 1)*exp(-x)/x |
| 62 | +
|
| 63 | + >>> together(1/exp(2*x) + 1/(x*exp(3*x))) |
| 64 | + (x*exp(x) + 1)*exp(-3*x)/x |
| 65 | +
|
| 66 | + """ |
| 67 | + def _together(expr): |
| 68 | + if isinstance(expr, Basic): |
| 69 | + if expr.is_Atom or (expr.is_Function and not deep): |
| 70 | + return expr |
| 71 | + elif expr.is_Add: |
| 72 | + return gcd_terms(list(map(_together, Add.make_args(expr))), |
| 73 | + fraction=fraction) |
| 74 | + elif expr.is_Pow: |
| 75 | + base = _together(expr.base) |
| 76 | + |
| 77 | + if deep: |
| 78 | + exp = _together(expr.exp) |
| 79 | + else: |
| 80 | + exp = expr.exp |
| 81 | + |
| 82 | + return expr.func(base, exp) |
| 83 | + else: |
| 84 | + return expr.func(*[_together(arg) for arg in expr.args]) |
| 85 | + elif iterable(expr): |
| 86 | + return expr.__class__([_together(ex) for ex in expr]) |
| 87 | + |
| 88 | + return expr |
| 89 | + |
| 90 | + return _together(sympify(expr)) |
| 91 | + |
| 92 | + |
| 93 | +# Apply the monkey patch |
| 94 | +simplify = importlib.import_module(sympy.simplify.__module__) |
| 95 | +simplify.together = together |
0 commit comments