Exemple #1
0
def test_to_Sequence():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    n = symbols('n', integer=True)
    _, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
    p = HolonomicFunction(x**2 * Dx**4 + x + Dx, x).to_sequence()
    q = [(HolonomicSequence(1 + (n + 2) * Sn**2 +
                            (n**4 + 6 * n**3 + 11 * n**2 + 6 * n) * Sn**3), 0,
          1)]
    assert p == q
    p = HolonomicFunction(x**2 * Dx**4 + x**3 + Dx**2, x).to_sequence()
    q = [
        (HolonomicSequence(1 + (n**4 + 14 * n**3 + 72 * n**2 + 163 * n + 140) *
                           Sn**5), 0, 0)
    ]
    assert p == q
    p = HolonomicFunction(x**3 * Dx**4 + 1 + Dx**2, x).to_sequence()
    q = [(HolonomicSequence(1 + (n**4 - 2 * n**3 - n**2 + 2 * n) * Sn +
                            (n**2 + 3 * n + 2) * Sn**2), 0, 0)]
    assert p == q
    p = HolonomicFunction(3 * x**3 * Dx**4 + 2 * x * Dx + x * Dx**3,
                          x).to_sequence()
    q = [(HolonomicSequence(2 * n +
                            (3 * n**4 - 6 * n**3 - 3 * n**2 + 6 * n) * Sn +
                            (n**3 + 3 * n**2 + 2 * n) * Sn**2), 0, 1)]
    assert p == q
Exemple #2
0
def test_DDM_str():
    sdm = SDM({0: {0: ZZ(1)}, 1: {1: ZZ(1)}}, (2, 2), ZZ)
    assert str(sdm) == '{0: {0: 1}, 1: {1: 1}}'
    if HAS_GMPY:  # pragma: no cover
        assert repr(sdm) == 'SDM({0: {0: mpz(1)}, 1: {1: mpz(1)}}, (2, 2), ZZ)'
    else:  # pragma: no cover
        assert repr(sdm) == 'SDM({0: {0: 1}, 1: {1: 1}}, (2, 2), ZZ)'
Exemple #3
0
def test_SDM_mul():
    A = SDM({0: {0: ZZ(2)}}, (2, 2), ZZ)
    B = SDM({0: {0: ZZ(4)}}, (2, 2), ZZ)
    assert A * ZZ(2) == B
    assert ZZ(2) * A == B

    raises(TypeError, lambda: A * QQ(1, 2))
    raises(TypeError, lambda: QQ(1, 2) * A)
Exemple #4
0
def test_SDM():
    A = SDM({0: {0: ZZ(1)}}, (2, 2), ZZ)
    assert A.domain == ZZ
    assert A.shape == (2, 2)
    assert dict(A) == {0: {0: ZZ(1)}}

    raises(DDMBadInputError, lambda: SDM({5: {1: ZZ(0)}}, (2, 2), ZZ))
    raises(DDMBadInputError, lambda: SDM({0: {5: ZZ(0)}}, (2, 2), ZZ))
Exemple #5
0
def test_SDM_to_list():
    A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ)
    assert A.to_list() == [[ZZ(0), ZZ(1)], [ZZ(0), ZZ(0)]]

    A = SDM({}, (0, 2), ZZ)
    assert A.to_list() == []

    A = SDM({}, (2, 0), ZZ)
    assert A.to_list() == [[], []]
Exemple #6
0
def test_SDM_convert_to():
    A = SDM({0: {1: ZZ(1)}, 1: {0: ZZ(2), 1: ZZ(3)}}, (2, 2), ZZ)
    B = SDM({0: {1: QQ(1)}, 1: {0: QQ(2), 1: QQ(3)}}, (2, 2), QQ)
    C = A.convert_to(QQ)
    assert C == B
    assert C.domain == QQ

    D = A.convert_to(ZZ)
    assert D == A
    assert D.domain == ZZ
Exemple #7
0
def test_SDM_sub():
    A = SDM({0: {1: ZZ(1)}, 1: {0: ZZ(2), 1: ZZ(3)}}, (2, 2), ZZ)
    B = SDM({0: {0: ZZ(1)}, 1: {0: ZZ(-2), 1: ZZ(3)}}, (2, 2), ZZ)
    C = SDM({0: {0: ZZ(-1), 1: ZZ(1)}, 1: {0: ZZ(4)}}, (2, 2), ZZ)
    assert A.sub(B) == A - B == C

    raises(TypeError, lambda: A - [])
def test_to_expr():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x, 0, 1).to_expr()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4*x - 1)*Dx + \
        (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr()
    q = 1 / (x**2 - 2 * x + 1)
    assert p == q
    p = expr_to_holonomic(sin(x)**2 / x).integrate((x, 0, x)).to_expr()
    q = (sin(x)**2 / x).integrate((x, 0, x))
    assert p == q
    C_1, C_2, C_3 = symbols('C_1, C_2, C_3')
    p = expr_to_holonomic(log(1 + x**2)).to_expr()
    q = C_2 * log(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(log(1 + x**2)).diff().to_expr()
    q = C_1 * x / (x**2 + 1)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).to_expr()
    q = 3 * C_3 * x - 3 * sqrt(pi) * C_3 * erf(x) / 2 + x + 2 * x / sqrt(pi)
    assert p == q
    p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
    assert p == sqrt(x)
    p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
    assert p == sqrt(1 + x**2)
    p = expr_to_holonomic((2 * x**2 + 1)**(S(2) / 3)).to_expr()
    assert p == (2 * x**2 + 1)**(S(2) / 3)
Exemple #9
0
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr()
    q = 1 - cos(x)
    assert p == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, 3))
    q = 1 - cos(3)
    assert p == q
    p = expr_to_holonomic(sin(x)/x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)/x)
    assert p.integrate(x).to_expr() == Si(x)
    assert p.integrate((x, 0, 2)) == Si(2)
    p = expr_to_holonomic(sin(x)**2/x)
    q = p.to_expr()
    assert p.integrate(x).to_expr() == q.integrate((x, 0, x))
    assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1))
    assert expr_to_holonomic(1/x).integrate(x).to_expr() == log(x)
    p = expr_to_holonomic((x + 1)**3*exp(-x), x0=-1, lenics=4).integrate(x).to_expr()
    q = (-x**3 - 6*x**2 - 15*x + 6*exp(x + 1) - 16)*exp(-x)
    assert p == q
Exemple #10
0
def test_HolonomicFunction_addition():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 * x, x)
    q = HolonomicFunction((2) * Dx + (x) * Dx**2, x)
    assert p == q
    p = HolonomicFunction(x * Dx + 1, x)
    q = HolonomicFunction(Dx + 1, x)
    r = HolonomicFunction((x - 2) + (x**2 - 2) * Dx + (x**2 - x) * Dx**2, x)
    assert p + q == r
    p = HolonomicFunction(x * Dx + Dx**2 * (x**2 + 2), x)
    q = HolonomicFunction(Dx - 3, x)
    r = HolonomicFunction((-54 * x**2 - 126 * x - 150) + (-135 * x**3 - 252 * x**2 - 270 * x + 140) * Dx +\
                 (-27 * x**4 - 24 * x**2 + 14 * x - 150) * Dx**2 + \
                 (9 * x**4 + 15 * x**3 + 38 * x**2 + 30 * x +40) * Dx**3, x)
    assert p + q == r
    p = HolonomicFunction(Dx**5 - 1, x)
    q = HolonomicFunction(x**3 + Dx, x)
    r = HolonomicFunction((-x**18 + 45*x**14 - 525*x**10 + 1575*x**6 - x**3 - 630*x**2) + \
        (-x**15 + 30*x**11 - 195*x**7 + 210*x**3 - 1)*Dx + (x**18 - 45*x**14 + 525*x**10 - \
        1575*x**6 + x**3 + 630*x**2)*Dx**5 + (x**15 - 30*x**11 + 195*x**7 - 210*x**3 + \
        1)*Dx**6, x)
    assert p+q == r

    p = x**2 + 3*x + 8
    q = x**3 - 7*x + 5
    p = p*Dx - p.diff()
    q = q*Dx - q.diff()
    r = HolonomicFunction(p, x) + HolonomicFunction(q, x)
    s = HolonomicFunction((6*x**2 + 18*x + 14) + (-4*x**3 - 18*x**2 - 62*x + 10)*Dx +\
        (x**4 + 6*x**3 + 31*x**2 - 10*x - 71)*Dx**2, x)
    assert r == s
Exemple #11
0
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr()
    q = 1 - cos(x)
    assert p == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, 3))
    q = 1 - cos(3)
    assert p == q
    p = expr_to_holonomic(sin(x) / x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x) / x)
    assert p.integrate(x).to_expr() == Si(x)
    assert p.integrate((x, 0, 2)) == Si(2)
    p = expr_to_holonomic(sin(x)**2 / x)
    q = p.to_expr()
    assert p.integrate(x).to_expr() == q.integrate((x, 0, x))
    assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1))
    assert expr_to_holonomic(1 / x).integrate(x).to_expr() == log(x)
    p = expr_to_holonomic((x + 1)**3 * exp(-x), x0=-1,
                          lenics=4).integrate(x).to_expr()
    q = (-x**3 - 6 * x**2 - 15 * x + 6 * exp(x + 1) - 16) * exp(-x)
    assert p == q
Exemple #12
0
def test_QuotientRing():
    I = QQ.old_poly_ring(x).ideal(x**2 + 1)
    R = QQ.old_poly_ring(x) / I

    assert R == QQ.old_poly_ring(x) / [x**2 + 1]
    assert R == QQ.old_poly_ring(x) / QQ.old_poly_ring(x).ideal(x**2 + 1)
    assert R != QQ.old_poly_ring(x)

    assert R.convert(1) / x == -x + I
    assert -1 + I == x**2 + I
    assert R.convert(ZZ(1), ZZ) == 1 + I
    assert R.convert(R.convert(x), R) == R.convert(x)

    X = R.convert(x)
    Y = QQ.old_poly_ring(x).convert(x)
    assert -1 + I == X**2 + I
    assert -1 + I == Y**2 + I
    assert R.to_sympy(X) == x

    raises(ValueError,
           lambda: QQ.old_poly_ring(x) / QQ.old_poly_ring(x, y).ideal(x))

    R = QQ.old_poly_ring(x, order="ilex")
    I = R.ideal(x)
    assert R.convert(1) + I == (R / I).convert(1)
Exemple #13
0
def test_HolonomicFunction_composition():
    x = symbols("x")
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), "Dx")
    p = HolonomicFunction(Dx - 1, x).composition(x ** 2 + x)
    r = HolonomicFunction((-2 * x - 1) + Dx, x)
    assert p == r
    p = HolonomicFunction(Dx ** 2 + 1, x).composition(x ** 5 + x ** 2 + 1)
    r = HolonomicFunction(
        (125 * x ** 12 + 150 * x ** 9 + 60 * x ** 6 + 8 * x ** 3)
        + (-20 * x ** 3 - 2) * Dx
        + (5 * x ** 4 + 2 * x) * Dx ** 2,
        x,
    )
    assert p == r
    p = HolonomicFunction(Dx ** 2 * x + x, x).composition(2 * x ** 3 + x ** 2 + 1)
    r = HolonomicFunction(
        (216 * x ** 9 + 324 * x ** 8 + 180 * x ** 7 + 152 * x ** 6 + 112 * x ** 5 + 36 * x ** 4 + 4 * x ** 3)
        + (24 * x ** 4 + 16 * x ** 3 + 3 * x ** 2 - 6 * x - 1) * Dx
        + (6 * x ** 5 + 5 * x ** 4 + x ** 3 + 3 * x ** 2 + x) * Dx ** 2,
        x,
    )
    assert p == r
    p = HolonomicFunction(Dx ** 2 + 1, x).composition(1 - x ** 2)
    r = HolonomicFunction((4 * x ** 3) - Dx + x * Dx ** 2, x)
    assert p == r
    p = HolonomicFunction(Dx ** 2 + 1, x).composition(x - 2 / (x ** 2 + 1))
    r = HolonomicFunction(
        (
            x ** 12
            + 6 * x ** 10
            + 12 * x ** 9
            + 15 * x ** 8
            + 48 * x ** 7
            + 68 * x ** 6
            + 72 * x ** 5
            + 111 * x ** 4
            + 112 * x ** 3
            + 54 * x ** 2
            + 12 * x
            + 1
        )
        + (12 * x ** 8 + 32 * x ** 6 + 24 * x ** 4 - 4) * Dx
        + (
            x ** 12
            + 6 * x ** 10
            + 4 * x ** 9
            + 15 * x ** 8
            + 16 * x ** 7
            + 20 * x ** 6
            + 24 * x ** 5
            + 15 * x ** 4
            + 16 * x ** 3
            + 6 * x ** 2
            + 4 * x
            + 1
        )
        * Dx ** 2,
        x,
    )
    assert p == r
Exemple #14
0
def test_HolonomicFunction_multiplication():
    x = symbols("x")
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), "Dx")
    p = HolonomicFunction(Dx + x + x * Dx ** 2, x)
    q = HolonomicFunction(x * Dx + Dx * x + Dx ** 2, x)
    r = HolonomicFunction(
        (8 * x ** 6 + 4 * x ** 4 + 6 * x ** 2 + 3)
        + (24 * x ** 5 - 4 * x ** 3 + 24 * x) * Dx
        + (8 * x ** 6 + 20 * x ** 4 + 12 * x ** 2 + 2) * Dx ** 2
        + (8 * x ** 5 + 4 * x ** 3 + 4 * x) * Dx ** 3
        + (2 * x ** 4 + x ** 2) * Dx ** 4,
        x,
    )
    assert p * q == r
    p = HolonomicFunction(Dx ** 2 + 1, x)
    q = HolonomicFunction(Dx - 1, x)
    r = HolonomicFunction((2) + (-2) * Dx + (1) * Dx ** 2, x)
    assert p * q == r
    p = HolonomicFunction(Dx ** 2 + 1 + x + Dx, x)
    q = HolonomicFunction((Dx * x - 1) ** 2, x)
    r = HolonomicFunction(
        (4 * x ** 7 + 11 * x ** 6 + 16 * x ** 5 + 4 * x ** 4 - 6 * x ** 3 - 7 * x ** 2 - 8 * x - 2)
        + (8 * x ** 6 + 26 * x ** 5 + 24 * x ** 4 - 3 * x ** 3 - 11 * x ** 2 - 6 * x - 2) * Dx
        + (8 * x ** 6 + 18 * x ** 5 + 15 * x ** 4 - 3 * x ** 3 - 6 * x ** 2 - 6 * x - 2) * Dx ** 2
        + (8 * x ** 5 + 10 * x ** 4 + 6 * x ** 3 - 2 * x ** 2 - 4 * x) * Dx ** 3
        + (4 * x ** 5 + 3 * x ** 4 - x ** 2) * Dx ** 4,
        x,
    )
    assert p * q == r
    p = HolonomicFunction(x * Dx ** 2 - 1, x)
    q = HolonomicFunction(Dx * x - x, x)
    r = HolonomicFunction((x - 3) + (-2 * x + 2) * Dx + (x) * Dx ** 2, x)
    assert p * q == r
Exemple #15
0
def test_series():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 + 2*x*Dx, x, 0, [0, 1]).series(n=10)
    q = x - x**3/3 + x**5/10 - x**7/42 + x**9/216 + O(x**10)
    assert p == q
    p = HolonomicFunction(Dx - 1, x).composition(x**2, 0, [1])  # e^(x**2)
    q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0])  # cos(x)
    r = (p * q).series(n=10)  # expansion of cos(x) * exp(x**2)
    s = 1 + x**2/2 + x**4/24 - 31*x**6/720 - 179*x**8/8064 + O(x**10)
    assert r == s
    t = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1])  # log(1 + x)
    r = (p * t + q).series(n=10)
    s = 1 + x - x**2 + 4*x**3/3 - 17*x**4/24 + 31*x**5/30 - 481*x**6/720 +\
     71*x**7/105 - 20159*x**8/40320 + 379*x**9/840 + O(x**10)
    assert r == s
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [0, 1]).series(n=7)
    q = x + x**3/6 - 3*x**4/16 + x**5/20 - 23*x**6/960 + O(x**7)
    assert p == q
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [1, 0]).series(n=7)
    q = 1 - 3*x**2/4 - x**3/4 - 5*x**4/32 - 3*x**5/40 - 17*x**6/384 + O(x**7)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).series(n=10)
    C_3 = symbols('C_3')
    q = (erf(x) + x).series(n=10)
    assert p.subs(C_3, -2/(3*sqrt(pi))) == q
    assert expr_to_holonomic(sqrt(x**3 + x)).series(n=10) == sqrt(x**3 + x).series(n=10)
    assert expr_to_holonomic((2*x - 3*x**2)**(S(1)/3)).series() == ((2*x - 3*x**2)**(S(1)/3)).series()
    assert  expr_to_holonomic(sqrt(x**2-x)).series() == (sqrt(x**2-x)).series()
    assert expr_to_holonomic(cos(x)**2/x**2, y0={-2: [1, 0, -1]}).series(n=10) == (cos(x)**2/x**2).series(n=10)
    assert expr_to_holonomic(cos(x)**2/x**2, x0=1).series(n=10) == (cos(x)**2/x**2).series(n=10, x0=1)
    assert expr_to_holonomic(cos(x-1)**2/(x-1)**2, x0=1, y0={-2: [1, 0, -1]}).series(n=10) \
        == (cos(x-1)**2/(x-1)**2).series(x0=1, n=10)
Exemple #16
0
def test_to_expr():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x, 0, [1]).to_expr()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4*x - 1)*Dx + \
        (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr().expand()
    q = 1 / (x**2 - 2 * x + 1)
    assert p == q
    p = expr_to_holonomic(sin(x)**2 / x).integrate((x, 0, x)).to_expr()
    q = (sin(x)**2 / x).integrate((x, 0, x))
    assert p == q
    C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
    p = expr_to_holonomic(log(1 + x**2)).to_expr()
    q = C_2 * log(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(log(1 + x**2)).diff().to_expr()
    q = C_0 * x / (x**2 + 1)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).to_expr()
    q = 3 * C_3 * x - 3 * sqrt(pi) * C_3 * erf(x) / 2 + x + 2 * x / sqrt(pi)
    assert p == q
    p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
    assert p == sqrt(x)
    assert expr_to_holonomic(sqrt(x)).to_expr() == sqrt(x)
    p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
    assert p == sqrt(1 + x**2)
    p = expr_to_holonomic((2 * x**2 + 1)**Rational(2, 3)).to_expr()
    assert p == (2 * x**2 + 1)**Rational(2, 3)
    p = expr_to_holonomic(sqrt(-x**2 + 2 * x)).to_expr()
    assert p == sqrt(x) * sqrt(-x + 2)
    p = expr_to_holonomic((-2 * x**3 + 7 * x)**Rational(2, 3)).to_expr()
    q = x**Rational(2, 3) * (-2 * x**2 + 7)**Rational(2, 3)
    assert p == q
    p = from_hyper(hyper((-2, -3), (S.Half, ), x))
    s = hyperexpand(hyper((-2, -3), (S.Half, ), x))
    D_0 = Symbol('D_0')
    C_0 = Symbol('C_0')
    assert (p.to_expr().subs({C_0: 1, D_0: 0}) - s).simplify() == 0
    p.y0 = {0: [1], S.Half: [0]}
    assert p.to_expr() == s
    assert expr_to_holonomic(x**5).to_expr() == x**5
    assert expr_to_holonomic(2*x**3-3*x**2).to_expr().expand() == \
        2*x**3-3*x**2
    a = symbols("a")
    p = (expr_to_holonomic(1.4 * x) * expr_to_holonomic(a * x, x)).to_expr()
    q = 1.4 * a * x**2
    assert p == q
    p = (expr_to_holonomic(1.4 * x) + expr_to_holonomic(a * x, x)).to_expr()
    q = x * (a + 1.4)
    assert p == q
    p = (expr_to_holonomic(1.4 * x) + expr_to_holonomic(x)).to_expr()
    assert p == 2.4 * x
Exemple #17
0
def test_HolonomicFunction_addition():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 * x, x)
    q = HolonomicFunction((2) * Dx + (x) * Dx**2, x)
    assert p == q
    p = HolonomicFunction(x * Dx + 1, x)
    q = HolonomicFunction(Dx + 1, x)
    r = HolonomicFunction((x - 2) + (x**2 - 2) * Dx + (x**2 - x) * Dx**2, x)
    assert p + q == r
    p = HolonomicFunction(x * Dx + Dx**2 * (x**2 + 2), x)
    q = HolonomicFunction(Dx - 3, x)
    r = HolonomicFunction((-54 * x**2 - 126 * x - 150) + (-135 * x**3 - 252 * x**2 - 270 * x + 140) * Dx +\
                 (-27 * x**4 - 24 * x**2 + 14 * x - 150) * Dx**2 + \
                 (9 * x**4 + 15 * x**3 + 38 * x**2 + 30 * x +40) * Dx**3, x)
    assert p + q == r
    p = HolonomicFunction(Dx**5 - 1, x)
    q = HolonomicFunction(x**3 + Dx, x)
    r = HolonomicFunction((-x**18 + 45*x**14 - 525*x**10 + 1575*x**6 - x**3 - 630*x**2) + \
        (-x**15 + 30*x**11 - 195*x**7 + 210*x**3 - 1)*Dx + (x**18 - 45*x**14 + 525*x**10 - \
        1575*x**6 + x**3 + 630*x**2)*Dx**5 + (x**15 - 30*x**11 + 195*x**7 - 210*x**3 + \
        1)*Dx**6, x)
    assert p + q == r

    p = x**2 + 3 * x + 8
    q = x**3 - 7 * x + 5
    p = p * Dx - p.diff()
    q = q * Dx - q.diff()
    r = HolonomicFunction(p, x) + HolonomicFunction(q, x)
    s = HolonomicFunction((6*x**2 + 18*x + 14) + (-4*x**3 - 18*x**2 - 62*x + 10)*Dx +\
        (x**4 + 6*x**3 + 31*x**2 - 10*x - 71)*Dx**2, x)
    assert r == s
Exemple #18
0
def test_to_expr():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x, 0, 1).to_expr()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4*x - 1)*Dx + \
        (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr()
    q = 1/(x**2 - 2*x + 1)
    assert p == q
    p = expr_to_holonomic(sin(x)**2/x).integrate((x, 0, x)).to_expr()
    q = (sin(x)**2/x).integrate((x, 0, x))
    assert p == q
    C_1, C_2, C_3 = symbols('C_1, C_2, C_3')
    p = expr_to_holonomic(log(1+x**2)).to_expr()
    q = C_2*log(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(log(1+x**2)).diff().to_expr()
    q = C_1*x/(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).to_expr()
    q = 3*C_3*x - 3*sqrt(pi)*C_3*erf(x)/2 + x + 2*x/sqrt(pi)
    assert p == q
    p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
    assert p == sqrt(x)
    p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
    assert p == sqrt(1+x**2)
    p = expr_to_holonomic((2*x**2 + 1)**(S(2)/3)).to_expr()
    assert p == (2*x**2 + 1)**(S(2)/3)
Exemple #19
0
def test_series():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 + 2 * x * Dx, x, 0, [0, 1]).series(n=10)
    q = x - x**3 / 3 + x**5 / 10 - x**7 / 42 + x**9 / 216 + O(x**10)
    assert p == q
    p = HolonomicFunction(Dx - 1, x).composition(x**2, 0, 1)  # e^(x**2)
    q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0])  # cos(x)
    r = (p * q).series(n=10)  # expansion of cos(x) * exp(x**2)
    s = 1 + x**2 / 2 + x**4 / 24 - 31 * x**6 / 720 - 179 * x**8 / 8064 + O(x**
                                                                           10)
    assert r == s
    t = HolonomicFunction((1 + x) * Dx**2 + Dx, x, 0, [0, 1])  # log(1 + x)
    r = (p * t + q).series(n=10)
    s = 1 + x - x**2 + 4*x**3/3 - 17*x**4/24 + 31*x**5/30 - 481*x**6/720 +\
     71*x**7/105 - 20159*x**8/40320 + 379*x**9/840 + O(x**10)
    assert r == s
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [0, 1]).series(n=7)
    q = x + x**3 / 6 - 3 * x**4 / 16 + x**5 / 20 - 23 * x**6 / 960 + O(x**7)
    assert p == q
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [1, 0]).series(n=7)
    q = 1 - 3 * x**2 / 4 - x**3 / 4 - 5 * x**4 / 32 - 3 * x**5 / 40 - 17 * x**6 / 384 + O(
        x**7)
    assert p == q
Exemple #20
0
def test_series():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 + 2*x*Dx, x, 0, [0, 1]).series(n=10)
    q = x - x**3/3 + x**5/10 - x**7/42 + x**9/216 + O(x**10)
    assert p == q
    p = HolonomicFunction(Dx - 1, x).composition(x**2, 0, [1])  # e^(x**2)
    q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0])  # cos(x)
    r = (p * q).series(n=10)  # expansion of cos(x) * exp(x**2)
    s = 1 + x**2/2 + x**4/24 - 31*x**6/720 - 179*x**8/8064 + O(x**10)
    assert r == s
    t = HolonomicFunction((1 + x)*Dx**2 + Dx, x, 0, [0, 1])  # log(1 + x)
    r = (p * t + q).series(n=10)
    s = 1 + x - x**2 + 4*x**3/3 - 17*x**4/24 + 31*x**5/30 - 481*x**6/720 +\
     71*x**7/105 - 20159*x**8/40320 + 379*x**9/840 + O(x**10)
    assert r == s
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [0, 1]).series(n=7)
    q = x + x**3/6 - 3*x**4/16 + x**5/20 - 23*x**6/960 + O(x**7)
    assert p == q
    p = HolonomicFunction((6+6*x-3*x**2) - (10*x-3*x**2-3*x**3)*Dx + \
        (4-6*x**3+2*x**4)*Dx**2, x, 0, [1, 0]).series(n=7)
    q = 1 - 3*x**2/4 - x**3/4 - 5*x**4/32 - 3*x**5/40 - 17*x**6/384 + O(x**7)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).series(n=10)
    C_3 = symbols('C_3')
    q = (erf(x) + x).series(n=10)
    assert p.subs(C_3, -2/(3*sqrt(pi))) == q
    assert expr_to_holonomic(sqrt(x**3 + x)).series(n=10) == sqrt(x**3 + x).series(n=10)
    assert expr_to_holonomic((2*x - 3*x**2)**(S(1)/3)).series() == ((2*x - 3*x**2)**(S(1)/3)).series()
    assert  expr_to_holonomic(sqrt(x**2-x)).series() == (sqrt(x**2-x)).series()
    assert expr_to_holonomic(cos(x)**2/x**2, y0={-2: [1, 0, -1]}).series(n=10) == (cos(x)**2/x**2).series(n=10)
    assert expr_to_holonomic(cos(x)**2/x**2, x0=1).series(n=10) == (cos(x)**2/x**2).series(n=10, x0=1)
    assert expr_to_holonomic(cos(x-1)**2/(x-1)**2, x0=1, y0={-2: [1, 0, -1]}).series(n=10) \
        == (cos(x-1)**2/(x-1)**2).series(x0=1, n=10)
Exemple #21
0
def test_multiplication_initial_condition():
    x = symbols('x')
    R, Dx = DiffOperatorAlgebra(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 + x*Dx - 1, x, 0, [3, 1])
    q = HolonomicFunction(Dx**2 + 1, x, 0, [1, 1])
    r = HolonomicFunction((x**4 + 14*x**2 + 60) + 4*x*Dx + (x**4 + 9*x**2 + 20)*Dx**2 + \
        (2*x**3 + 18*x)*Dx**3 + (x**2 + 10)*Dx**4, x, 0, [3, 4, 2, 3])
    assert p * q == r
    p = HolonomicFunction(Dx**2 + x, x, 0, [1, 0])
    q = HolonomicFunction(Dx**3 - x**2, x, 0, [3, 3, 3])
    r = HolonomicFunction((27*x**8 - 37*x**7 - 10*x**6 - 492*x**5 - 552*x**4 + 160*x**3 + \
        1212*x**2 + 216*x + 360) + (162*x**7 - 384*x**6 - 294*x**5 - 84*x**4 + 24*x**3 + \
        756*x**2 + 120*x - 1080)*Dx + (81*x**6 - 246*x**5 + 228*x**4 + 36*x**3 + \
        660*x**2 - 720*x)*Dx**2 + (-54*x**6 + 128*x**5 - 18*x**4 - 240*x**2 + 600)*Dx**3 + \
        (81*x**5 - 192*x**4 - 84*x**3 + 162*x**2 - 60*x - 180)*Dx**4 + (-108*x**3 + \
        192*x**2 + 72*x)*Dx**5 + (27*x**4 - 64*x**3 - 36*x**2 + 60)*Dx**6, x, 0, [3, 3, 3, -3, -12, -24])
    assert p * q == r
    p = HolonomicFunction(Dx - 1, x, 0, [2])
    q = HolonomicFunction(Dx**2 + 1, x, 0, [0, 1])
    r = HolonomicFunction(2 -2*Dx + Dx**2, x, 0, [0, 2])
    assert p * q == r
    q = HolonomicFunction(x*Dx**2+1+2*Dx,x,0,[0,1])
    r = HolonomicFunction((x - 1) + (-2*x + 2)*Dx + x*Dx**2, x, 0, [0, 2])
    assert p * q == r
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 3])
    q = HolonomicFunction(Dx**3 + 1, x, 0, [1, 2, 1])
    r = HolonomicFunction(6*Dx + 3*Dx**2 + 2*Dx**3 - 3*Dx**4 + Dx**6, x, 0, [1, 5, 14, 17, 17, 2])
    assert p * q == r
Exemple #22
0
def test_to_expr():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x, 0, [1]).to_expr()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4*x - 1)*Dx + \
        (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr().expand()
    q = 1/(x**2 - 2*x + 1)
    assert p == q
    p = expr_to_holonomic(sin(x)**2/x).integrate((x, 0, x)).to_expr()
    q = (sin(x)**2/x).integrate((x, 0, x))
    assert p == q
    C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
    p = expr_to_holonomic(log(1+x**2)).to_expr()
    q = C_2*log(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(log(1+x**2)).diff().to_expr()
    q = C_0*x/(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).to_expr()
    q = 3*C_3*x - 3*sqrt(pi)*C_3*erf(x)/2 + x + 2*x/sqrt(pi)
    assert p == q
    p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
    assert p == sqrt(x)
    assert expr_to_holonomic(sqrt(x)).to_expr() == sqrt(x)
    p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
    assert p == sqrt(1+x**2)
    p = expr_to_holonomic((2*x**2 + 1)**(S(2)/3)).to_expr()
    assert p == (2*x**2 + 1)**(S(2)/3)
    p = expr_to_holonomic(sqrt(-x**2+2*x)).to_expr()
    assert p == sqrt(x)*sqrt(-x + 2)
    p = expr_to_holonomic((-2*x**3+7*x)**(S(2)/3)).to_expr()
    q = x**(S(2)/3)*(-2*x**2 + 7)**(S(2)/3)
    assert p == q
    p = from_hyper(hyper((-2, -3), (S(1)/2, ), x))
    s = hyperexpand(hyper((-2, -3), (S(1)/2, ), x))
    D_0 = Symbol('D_0')
    C_0 = Symbol('C_0')
    assert (p.to_expr().subs({C_0:1, D_0:0}) - s).simplify() == 0
    p.y0 = {0: [1], S(1)/2: [0]}
    assert p.to_expr() == s
    assert expr_to_holonomic(x**5).to_expr() == x**5
    assert expr_to_holonomic(2*x**3-3*x**2).to_expr().expand() == \
        2*x**3-3*x**2
    a = symbols("a")
    p = (expr_to_holonomic(1.4*x)*expr_to_holonomic(a*x, x)).to_expr()
    q = 1.4*a*x**2
    assert p == q
    p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(a*x, x)).to_expr()
    q = x*(a + 1.4)
    assert p == q
    p = (expr_to_holonomic(1.4*x)+expr_to_holonomic(x)).to_expr()
    assert p == 2.4*x
Exemple #23
0
def test_to_Sequence():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    n = symbols('n', integer=True)
    _, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), 'Sn')
    p = HolonomicFunction(x**2*Dx**4 + x + Dx, x).to_sequence()
    q = [(HolonomicSequence(1 + (n + 2)*Sn**2 + (n**4 + 6*n**3 + 11*n**2 + 6*n)*Sn**3), 0, 1)]
    assert p == q
    p = HolonomicFunction(x**2*Dx**4 + x**3 + Dx**2, x).to_sequence()
    q = [(HolonomicSequence(1 + (n**4 + 14*n**3 + 72*n**2 + 163*n + 140)*Sn**5), 0, 0)]
    assert p == q
    p = HolonomicFunction(x**3*Dx**4 + 1 + Dx**2, x).to_sequence()
    q = [(HolonomicSequence(1 + (n**4 - 2*n**3 - n**2 + 2*n)*Sn + (n**2 + 3*n + 2)*Sn**2), 0, 0)]
    assert p == q
    p = HolonomicFunction(3*x**3*Dx**4 + 2*x*Dx + x*Dx**3, x).to_sequence()
    q = [(HolonomicSequence(2*n + (3*n**4 - 6*n**3 - 3*n**2 + 6*n)*Sn + (n**3 + 3*n**2 + 2*n)*Sn**2), 0, 1)]
    assert p == q
Exemple #24
0
def test_SDM_getitem():
    A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ)
    assert A.getitem(0, 0) == ZZ.zero
    assert A.getitem(0, 1) == ZZ.one
    assert A.getitem(1, 0) == ZZ.zero
    assert A.getitem(-2, -2) == ZZ.zero
    assert A.getitem(-2, -1) == ZZ.one
    assert A.getitem(-1, -2) == ZZ.zero
    raises(IndexError, lambda: A.getitem(2, 0))
    raises(IndexError, lambda: A.getitem(0, 2))
Exemple #25
0
def test_pickling_polys_polyclasses():
    from sympy.polys.polyclasses import DMP, DMF, ANP

    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)
Exemple #26
0
def test_pretty_Domain():
    expr = FF(23)

    assert  pretty(expr) == "GF(23)"
    assert upretty(expr) == u"ℤ₂₃"

    expr = ZZ

    assert  pretty(expr) == "ZZ"
    assert upretty(expr) == u"ℤ"

    expr = QQ

    assert  pretty(expr) == "QQ"
    assert upretty(expr) == u"ℚ"

    expr = RR

    assert  pretty(expr) == "RR"
    assert upretty(expr) == u"ℝ"

    expr = QQ[x]

    assert  pretty(expr) == "QQ[x]"
    assert upretty(expr) == u"ℚ[x]"

    expr = QQ[x, y]

    assert  pretty(expr) == "QQ[x, y]"
    assert upretty(expr) == u"ℚ[x, y]"

    expr = ZZ.frac_field(x)

    assert  pretty(expr) == "ZZ(x)"
    assert upretty(expr) == u"ℤ(x)"

    expr = ZZ.frac_field(x, y)

    assert  pretty(expr) == "ZZ(x, y)"
    assert upretty(expr) == u"ℤ(x, y)"
Exemple #27
0
def test_pretty_Domain():
    expr = FF(23)

    assert pretty(expr) == "GF(23)"
    assert upretty(expr) == u"ℤ₂₃"

    expr = ZZ

    assert pretty(expr) == "ZZ"
    assert upretty(expr) == u"ℤ"

    expr = QQ

    assert pretty(expr) == "QQ"
    assert upretty(expr) == u"ℚ"

    expr = RR

    assert pretty(expr) == "RR"
    assert upretty(expr) == u"ℝ"

    expr = QQ[x]

    assert pretty(expr) == "QQ[x]"
    assert upretty(expr) == u"ℚ[x]"

    expr = QQ[x, y]

    assert pretty(expr) == "QQ[x, y]"
    assert upretty(expr) == u"ℚ[x, y]"

    expr = ZZ.frac_field(x)

    assert pretty(expr) == "ZZ(x)"
    assert upretty(expr) == u"ℤ(x)"

    expr = ZZ.frac_field(x, y)

    assert pretty(expr) == "ZZ(x, y)"
    assert upretty(expr) == u"ℤ(x, y)"
Exemple #28
0
def test_to_Sequence():
    x = symbols("x")
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), "Dx")
    n = symbols("n", integer=True)
    _, Sn = RecurrenceOperators(ZZ.old_poly_ring(n), "Sn")
    p = HolonomicFunction(x ** 2 * Dx ** 4 + x + Dx, x).to_sequence()
    q = (HolonomicSequence(1 + (n + 2) * Sn ** 2 + (n ** 4 + 6 * n ** 3 + 11 * n ** 2 + 6 * n) * Sn ** 3), 1)
    assert p == q
    p = HolonomicFunction(x ** 2 * Dx ** 4 + x ** 3 + Dx ** 2, x).to_sequence()
    q = (HolonomicSequence(1 + (n ** 4 + 14 * n ** 3 + 72 * n ** 2 + 163 * n + 140) * Sn ** 5, n), 0)
    assert p == q
    p = HolonomicFunction(x ** 3 * Dx ** 4 + 1 + Dx ** 2, x).to_sequence()
    q = (HolonomicSequence(1 + (n ** 4 - 2 * n ** 3 - n ** 2 + 2 * n) * Sn + (n ** 2 + 3 * n + 2) * Sn ** 2, n), 3)
    assert p == q
    p = HolonomicFunction(3 * x ** 3 * Dx ** 4 + 2 * x * Dx + x * Dx ** 3, x).to_sequence()
    q = (
        HolonomicSequence(
            2 * n + (3 * n ** 4 - 6 * n ** 3 - 3 * n ** 2 + 6 * n) * Sn + (n ** 3 + 3 * n ** 2 + 2 * n) * Sn ** 2, n
        ),
        3,
    )
    assert p == q
Exemple #29
0
def test_SDM_vstack():
    A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ)
    B = SDM({1: {1: ZZ(1)}}, (2, 2), ZZ)
    AA = SDM({0: {1: ZZ(1)}, 2: {1: ZZ(1)}}, (4, 2), ZZ)
    AB = SDM({0: {1: ZZ(1)}, 3: {1: ZZ(1)}}, (4, 2), ZZ)
    assert SDM.vstack(A) == A
    assert SDM.vstack(A, A) == AA
    assert SDM.vstack(A, B) == AB
Exemple #30
0
def test_SDM_hstack():
    A = SDM({0: {1: ZZ(1)}}, (2, 2), ZZ)
    B = SDM({1: {1: ZZ(1)}}, (2, 2), ZZ)
    AA = SDM({0: {1: ZZ(1), 3: ZZ(1)}}, (2, 4), ZZ)
    AB = SDM({0: {1: ZZ(1)}, 1: {3: ZZ(1)}}, (2, 4), ZZ)
    assert SDM.hstack(A) == A
    assert SDM.hstack(A, A) == AA
    assert SDM.hstack(A, B) == AB
Exemple #31
0
def test_polymatrix_constructor():
    M1 = PolyMatrix([[x, y]], ring=QQ[x, y])
    assert M1.ring == QQ[x, y]
    assert M1.domain == QQ
    assert M1.gens == (x, y)
    assert M1.shape == (1, 2)
    assert M1.rows == 1
    assert M1.cols == 2
    assert len(M1) == 2
    assert list(M1) == [Poly(x, (x, y), domain=QQ), Poly(y, (x, y), domain=QQ)]

    M2 = PolyMatrix([[x, y]], ring=QQ[x][y])
    assert M2.ring == QQ[x][y]
    assert M2.domain == QQ[x]
    assert M2.gens == (y, )
    assert M2.shape == (1, 2)
    assert M2.rows == 1
    assert M2.cols == 2
    assert len(M2) == 2
    assert list(M2) == [
        Poly(x, (y, ), domain=QQ[x]),
        Poly(y, (y, ), domain=QQ[x])
    ]

    assert PolyMatrix([[x, y]], y) == PolyMatrix([[x, y]],
                                                 ring=ZZ.frac_field(x)[y])
    assert PolyMatrix([[x, y]], ring='ZZ[x,y]') == PolyMatrix([[x, y]],
                                                              ring=ZZ[x, y])

    assert PolyMatrix([[x, y]], (x, y)) == PolyMatrix([[x, y]], ring=QQ[x, y])
    assert PolyMatrix([[x, y]], x, y) == PolyMatrix([[x, y]], ring=QQ[x, y])
    assert PolyMatrix([x, y]) == PolyMatrix([[x], [y]], ring=QQ[x, y])
    assert PolyMatrix(1, 2, [x, y]) == PolyMatrix([[x, y]], ring=QQ[x, y])
    assert PolyMatrix(1, 2, lambda i, j: [x, y][j]) == PolyMatrix([[x, y]],
                                                                  ring=QQ[x,
                                                                          y])
    assert PolyMatrix(0, 2, [], x, y).shape == (0, 2)
    assert PolyMatrix(2, 0, [], x, y).shape == (2, 0)
    assert PolyMatrix([[], []], x, y).shape == (2, 0)
    assert PolyMatrix(ring=QQ[x, y]) == PolyMatrix(
        0, 0, [], ring=QQ[x, y]) == PolyMatrix([], ring=QQ[x, y])
    raises(TypeError, lambda: PolyMatrix())
    raises(TypeError, lambda: PolyMatrix(1))

    assert PolyMatrix([Poly(x), Poly(y)]) == PolyMatrix([[x], [y]],
                                                        ring=ZZ[x, y])

    # XXX: Maybe a bug in parallel_poly_from_expr (x lost from gens and domain):
    assert PolyMatrix([Poly(y, x), 1]) == PolyMatrix([[y], [1]], ring=QQ[y])
Exemple #32
0
def test_to_sympy():
    x = symbols("x")
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), "Dx")
    p = HolonomicFunction(Dx - 1, x, 0, 1).to_sympy()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx ** 2 + 1, x, 0, [1, 0]).to_sympy()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx ** 2 - 1, x, 0, [1, 0]).to_sympy()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4 * x - 1) * Dx + (x ** 2 - x) * Dx ** 2, x, 0, [1, 2]).to_sympy()
    q = 1 / (x ** 2 - 2 * x + 1)
    assert p == q
Exemple #33
0
def test_diff():
    x, y = symbols('x, y')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(x*Dx**2 + 1, x, 0, [0, 1])
    assert p.diff().to_expr() == p.to_expr().diff().simplify()
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0])
    assert p.diff(x, 2).to_expr() == p.to_expr()
    p = expr_to_holonomic(Si(x))
    assert p.diff().to_expr() == sin(x)/x
    assert p.diff(y) == 0
    C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
    q = Si(x)
    assert p.diff(x).to_expr() == q.diff()
    assert p.diff(x, 2).to_expr().subs(C_0, -S(1)/3) == q.diff(x, 2).simplify()
    assert p.diff(x, 3).series().subs({C_3:-S(1)/3, C_0:0}) == q.diff(x, 3).series()
Exemple #34
0
def test_diff():
    x, y = symbols('x, y')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(x*Dx**2 + 1, x, 0, [0, 1])
    assert p.diff().to_expr() == p.to_expr().diff().simplify()
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0])
    assert p.diff(x, 2).to_expr() == p.to_expr()
    p = expr_to_holonomic(Si(x))
    assert p.diff().to_expr() == sin(x)/x
    assert p.diff(y) == 0
    C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
    q = Si(x)
    assert p.diff(x).to_expr() == q.diff()
    assert p.diff(x, 2).to_expr().subs(C_0, -S(1)/3) == q.diff(x, 2).simplify()
    assert p.diff(x, 3).series().subs({C_3:-S(1)/3, C_0:0}) == q.diff(x, 3).series()
Exemple #35
0
def test_to_expr():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x, 0, 1).to_expr()
    q = exp(x)
    assert p == q
    p = HolonomicFunction(Dx**2 + 1, x, 0, [1, 0]).to_expr()
    q = cos(x)
    assert p == q
    p = HolonomicFunction(Dx**2 - 1, x, 0, [1, 0]).to_expr()
    q = cosh(x)
    assert p == q
    p = HolonomicFunction(2 + (4*x - 1)*Dx + \
        (x**2 - x)*Dx**2, x, 0, [1, 2]).to_expr().expand()
    q = 1 / (x**2 - 2 * x + 1)
    assert p == q
    p = expr_to_holonomic(sin(x)**2 / x).integrate((x, 0, x)).to_expr()
    q = (sin(x)**2 / x).integrate((x, 0, x))
    assert p == q
    C_0, C_1, C_2, C_3 = symbols('C_0, C_1, C_2, C_3')
    p = expr_to_holonomic(log(1 + x**2)).to_expr()
    q = C_2 * log(x**2 + 1)
    assert p == q
    p = expr_to_holonomic(log(1 + x**2)).diff().to_expr()
    q = C_0 * x / (x**2 + 1)
    assert p == q
    p = expr_to_holonomic(erf(x) + x).to_expr()
    q = 3 * C_3 * x - 3 * sqrt(pi) * C_3 * erf(x) / 2 + x + 2 * x / sqrt(pi)
    assert p == q
    p = expr_to_holonomic(sqrt(x), x0=1).to_expr()
    assert p == sqrt(x)
    assert expr_to_holonomic(sqrt(x)).to_expr() == sqrt(x)
    p = expr_to_holonomic(sqrt(1 + x**2)).to_expr()
    assert p == sqrt(1 + x**2)
    p = expr_to_holonomic((2 * x**2 + 1)**(S(2) / 3)).to_expr()
    assert p == (2 * x**2 + 1)**(S(2) / 3)
    p = expr_to_holonomic(sqrt(-x**2 + 2 * x)).to_expr()
    assert p == sqrt(x) * sqrt(-x + 2)
    p = expr_to_holonomic((-2 * x**3 + 7 * x)**(S(2) / 3)).to_expr()
    q = x**(S(2) / 3) * (-2 * x**2 + 7)**(S(2) / 3)
    assert p == q
    p = from_hyper(hyper((-2, -3), (S(1) / 2, ), x))
    s = hyperexpand(hyper((-2, -3), (S(1) / 2, ), x))
    D_0 = Symbol('D_0')
    C_0 = Symbol('C_0')
    assert (p.to_expr().subs({C_0: 1, D_0: 0}) - s).simplify() == 0
    p.singular_ics = [(0, [1]), (S(1) / 2, [0])]
    assert p.to_expr() == s
Exemple #36
0
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr()
    q = 1 - cos(x)
    assert p == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, 3))
    q = 1 - cos(3)
    assert p == q
    p = expr_to_holonomic(sin(x) / x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)**2 / x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x) / x)
    assert p.integrate(x).to_expr() == Si(x)
    assert p.integrate((x, 0, 2)) == Si(2)
    p = expr_to_holonomic(sin(x)**2 / x)
    q = p.to_expr()
    assert p.integrate(x).to_expr() == q.integrate((x, 0, x))
    assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1))
    assert expr_to_holonomic(1 / x, x0=1).integrate(x).to_expr() == log(x)
    p = expr_to_holonomic((x + 1)**3 * exp(-x), x0=-1).integrate(x).to_expr()
    q = (-x**3 - 6 * x**2 - 15 * x + 6 * exp(x + 1) - 16) * exp(-x)
    assert p == q
    p = expr_to_holonomic(cos(x)**2 / x**2, y0={
        -2: [1, 0, -1]
    }).integrate(x).to_expr()
    q = -Si(2 * x) - cos(x)**2 / x
    assert p == q
    p = expr_to_holonomic(sqrt(x**2 + x)).integrate(x).to_expr()
    q = (x**Rational(3, 2) * (2 * x**2 + 3 * x + 1) -
         x * sqrt(x + 1) * asinh(sqrt(x))) / (4 * x * sqrt(x + 1))
    assert p == q
    p = expr_to_holonomic(sqrt(x**2 + 1)).integrate(x).to_expr()
    q = (sqrt(x**2 + 1)).integrate(x)
    assert (p - q).simplify() == 0
    p = expr_to_holonomic(1 / x**2, y0={-2: [1, 0, 0]})
    r = expr_to_holonomic(1 / x**2, lenics=3)
    assert p == r
    q = expr_to_holonomic(cos(x)**2)
    assert (r * q).integrate(x).to_expr() == -Si(2 * x) - cos(x)**2 / x
Exemple #37
0
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = from_sympy(sin(x)**2 / x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = from_sympy(sin(x)).integrate((x, 0, x)).to_sympy()
    q = 1 - cos(x)
    assert p == q
    p = from_sympy(sin(x)).integrate((x, 0, 3))
    q = '1.98999246812687'
    assert sstr(p) == q
    p = from_sympy(sin(x) / x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = from_sympy(sin(x)**2 / x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = from_sympy(sin(x)**2/x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = from_sympy(sin(x)).integrate((x, 0, x)).to_sympy()
    q = 1 - cos(x)
    assert p == q
    p = from_sympy(sin(x)).integrate((x, 0, 3))
    q = '1.98999246812687'
    assert sstr(p) == q
    p = from_sympy(sin(x)/x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = from_sympy(sin(x)**2/x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
Exemple #39
0
def test_series():
    x = symbols("x")
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), "Dx")
    p = HolonomicFunction(Dx ** 2 + 2 * x * Dx, x, 0, [0, 1]).series(n=10)
    q = x - x ** 3 / 3 + x ** 5 / 10 - x ** 7 / 42 + x ** 9 / 216 + O(x ** 10)
    assert p == q
    p = HolonomicFunction(Dx - 1, x).composition(x ** 2, 0, 1)  # e^(x**2)
    q = HolonomicFunction(Dx ** 2 + 1, x, 0, [1, 0])  # cos(x)
    r = (p * q).series(n=10)  # expansion of cos(x) * exp(x**2)
    s = 1 + x ** 2 / 2 + x ** 4 / 24 - 31 * x ** 6 / 720 - 179 * x ** 8 / 8064 + O(x ** 10)
    assert r == s
    t = HolonomicFunction((1 + x) * Dx ** 2 + Dx, x, 0, [0, 1])  # log(1 + x)
    r = (p * t + q).series(n=10)
    s = (
        1
        + x
        - x ** 2
        + 4 * x ** 3 / 3
        - 17 * x ** 4 / 24
        + 31 * x ** 5 / 30
        - 481 * x ** 6 / 720
        + 71 * x ** 7 / 105
        - 20159 * x ** 8 / 40320
        + 379 * x ** 9 / 840
        + O(x ** 10)
    )
    assert r == s
    p = HolonomicFunction(
        (6 + 6 * x - 3 * x ** 2) - (10 * x - 3 * x ** 2 - 3 * x ** 3) * Dx + (4 - 6 * x ** 3 + 2 * x ** 4) * Dx ** 2,
        x,
        0,
        [0, 1],
    ).series(n=7)
    q = x + x ** 3 / 6 - 3 * x ** 4 / 16 + x ** 5 / 20 - 23 * x ** 6 / 960 + O(x ** 7)
    assert p == q
    p = HolonomicFunction(
        (6 + 6 * x - 3 * x ** 2) - (10 * x - 3 * x ** 2 - 3 * x ** 3) * Dx + (4 - 6 * x ** 3 + 2 * x ** 4) * Dx ** 2,
        x,
        0,
        [1, 0],
    ).series(n=7)
    q = 1 - 3 * x ** 2 / 4 - x ** 3 / 4 - 5 * x ** 4 / 32 - 3 * x ** 5 / 40 - 17 * x ** 6 / 384 + O(x ** 7)
    assert p == q
Exemple #40
0
def test_integrate():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 2, 3))
    q = '0.166270406994788'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, x)).to_expr()
    q = 1 - cos(x)
    assert p == q
    p = expr_to_holonomic(sin(x)).integrate((x, 0, 3))
    q = 1 - cos(3)
    assert p == q
    p = expr_to_holonomic(sin(x)/x, x0=1).integrate((x, 1, 2))
    q = '0.659329913368450'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)**2/x, x0=1).integrate((x, 1, 0))
    q = '-0.423690480850035'
    assert sstr(p) == q
    p = expr_to_holonomic(sin(x)/x)
    assert p.integrate(x).to_expr() == Si(x)
    assert p.integrate((x, 0, 2)) == Si(2)
    p = expr_to_holonomic(sin(x)**2/x)
    q = p.to_expr()
    assert p.integrate(x).to_expr() == q.integrate((x, 0, x))
    assert p.integrate((x, 0, 1)) == q.integrate((x, 0, 1))
    assert expr_to_holonomic(1/x, x0=1).integrate(x).to_expr() == log(x)
    p = expr_to_holonomic((x + 1)**3*exp(-x), x0=-1).integrate(x).to_expr()
    q = (-x**3 - 6*x**2 - 15*x + 6*exp(x + 1) - 16)*exp(-x)
    assert p == q
    p = expr_to_holonomic(cos(x)**2/x**2, y0={-2: [1, 0, -1]}).integrate(x).to_expr()
    q = -Si(2*x) - cos(x)**2/x
    assert p == q
    p = expr_to_holonomic(sqrt(x**2+x)).integrate(x).to_expr()
    q = (x**(3/2)*(2*x**2 + 3*x + 1) - x*sqrt(x + 1)*asinh(sqrt(x)))/(4*x*sqrt(x + 1))
    assert p == q
    p = expr_to_holonomic(sqrt(x**2+1)).integrate(x).to_expr()
    q = (sqrt(x**2+1)).integrate(x)
    assert (p-q).simplify() == 0
    p = expr_to_holonomic(1/x**2, y0={-2:[1, 0, 0]})
    r = expr_to_holonomic(1/x**2, lenics=3)
    assert p == r
    q = expr_to_holonomic(cos(x)**2)
    assert (r*q).integrate(x).to_expr() == -Si(2*x) - cos(x)**2/x
Exemple #41
0
def test_HolonomicFunction_addition():
    x = symbols('x')
    R, Dx = DiffOperatorAlgebra(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx**2 * x, x)
    q = HolonomicFunction((2) * Dx + (x) * Dx**2, x)
    assert p == q
    p = HolonomicFunction(x * Dx + 1, x)
    q = HolonomicFunction(Dx + 1, x)
    r = HolonomicFunction((x - 2) + (x**2 - 2) * Dx + (x**2 - x) * Dx**2, x)
    assert p + q == r
    p = HolonomicFunction(x * Dx + Dx**2 * (x**2 + 2), x)
    q = HolonomicFunction(Dx - 3, x)
    r = HolonomicFunction((-54 * x**2 - 126 * x - 150) + (-135 * x**3 - 252 * x**2 - 270 * x + 140) * Dx +\
                 (-27 * x**4 - 24 * x**2 + 14 * x - 150) * Dx**2 + \
                 (9 * x**4 + 15 * x**3 + 38 * x**2 + 30 * x +40) * Dx**3, x)
    assert p + q == r
    p = HolonomicFunction(Dx**5 - 1, x)
    q = HolonomicFunction(x**3 + Dx, x)
    r = HolonomicFunction((-x**18 + 45*x**14 - 525*x**10 + 1575*x**6 - x**3 - 630*x**2) + \
        (-x**15 + 30*x**11 - 195*x**7 + 210*x**3 - 1)*Dx + (x**18 - 45*x**14 + 525*x**10 - \
        1575*x**6 + x**3 + 630*x**2)*Dx**5 + (x**15 - 30*x**11 + 195*x**7 - 210*x**3 + \
        1)*Dx**6, x)
    assert p+q == r
Exemple #42
0
def test_HolonomicFunction_multiplication():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx + x + x * Dx**2, x)
    q = HolonomicFunction(x * Dx + Dx * x + Dx**2, x)
    r = HolonomicFunction((8*x**6 + 4*x**4 + 6*x**2 + 3) + (24*x**5 - 4*x**3 + 24*x)*Dx + \
        (8*x**6 + 20*x**4 + 12*x**2 + 2)*Dx**2 + (8*x**5 + 4*x**3 + 4*x)*Dx**3 + \
        (2*x**4 + x**2)*Dx**4, x)
    assert p * q == r
    p = HolonomicFunction(Dx**2 + 1, x)
    q = HolonomicFunction(Dx - 1, x)
    r = HolonomicFunction((2) + (-2) * Dx + (1) * Dx**2, x)
    assert p * q == r
    p = HolonomicFunction(Dx**2 + 1 + x + Dx, x)
    q = HolonomicFunction((Dx * x - 1)**2, x)
    r = HolonomicFunction((4*x**7 + 11*x**6 + 16*x**5 + 4*x**4 - 6*x**3 - 7*x**2 - 8*x - 2) + \
        (8*x**6 + 26*x**5 + 24*x**4 - 3*x**3 - 11*x**2 - 6*x - 2)*Dx + \
        (8*x**6 + 18*x**5 + 15*x**4 - 3*x**3 - 6*x**2 - 6*x - 2)*Dx**2 + (8*x**5 + \
            10*x**4 + 6*x**3 - 2*x**2 - 4*x)*Dx**3 + (4*x**5 + 3*x**4 - x**2)*Dx**4, x)
    assert p * q == r
    p = HolonomicFunction(x * Dx**2 - 1, x)
    q = HolonomicFunction(Dx * x - x, x)
    r = HolonomicFunction((x - 3) + (-2 * x + 2) * Dx + (x) * Dx**2, x)
    assert p * q == r
Exemple #43
0
def test_HolonomicFunction_composition():
    x = symbols('x')
    R, Dx = DifferentialOperators(ZZ.old_poly_ring(x), 'Dx')
    p = HolonomicFunction(Dx - 1, x).composition(x**2 + x)
    r = HolonomicFunction((-2 * x - 1) + Dx, x)
    assert p == r
    p = HolonomicFunction(Dx**2 + 1, x).composition(x**5 + x**2 + 1)
    r = HolonomicFunction((125*x**12 + 150*x**9 + 60*x**6 + 8*x**3) + (-20*x**3 - 2)*Dx + \
        (5*x**4 + 2*x)*Dx**2, x)
    assert p == r
    p = HolonomicFunction(Dx**2 * x + x, x).composition(2 * x**3 + x**2 + 1)
    r = HolonomicFunction((216*x**9 + 324*x**8 + 180*x**7 + 152*x**6 + 112*x**5 + \
        36*x**4 + 4*x**3) + (24*x**4 + 16*x**3 + 3*x**2 - 6*x - 1)*Dx + (6*x**5 + 5*x**4 + \
        x**3 + 3*x**2 + x)*Dx**2, x)
    assert p == r
    p = HolonomicFunction(Dx**2 + 1, x).composition(1 - x**2)
    r = HolonomicFunction((4 * x**3) - Dx + x * Dx**2, x)
    assert p == r
    p = HolonomicFunction(Dx**2 + 1, x).composition(x - 2 / (x**2 + 1))
    r = HolonomicFunction((x**12 + 6*x**10 + 12*x**9 + 15*x**8 + 48*x**7 + 68*x**6 + \
        72*x**5 + 111*x**4 + 112*x**3 + 54*x**2 + 12*x + 1) + (12*x**8 + 32*x**6 + \
        24*x**4 - 4)*Dx + (x**12 + 6*x**10 + 4*x**9 + 15*x**8 + 16*x**7 + 20*x**6 + 24*x**5+ \
        15*x**4 + 16*x**3 + 6*x**2 + 4*x + 1)*Dx**2, x)
    assert p == r
Exemple #44
0
def test_trace():
    a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
    b = eye(2, ZZ)

    assert trace(a, ZZ) == ZZ(10)
    assert trace(b, ZZ) == ZZ(2)
Exemple #45
0
def test_transpose():
    a = [[ZZ(3), ZZ(7), ZZ(4)], [ZZ(2), ZZ(4), ZZ(5)], [ZZ(6), ZZ(2), ZZ(3)]]
    b = eye(4, ZZ)

    assert transpose(a, ZZ) == ([[ZZ(3), ZZ(2), ZZ(6)], [ZZ(7), ZZ(4), ZZ(2)], [ZZ(4), ZZ(5), ZZ(3)]])
    assert transpose(b, ZZ) == b
Exemple #46
0
def test_C8():
    # Modular multiplicative inverse. Would be nice if divmod could do this.
    assert ZZ.invert(5, 7) == 3
    assert ZZ.invert(5, 6) == 5