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_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_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_doit(): rs = RootSum(x**2 + 1, Lambda(x, exp(x))) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-I) + exp(I) rs = RootSum(x**2 + a, Lambda(x, exp(x)), x) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
def test_RootSum_evalf(): rs = RootSum(x**2 + 1, Lambda(x, exp(x))) assert rs.evalf(20, chop=True).epsilon_eq( Float("1.0806046117362794348", 20), Float("1e-20")) is true assert rs.evalf(15, chop=True).epsilon_eq( Float("1.08060461173628", 15), Float("1e-15")) is true rs = RootSum(x**2 + a, Lambda(x, exp(x)), x) assert rs.evalf() == rs
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___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_pickling_polys_rootoftools(): f = x**3 + x + 3 for c in (RootOf, RootOf(f, 0)): check(c) for c in (RootSum, RootSum(f, Lambda(x, exp(x)))): check(c)
def test_RootSum_evalf(): rs = RootSum(x**2 + 1, Lambda(x, exp(x))) assert rs.evalf(20, chop=True).epsilon_eq( Float("1.0806046117362794348", 20), Float("1e-20")) is true assert rs.evalf(15, chop=True).epsilon_eq(Float("1.08060461173628", 15), Float("1e-15")) is true rs = RootSum(x**2 + a, Lambda(x, exp(x)), x) assert rs.evalf() == rs
def test_RootSum_doit(): rs = RootSum(x**2 + 1, Lambda(x, exp(x))) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-I) + exp(I) rs = RootSum(x**2 + a, Lambda(x, exp(x)), x) assert isinstance(rs, RootSum) is True assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-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}
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 sympy/sympy#5571 assert hash(RootSum((x - 7) * f**3, g)) == hash(log(7 * x) + 3 * RootSum(f, g)) pytest.raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y)) pytest.raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: 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))) == Rational(11, 3) assert RootSum(f, Lambda(x, y / (x + x**2))) == Rational(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))) assert isinstance( RootSum(x**7 + 2 * x + 1, Lambda(x, log(x))).doit(), RootSum)