Beispiel #1
0
def test_prde_special_denom():
    a = Poly(t + 1, t)
    ba = Poly(t**2, t)
    bd = Poly(1, t)
    G = [(Poly(t, t), Poly(1, t)), (Poly(t**2, t), Poly(1, t)), (Poly(t**3, t), Poly(1, t))]
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert prde_special_denom(a, ba, bd, G, DE) == \
        (Poly(t + 1, t), Poly(t**2, t), [(Poly(t, t), Poly(1, t)),
        (Poly(t**2, t), Poly(1, t)), (Poly(t**3, t), Poly(1, t))], Poly(1, t))
    G = [(Poly(t, t), Poly(1, t)), (Poly(1, t), Poly(t, t))]
    assert prde_special_denom(Poly(1, t), Poly(t**2, t), Poly(1, t), G, DE) == \
        (Poly(1, t), Poly(t**2 - 1, t), [(Poly(t**2, t), Poly(1, t)),
        (Poly(1, t), Poly(1, t))], Poly(t, t))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-2*x*t0, t0)]})
    DE.decrement_level()
    G = [(Poly(t, t), Poly(t**2, t)), (Poly(2*t, t), Poly(t, t))]
    assert prde_special_denom(Poly(5*x*t + 1, t), Poly(t**2 + 2*x**3*t, t), Poly(t**3 + 2, t), G, DE) == \
        (Poly(5*x*t + 1, t), Poly(0, t), [(Poly(t, t), Poly(t**2, t)),
        (Poly(2*t, t), Poly(t, t))], Poly(1, x))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly((t**2 + 1)*2*x, t)]})
    G = [(Poly(t + x, t), Poly(t*x, t)), (Poly(2*t, t), Poly(x**2, x))]
    assert prde_special_denom(Poly(5*x*t + 1, t), Poly(t**2 + 2*x**3*t, t), Poly(t**3, t), G, DE) == \
        (Poly(5*x*t + 1, t), Poly(0, t), [(Poly(t + x, t), Poly(x*t, t)),
        (Poly(2*t, t, x), Poly(x**2, t, x))], Poly(1, t))
    assert prde_special_denom(Poly(t + 1, t), Poly(t**2, t), Poly(t**3, t), G, DE) == \
        (Poly(t + 1, t), Poly(0, t), [(Poly(t + x, t), Poly(x*t, t)), (Poly(2*t, t, x),
        Poly(x**2, t, x))], Poly(1, t))
Beispiel #2
0
def test_rischDE():
    # TODO: Add more tests for rischDE, including ones from the text
    DE = DifferentialExtension(extension={'D':[Poly(1, x), Poly(t, t)]})
    DE.decrement_level()
    assert rischDE(Poly(-2*x, x), Poly(1, x), Poly(1 - 2*x - 2*x**2, x),
    Poly(1, x), DE) == \
        (Poly(x + 1, x), Poly(1, x))
Beispiel #3
0
def test_DifferentialExtension_all_attrs():
    # Test 'unimportant' attributes
    DE = DifferentialExtension(exp(x)*log(x), x, handle_first='exp')
    assert DE.f == exp(x)*log(x)
    assert DE.newf == t0*t1
    assert DE.x == x
    assert DE.cases == ['base', 'exp', 'primitive']
    assert DE.case == 'primitive'

    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1/x, t1) == DE.D[DE.level]
    raises(ValueError, lambda: DE.increment_level())
    DE.decrement_level()
    assert DE.level == -2
    assert DE.t == t0 == DE.T[DE.level]
    assert DE.d == Poly(t0, t0) == DE.D[DE.level]
    assert DE.case == 'exp'
    DE.decrement_level()
    assert DE.level == -3
    assert DE.t == x == DE.T[DE.level] == DE.x
    assert DE.d == Poly(1, x) == DE.D[DE.level]
    assert DE.case == 'base'
    raises(ValueError, lambda: DE.decrement_level())
    DE.increment_level()
    DE.increment_level()
    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1/x, t1) == DE.D[DE.level]
    assert DE.case == 'primitive'

    # Test methods
    assert DE.indices('log') == [2]
    assert DE.indices('exp') == [1]
Beispiel #4
0
def test_DifferentialExtension_all_attrs():
    # Test 'unimportant' attributes
    DE = DifferentialExtension(exp(x) * log(x), x, dummy=False, handle_first="exp")
    assert DE.f == exp(x) * log(x)
    assert DE.newf == t0 * t1
    assert DE.x == x
    assert DE.cases == ["base", "exp", "primitive"]
    assert DE.case == "primitive"

    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1 / x, t1) == DE.D[DE.level]
    raises(ValueError, lambda: DE.increment_level())
    DE.decrement_level()
    assert DE.level == -2
    assert DE.t == t0 == DE.T[DE.level]
    assert DE.d == Poly(t0, t0) == DE.D[DE.level]
    assert DE.case == "exp"
    DE.decrement_level()
    assert DE.level == -3
    assert DE.t == x == DE.T[DE.level] == DE.x
    assert DE.d == Poly(1, x) == DE.D[DE.level]
    assert DE.case == "base"
    raises(ValueError, lambda: DE.decrement_level())
    DE.increment_level()
    DE.increment_level()
    assert DE.level == -1
    assert DE.t == t1 == DE.T[DE.level]
    assert DE.d == Poly(1 / x, t1) == DE.D[DE.level]
    assert DE.case == "primitive"
Beispiel #5
0
def test_special_denom():
    # TODO: add more tests here
    DE = DifferentialExtension(extension={'D':[Poly(1, x), Poly(t, t)]})
    assert special_denom(Poly(1, t), Poly(t**2, t), Poly(1, t), Poly(t**2 - 1, t),
    Poly(t, t), DE) == \
        (Poly(1, t), Poly(t**2 - 1, t), Poly(t**2 - 1, t), Poly(t, t))
#    assert special_denom(Poly(1, t), Poly(2*x, t), Poly((1 + 2*x)*t, t), DE) == 1

    # Issue 841
    # Note, this isn't a very good test, because the denominator is just 1,
    # but at least it tests the exp cancellation case
    DE = DifferentialExtension(extension={'D':[Poly(1, x), Poly(-2*x*t0, t0),
        Poly(I*k*t1, t1)]})
    DE.decrement_level()
    assert special_denom(Poly(1, t0), Poly(I*k, t0), Poly(1, t0), Poly(t0, t0),
    Poly(1, t0), DE) == \
        (Poly(1, t0), Poly(I*k, t0), Poly(t0, t0), Poly(1, t0))
Beispiel #6
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)
Beispiel #7
0
def test_residue_reduce():
    a = Poly(2 * t**2 - t - x**2, t)
    d = Poly(t**3 - x**2 * t, t)
    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(1 / x, t)],
        'Tfuncs': [log]
    })
    assert residue_reduce(a, d, DE, z, invert=False) == \
        ([(Poly(z**2 - S(1)/4, z), Poly((1 + 3*x*z - 6*z**2 -
        2*x**2 + 4*x**2*z**2)*t - x*z + x**2 + 2*x**2*z**2 - 2*z*x**3, t))], False)
    assert residue_reduce(a, d, DE, z, invert=True) == \
        ([(Poly(z**2 - S(1)/4, z), Poly(t + 2*x*z, t))], False)
    assert residue_reduce(Poly(-2/x, t), Poly(t**2 - 1, t,), DE, z, invert=False) == \
        ([(Poly(z**2 - 1, z), Poly(-2*z*t/x - 2/x, t))], True)
    ans = residue_reduce(Poly(-2 / x, t),
                         Poly(t**2 - 1, t),
                         DE,
                         z,
                         invert=True)
    assert ans == ([(Poly(z**2 - 1, z), Poly(t + z, t))], True)
    assert residue_reduce_to_basic(ans[0], DE,
                                   z) == -log(-1 + log(x)) + log(1 + log(x))

    DE = DifferentialExtension(extension={
        'D': [Poly(1, x),
              Poly(-t**2 - t / x - (1 - nu**2 / x**2), t)]
    })
    # TODO: Skip or make faster
    assert residue_reduce(Poly((-2*nu**2 - x**4)/(2*x**2)*t - (1 + x**2)/x, t),
    Poly(t**2 + 1 + x**2/2, t), DE, z) == \
        ([(Poly(z + S(1)/2, z, domain='QQ'), Poly(t**2 + 1 + x**2/2, t, domain='EX'))], True)
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x), Poly(1 + t**2, t)]})
    assert residue_reduce(Poly(-2*x*t + 1 - x**2, t),
    Poly(t**2 + 2*x*t + 1 + x**2, t), DE, z) == \
        ([(Poly(z**2 + S(1)/4, z), Poly(t + x + 2*z, t))], True)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert residue_reduce(Poly(t, t), Poly(t + sqrt(2), t), DE, z) == \
        ([(Poly(z - 1, z), Poly(t + sqrt(2), t))], True)
Beispiel #8
0
def test_DifferentialExtension_extension_flag():
    raises(ValueError, lambda: DifferentialExtension(extension={'T': [x, t]}))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert DE._important_attrs == (None, None, [Poly(1, x),
                                                Poly(t, t)], [x, t], None,
                                   None, None, None)
    assert DE.d == Poly(t, t)
    assert DE.t == t
    assert DE.level == -1
    assert DE.cases == ['base', 'exp']
    assert DE.x == x
    assert DE.case == 'exp'

    DE = DifferentialExtension(extension={
        'D': [Poly(1, x), Poly(t, t)],
        'exts': [None, 'exp'],
        'extargs': [None, x]
    })
    assert DE._important_attrs == (None, None, [Poly(1, x),
                                                Poly(t, t)], [x, t], None,
                                   None, [None, 'exp'], [None, x])
    raises(ValueError, lambda: DifferentialExtension())
Beispiel #9
0
def test_DifferentialExtension_misc():
    # Odd ends
    assert DifferentialExtension(sin(y)*exp(x), x, dummy=False)._important_attrs == \
        (Poly(sin(y)*t0, t0, domain='ZZ[sin(y)]'), Poly(1, t0, domain='ZZ'),
        [Poly(1, x, domain='ZZ'), Poly(t0, t0, domain='ZZ')], [x, t0],
        [Lambda(i, exp(i))], [], [1], [x], [], [])
    raises(NotImplementedError, lambda: DifferentialExtension(sin(x), x))
    assert DifferentialExtension(10**x, x, dummy=False)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(log(10)*t0, t0)], [x, t0],
        [Lambda(i, exp(i*log(10)))], [(exp(x*log(10)), 10**x)], [1], [x*log(10)],
        [], [])
    assert DifferentialExtension(
        log(x) + log(x**2), x, dummy=False)._important_attrs in [
            (Poly(3 * t0, t0), Poly(2, t0), [Poly(1, x),
                                             Poly(2 / x, t0)], [x, t0],
             [Lambda(i, log(i**2))], [], [], [], [1], [x**2]),
            (Poly(3 * t0, t0), Poly(1, t0), [Poly(1, x),
                                             Poly(1 / x, t0)], [x, t0],
             [Lambda(i, log(i))], [], [], [], [1], [x])
        ]
    assert DifferentialExtension(S.Zero, x, dummy=False)._important_attrs == \
        (Poly(0, x), Poly(1, x), [Poly(1, x)], [x], [], [], [], [], [], [])
Beispiel #10
0
def test_DifferentialExtension_handle_first():
    assert DifferentialExtension(exp(x)*log(x), x, handle_first='log',
    dummy=False)._important_attrs == \
        (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(1/x, t0),
        Poly(t1, t1)], [x, t0, t1], [Lambda(i, log(i)), Lambda(i, exp(i))],
        [], [2], [x], [1], [x])
    assert DifferentialExtension(exp(x)*log(x), x, handle_first='exp',
    dummy=False)._important_attrs == \
        (Poly(t0*t1, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0),
        Poly(1/x, t1)], [x, t0, t1], [Lambda(i, exp(i)), Lambda(i, log(i))],
        [], [1], [x], [2], [x])

    # This one must have the log first, regardless of what we set it to
    # (because the log is inside of the exponential: x**x == exp(x*log(x)))
    assert DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x,
    handle_first='exp', dummy=False)._important_attrs == \
        DifferentialExtension(-x**x*log(x)**2 + x**x - x**x/x, x,
        handle_first='log', dummy=False)._important_attrs == \
        (Poly((-1 + x - x*t0**2)*t1, t1), Poly(x, t1),
            [Poly(1, x), Poly(1/x, t0), Poly((1 + t0)*t1, t1)], [x, t0, t1],
            [Lambda(i, log(i)), Lambda(i, exp(t0*i))], [(exp(x*log(x)), x**x)],
            [2], [t0*x], [1], [x])
Beispiel #11
0
def test_DifferentialExtension_extension_flag():
    raises(ValueError, lambda: DifferentialExtension(extension={"T": [x, t]}))
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(t, t)]})
    assert DE._important_attrs == (
        None,
        None,
        [Poly(1, x), Poly(t, t)],
        [x, t],
        None,
        None,
        None,
        None,
    )
    assert DE.d == Poly(t, t)
    assert DE.t == t
    assert DE.level == -1
    assert DE.cases == ["base", "exp"]
    assert DE.x == x
    assert DE.case == "exp"

    DE = DifferentialExtension(
        extension={
            "D": [Poly(1, x), Poly(t, t)],
            "exts": [None, "exp"],
            "extargs": [None, x],
        }
    )
    assert DE._important_attrs == (
        None,
        None,
        [Poly(1, x), Poly(t, t)],
        [x, t],
        None,
        None,
        [None, "exp"],
        [None, x],
    )
    raises(ValueError, lambda: DifferentialExtension())
Beispiel #12
0
def test_prde_cancel_liouvillian():
    ### 1. case == 'primitive'
    # used when integrating f = log(x) - log(x - 1)
    # Not taken from 'the' book
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(1 / x, t)]})
    p0 = Poly(0, t, field=True)
    h, A = prde_cancel_liouvillian(
        Poly(-1 / (x - 1), t), [Poly(-x + 1, t), Poly(1, t)], 1, DE
    )
    V = A.nullspace()
    h == [
        p0,
        p0,
        Poly((x - 1) * t, t),
        p0,
        p0,
        p0,
        p0,
        p0,
        p0,
        p0,
        Poly(x - 1, t),
        Poly(-(x ** 2) + x, t),
        p0,
        p0,
        p0,
        p0,
    ]
    assert A.rank() == 16
    assert (Matrix([h]) * V[0][:16, :]) == Matrix([[Poly(0, t, domain="QQ(x)")]])

    ### 2. case == 'exp'
    # used when integrating log(x/exp(x) + 1)
    # Not taken from book
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(-t, t)]})
    assert prde_cancel_liouvillian(
        Poly(0, t, domain="QQ[x]"), [Poly(1, t, domain="QQ(x)")], 0, DE
    ) == ([Poly(1, t, domain="QQ"), Poly(x, t)], Matrix([[-1, 0, 1]]))
Beispiel #13
0
def test_derivation():
    p = Poly(4*x**4*t**5 + (-4*x**3 - 4*x**4)*t**4 + (-3*x**2 + 2*x**3)*t**3 +
        (2*x + 7*x**2 + 2*x**3)*t**2 + (1 - 4*x - 4*x**2)*t - 1 + 2*x, t)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-t**2 - 3/(2*x)*t + 1/(2*x), t)]})
    assert derivation(p, DE) == Poly(-20*x**4*t**6 + (2*x**3 + 16*x**4)*t**5 +
        (21*x**2 + 12*x**3)*t**4 + (7*x/2 - 25*x**2 - 12*x**3)*t**3 +
        (-5 - 15*x/2 + 7*x**2)*t**2 - (3 - 8*x - 10*x**2 - 4*x**3)/(2*x)*t +
        (1 - 4*x**2)/(2*x), t)
    assert derivation(Poly(1, t), DE) == Poly(0, t)
    assert derivation(Poly(t, t), DE) == DE.d
    assert derivation(Poly(t**2 + 1/x*t + (1 - 2*x)/(4*x**2), t), DE) == \
        Poly(-2*t**3 - 4/x*t**2 - (5 - 2*x)/(2*x**2)*t - (1 - 2*x)/(2*x**3), t, domain='ZZ(x)')
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1/x, t1), Poly(t, t)]})
    assert derivation(Poly(x*t*t1, t), DE) == Poly(t*t1 + x*t*t1 + t, t)
    assert derivation(Poly(x*t*t1, t), DE, coefficientD=True) == \
        Poly((1 + t1)*t, t)
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert derivation(Poly(x, x), DE) == Poly(1, x)
    # Test basic option
    assert derivation((x + 1)/(x - 1), DE, basic=True) == -2/(1 - 2*x + x**2)
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert derivation((t + 1)/(t - 1), DE, basic=True) == -2*t/(1 - 2*t + t**2)
    assert derivation(t + 1, DE, basic=True) == t
Beispiel #14
0
def test_DifferentialExtension_printing():
    DE = DifferentialExtension(exp(2*x**2) + log(exp(x**2) + 1), x)
    assert repr(DE) == ("DifferentialExtension(dict([('f', exp(2*x**2) + log(exp(x**2) + 1)), "
        "('x', x), ('T', [x, t0, t1]), ('D', [Poly(1, x, domain='ZZ'), Poly(2*x*t0, t0, domain='ZZ[x]'), "
        "Poly(2*t0*x/(t0 + 1), t1, domain='ZZ(x,t0)')]), ('fa', Poly(t1 + t0**2, t1, domain='ZZ[t0]')), "
        "('fd', Poly(1, t1, domain='ZZ')), ('Tfuncs', [Lambda(i, exp(i**2)), Lambda(i, log(t0 + 1))]), "
        "('backsubs', []), ('exts', [None, 'exp', 'log']), ('extargs', [None, x**2, t0 + 1]), "
        "('cases', ['base', 'exp', 'primitive']), ('case', 'primitive'), ('t', t1), "
        "('d', Poly(2*t0*x/(t0 + 1), t1, domain='ZZ(x,t0)')), ('newf', t0**2 + t1), ('level', -1), "
        "('dummy', False)]))")

    assert str(DE) == ("DifferentialExtension({fa=Poly(t1 + t0**2, t1, domain='ZZ[t0]'), "
        "fd=Poly(1, t1, domain='ZZ'), D=[Poly(1, x, domain='ZZ'), Poly(2*x*t0, t0, domain='ZZ[x]'), "
        "Poly(2*t0*x/(t0 + 1), t1, domain='ZZ(x,t0)')]})")
Beispiel #15
0
def test_prde_special_denom():
    a = Poly(t + 1, t)
    ba = Poly(t**2, t)
    bd = Poly(1, t)
    G = [(Poly(t, t), Poly(1, t)), (Poly(t**2, t), Poly(1, t)),
         (Poly(t**3, t), Poly(1, t))]
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert prde_special_denom(a, ba, bd, G, DE) == \
        (Poly(t + 1, t), Poly(t**2, t), [(Poly(t, t), Poly(1, t)),
        (Poly(t**2, t), Poly(1, t)), (Poly(t**3, t), Poly(1, t))], Poly(1, t))
    G = [(Poly(t, t), Poly(1, t)), (Poly(1, t), Poly(t, t))]
    assert prde_special_denom(Poly(1, t), Poly(t**2, t), Poly(1, t), G, DE) == \
        (Poly(1, t), Poly(t**2 - 1, t), [(Poly(t**2, t), Poly(1, t)),
        (Poly(1, t), Poly(1, t))], Poly(t, t))
Beispiel #16
0
def test_limited_integrate():
    DE = DifferentialExtension(extension={"D": [Poly(1, x)]})
    G = [(Poly(x, x), Poly(x + 1, x))]
    assert limited_integrate(
        Poly(-(1 + x + 5 * x ** 2 - 3 * x ** 3), x),
        Poly(1 - x - x ** 2 + x ** 3, x),
        G,
        DE,
    ) == ((Poly(x ** 2 - x + 2, x), Poly(x - 1, x)), [2])
    G = [(Poly(1, x), Poly(x, x))]
    assert limited_integrate(Poly(5 * x ** 2, x), Poly(3, x), G, DE) == (
        (Poly(5 * x ** 3 / 9, x), Poly(1, x)),
        [0],
    )
Beispiel #17
0
def test_DifferentialExtension_log():
    assert DifferentialExtension(
        log(x) * log(x + 1) * log(2 * x ** 2 + 2 * x), x
    )._important_attrs == (
        Poly(t0 * t1 ** 2 + (t0 * log(2) + t0 ** 2) * t1, t1),
        Poly(1, t1),
        [Poly(1, x), Poly(1 / x, t0), Poly(1 / (x + 1), t1, expand=False)],
        [x, t0, t1],
        [Lambda(i, log(i)), Lambda(i, log(i + 1))],
        [],
        [None, "log", "log"],
        [None, x, x + 1],
    )
    assert DifferentialExtension(x ** x * log(x), x)._important_attrs == (
        Poly(t0 * t1, t1),
        Poly(1, t1),
        [Poly(1, x), Poly(1 / x, t0), Poly((1 + t0) * t1, t1)],
        [x, t0, t1],
        [Lambda(i, log(i)), Lambda(i, exp(t0 * i))],
        [(exp(x * log(x)), x ** x)],
        [None, "log", "exp"],
        [None, x, t0 * x],
    )
Beispiel #18
0
def test_constant_system():
    A = Matrix(
        [
            [-(x + 3) / (x - 1), (x + 1) / (x - 1), 1],
            [-x - 3, x + 1, x - 1],
            [2 * (x + 3) / (x - 1), 0, 0],
        ]
    )
    u = Matrix([(x + 1) / (x - 1), x + 1, 0])
    DE = DifferentialExtension(extension={"D": [Poly(1, x)]})
    assert constant_system(A, u, DE) == (
        Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 0], [0, 0, 1]]),
        Matrix([0, 1, 0, 0]),
    )
Beispiel #19
0
def test_laurent_series():
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(1, t)]})
    a = Poly(36, t)
    d = Poly((t - 2) * (t ** 2 - 1) ** 2, t)
    F = Poly(t ** 2 - 1, t)
    n = 2
    assert laurent_series(a, d, F, n, DE) == (
        Poly(-3 * t ** 3 + 3 * t ** 2 - 6 * t - 8, t),
        Poly(t ** 5 + t ** 4 - 2 * t ** 3 - 2 * t ** 2 + t + 1, t),
        [
            Poly(-3 * t ** 3 - 6 * t ** 2, t),
            Poly(2 * t ** 6 + 6 * t ** 5 - 8 * t ** 3, t),
        ],
    )
Beispiel #20
0
def test_DifferentialExtension_exp():
    i = Symbol('i')
    assert DifferentialExtension(exp(x) + exp(x**2), x, dummy=False)._important_attrs == \
        (Poly(t1 + t0, t1), Poly(1, t1), [Poly(1, x,), Poly(t0, t0),
        Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [1, 2], [x, x**2], [], [])
    assert DifferentialExtension(exp(x) + exp(2*x), x, dummy=False)._important_attrs == \
        (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0, t0)], [x, t0],
        [Lambda(i, exp(i))], [], [1], [x], [], [])
    assert DifferentialExtension(exp(x) + exp(x/2), x, dummy=False)._important_attrs == \
        (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)],
        [x, t0], [Lambda(i, exp(i/2))], [], [1], [x/2], [], [])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2), x,
            dummy=False)._important_attrs == \
        (Poly((1 + t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0),
        Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [1, 2], [x, x**2], [], [])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2 + 1), x,
            dummy=False)._important_attrs == \
        (Poly((1 + S.Exp1*t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [1, 2], [x, x**2], [], [])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2), x,
            dummy=False)._important_attrs == \
        (Poly((t0 + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1],
        [Lambda(i, exp(i/2)), Lambda(i, exp(i**2))],
        [(exp(x/2), sqrt(exp(x)))], [1, 2], [x/2, x**2], [], [])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2 + 3), x,
    dummy=False)._important_attrs == \
        (Poly((t0*exp(3) + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i/2)),
        Lambda(i, exp(i**2))], [(exp(x/2), sqrt(exp(x)))], [1, 2], [x/2, x**2],
        [], [])
    assert DifferentialExtension(sqrt(exp(x)), x, dummy=False)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0],
        [Lambda(i, exp(i/2))], [(exp(x/2), sqrt(exp(x)))], [1], [x/2], [], [])

    assert DifferentialExtension(exp(x/2), x, dummy=False)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0],
        [Lambda(i, exp(i/2))], [], [1], [x/2], [], [])
Beispiel #21
0
def test_prde_no_cancel():
    # b large
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert prde_no_cancel_b_large(Poly(1, x), [Poly(x**2, x), Poly(1, x)], 2, DE) == \
        ([Poly(x**2 - 2*x + 2, x), Poly(1, x)], Matrix([[1, 0, -1, 0],
                                                        [0, 1, 0, -1]]))
    assert prde_no_cancel_b_large(Poly(1, x), [Poly(x**3, x), Poly(1, x)], 3, DE) == \
        ([Poly(x**3 - 3*x**2 + 6*x - 6, x), Poly(1, x)], Matrix([[1, 0, -1, 0],
                                                                 [0, 1, 0, -1]]))
    # b small
    # XXX: Is there a better example of a monomial with D.degree() > 2?
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x), Poly(t**3 + 1, t)]})

    # My original q was t**4 + t + 1, but this solution implies q == t**4
    # (c1 = 4), with some of the ci for the original q equal to 0.
    G = [
        Poly(t**6, t),
        Poly(x * t**5, t),
        Poly(t**3, t),
        Poly(x * t**2, t),
        Poly(1 + x, t)
    ]
    assert prde_no_cancel_b_small(Poly(x*t, t), G, 4, DE) == \
        ([Poly(t**4/4 - x/12*t**3 + x**2/24*t**2 + (-S(11)/12 - x**3/24)*t + x/24, t),
        Poly(x/3*t**3 - x**2/6*t**2 + (-S(1)/3 + x**3/6)*t - x/6, t), Poly(t, t),
        Poly(0, t), Poly(0, t)], Matrix([[1, 0,      -1, 0, 0,  0,  0,  0,  0,  0],
                                         [0, 1, -S(1)/4, 0, 0,  0,  0,  0,  0,  0],
                                         [0, 0,       0, 0, 0,  0,  0,  0,  0,  0],
                                         [0, 0,       0, 1, 0,  0,  0,  0,  0,  0],
                                         [0, 0,       0, 0, 1,  0,  0,  0,  0,  0],
                                         [1, 0,       0, 0, 0, -1,  0,  0,  0,  0],
                                         [0, 1,       0, 0, 0,  0, -1,  0,  0,  0],
                                         [0, 0,       1, 0, 0,  0,  0, -1,  0,  0],
                                         [0, 0,       0, 1, 0,  0,  0,  0, -1,  0],
                                         [0, 0,       0, 0, 1,  0,  0,  0,  0, -1]]))
Beispiel #22
0
def test_integrate_nonlinear_no_specials():
    a, d, = Poly(
        x**2 * t**5 + x * t**4 - nu**2 * t**3 - x * (x**2 + 1) * t**2 -
        (x**2 - nu**2) * t - x**5 / 4, t), Poly(
            x**2 * t**4 + x**2 * (x**2 + 2) * t**2 + x**2 + x**4 + x**6 / 4, t)
    # f(x) == phi_nu(x), the logarithmic derivative of J_v, the Bessel function,
    # which has no specials (see Chapter 5, note 4 of Bronstein's book).
    f = Function('phi_nu')
    DE = DifferentialExtension(
        extension={
            'D': [Poly(1, x),
                  Poly(-t**2 - t / x - (1 - nu**2 / x**2), t)],
            'Tfuncs': [f]
        })
    assert integrate_nonlinear_no_specials(a, d, DE) == \
        (-log(1 + f(x)**2 + x**2/2)/2 - (4 + x**2)/(4 + 2*x**2 + 4*f(x)**2), True)
    assert integrate_nonlinear_no_specials(Poly(t, t), Poly(1, t), DE) == \
        (0, False)
Beispiel #23
0
def test_DifferentialExtension_exp():
    assert DifferentialExtension(exp(x) + exp(x**2), x)._important_attrs == \
        (Poly(t1 + t0, t1), Poly(1, t1), [Poly(1, x,), Poly(t0, t0),
        Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2])
    assert DifferentialExtension(exp(x) + exp(2*x), x)._important_attrs == \
        (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0, t0)], [x, t0],
        [Lambda(i, exp(i))], [], [None, 'exp'], [None, x])
    assert DifferentialExtension(exp(x) + exp(x/2), x)._important_attrs == \
        (Poly(t0**2 + t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)],
        [x, t0], [Lambda(i, exp(i/2))], [], [None, 'exp'], [None, x/2])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2), x)._important_attrs == \
        (Poly((1 + t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x), Poly(t0, t0),
        Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x + x**2 + 1), x)._important_attrs == \
        (Poly((1 + S.Exp1*t0)*t1 + t0, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i)),
        Lambda(i, exp(i**2))], [], [None, 'exp', 'exp'], [None, x, x**2])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2), x)._important_attrs == \
        (Poly((t0 + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1],
        [Lambda(i, exp(i/2)), Lambda(i, exp(i**2))],
        [(exp(x/2), sqrt(exp(x)))], [None, 'exp', 'exp'], [None, x/2, x**2])
    assert DifferentialExtension(exp(x) + exp(x**2) + exp(x/2 + x**2 + 3), x)._important_attrs == \
        (Poly((t0*exp(3) + 1)*t1 + t0**2, t1), Poly(1, t1), [Poly(1, x),
        Poly(t0/2, t0), Poly(2*x*t1, t1)], [x, t0, t1], [Lambda(i, exp(i/2)),
        Lambda(i, exp(i**2))], [(exp(x/2), sqrt(exp(x)))], [None, 'exp', 'exp'],
        [None, x/2, x**2])
    assert DifferentialExtension(sqrt(exp(x)), x)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0],
        [Lambda(i, exp(i/2))], [(exp(x/2), sqrt(exp(x)))], [None, 'exp'], [None, x/2])

    assert DifferentialExtension(exp(x/2), x)._important_attrs == \
        (Poly(t0, t0), Poly(1, t0), [Poly(1, x), Poly(t0/2, t0)], [x, t0],
        [Lambda(i, exp(i/2))], [], [None, 'exp'], [None, x/2])
Beispiel #24
0
def test_DecrementLevel():
    DE = DifferentialExtension(x * log(exp(x) + 1), x)
    assert DE.level == -1
    assert DE.t == t1
    assert DE.d == Poly(t0 / (t0 + 1), t1)
    assert DE.case == "primitive"

    with DecrementLevel(DE):
        assert DE.level == -2
        assert DE.t == t0
        assert DE.d == Poly(t0, t0)
        assert DE.case == "exp"

        with DecrementLevel(DE):
            assert DE.level == -3
            assert DE.t == x
            assert DE.d == Poly(1, x)
            assert DE.case == "base"

        assert DE.level == -2
        assert DE.t == t0
        assert DE.d == Poly(t0, t0)
        assert DE.case == "exp"

    assert DE.level == -1
    assert DE.t == t1
    assert DE.d == Poly(t0 / (t0 + 1), t1)
    assert DE.case == "primitive"

    # Test that __exit__ is called after an exception correctly
    try:
        with DecrementLevel(DE):
            raise _TestingException
    except _TestingException:
        pass
    else:
        raise AssertionError("Did not raise.")

    assert DE.level == -1
    assert DE.t == t1
    assert DE.d == Poly(t0 / (t0 + 1), t1)
    assert DE.case == "primitive"
Beispiel #25
0
def test_bound_degree_fail():
    # Primitive
    DE = DifferentialExtension(
        extension={"D": [Poly(1, x), Poly(t0 / x ** 2, t0), Poly(1 / x, t)]}
    )
    assert (
        bound_degree(
            Poly(t ** 2, t),
            Poly(-(1 / x ** 2 * t ** 2 + 1 / x), t),
            Poly(
                (2 * x - 1) * t ** 4
                + (t0 + x) / x * t ** 3
                - (t0 + 4 * x ** 2) / 2 * x * t ** 2
                + x * t,
                t,
            ),
            DE,
        )
        == 3
    )
Beispiel #26
0
def test_param_poly_rischDE():
    DE = DifferentialExtension(extension={"D": [Poly(1, x)]})
    a = Poly(x ** 2 - x, x, field=True)
    b = Poly(1, x, field=True)
    q = [Poly(x, x, field=True), Poly(x ** 2, x, field=True)]
    h, A = param_poly_rischDE(a, b, q, 3, DE)

    assert A.nullspace() == [Matrix([0, 1, 1, 1])]  # c1, c2, d1, d2
    # Solution of a*Dp + b*p = c1*q1 + c2*q2 = q2 = x**2
    # is d1*h1 + d2*h2 = h1 + h2 = x.
    assert h[0] + h[1] == Poly(x, x)
    # a*Dp + b*p = q1 = x has no solution.

    a = Poly(x ** 2 - x, x, field=True)
    b = Poly(x ** 2 - 5 * x + 3, x, field=True)
    q = [Poly(1, x, field=True), Poly(x, x, field=True), Poly(x ** 2, x, field=True)]
    h, A = param_poly_rischDE(a, b, q, 3, DE)

    assert A.nullspace() == [Matrix([3, -5, 1, -5, 1, 1])]
    p = -5 * h[0] + h[1] + h[2]  # Poly(1, x)
    assert a * derivation(p, DE) + b * p == Poly(x ** 2 - 5 * x + 3, x)
Beispiel #27
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 x: exp(tan(x))]})
    assert integrate_hyperexponential(a, d, DE) == \
        (exp(2*tan(x))*tan(x) + exp(tan(x)), 1 + t1**2, True)
        # exp(2*tan(x))*tan(x) + tan(x) + exp(tan(x))
    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 x: 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) == \
        (-(55 - 50*exp(x))/(25 + 125*exp(2*x)) + log(1 + exp(2*x)), -1, True)
        # -(55 - 50*exp(x))/(25 + 125*exp(2*x)) - x + log(1 + exp(2*x))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t0, t0), Poly(t0*t, t)],
        'Tfuncs': [exp, lambda x: exp(exp(x))]})
    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 x: exp(-exp(x))]})
    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)
Beispiel #28
0
def test_special_denom():
    # TODO: add more tests here
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(t, t)]})
    assert special_denom(
        Poly(1, t), Poly(t ** 2, t), Poly(1, t), Poly(t ** 2 - 1, t), Poly(t, t), DE
    ) == (Poly(1, t), Poly(t ** 2 - 1, t), Poly(t ** 2 - 1, t), Poly(t, t))
    #    assert special_denom(Poly(1, t), Poly(2*x, t), Poly((1 + 2*x)*t, t), DE) == 1

    # issue 3940
    # Note, this isn't a very good test, because the denominator is just 1,
    # but at least it tests the exp cancellation case
    DE = DifferentialExtension(
        extension={"D": [Poly(1, x), Poly(-2 * x * t0, t0), Poly(I * k * t1, t1)]}
    )
    DE.decrement_level()
    assert special_denom(
        Poly(1, t0), Poly(I * k, t0), Poly(1, t0), Poly(t0, t0), Poly(1, t0), DE
    ) == (Poly(1, t0), Poly(I * k, t0), Poly(t0, t0), Poly(1, t0))

    assert special_denom(
        Poly(1, t),
        Poly(t ** 2, t),
        Poly(1, t),
        Poly(t ** 2 - 1, t),
        Poly(t, t),
        DE,
        case="tan",
    ) == (
        Poly(1, t, t0, domain="ZZ"),
        Poly(t ** 2, t0, t, domain="ZZ[x]"),
        Poly(t, t, t0, domain="ZZ"),
        Poly(1, t0, domain="ZZ"),
    )

    raises(
        ValueError,
        lambda: special_denom(
            Poly(1, t),
            Poly(t ** 2, t),
            Poly(1, t),
            Poly(t ** 2 - 1, t),
            Poly(t, t),
            DE,
            case="unrecognized_case",
        ),
    )
Beispiel #29
0
def test_special_denom():
    # TODO: add more tests here
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert special_denom(Poly(1, t), Poly(t**2, t), Poly(1, t), Poly(t**2 - 1, t),
    Poly(t, t), DE) == \
        (Poly(1, t), Poly(t**2 - 1, t), Poly(t**2 - 1, t), Poly(t, t))
#    assert special_denom(Poly(1, t), Poly(2*x, t), Poly((1 + 2*x)*t, t), DE) == 1

    # issue 3940
    # Note, this isn't a very good test, because the denominator is just 1,
    # but at least it tests the exp cancellation case
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(-2*x*t0, t0),
        Poly(I*k*t1, t1)]})
    DE.decrement_level()
    assert special_denom(Poly(1, t0), Poly(I*k, t0), Poly(1, t0), Poly(t0, t0),
    Poly(1, t0), DE) == \
        (Poly(1, t0), Poly(I*k, t0), Poly(t0, t0), Poly(1, t0))
Beispiel #30
0
def test_integrate_hyperexponential_polynomial():
    # Without proper cancellation within integrate_hyperexponential_polynomial(),
    # this will take a long time to complete, and will return a complicated
    # expression
    p = Poly(
        (-28 * x**11 * t0 - 6 * x**8 * t0 + 6 * x**9 * t0 - 15 * x**8 * t0**2 +
         15 * x**7 * t0**2 + 84 * x**10 * t0**2 - 140 * x**9 * t0**3 -
         20 * x**6 * t0**3 + 20 * x**7 * t0**3 - 15 * x**6 * t0**4 +
         15 * x**5 * t0**4 + 140 * x**8 * t0**4 - 84 * x**7 * t0**5 -
         6 * x**4 * t0**5 + 6 * x**5 * t0**5 + x**3 * t0**6 - x**4 * t0**6 +
         28 * x**6 * t0**6 - 4 * x**5 * t0**7 + x**9 - x**10 + 4 * x**12) /
        (-8 * x**11 * t0 + 28 * x**10 * t0**2 - 56 * x**9 * t0**3 +
         70 * x**8 * t0**4 - 56 * x**7 * t0**5 + 28 * x**6 * t0**6 -
         8 * x**5 * t0**7 + x**4 * t0**8 + x**12) * t1**2 +
        (-28 * x**11 * t0 - 12 * x**8 * t0 + 12 * x**9 * t0 -
         30 * x**8 * t0**2 + 30 * x**7 * t0**2 + 84 * x**10 * t0**2 -
         140 * x**9 * t0**3 - 40 * x**6 * t0**3 + 40 * x**7 * t0**3 -
         30 * x**6 * t0**4 + 30 * x**5 * t0**4 + 140 * x**8 * t0**4 -
         84 * x**7 * t0**5 - 12 * x**4 * t0**5 + 12 * x**5 * t0**5 -
         2 * x**4 * t0**6 + 2 * x**3 * t0**6 + 28 * x**6 * t0**6 -
         4 * x**5 * t0**7 + 2 * x**9 - 2 * x**10 + 4 * x**12) /
        (-8 * x**11 * t0 + 28 * x**10 * t0**2 - 56 * x**9 * t0**3 +
         70 * x**8 * t0**4 - 56 * x**7 * t0**5 + 28 * x**6 * t0**6 -
         8 * x**5 * t0**7 + x**4 * t0**8 + x**12) * t1 +
        (-2 * x**2 * t0 + 2 * x**3 * t0 + x * t0**2 - x**2 * t0**2 + x**3 -
         x**4) / (-4 * x**5 * t0 + 6 * x**4 * t0**2 - 4 * x**3 * t0**3 +
                  x**2 * t0**4 + x**6),
        t1,
        z,
        expand=False)
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x),
                         Poly(1 / x, t0),
                         Poly(2 * x * t1, t1)]})
    assert integrate_hyperexponential_polynomial(p, DE, z) == (Poly(
        (x - t0) * t1**2 + (-2 * t0 + 2 * x) * t1,
        t1), Poly(-2 * x * t0 + x**2 + t0**2, t1), True)
Beispiel #31
0
def test_spde():
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x), Poly(t**2 + 1, t)]})
    raises(
        NonElementaryIntegralException,
        lambda: spde(Poly(t, t), Poly((t - 1) *
                                      (t**2 + 1), t), Poly(1, t), 0, DE))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(t, t)]})
    assert spde(Poly(t**2 + x*t*2 + x**2, t), Poly(t**2/x**2 + (2/x - 1)*t, t),
    Poly(t**2/x**2 + (2/x - 1)*t, t), 0, DE) == \
        (Poly(0, t), Poly(0, t), 0, Poly(0, t), Poly(1, t))
    DE = DifferentialExtension(
        extension={'D': [Poly(1, x),
                         Poly(t0 / x**2, t0),
                         Poly(1 / x, t)]})
    assert spde(Poly(t**2, t), Poly(-t**2/x**2 - 1/x, t),
    Poly((2*x - 1)*t**4 + (t0 + x)/x*t**3 - (t0 + 4*x**2)/(2*x)*t**2 + x*t, t), 3, DE) == \
        (Poly(0, t), Poly(0, t), 0, Poly(0, t),
        Poly(t0*t**2/2 + x**2*t**2 - x**2*t, t))
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert spde(Poly(x**2 + x + 1, x), Poly(-2*x - 1, x), Poly(x**5/2 +
    3*x**4/4 + x**3 - x**2 + 1, x), 4, DE) == \
        (Poly(0, x), Poly(x/2 - S(1)/4, x), 2, Poly(x**2 + x + 1, x), Poly(5*x/4, x))
    assert spde(Poly(x**2 + x + 1, x), Poly(-2*x - 1, x), Poly(x**5/2 +
    3*x**4/4 + x**3 - x**2 + 1, x), n, DE) == \
        (Poly(0, x), Poly(x/2 - S(1)/4, x), -2 + n, Poly(x**2 + x + 1, x), Poly(5*x/4, x))
    DE = DifferentialExtension(extension={'D': [Poly(1, x), Poly(1, t)]})
    raises(
        NonElementaryIntegralException,
        lambda: spde(Poly((t - 1) *
                          (t**2 + 1)**2, t), Poly(
                              (t - 1) * (t**2 + 1), t), Poly(1, t), 0, DE))
    DE = DifferentialExtension(extension={'D': [Poly(1, x)]})
    assert spde(Poly(x**2 - x, x), Poly(1, x),
                Poly(9 * x**4 - 10 * x**3 + 2 * x**2, x), 4,
                DE) == (Poly(0, x), Poly(0, x), 0, Poly(0, x),
                        Poly(3 * x**3 - 2 * x**2, x))
    assert spde(Poly(x**2 - x, x), Poly(x**2 - 5*x + 3, x), Poly(x**7 - x**6 - 2*x**4 + 3*x**3 - x**2, x), 5, DE) == \
        (Poly(1, x), Poly(x + 1, x), 1, Poly(x**4 - x**3, x), Poly(x**3 - x**2, x))
Beispiel #32
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,
    )
Beispiel #33
0
def test_residue_reduce():
    a = Poly(2 * t ** 2 - t - x ** 2, t)
    d = Poly(t ** 3 - x ** 2 * t, t)
    DE = DifferentialExtension(
        extension={"D": [Poly(1, x), Poly(1 / x, t)], "Tfuncs": [log]}
    )
    assert residue_reduce(a, d, DE, z, invert=False) == (
        [
            (
                Poly(z ** 2 - Rational(1, 4), z),
                Poly(
                    (1 + 3 * x * z - 6 * z ** 2 - 2 * x ** 2 + 4 * x ** 2 * z ** 2) * t
                    - x * z
                    + x ** 2
                    + 2 * x ** 2 * z ** 2
                    - 2 * z * x ** 3,
                    t,
                ),
            )
        ],
        False,
    )
    assert residue_reduce(a, d, DE, z, invert=True) == (
        [(Poly(z ** 2 - Rational(1, 4), z), Poly(t + 2 * x * z, t))],
        False,
    )
    assert residue_reduce(
        Poly(-2 / x, t), Poly(t ** 2 - 1, t,), DE, z, invert=False
    ) == ([(Poly(z ** 2 - 1, z), Poly(-2 * z * t / x - 2 / x, t))], True)
    ans = residue_reduce(Poly(-2 / x, t), Poly(t ** 2 - 1, t), DE, z, invert=True)
    assert ans == ([(Poly(z ** 2 - 1, z), Poly(t + z, t))], True)
    assert residue_reduce_to_basic(ans[0], DE, z) == -log(-1 + log(x)) + log(1 + log(x))

    DE = DifferentialExtension(
        extension={
            "D": [Poly(1, x), Poly(-(t ** 2) - t / x - (1 - nu ** 2 / x ** 2), t)]
        }
    )
    # TODO: Skip or make faster
    assert residue_reduce(
        Poly((-2 * nu ** 2 - x ** 4) / (2 * x ** 2) * t - (1 + x ** 2) / x, t),
        Poly(t ** 2 + 1 + x ** 2 / 2, t),
        DE,
        z,
    ) == (
        [
            (
                Poly(z + S.Half, z, domain="QQ"),
                Poly(t ** 2 + 1 + x ** 2 / 2, t, domain="EX"),
            )
        ],
        True,
    )
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(1 + t ** 2, t)]})
    assert residue_reduce(
        Poly(-2 * x * t + 1 - x ** 2, t),
        Poly(t ** 2 + 2 * x * t + 1 + x ** 2, t),
        DE,
        z,
    ) == ([(Poly(z ** 2 + Rational(1, 4), z), Poly(t + x + 2 * z, t))], True)
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(t, t)]})
    assert residue_reduce(Poly(t, t), Poly(t + sqrt(2), t), DE, z) == (
        [(Poly(z - 1, z), Poly(t + sqrt(2), t))],
        True,
    )
Beispiel #34
0
def test_hermite_reduce():
    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(t ** 2 + 1, t)]})

    assert hermite_reduce(Poly(x - t, t), Poly(t ** 2, t), DE) == (
        (Poly(-x, t), Poly(t, t)),
        (Poly(0, t), Poly(1, t)),
        (Poly(-x, t), Poly(1, t)),
    )

    DE = DifferentialExtension(
        extension={
            "D": [Poly(1, x), Poly(-(t ** 2) - t / x - (1 - nu ** 2 / x ** 2), t)]
        }
    )

    assert hermite_reduce(
        Poly(
            x ** 2 * t ** 5
            + x * t ** 4
            - nu ** 2 * t ** 3
            - x * (x ** 2 + 1) * t ** 2
            - (x ** 2 - nu ** 2) * t
            - x ** 5 / 4,
            t,
        ),
        Poly(
            x ** 2 * t ** 4
            + x ** 2 * (x ** 2 + 2) * t ** 2
            + x ** 2
            + x ** 4
            + x ** 6 / 4,
            t,
        ),
        DE,
    ) == (
        (Poly(-(x ** 2) - 4, t), Poly(4 * t ** 2 + 2 * x ** 2 + 4, t)),
        (
            Poly((-2 * nu ** 2 - x ** 4) * t - (2 * x ** 3 + 2 * x), t),
            Poly(2 * x ** 2 * t ** 2 + x ** 4 + 2 * x ** 2, t),
        ),
        (Poly(x * t + 1, t), Poly(x, t)),
    )

    DE = DifferentialExtension(extension={"D": [Poly(1, x), Poly(1 / x, t)]})

    a = Poly(
        (-2 + 3 * x) * t ** 3 + (-1 + x) * t ** 2 + (-4 * x + 2 * x ** 2) * t + x ** 2,
        t,
    )
    d = Poly(
        x * t ** 6
        - 4 * x ** 2 * t ** 5
        + 6 * x ** 3 * t ** 4
        - 4 * x ** 4 * t ** 3
        + x ** 5 * t ** 2,
        t,
    )

    assert hermite_reduce(a, d, DE) == (
        (
            Poly(3 * t ** 2 + t + 3 * x, t),
            Poly(3 * t ** 4 - 9 * x * t ** 3 + 9 * x ** 2 * t ** 2 - 3 * x ** 3 * t, t),
        ),
        (Poly(0, t), Poly(1, t)),
        (Poly(0, t), Poly(1, t)),
    )

    assert hermite_reduce(
        Poly(-(t ** 2) + 2 * t + 2, t), Poly(-x * t ** 2 + 2 * x * t - x, t), DE
    ) == (
        (Poly(3, t), Poly(t - 1, t)),
        (Poly(0, t), Poly(1, t)),
        (Poly(1, t), Poly(x, t)),
    )

    assert hermite_reduce(
        Poly(
            -(x ** 2) * t ** 6
            + (-1 - 2 * x ** 3 + x ** 4) * t ** 3
            + (-3 - 3 * x ** 4) * t ** 2
            - 2 * x * t
            - x
            - 3 * x ** 2,
            t,
        ),
        Poly(x ** 4 * t ** 6 - 2 * x ** 2 * t ** 3 + 1, t),
        DE,
    ) == (
        (Poly(x ** 3 * t + x ** 4 + 1, t), Poly(x ** 3 * t ** 3 - x, t)),
        (Poly(0, t), Poly(1, t)),
        (Poly(-1, t), Poly(x ** 2, t)),
    )

    assert hermite_reduce(
        Poly(
            (-2 + 3 * x) * t ** 3
            + (-1 + x) * t ** 2
            + (-4 * x + 2 * x ** 2) * t
            + x ** 2,
            t,
        ),
        Poly(
            x * t ** 6
            - 4 * x ** 2 * t ** 5
            + 6 * x ** 3 * t ** 4
            - 4 * x ** 4 * t ** 3
            + x ** 5 * t ** 2,
            t,
        ),
        DE,
    ) == (
        (
            Poly(3 * t ** 2 + t + 3 * x, t),
            Poly(3 * t ** 4 - 9 * x * t ** 3 + 9 * x ** 2 * t ** 2 - 3 * x ** 3 * t, t),
        ),
        (Poly(0, t), Poly(1, t)),
        (Poly(0, t), Poly(1, t)),
    )
Beispiel #35
0
def test_DifferentialExtension_equality():
    DE1 = DE2 = DifferentialExtension(log(x), x)
    assert DE1 == DE2