Exemple #1
0
def test_sympyissue_2708():
    # This test needs to use an integration function that can
    # not be evaluated in closed form.  Update as needed.
    f = 1/(a + z + log(z))
    integral_f = NonElementaryIntegral(f, (z, 2, 3))
    assert integrate(f + exp(z), (z, 2, 3)) == integral_f - exp(2) + exp(3)

    assert integrate(2*f + exp(z), (z, 2, 3)) == 2*integral_f - exp(2) + exp(3)
    assert (integrate(exp(1.2*n*s*z*(-t + z)/t), (z, 0, x)) ==
            1.0*NonElementaryIntegral(exp(-1.2*n*s*z)*exp(1.2*n*s*z**2/t),
                                      (z, 0, x)))
Exemple #2
0
def test_risch_option():
    # risch=True only allowed on indefinite integrals
    pytest.raises(ValueError, lambda: integrate(1/log(x), (x, 0, oo), risch=True))
    assert integrate(exp(-x**2), x, risch=True) == NonElementaryIntegral(exp(-x**2), x)
    assert integrate(log(1/x)*y, x, y, risch=True) == y**2*(x*log(1/x)/2 + x/2)
    assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
    # TODO: How to test risch=False?

    # issue sympy/sympy#2708
    f = 1/(a + z + log(z))
    integral_f = NonElementaryIntegral(f, (z, 2, 3))
    assert Integral(f, (z, 2, 3)).doit() == integral_f
Exemple #3
0
def test_NonElementaryIntegral():
    assert isinstance(risch_integrate(exp(x**2), x), NonElementaryIntegral)
    assert isinstance(risch_integrate(x**x * log(x), x), NonElementaryIntegral)
    # Make sure methods of Integral still give back a NonElementaryIntegral
    assert isinstance(
        NonElementaryIntegral(x**x * t0, x).subs({t0: log(x)}),
        NonElementaryIntegral)
Exemple #4
0
def test_risch_integrate():
    assert risch_integrate(t0 * exp(x), x) == t0 * exp(x)
    assert risch_integrate(
        sin(x), x, rewrite_complex=True) == -exp(I * x) / 2 - exp(-I * x) / 2

    # From my GSoC writeup
    assert risch_integrate((1 + 2*x**2 + x**4 + 2*x**3*exp(2*x**2)) /
                           (x**4*exp(x**2) + 2*x**2*exp(x**2) + exp(x**2)), x) == \
        NonElementaryIntegral(exp(-x**2), x) + exp(x**2)/(1 + x**2)

    assert risch_integrate(0, x) == 0

    # These are tested here in addition to in test_DifferentialExtension above
    # (symlogs) to test that backsubs works correctly.  The integrals should be
    # written in terms of the original logarithms in the integrands.

    # XXX: Unfortunately, making backsubs work on this one is a little
    # trickier, because x**x is converted to exp(x*log(x)), and so log(x**x)
    # is converted to x*log(x). (x**2*log(x)).subs({x*log(x): log(x**x)}) is
    # smart enough, the issue is that these splits happen at different places
    # in the algorithm.  Maybe a heuristic is in order
    assert risch_integrate(log(x**x), x) == x**2 * log(x) / 2 - x**2 / 4

    assert risch_integrate(log(x**y), x) == x * log(x**y) - x * y
    assert risch_integrate(log(sqrt(x)), x) == x * log(sqrt(x)) - x / 2
Exemple #5
0
def test_issue_2708():
    # This test needs to use an integration function that can
    # not be evaluated in closed form.  Update as needed.
    f = 1 / (a + z + log(z))
    integral_f = NonElementaryIntegral(f, (z, 2, 3))
    assert Integral(f, (z, 2, 3)).doit() == integral_f
    assert integrate(f + exp(z), (z, 2, 3)) == integral_f - exp(2) + exp(3)
Exemple #6
0
def test_risch_option():
    # risch=True only allowed on indefinite integrals
    pytest.raises(ValueError,
                  lambda: integrate(1 / log(x), (x, 0, oo), risch=True))
    assert integrate(exp(-x**2), x,
                     risch=True) == NonElementaryIntegral(exp(-x**2), x)
    assert integrate(log(1 / x) * y, x, y,
                     risch=True) == y**2 * (x * log(1 / x) / 2 + x / 2)
    assert integrate(erf(x), x, risch=True) == Integral(erf(x), x)
Exemple #7
0
def test_integrate_primitive():
    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(1 / x, t)],
        'Tfuncs': [log]
    })
    assert integrate_primitive(Poly(t, t), Poly(1, t),
                               DE) == (x * log(x), -1, True)
    assert integrate_primitive(Poly(x, t), Poly(t, t),
                               DE) == (0, NonElementaryIntegral(x / log(x),
                                                                x), False)

    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x),
                  Poly(1 / x, t1),
                  Poly(1 / (x + 1), t2)],
            'Tfuncs': [log, Lambda(i, log(i + 1))]
        })
    assert integrate_primitive(Poly(t1, t2), Poly(t2, t2), DE) == \
        (0, NonElementaryIntegral(log(x)/log(1 + x), x), False)

    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x),
                  Poly(1 / x, t1),
                  Poly(1 / (x * t1), t2)],
            'Tfuncs': [log, Lambda(i, log(log(i)))]
        })
    assert integrate_primitive(Poly(t2, t2), Poly(t1, t2), DE) == \
        (0, NonElementaryIntegral(log(log(x))/log(x), x), False)

    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(1 / x, t0)],
        'Tfuncs': [log]
    })
    assert integrate_primitive(Poly(x**2*t0**3 + (3*x**2 + x)*t0**2 + (3*x**2
                                                                       + 2*x)*t0 + x**2 + x, t0), Poly(x**2*t0**4 + 4*x**2*t0**3 + 6*x**2*t0**2 +
                                                                                                       4*x**2*t0 + x**2, t0), DE) == \
        (-1/(log(x) + 1), NonElementaryIntegral(1/(log(x) + 1), x), False)
Exemple #8
0
def test_xtothex():
    a = risch_integrate(x**x, x)
    assert a == NonElementaryIntegral(x**x, x)
    assert isinstance(a, NonElementaryIntegral)
Exemple #9
0
def test_integrate_hyperexponential():
    # TODO: Add tests for integrate_hyperexponential() from the book
    a = Poly(
        (1 + 2 * t1 + t1**2 + 2 * t1**3) * t**2 + (1 + t1**2) * t + 1 + t1**2,
        t)
    d = Poly(1, t)
    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x),
                  Poly(1 + t1**2, t1),
                  Poly(t * (1 + t1**2), t)],
            'Tfuncs': [tan, Lambda(i, exp(tan(i)))]
        })
    assert integrate_hyperexponential(a, d, DE) == \
        (exp(2*tan(x))*tan(x) + exp(tan(x)), 1 + t1**2, True)
    a = Poly((t1**3 + (x + 1) * t1**2 + t1 + x + 2) * t, t)
    assert integrate_hyperexponential(a, d, DE) == \
        ((x + tan(x))*exp(tan(x)), 0, True)

    a = Poly(t, t)
    d = Poly(1, t)
    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(2 * x * t, t)],
        'Tfuncs': [Lambda(i, exp(x**2))]
    })

    assert integrate_hyperexponential(a, d, DE) == \
        (0, NonElementaryIntegral(exp(x**2), x), False)

    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(t, t)],
        'Tfuncs': [exp]
    })
    assert integrate_hyperexponential(a, d, DE) == (exp(x), 0, True)

    a = Poly(
        25 * t**6 - 10 * t**5 + 7 * t**4 - 8 * t**3 + 13 * t**2 + 2 * t - 1, t)
    d = Poly(25 * t**6 + 35 * t**4 + 11 * t**2 + 1, t)
    assert integrate_hyperexponential(a, d, DE) == \
        (-(11 - 10*exp(x))/(5 + 25*exp(2*x)) + log(1 + exp(2*x)), -1, True)
    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x), Poly(t0, t0),
                  Poly(t0 * t, t)],
            'Tfuncs': [exp, Lambda(i, exp(exp(i)))]
        })
    assert integrate_hyperexponential(Poly(2 * t0 * t**2, t), Poly(1, t),
                                      DE) == (exp(2 * exp(x)), 0, True)

    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x), Poly(t0, t0),
                  Poly(-t0 * t, t)],
            'Tfuncs': [exp, Lambda(i, exp(-exp(i)))]
        })
    assert integrate_hyperexponential(Poly(-27*exp(9) - 162*t0*exp(9) +
                                           27*x*t0*exp(9), t), Poly((36*exp(18) + x**2*exp(18) - 12*x*exp(18))*t, t), DE) == \
        (27*exp(exp(x))/(-6*exp(9) + x*exp(9)), 0, True)

    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(t, t)],
        'Tfuncs': [exp]
    })
    assert integrate_hyperexponential(Poly(x**2/2*t, t), Poly(1, t), DE) == \
        ((2 - 2*x + x**2)*exp(x)/2, 0, True)
    assert integrate_hyperexponential(Poly(1 + t, t), Poly(t, t), DE) == \
        (-exp(-x), 1, True)  # x - exp(-x)
    assert integrate_hyperexponential(Poly(x, t), Poly(t + 1, t), DE) == \
        (0, NonElementaryIntegral(x/(1 + exp(x)), x), False)

    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x),
                  Poly(1 / x, t0),
                  Poly(2 * x * t1, t1)],
            'Tfuncs': [log, Lambda(i, exp(i**2))]
        })

    elem, nonelem, b = integrate_hyperexponential(
        Poly(
            (8 * x**7 - 12 * x**5 + 6 * x**3 - x) * t1**4 +
            (8 * t0 * x**7 - 8 * t0 * x**6 - 4 * t0 * x**5 + 2 * t0 * x**3 +
             2 * t0 * x**2 - t0 * x + 24 * x**8 - 36 * x**6 - 4 * x**5 +
             22 * x**4 + 4 * x**3 - 7 * x**2 - x + 1) * t1**3 +
            (8 * t0 * x**8 - 4 * t0 * x**6 - 16 * t0 * x**5 - 2 * t0 * x**4 +
             12 * t0 * x**3 + t0 * x**2 - 2 * t0 * x + 24 * x**9 - 36 * x**7 -
             8 * x**6 + 22 * x**5 + 12 * x**4 - 7 * x**3 - 6 * x**2 + x + 1) *
            t1**2 + (8 * t0 * x**8 - 8 * t0 * x**6 - 16 * t0 * x**5 +
                     6 * t0 * x**4 + 10 * t0 * x**3 - 2 * t0 * x**2 - t0 * x +
                     8 * x**10 - 12 * x**8 - 4 * x**7 + 2 * x**6 + 12 * x**5 +
                     3 * x**4 - 9 * x**3 - x**2 + 2 * x) * t1 + 8 * t0 * x**7 -
            12 * t0 * x**6 - 4 * t0 * x**5 + 8 * t0 * x**4 - t0 * x**2 -
            4 * x**7 + 4 * x**6 + 4 * x**5 - 4 * x**4 - x**3 + x**2, t1),
        Poly((8 * x**7 - 12 * x**5 + 6 * x**3 - x) * t1**4 +
             (24 * x**8 + 8 * x**7 - 36 * x**6 - 12 * x**5 + 18 * x**4 +
              6 * x**3 - 3 * x**2 - x) * t1**3 +
             (24 * x**9 + 24 * x**8 - 36 * x**7 - 36 * x**6 + 18 * x**5 +
              18 * x**4 - 3 * x**3 - 3 * x**2) * t1**2 +
             (8 * x**10 + 24 * x**9 - 12 * x**8 - 36 * x**7 + 6 * x**6 +
              18 * x**5 - x**4 - 3 * x**3) * t1 + 8 * x**10 - 12 * x**8 +
             6 * x**6 - x**4, t1), DE)

    assert factor(elem) == -((x - 1) * log(x) / ((x + exp(x**2)) *
                                                 (2 * x**2 - 1)))
    assert (nonelem,
            b) == (NonElementaryIntegral(exp(x**2) / (exp(x**2) + 1),
                                         x), False)