def test_pretty_RootSum(): expr = RootSum(x**5 + 11 * x - 2, auto=False) ascii_str = \ """\ / 5 \\\n\ RootSum\\x + 11*x - 2/\ """ ucode_str = \ u"""\ ⎛ 5 ⎞\n\ RootSum⎝x + 11⋅x - 2⎠\ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str expr = RootSum(x**5 + 11 * x - 2, Lambda(z, exp(z))) ascii_str = \ """\ / 5 / z\\\\\n\ RootSum\\x + 11*x - 2, Lambda\\z, e //\ """ ucode_str = \ u"""\ ⎛ 5 ⎛ z⎞⎞\n\ RootSum⎝x + 11⋅x - 2, Λ⎝z, ℯ ⎠⎠\ """ assert pretty(expr) == ascii_str assert upretty(expr) == ucode_str
def test_RootsOf(): f = Poly((x-4)**4, x) roots = RootsOf(f) assert roots.count == 4 assert list(roots.roots()) == [ Integer(4), Integer(4), Integer(4), Integer(4) ] assert RootSum(lambda r: r**2, f) == 64 roots = RootsOf(x**5+x+1, x) assert roots.count == 5 f = Poly(x**5+x+1, x) assert list(roots.roots()) == [ RootOf(f, 0), RootOf(f, 1), RootOf(f, 2), RootOf(f, 3), RootOf(f, 4) ] assert RootSum(lambda r: r**2, f).doit() == RootOf(f, 0)**2 + \ RootOf(f, 1)**2 + RootOf(f, 2)**2 + RootOf(f, 3)**2 + RootOf(f, 4)**2 assert RootSum(Lambda(x, x), Poly(0, x), evaluate=True) == S.Zero assert RootSum(Lambda(x, x), Poly(0, x), evaluate=False) != S.Zero assert RootSum(Lambda(x, x), Poly(x-1, x), evaluate=False).doit() == S.One
def test_apart_full(): f = 1 / (x**2 + 1) assert apart(f, full=False) == f assert (apart( f, full=True) == -RootSum(x**2 + 1, Lambda(a, a / (x - a)), auto=False) / 2) f = 1 / (x**3 + x + 1) assert apart(f, full=False) == f assert apart(f, full=True) == RootSum( x**3 + x + 1, Lambda( a, (a**2 * Rational(6, 31) - a * Rational(9, 31) + Rational(4, 31)) / (x - a), ), auto=False, ) f = 1 / (x**5 + 1) assert apart(f, full=False) == (Rational(-1, 5)) * ( (x**3 - 2 * x**2 + 3 * x - 4) / (x**4 - x**3 + x**2 - x + 1)) + (Rational(1, 5)) / (x + 1) assert apart(f, full=True) == -RootSum( x**4 - x**3 + x**2 - x + 1, Lambda(a, a / (x - a)), auto=False) / 5 + ( Rational(1, 5)) / (x + 1)
def test_apart_full(): f = 1 / (x**2 + 1) assert apart(f, full=False) == f assert apart(f, full=True) == \ -RootSum(x**2 + 1, Lambda(a, a/(x - a)), auto=False)/2 f = 1 / (x**3 + x + 1) assert apart(f, full=False) == f assert apart(f, full=True) == \ RootSum(x**3 + x + 1, Lambda(a, (6*a**2/31 - 9*a/31 + S(4)/31)/(x - a)), auto=False) f = 1 / (x**5 + 1) assert apart(f, full=False) == \ (-S(1)/5)*((x**3 - 2*x**2 + 3*x - 4)/(x**4 - x**3 + x**2 - x + 1)) + (S(1)/5)/(x + 1) assert apart(f, full=True) == \ -RootSum(x**4 - x**3 + x**2 - x + 1, Lambda(a, a/(x - a)), auto=False)/5 + (S(1)/5)/(x + 1)
def test_latex_RootSum(): assert latex(RootSum(x**5 + x + 3, sin)) == \ r"\operatorname{RootSum} {\left(x^{5} + x + 3, \Lambda {\left (x, \sin{\left (x \right )} \right )}\right)}"
def ratint(f, x, **flags): """Performs indefinite integration of rational functions. Given a field :math:`K` and a rational function :math:`f = p/q`, where :math:`p` and :math:`q` are polynomials in :math:`K[x]`, returns a function :math:`g` such that :math:`f = g'`. >>> from sympy.integrals.rationaltools import ratint >>> from sympy.abc import x >>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x) (12*x + 6)/(x**2 - 1) + 4*log(x - 2) - 4*log(x + 1) References ========== .. [Bro05] M. Bronstein, Symbolic Integration I: Transcendental Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70 See Also ======== sympy.integrals.integrals.Integral.doit ratint_logpart, ratint_ratpart """ if type(f) is not tuple: p, q = f.as_numer_denom() else: p, q = f p, q = Poly(p, x, composite=False, field=True), Poly(q, x, composite=False, field=True) coeff, p, q = p.cancel(q) poly, p = p.div(q) result = poly.integrate(x).as_expr() if p.is_zero: return coeff * result g, h = ratint_ratpart(p, q, x) P, Q = h.as_numer_denom() P = Poly(P, x) Q = Poly(Q, x) q, r = P.div(Q) result += g + q.integrate(x).as_expr() if not r.is_zero: symbol = flags.get('symbol', 't') if not isinstance(symbol, Symbol): t = Dummy(symbol) else: t = symbol.as_dummy() L = ratint_logpart(r, Q, x, t) real = flags.get('real') if real is None: if type(f) is not tuple: atoms = f.atoms() else: p, q = f atoms = p.atoms() | q.atoms() for elt in atoms - set([x]): if not elt.is_real: real = False break else: real = True eps = S(0) if not real: for h, q in L: eps += RootSum(q, Lambda(t, t * log(h.as_expr())), quadratic=True) else: for h, q in L: R = log_to_real(h, q, x, t) if R is not None: eps += R else: eps += RootSum(q, Lambda(t, t * log(h.as_expr())), quadratic=True) result += eps return coeff * result
def ratint(f, x, **flags): """Performs indefinite integration of rational functions. Given a field K and a rational function f = p/q, where p and q are polynomials in K[x], returns a function g such that f = g'. >>> from sympy.integrals.rationaltools import ratint >>> from sympy.abc import x >>> ratint(36/(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2), x) -4*log(1 + x) + 4*log(-2 + x) - (6 + 12*x)/(1 - x**2) References ========== .. [Bro05] M. Bronstein, Symbolic Integration I: Transcendental Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70 """ if type(f) is not tuple: p, q = f.as_numer_denom() else: p, q = f p, q = Poly(p, x), Poly(q, x) c, p, q = p.cancel(q) poly, p = p.div(q) poly = poly.to_field() result = c * poly.integrate(x).as_basic() if p.is_zero: return result g, h = ratint_ratpart(p, q, x) P, Q = h.as_numer_denom() P = Poly(P, x) Q = Poly(Q, x) q, r = P.div(Q) result += g + q.integrate(x).as_basic() if not r.is_zero: symbol = flags.get('symbol', 't') if not isinstance(symbol, Symbol): t = Dummy(symbol) else: t = symbol L = ratint_logpart(r, Q, x, t) real = flags.get('real') if real is None: if type(f) is not tuple: atoms = f.atoms() else: p, q = f atoms = p.atoms() \ | q.atoms() for elt in atoms - set([x]): if not elt.is_real: real = False break else: real = True eps = S(0) if not real: for h, q in L: eps += RootSum(Lambda(t, t * log(h.as_basic())), q) else: for h, q in L: R = log_to_real(h, q, x, t) if R is not None: eps += R else: eps += RootSum(Lambda(t, t * log(h.as_basic())), q) result += eps return result
def test_latex_RootSum(): assert latex(RootSum(x**5 + x + 3, sin)) == \ r"\operatorname{RootSum}\left(x^{5} + x + 3, \operatorname{\Lambda}\left(x, \operatorname{sin}\left(x\right)\right)\right)"