def test_RootSum(): f = x**5 + 2 * x - 1 assert str(RootSum(f, Lambda(z, z), auto=False)) == 'RootSum(x**5 + 2*x - 1)' assert str(RootSum(f, Lambda( z, z**2), auto=False)) == 'RootSum(x**5 + 2*x - 1, Lambda(z, z**2))'
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_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_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 + 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_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_ratint(): assert ratint(Integer(0), x) == 0 assert ratint(Integer(7), x) == 7*x assert ratint(x, x) == x**2/2 assert ratint(2*x, x) == x**2 assert ratint(-2*x, x) == -x**2 assert ratint(8*x**7 + 2*x + 1, x) == x**8 + x**2 + x f = Integer(1) g = x + 1 assert ratint(f / g, x) == log(x + 1) assert ratint((f, g), x) == log(x + 1) f = x**3 - x g = x - 1 assert ratint(f/g, x) == x**3/3 + x**2/2 f = x g = (x - a)*(x + a) assert ratint(f/g, x) == log(x**2 - a**2)/2 f = Integer(1) g = x**2 + 1 assert ratint(f/g, x, extended_real=None) == atan(x) assert ratint(f/g, x, extended_real=True) == atan(x) assert ratint(f/g, x, extended_real=False) == I*log(x + I)/2 - I*log(x - I)/2 f = Integer(36) g = x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2 assert ratint(f/g, x) == \ -4*log(x + 1) + 4*log(x - 2) + (12*x + 6)/(x**2 - 1) f = x**4 - 3*x**2 + 6 g = x**6 - 5*x**4 + 5*x**2 + 4 assert ratint(f/g, x) == \ atan(x) + atan(x**3) + atan(x/2 - 3*x**3/2 + Rational(1, 2)*x**5) f = x**7 - 24*x**4 - 4*x**2 + 8*x - 8 g = x**8 + 6*x**6 + 12*x**4 + 8*x**2 assert ratint(f/g, x) == \ (4 + 6*x + 8*x**2 + 3*x**3)/(4*x + 4*x**3 + x**5) + log(x) assert ratint((x**3*f)/(x*g), x) == \ -(12 - 16*x + 6*x**2 - 14*x**3)/(4 + 4*x**2 + x**4) - \ 5*sqrt(2)*atan(x*sqrt(2)/2) + Rational(1, 2)*x**2 - 3*log(2 + x**2) f = x**5 - x**4 + 4*x**3 + x**2 - x + 5 g = x**4 - 2*x**3 + 5*x**2 - 4*x + 4 assert ratint(f/g, x) == \ x + Rational(1, 2)*x**2 + Rational(1, 2)*log(2 - x + x**2) - (4*x - 9)/(14 - 7*x + 7*x**2) + \ 13*sqrt(7)*atan(-Rational(1, 7)*sqrt(7) + 2*x*sqrt(7)/7)/49 assert ratint(1/(x**2 + x + 1), x) == \ 2*sqrt(3)*atan(sqrt(3)/3 + 2*x*sqrt(3)/3)/3 assert ratint(1/(x**3 + 1), x) == \ -log(1 - x + x**2)/6 + log(1 + x)/3 + sqrt(3)*atan(-sqrt(3) / 3 + 2*x*sqrt(3)/3)/3 assert ratint(1/(x**2 + x + 1), x, extended_real=False) == \ -I*3**half*log(half + x - half*I*3**half)/3 + \ I*3**half*log(half + x + half*I*3**half)/3 assert ratint(1/(x**3 + 1), x, extended_real=False) == log(1 + x)/3 + \ (-Rational(1, 6) + I*3**half/6)*log(-half + x + I*3**half/2) + \ (-Rational(1, 6) - I*3**half/6)*log(-half + x - I*3**half/2) # issue sympy/sympy#4991 assert ratint(1/(x*(a + b*x)**3), x) == \ (3*a + 2*b*x)/(2*a**4 + 4*a**3*b*x + 2*a**2*b**2*x**2) + ( log(x) - log(a/b + x))/a**3 assert ratint(x/(1 - x**2), x) == -log(x**2 - 1)/2 assert ratint(-x/(1 - x**2), x) == log(x**2 - 1)/2 assert ratint((x/4 - 4/(1 - x)).diff(x), x) == x/4 + 4/(x - 1) ans = atan(x) assert ratint(1/(x**2 + 1), x, symbol=x) == ans assert ratint(1/(x**2 + 1), x, symbol='x') == ans assert ratint(1/(x**2 + 1), x, symbol=a) == ans ans = (-sqrt(2)*log(x**2 + x*(-2 - sqrt(2)) + sqrt(2) + 2)/8 + sqrt(2)*log(x**2 + x*(-2 + sqrt(2)) - sqrt(2) + 2)/8 - sqrt(2)*atan(-sqrt(2)*x + 1 + sqrt(2))/4 + sqrt(2)*atan(sqrt(2)*x - sqrt(2) + 1)/4) assert ratint(1/((x - 1)**4 + 1), x) == ans ans = RootSum(776887*t**7 + 27216*t**5 - 15120*t**4 + 3780*t**3 - 504*t**2 + 35*t - 1, Lambda(t, t*log(x + 6041073312*t**6/117649 + 1006845552*t**5/117649 + 379439208*t**4/117649 - 54333252*t**3/117649 + 20337738*t**2/117649 - 529481*t/117649 + 46656/117649))) assert ratint(1/(x**7 - x + 1), x) == ans
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 diofant.integrals.rationaltools import ratint >>> from diofant.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 ======== diofant.integrals.integrals.Integral.doit diofant.integrals.rationaltools.ratint_logpart diofant.integrals.rationaltools.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) ereal = flags.get('extended_real') if ereal is None: if type(f) is not tuple: atoms = f.atoms() else: p, q = f atoms = p.atoms() | q.atoms() for elt in atoms - {x}: if not elt.is_extended_real: ereal = False break else: ereal = True eps = Integer(0) if not ereal: 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 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)
def test_RootSum(): r = RootSum(x**3 + x + 3, Lambda(y, log(y * z))) assert mathematica_code(r) == ('RootSum[Function[{x}, x^3 + x + 3], ' 'Function[{y}, Log[y*z]]]')