def test_RootSum_diff(): f = x**3 + x + 3 g = Lambda(r, exp(r * x)) h = Lambda(r, r * exp(r * x)) assert RootSum(f, g).diff(x) == RootSum(f, h)
def test_RootSum_rational(): assert RootSum(z**5 - z + 1, Lambda(z, z/(x - z))) == (4*x - 5)/(x**5 - x + 1) f = 161*z**3 + 115*z**2 + 19*z + 1 g = Lambda(z, z*log(-3381*z**4/4 - 3381*z**3/4 - 625*z**2/2 - 125*z/2 - 5 + exp(x))) assert RootSum(f, g).diff(x) == -((5*exp(2*x) - 6*exp(x) + 4)*exp(x)/(exp(3*x) - exp(2*x) + 1))/7
def test_RootSum_evalf(): rs = RootSum(x**2 + 1, exp) assert rs.evalf(n=20, chop=True).epsilon_eq(Float("1.0806046117362794348")) assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628")) rs = RootSum(x**2 + a, exp, x) assert rs.evalf() == rs
def test_RootSum_subs(): f = x**3 + x + 3 g = Lambda(r, exp(r * x)) F = y**3 + y + 3 G = Lambda(r, exp(r * y)) assert RootSum(f, g).subs(y, 1) == RootSum(f, g) assert RootSum(f, g).subs(x, y) == RootSum(F, G)
def test_RootSum_doit(): rs = RootSum(x**2 + 1, exp) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-I) + exp(I) rs = RootSum(x**2 + a, exp, x) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
def test_RootSum_independent(): f = (x**3 - a)**2 * (x**4 - b)**3 g = Lambda(x, 5 * tan(x) + 7) h = Lambda(x, tan(x)) r0 = RootSum(x**3 - a, h, x) r1 = RootSum(x**4 - b, h, x) assert RootSum(f, g, x).as_ordered_terms() == [10 * r0, 15 * r1, 126]
def test_RootSum_evalf(): rs = RootSum(x**2 + 1, exp) assert rs.evalf(n=20, chop=True).epsilon_eq( Float("1.0806046117362794348", 20), Float("1e-20")) is S.true assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628", 15), Float("1e-15")) is S.true rs = RootSum(x**2 + a, exp, x) assert rs.evalf() == rs
def test_RootSum___eq__(): f = Lambda(x, exp(x)) assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 1, f)) is True assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 1, f)) is True assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 2, f)) is False assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 2, f)) is False
def test_polys(): x = Symbol("x") ZZ = PythonIntegerRing() QQ = SymPyRationalField() for c in (Poly, Poly(x, x)): check(c) for c in (GFP, GFP([ZZ(1), ZZ(2), ZZ(3)], ZZ(7), ZZ)): check(c) for c in (DMP, DMP([ZZ(1), ZZ(2), ZZ(3)], 0, ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)], ZZ))): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c) for c in (PythonIntegerRing, PythonIntegerRing()): check(c) for c in (SymPyIntegerRing, SymPyIntegerRing()): check(c) for c in (SymPyRationalField, SymPyRationalField()): check(c) for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')): check(c) for c in (FractionField, FractionField(ZZ, 'x', 'y')): check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c) from sympy.polys.domains import HAS_FRACTION, HAS_GMPY if HAS_FRACTION: from sympy.polys.domains import PythonRationalField for c in (PythonRationalField, PythonRationalField()): check(c) if HAS_GMPY: from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField for c in (GMPYIntegerRing, GMPYIntegerRing()): check(c) for c in (GMPYRationalField, GMPYRationalField()): check(c) f = x**3 + x + 3 g = lambda x: x for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)): check(c)
def test_pickling_polys_rootoftools(): from sympy.polys.rootoftools import CRootOf, RootSum x = Symbol('x') f = x**3 + x + 3 for c in (CRootOf, CRootOf(f, 0)): check(c) for c in (RootSum, RootSum(f, exp)): check(c)
def test_apart_full(): f = 1 / (x**2 + 1) assert apart(f, full=False) == f assert apart(f, full=True).dummy_eq( -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).dummy_eq( 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).dummy_eq(-RootSum( x**4 - x**3 + x**2 - x + 1, Lambda(a, a / (x - a)), auto=False) / 5 + (Rational(1, 5)) / (x + 1))
def test_polys(): x = Symbol("X") ZZ = PythonIntegerRing() QQ = PythonRationalField() for c in (Poly, Poly(x, x)): check(c) for c in (DMP, DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)]], ZZ)): check(c) for c in (DMF, DMF(([ZZ(1), ZZ(2)], [ZZ(1), ZZ(3)]), ZZ)): check(c) for c in (ANP, ANP([QQ(1), QQ(2)], [QQ(1), QQ(2), QQ(3)], QQ)): check(c) for c in (PythonIntegerRing, PythonIntegerRing()): check(c) for c in (PythonRationalField, PythonRationalField()): check(c) for c in (PolynomialRing, PolynomialRing(ZZ, 'x', 'y')): check(c) for c in (FractionField, FractionField(ZZ, 'x', 'y')): check(c) for c in (ExpressionDomain, ExpressionDomain()): check(c) from sympy.core.compatibility import HAS_GMPY if HAS_GMPY: from sympy.polys.domains import GMPYIntegerRing, GMPYRationalField for c in (GMPYIntegerRing, GMPYIntegerRing()): check(c) for c in (GMPYRationalField, GMPYRationalField()): check(c) f = x**3 + x + 3 g = exp for c in (RootOf, RootOf(f, 0), RootSum, RootSum(f, g)): check(c)
def test_RootSum___new__(): f = x**3 + x + 3 g = Lambda(r, log(r * x)) s = RootSum(f, g) assert isinstance(s, RootSum) is True assert RootSum(f**2, g) == 2 * RootSum(f, g) assert RootSum((x - 7) * f**3, g) == log(7 * x) + 3 * RootSum(f, g) # issue 5571 assert hash(RootSum((x - 7) * f**3, g)) == hash(log(7 * x) + 3 * RootSum(f, g)) raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y)) raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x)) assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x))) assert RootSum(f, log) == RootSum(f, Lambda(x, log(x))) assert isinstance(RootSum(f, auto=False), RootSum) is True assert RootSum(f) == 0 assert RootSum(f, Lambda(x, x)) == 0 assert RootSum(f, Lambda(x, x**2)) == -2 assert RootSum(f, Lambda(x, 1)) == 3 assert RootSum(f, Lambda(x, 2)) == 6 assert RootSum(f, auto=False).is_commutative is True assert RootSum(f, Lambda(x, 1 / (x + x**2))) == S(11) / 3 assert RootSum(f, Lambda(x, y / (x + x**2))) == S(11) / 3 * y assert RootSum(x**2 - 1, Lambda(x, 3 * x**2), x) == 6 assert RootSum(x**2 - y, Lambda(x, 3 * x**2), x) == 6 * y assert RootSum(x**2 - 1, Lambda(x, z * x**2), x) == 2 * z assert RootSum(x**2 - y, Lambda(x, z * x**2), x) == 2 * z * y assert RootSum(x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1) assert RootSum(x**3 + a*x + a**3, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(a*x))) assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \ RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
def ratint(f, x, **flags): """ Performs indefinite integration of rational functions. Explanation =========== 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'`. Examples ======== >>> 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 ========== .. [1] M. Bronstein, Symbolic Integration I: Transcendental Functions, Second Edition, Springer-Verlag, 2005, pp. 35-70 See Also ======== sympy.integrals.integrals.Integral.doit sympy.integrals.rationaltools.ratint_logpart sympy.integrals.rationaltools.ratint_ratpart """ if isinstance(f, tuple): p, q = f else: p, q = f.as_numer_denom() 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 isinstance(f, tuple): p, q = f atoms = p.atoms() | q.atoms() else: atoms = f.atoms() for elt in atoms - {x}: if not elt.is_extended_real: real = False break else: real = True eps = S.Zero if not real: for h, q in L: _, h = h.primitive() eps += RootSum(q, Lambda(t, t * log(h.as_expr())), quadratic=True) else: for h, q in L: _, h = h.primitive() 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 test_sympy__polys__rootoftools__RootSum(): from sympy.polys.rootoftools import RootSum assert _test_args(RootSum(x**3 + x + 1, sin))
def test_RootSum___new__(): f = x**3 + x + 3 g = Lambda(r, log(r * x)) s = RootSum(f, g) rootofs = sum(log(RootOf(f, i) * x) for i in (0, 1, 2)) assert isinstance(s, RootSum) == True assert s.doit() == rootofs assert RootSum(f**2, g) == 2 * RootSum(f, g) assert RootSum(f**2, g).doit() == 2 * rootofs assert RootSum((x - 7) * f**3, g) == log(7 * x) + 3 * RootSum(f, g) assert RootSum((x - 7) * f**3, g).doit() == log(7 * x) + 3 * rootofs raises(MultivariatePolynomialError, "RootSum(x**3 + x + y)") raises(ValueError, "RootSum(x**2 + 3, lambda x: x)") assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x))) assert RootSum(f, log) == RootSum(f, Lambda(x, log(x))) assert isinstance(RootSum(f, auto=False), RootSum) == True assert RootSum(f) == 0 assert RootSum(f, Lambda(x, x)) == 0 assert RootSum(f, Lambda(x, x**2)) == -2 assert RootSum(f, Lambda(x, 1)) == 3 assert RootSum(f, Lambda(x, 2)) == 6 assert RootSum(f, auto=False).is_commutative == True assert RootSum(f, Lambda(x, 1 / (x + x**2))) == S(11) / 3 assert RootSum(f, Lambda(x, y / (x + x**2))) == S(11) / 3 * y assert RootSum(x**2 - 1, Lambda(x, 3 * x**2), x) == 6 assert RootSum(x**2 - y, Lambda(x, 3 * x**2), x) == 6 * y assert RootSum(x**2 - 1, Lambda(x, z * x**2), x) == 2 * z assert RootSum(x**2 - y, Lambda(x, z * x**2), x) == 2 * z * y assert RootSum(x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1) assert RootSum(x**3 + a * x + a**3, tan, x) == RootSum(x**3 + x + 1, Lambda(x, tan(a * x))) assert RootSum(a**3 * x**3 + a * x + 1, tan, x) == RootSum(x**3 + x + 1, Lambda(x, tan(x / a)))
def test_RootSum_free_symbols(): assert RootSum(x**3 + x + 3, Lambda(r, exp(r))).free_symbols == set() assert RootSum(x**3 + x + 3, Lambda(r, exp(a * r))).free_symbols == {a} assert RootSum(x**3 + x + y, Lambda(r, exp(a * r)), x).free_symbols == {a, y}