/usr/share/pyshared/sympy/polys/rationaltools.py is in python-sympy 0.7.1.rc1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """Tools for manipulation of rational expressions. """
from sympy.core import Basic, Add, sympify
from sympy.core.exprtools import gcd_terms
def together(expr, deep=False):
"""
Denest and combine rational expressions using symbolic methods.
This function takes an expression or a container of expressions
and puts it (them) together by denesting and combining rational
subexpressions. No heroic measures are taken to minimize degree
of the resulting numerator and denominator. To obtain completely
reduced expression use :func:`cancel`. However, :func:`together`
can preserve as much as possible of the structure of the input
expression in the output (no expansion is performed).
A wide variety of objects can be put together including lists,
tuples, sets, relational objects, integrals and others. It is
also possible to transform interior of function applications,
by setting ``deep`` flag to ``True``.
By definition, :func:`together` is a complement to :func:`apart`,
so ``apart(together(expr))`` should return expr unchanged. Note
however, that :func:`together` uses only symbolic methods, so
it might be necessary to use :func:`cancel` to perform algebraic
simplification and minimise degree of the numerator and denominator.
**Example**
>>> from sympy import together, exp
>>> from sympy.abc import x, y, z
>>> together(1/x + 1/y)
(x + y)/(x*y)
>>> together(1/x + 1/y + 1/z)
(x*y + x*z + y*z)/(x*y*z)
>>> together(1/(x*y) + 1/y**2)
(x + y)/(x*y**2)
>>> together(1/(1 + 1/x) + 1/(1 + 1/y))
(x*(y + 1) + y*(x + 1))/((x + 1)*(y + 1))
>>> together(exp(1/x + 1/y))
exp(1/y + 1/x)
>>> together(exp(1/x + 1/y), deep=True)
exp((x + y)/(x*y))
>>> together(1/exp(x) + 1/(x*exp(x)))
(x + 1)*exp(-x)/x
>>> together(1/exp(2*x) + 1/(x*exp(3*x)))
(x*exp(x) + 1)*exp(-3*x)/x
"""
def _together(expr):
if isinstance(expr, Basic):
if expr.is_commutative is False:
numer, denom = expr.as_numer_denom()
return numer/denom
elif expr.is_Atom or (expr.is_Function and not deep):
return expr
elif expr.is_Add:
return gcd_terms(map(_together, Add.make_args(expr)))
elif expr.is_Pow:
base = _together(expr.base)
if deep:
exp = _together(expr.exp)
else:
exp = expr.exp
return expr.__class__(base, exp)
else:
return expr.__class__(*[ _together(arg) for arg in expr.args ])
elif hasattr(expr, '__iter__'):
return expr.__class__([ _together(ex) for ex in expr ])
return expr
return _together(sympify(expr))
|