Exemplo n.º 1
0
def ratint_ratpart(f, g, x):
    """
    Horowitz-Ostrogradsky algorithm.

    Given a field K and polynomials f and g in K[x], such that f and g
    are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
    such that f/g = A' + B and B has square-free denominator.

    Examples
    ========

        >>> from diofant.integrals.rationaltools import ratint_ratpart
        >>> from diofant.abc import x, y
        >>> from diofant import Poly
        >>> ratint_ratpart(Poly(1, x, domain='ZZ'),
        ... Poly(x + 1, x, domain='ZZ'), x)
        (0, 1/(x + 1))
        >>> ratint_ratpart(Poly(1, x, domain='EX'),
        ... Poly(x**2 + y**2, x, domain='EX'), x)
        (0, 1/(x**2 + y**2))
        >>> ratint_ratpart(Poly(36, x, domain='ZZ'),
        ... Poly(x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2, x, domain='ZZ'), x)
        ((12*x + 6)/(x**2 - 1), 12/(x**2 - x - 2))

    See Also
    ========

    diofant.integrals.rationaltools.ratint
    diofant.integrals.rationaltools.ratint_logpart
    """
    from diofant import solve

    f = Poly(f, x)
    g = Poly(g, x)

    u, v, _ = g.cofactors(g.diff())

    n = u.degree()
    m = v.degree()

    A_coeffs = [Dummy('a' + str(n - i)) for i in range(0, n)]
    B_coeffs = [Dummy('b' + str(m - i)) for i in range(0, m)]

    C_coeffs = A_coeffs + B_coeffs

    A = Poly(A_coeffs, x, domain=ZZ[C_coeffs])
    B = Poly(B_coeffs, x, domain=ZZ[C_coeffs])

    H = f - A.diff() * v + A * (u.diff() * v).quo(u) - B * u

    result = solve(H.coeffs(), C_coeffs)

    A = A.as_expr().subs(result)
    B = B.as_expr().subs(result)

    rat_part = cancel(A / u.as_expr(), x)
    log_part = cancel(B / v.as_expr(), x)

    return rat_part, log_part
Exemplo n.º 2
0
def test_atan2_expansion():
    assert cancel(atan2(x**2, x + 1).diff(x) - atan(x**2/(x + 1)).diff(x)) == 0
    assert cancel(atan(y/x).series(y, 0, 5) - atan2(y, x).series(y, 0, 5)
                  + atan2(0, x) - atan(0)) == O(y**5)
    assert cancel(atan(y/x).series(x, 1, 4) - atan2(y, x).series(x, 1, 4)
                  + atan2(y, 1) - atan(y)) == O((x - 1)**4, (x, 1))
    assert cancel(atan((y + x)/x).series(x, 1, 3) - atan2(y + x, x).series(x, 1, 3)
                  + atan2(1 + y, 1) - atan(1 + y)) == O((x - 1)**3, (x, 1))
    assert Matrix([atan2(y, x)]).jacobian([y, x]) == \
        Matrix([[x/(y**2 + x**2), -y/(y**2 + x**2)]])
Exemplo n.º 3
0
def test_sympyissue_9398():
    assert cancel(1e-14) != 0
    assert cancel(1e-14 * I) != 0

    assert simplify(1e-14) != 0
    assert simplify(1e-14 * I) != 0

    assert (I * Number(1.) * Number(10)**Number(-14)).simplify() != 0

    assert cancel(1e-20) != 0
    assert cancel(1e-20 * I) != 0

    assert simplify(1e-20) != 0
    assert simplify(1e-20 * I) != 0

    assert cancel(1e-100) != 0
    assert cancel(1e-100 * I) != 0

    assert simplify(1e-100) != 0
    assert simplify(1e-100 * I) != 0

    f = Float("1e-1000", 15)
    assert cancel(f) != 0
    assert cancel(f * I) != 0

    assert simplify(f) != 0
    assert simplify(f * I) != 0
Exemplo n.º 4
0
def test_binomial_symbolic():
    n = 10  # Because we're using for loops, can't do symbolic n
    p = symbols('p', positive=True)
    X = Binomial('X', n, p)
    assert simplify(E(X)) == n*p == simplify(moment(X, 1))
    assert simplify(variance(X)) == n*p*(1 - p) == simplify(cmoment(X, 2))
    assert cancel((skewness(X) - (1-2*p)/sqrt(n*p*(1-p)))) == 0

    # Test ability to change success/failure winnings
    H, T = symbols('H T')
    Y = Binomial('Y', n, p, succ=H, fail=T)
    assert simplify(E(Y) - (n*(H*p + T*(1 - p)))) == 0
Exemplo n.º 5
0
def test_sympyissue_6249():
    A = MatrixSymbol('A', 3, 3)
    B = MatrixSymbol('B', 3, 3)
    p = A * B - B * A
    assert cancel(p) == p
    assert combsimp(p) == p
    assert factor(p) == p
    assert separatevars(p) == p
    assert sqrtdenest(p) == p

    M = MatrixSymbol('M', 2, 1)
    assert simplify(M[0] / 2) == M[0] / 2
Exemplo n.º 6
0
def test_binomial_symbolic():
    n = 10  # Because we're using for loops, can't do symbolic n
    p = symbols('p', positive=True)
    X = Binomial('X', n, p)
    assert simplify(E(X)) == n * p == simplify(moment(X, 1))
    assert simplify(variance(X)) == n * p * (1 - p) == simplify(cmoment(X, 2))
    assert cancel((skewness(X) - (1 - 2 * p) / sqrt(n * p * (1 - p)))) == 0

    # Test ability to change success/failure winnings
    H, T = symbols('H T')
    Y = Binomial('Y', n, p, succ=H, fail=T)
    assert simplify(E(Y) - (n * (H * p + T * (1 - p)))) == 0
Exemplo n.º 7
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe / qe)
    Aeee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(13944145, 4720968))

    Heeo = harmonic(ne + pe / qo)
    Aeeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(2422020029, 702257080))

    Heoe = harmonic(ne + po / qe)
    Aeoe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(11818877030, 4286604231) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Heoo = harmonic(ne + po / qo)
    Aeoo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(11669332571, 3628714320))

    Hoee = harmonic(no + pe / qe)
    Aoee = (-log(10) + 2 * (Rational(-1, 4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
            (-sqrt(5) / 4 - Rational(1, 4)) *
            log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + pi *
            (Rational(1, 4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + Rational(5, 8))) +
            Rational(779405, 277704))

    Hoeo = harmonic(no + pe / qo)
    Aoeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            Rational(53857323, 16331560))

    Hooe = harmonic(no + po / qe)
    Aooe = (
        -log(20) + 2 *
        (Rational(1, 4) + sqrt(5) / 4) * log(Rational(-1, 4) + sqrt(5) / 4) +
        2 * (Rational(-1, 4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 - Rational(1, 4)) *
        log(sqrt(sqrt(5) / 8 + Rational(5, 8))) + 2 *
        (-sqrt(5) / 4 + Rational(1, 4)) * log(Rational(1, 4) + sqrt(5) / 4) +
        Rational(486853480, 186374097) + pi *
        (sqrt(5) / 8 + Rational(5, 8)) / sqrt(-sqrt(5) / 8 + Rational(5, 8)))

    Hooo = harmonic(no + po / qo)
    Aooo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            Rational(383693479, 125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e / a) == 1
        assert h.evalf() == a.evalf()
Exemplo n.º 8
0
def test_cancel():
    assert cancel(A * B - B * A) == A * B - B * A
    assert cancel(A * B * (x - 1)) == A * B * (x - 1)
    assert cancel(A * B * (x**2 - 1) / (x + 1)) == A * B * (x - 1)
    assert cancel(A * B * (x**2 - 1) / (x + 1) - B * A *
                  (x - 1)) == A * B * (x - 1) + (1 - x) * B * A
Exemplo n.º 9
0
def test_cancel():
    assert cancel(A*B - B*A) == A*B - B*A
    assert cancel(A*B*(x - 1)) == A*B*(x - 1)
    assert cancel(A*B*(x**2 - 1)/(x + 1)) == A*B*(x - 1)
    assert cancel(A*B*(x**2 - 1)/(x + 1) - B*A*(x - 1)) == A*B*(x - 1) + (1 - x)*B*A
Exemplo n.º 10
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe / qe)
    Aeee = (-log(10) + 2 * (-1 / Integer(4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + 5 / Integer(8))) + 2 *
            (-sqrt(5) / 4 - 1 / Integer(4)) *
            log(sqrt(sqrt(5) / 8 + 5 / Integer(8))) + pi *
            (1 / Integer(4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + 5 / Integer(8))) +
            13944145 / Integer(4720968))

    Heeo = harmonic(ne + pe / qo)
    Aeeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            2422020029 / Integer(702257080))

    Heoe = harmonic(ne + po / qe)
    Aeoe = (
        -log(20) + 2 *
        (1 / Integer(4) + sqrt(5) / 4) * log(-1 / Integer(4) + sqrt(5) / 4) +
        2 * (-1 / Integer(4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + 5 / Integer(8))) + 2 *
        (-sqrt(5) / 4 - 1 / Integer(4)) *
        log(sqrt(sqrt(5) / 8 + 5 / Integer(8))) + 2 *
        (-sqrt(5) / 4 + 1 / Integer(4)) * log(1 / Integer(4) + sqrt(5) / 4) +
        11818877030 / Integer(4286604231) + pi *
        (sqrt(5) / 8 + 5 / Integer(8)) / sqrt(-sqrt(5) / 8 + 5 / Integer(8)))

    Heoo = harmonic(ne + po / qo)
    Aeoo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            11669332571 / Integer(3628714320))

    Hoee = harmonic(no + pe / qe)
    Aoee = (-log(10) + 2 * (-1 / Integer(4) + sqrt(5) / 4) *
            log(sqrt(-sqrt(5) / 8 + 5 / Integer(8))) + 2 *
            (-sqrt(5) / 4 - 1 / Integer(4)) *
            log(sqrt(sqrt(5) / 8 + 5 / Integer(8))) + pi *
            (1 / Integer(4) + sqrt(5) / 4) /
            (2 * sqrt(-sqrt(5) / 8 + 5 / Integer(8))) +
            779405 / Integer(277704))

    Hoeo = harmonic(no + pe / qo)
    Aoeo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(4 * pi / 13) +
            2 * log(sin(2 * pi / 13)) * cos(32 * pi / 13) +
            2 * log(sin(5 * pi / 13)) * cos(80 * pi / 13) -
            2 * log(sin(6 * pi / 13)) * cos(5 * pi / 13) -
            2 * log(sin(4 * pi / 13)) * cos(pi / 13) +
            pi * cot(5 * pi / 13) / 2 -
            2 * log(sin(pi / 13)) * cos(3 * pi / 13) +
            53857323 / Integer(16331560))

    Hooe = harmonic(no + po / qe)
    Aooe = (
        -log(20) + 2 *
        (1 / Integer(4) + sqrt(5) / 4) * log(-1 / Integer(4) + sqrt(5) / 4) +
        2 * (-1 / Integer(4) + sqrt(5) / 4) *
        log(sqrt(-sqrt(5) / 8 + 5 / Integer(8))) + 2 *
        (-sqrt(5) / 4 - 1 / Integer(4)) *
        log(sqrt(sqrt(5) / 8 + 5 / Integer(8))) + 2 *
        (-sqrt(5) / 4 + 1 / Integer(4)) * log(1 / Integer(4) + sqrt(5) / 4) +
        486853480 / Integer(186374097) + pi *
        (sqrt(5) / 8 + 5 / Integer(8)) / sqrt(-sqrt(5) / 8 + 5 / Integer(8)))

    Hooo = harmonic(no + po / qo)
    Aooo = (-log(26) + 2 * log(sin(3 * pi / 13)) * cos(54 * pi / 13) +
            2 * log(sin(4 * pi / 13)) * cos(6 * pi / 13) +
            2 * log(sin(6 * pi / 13)) * cos(108 * pi / 13) -
            2 * log(sin(5 * pi / 13)) * cos(pi / 13) -
            2 * log(sin(pi / 13)) * cos(5 * pi / 13) +
            pi * cot(4 * pi / 13) / 2 -
            2 * log(sin(2 * pi / 13)) * cos(3 * pi / 13) +
            383693479 / Integer(125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e / a) == 1
        assert h.n() == a.n()
Exemplo n.º 11
0
def test_harmonic_rational():
    ne = Integer(6)
    no = Integer(5)
    pe = Integer(8)
    po = Integer(9)
    qe = Integer(10)
    qo = Integer(13)

    Heee = harmonic(ne + pe/qe)
    Aeee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + Rational(13944145, 4720968))

    Heeo = harmonic(ne + pe/qo)
    Aeeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13)
            + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13)
            - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2 - 2*log(sin(pi/13))*cos(3*pi/13)
            + Rational(2422020029, 702257080))

    Heoe = harmonic(ne + po/qe)
    Aeoe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
            + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
            + Rational(11818877030, 4286604231) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8)))

    Heoo = harmonic(ne + po/qo)
    Aeoo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13)
            + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13)
            - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2
            - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(11669332571, 3628714320))

    Hoee = harmonic(no + pe/qe)
    Aoee = (-log(10) + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + pi*(Rational(1, 4) + sqrt(5)/4)/(2*sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + Rational(779405, 277704))

    Hoeo = harmonic(no + pe/qo)
    Aoeo = (-log(26) + 2*log(sin(3*pi/13))*cos(4*pi/13) + 2*log(sin(2*pi/13))*cos(32*pi/13)
            + 2*log(sin(5*pi/13))*cos(80*pi/13) - 2*log(sin(6*pi/13))*cos(5*pi/13)
            - 2*log(sin(4*pi/13))*cos(pi/13) + pi*cot(5*pi/13)/2
            - 2*log(sin(pi/13))*cos(3*pi/13) + Rational(53857323, 16331560))

    Hooe = harmonic(no + po/qe)
    Aooe = (-log(20) + 2*(Rational(1, 4) + sqrt(5)/4)*log(Rational(-1, 4) + sqrt(5)/4)
            + 2*(Rational(-1, 4) + sqrt(5)/4)*log(sqrt(-sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 - Rational(1, 4))*log(sqrt(sqrt(5)/8 + Rational(5, 8)))
            + 2*(-sqrt(5)/4 + Rational(1, 4))*log(Rational(1, 4) + sqrt(5)/4)
            + Rational(486853480, 186374097) + pi*(sqrt(5)/8 + Rational(5, 8))/sqrt(-sqrt(5)/8 + Rational(5, 8)))

    Hooo = harmonic(no + po/qo)
    Aooo = (-log(26) + 2*log(sin(3*pi/13))*cos(54*pi/13) + 2*log(sin(4*pi/13))*cos(6*pi/13)
            + 2*log(sin(6*pi/13))*cos(108*pi/13) - 2*log(sin(5*pi/13))*cos(pi/13)
            - 2*log(sin(pi/13))*cos(5*pi/13) + pi*cot(4*pi/13)/2
            - 2*log(sin(2*pi/13))*cos(3*pi/13) + Rational(383693479, 125128080))

    H = [Heee, Heeo, Heoe, Heoo, Hoee, Hoeo, Hooe, Hooo]
    A = [Aeee, Aeeo, Aeoe, Aeoo, Aoee, Aoeo, Aooe, Aooo]

    for h, a in zip(H, A):
        e = expand_func(h).doit()
        assert cancel(e/a) == 1
        assert h.evalf() == a.evalf()