Beispiel #1
0
def test_sqrtdenest2():
    assert sqrtdenest(sqrt(16 - 2*r29 + 2*sqrt(55 - 10*r29))) == \
        r5 + sqrt(11 - 2*r29)
    e = sqrt(-r5 + sqrt(-2*r29 + 2*sqrt(-10*r29 + 55) + 16))
    assert sqrtdenest(e) == root(-2*r29 + 11, 4)
    r = sqrt(1 + r7)
    assert sqrtdenest(sqrt(1 + r)) == sqrt(1 + r)
    e = sqrt(((1 + sqrt(1 + 2*sqrt(3 + r2 + r5)))**2).expand())
    assert sqrtdenest(e) == 1 + sqrt(1 + 2*sqrt(r2 + r5 + 3))

    assert sqrtdenest(sqrt(5*r3 + 6*r2)) == sqrt(2)*root(3, 4) + root(27, 4)

    assert sqrtdenest(sqrt(((1 + r5 + sqrt(1 + r3))**2).expand())) == \
        1 + r5 + sqrt(1 + r3)

    assert sqrtdenest(sqrt(((1 + r5 + r7 + sqrt(1 + r3))**2).expand())) == \
        1 + sqrt(1 + r3) + r5 + r7

    e = sqrt(((1 + cos(2) + cos(3) + sqrt(1 + r3))**2).expand())
    assert sqrtdenest(e) == cos(3) + cos(2) + 1 + sqrt(1 + r3)

    e = sqrt(-2*r10 + 2*r2*sqrt(-2*r10 + 11) + 14)
    assert sqrtdenest(e) == sqrt(-2*r10 - 2*r2 + 4*r5 + 14)

    # check that the result is not more complicated than the input
    z = sqrt(-2*r29 + cos(2) + 2*sqrt(-10*r29 + 55) + 16)
    assert sqrtdenest(z) == z

    assert sqrtdenest(sqrt(r6 + sqrt(15))) == sqrt(r6 + sqrt(15))

    z = sqrt(15 - 2*sqrt(31) + 2*sqrt(55 - 10*r29))
    assert sqrtdenest(z) == z
Beispiel #2
0
def test_sympyissue_4956_5204():
    # issue sympy/sympy#4956
    v = ((-27*cbrt(12)*sqrt(31)*I +
          27*2**Rational(2, 3)*cbrt(3)*sqrt(31)*I) /
         (-2511*2**Rational(2, 3)*cbrt(3) +
          (29*cbrt(18) +
           9*cbrt(2)*3**Rational(2, 3)*sqrt(31)*I +
           87*cbrt(2)*root(3, 6)*I)**2))
    assert NS(v, 1, strict=False) == '0.e-198 - 0.e-198*I'

    # issue sympy/sympy#5204
    x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = symbols('x:10')
    v = ((-18873261792*x0 + 3110400000*I*x1*x5 + 1239810624*x1*x8 -
          97043832*x1*x9 + 304403832*x2*x6*(4*x0 + 1422)**Rational(2, 3) -
          56619785376*x2 - 41281887168*x5 - 1274950152*x6*x7 -
          13478400000*I*x8 + 5276370456*I*x9 - 357587765856 -
          108755765856*sqrt(3)*I)/((25596*x0 + 76788*x2 + 1106028)**2 +
                                   175732658352))
    v = v.subs(((x9, 2**Rational(2, 3)*root(3, 6)*x7),
                (x8, cbrt(2)*3**Rational(5, 6)*x4),
                (x7, x3**Rational(2, 3)), (x6, 6**Rational(2, 3)),
                (x5, cbrt(6)*x4), (x4, cbrt(x3)),
                (x3, 54*x0 + 1422), (x2, I*x1), (x1, sqrt(83)), (x0, sqrt(249))))

    assert NS(v, 5) == '0.077284 + 1.1104*I'
    assert NS(v, 1) == '0.08 + 1.*I'
Beispiel #3
0
def test_sympyissue_from_PR1599():
    n1, n2, n3, n4 = symbols('n1 n2 n3 n4', negative=True)
    assert simplify(I*sqrt(n1)) == -sqrt(-n1)
    assert (powsimp(sqrt(n1)*sqrt(n2)*sqrt(n3)) ==
            -I*sqrt(-n1)*sqrt(-n2)*sqrt(-n3))
    assert (powsimp(root(n1, 3)*root(n2, 3)*root(n3, 3)*root(n4, 3)) ==
            -cbrt(-1)*cbrt(-n1)*cbrt(-n2)*cbrt(-n3)*cbrt(-n4))
Beispiel #4
0
def test_rewrite():
    r3 = RootOf(x**3 + x - 1, 0)
    assert r3.evalf() == r3.rewrite(Pow).evalf()
    assert r3.rewrite(Pow) == (-1/(3*root(Rational(1, 2) + sqrt(93)/18, 3)) +
                               root(Rational(1, 2) + sqrt(93)/18, 3))
    r4 = RootOf(x**4 - x + 5, 0)
    assert r4.evalf() == r4.rewrite(Pow).evalf()
    r11 = RootOf(x**11 + x - 3, 0)
    assert r11.rewrite(Pow) == r11
Beispiel #5
0
def test_gruntz_other():
    assert gruntz(sqrt(log(x + 1)) - sqrt(log(x)), x) == 0  # p12, 2.5
    y = Symbol('y')
    assert gruntz(((1 + 1/x)**y - 1)*x, x) == y  # p12, 2.6
    n = Symbol('n', integer=True)
    assert gruntz(x**n/exp(x), x) == 0  # p14, 2.9
    assert gruntz((1 + 1/x)*x - 1/log(1 + 1/x), x) == Rational(1, 2)  # p15, 2.10
    m = Symbol('m', integer=True)
    assert gruntz((root(1 + 1/x, n) - 1)/(root(1 + 1/x, m) - 1),
                  x) == m/n  # p13, 2.7
Beispiel #6
0
def test_airybi():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airybi(z), airybi)

    assert airybi(0) == 3**Rational(5, 6) / (3 * gamma(Rational(2, 3)))
    assert airybi(oo) == oo
    assert airybi(-oo) == 0

    assert diff(airybi(z), z) == airybiprime(z)

    assert series(airybi(z), z, 0,
                  3) == (cbrt(3) * gamma(Rational(1, 3)) / (2 * pi) +
                         3**Rational(2, 3) * z * gamma(Rational(2, 3)) /
                         (2 * pi) + O(z**3))
    l = Limit(
        airybi(I / x) /
        (exp(Rational(2, 3) *
             (I / x)**Rational(3, 2)) * sqrt(pi * sqrt(I / x))), x, 0)
    assert l.doit() == l

    assert airybi(z).rewrite(hyper) == (root(3, 6) * z * hyper(
        (), (Rational(4, 3), ),
        z**Integer(3) / 9) / gamma(Rational(1, 3)) + 3**Rational(5, 6) * hyper(
            (), (Rational(2, 3), ),
            z**Integer(3) / 9) / (3 * gamma(Rational(2, 3))))

    assert isinstance(airybi(z).rewrite(besselj), airybi)
    assert (airybi(t).rewrite(besselj) == sqrt(3) * sqrt(-t) *
            (besselj(-1 / 3, 2 * (-t)**Rational(3, 2) / 3) -
             besselj(Rational(1, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3)
    assert airybi(z).rewrite(besseli) == (
        sqrt(3) * (z * besseli(Rational(1, 3), 2 * z**Rational(3, 2) / 3) /
                   cbrt(z**Rational(3, 2)) + cbrt(z**Rational(3, 2)) *
                   besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3)) / 3)
    assert airybi(p).rewrite(besseli) == (
        sqrt(3) * sqrt(p) *
        (besseli(-Rational(1, 3), 2 * p**Rational(3, 2) / 3) +
         besseli(Rational(1, 3), 2 * p**Rational(3, 2) / 3)) / 3)
    assert airybi(p).rewrite(besselj) == airybi(p)

    assert expand_func(airybi(
        2 *
        cbrt(3 * z**5))) == (sqrt(3) * (1 - cbrt(z**5) / z**Rational(5, 3)) *
                             airyai(2 * cbrt(3) * z**Rational(5, 3)) / 2 +
                             (1 + cbrt(z**5) / z**Rational(5, 3)) *
                             airybi(2 * cbrt(3) * z**Rational(5, 3)) / 2)
    assert expand_func(airybi(x * y)) == airybi(x * y)
    assert expand_func(airybi(log(x))) == airybi(log(x))
    assert expand_func(airybi(2 * root(3 * z**5, 5))) == airybi(
        2 * root(3 * z**5, 5))

    assert airybi(x).taylor_term(-1, x) == 0
Beispiel #7
0
def test_factor_terms():
    A = Symbol('A', commutative=False)
    assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \
        9*x*y + 9*x + _keep_coeff(Integer(3), x + 1)**_keep_coeff(Integer(2), x + 1) + 9
    assert factor_terms(9*(x + x*y + 1) + 3**(2 + 2*x)) == \
        _keep_coeff(Integer(9), 3**(2*x) + x*y + x + 1)
    assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \
        9*3**(2*x)*(a + 1)
    assert factor_terms(x + x*A) == \
        x*(1 + A)
    assert factor_terms(sin(x + x*A)) == \
        sin(x*(1 + A))
    assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \
        _keep_coeff(Integer(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1)
    assert factor_terms(x + (x*y + x)**(3*x + 3)) == \
        x + (x*(y + 1))**_keep_coeff(Integer(3), x + 1)
    assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \
        x*(a + 2*b)*(y + 1)
    i = Integral(x, (x, 0, oo))
    assert factor_terms(i) == i

    # check radical extraction
    eq = sqrt(2) + sqrt(10)
    assert factor_terms(eq) == eq
    assert factor_terms(eq, radical=True) == sqrt(2)*(1 + sqrt(5))
    eq = root(-6, 3) + root(6, 3)
    assert factor_terms(eq, radical=True) == cbrt(6)*(1 + cbrt(-1))

    eq = [x + x*y]
    ans = [x*(y + 1)]
    for c in [list, tuple, set]:
        assert factor_terms(c(eq)) == c(ans)
    assert factor_terms(Tuple(x + x*y)) == Tuple(x*(y + 1))
    assert factor_terms(Interval(0, 1)) == Interval(0, 1)
    e = 1/sqrt(a/2 + 1)
    assert factor_terms(e, clear=False) == 1/sqrt(a/2 + 1)
    assert factor_terms(e, clear=True) == sqrt(2)/sqrt(a + 2)

    eq = x/(x + 1/x) + 1/(x**2 + 1)
    assert factor_terms(eq, fraction=False) == eq
    assert factor_terms(eq, fraction=True) == 1

    assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \
        y*(2 + 1/(x + 1))/x**2

    # if not True, then processesing for this in factor_terms is not necessary
    assert gcd_terms(-x - y) == -x - y
    assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False)

    # if not True, then "special" processesing in factor_terms is not necessary
    assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1)
    e = exp(-x - 2) + x
    assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x
    assert factor_terms(e, sign=False) == e
    assert factor_terms(exp(-4*x - 2) - x) == -x + exp(Mul(-2, 2*x + 1, evaluate=False))
Beispiel #8
0
def test_factor_terms():
    A = Symbol('A', commutative=False)
    assert factor_terms(9*(x + x*y + 1) + (3*x + 3)**(2 + 2*x)) == \
        9*x*y + 9*x + _keep_coeff(Integer(3), x + 1)**_keep_coeff(Integer(2), x + 1) + 9
    assert factor_terms(9*(x + x*y + 1) + 3**(2 + 2*x)) == \
        _keep_coeff(Integer(9), 3**(2*x) + x*y + x + 1)
    assert factor_terms(3**(2 + 2*x) + a*3**(2 + 2*x)) == \
        9*3**(2*x)*(a + 1)
    assert factor_terms(x + x*A) == \
        x*(1 + A)
    assert factor_terms(sin(x + x*A)) == \
        sin(x*(1 + A))
    assert factor_terms((3*x + 3)**((2 + 2*x)/3)) == \
        _keep_coeff(Integer(3), x + 1)**_keep_coeff(Rational(2, 3), x + 1)
    assert factor_terms(x + (x*y + x)**(3*x + 3)) == \
        x + (x*(y + 1))**_keep_coeff(Integer(3), x + 1)
    assert factor_terms(a*(x + x*y) + b*(x*2 + y*x*2)) == \
        x*(a + 2*b)*(y + 1)
    i = Integral(x, (x, 0, oo))
    assert factor_terms(i) == i

    # check radical extraction
    eq = sqrt(2) + sqrt(10)
    assert factor_terms(eq) == eq
    assert factor_terms(eq, radical=True) == sqrt(2)*(1 + sqrt(5))
    eq = root(-6, 3) + root(6, 3)
    assert factor_terms(eq, radical=True) == cbrt(6)*(1 + cbrt(-1))

    eq = [x + x*y]
    ans = [x*(y + 1)]
    for c in [list, tuple, set]:
        assert factor_terms(c(eq)) == c(ans)
    assert factor_terms(Tuple(x + x*y)) == Tuple(x*(y + 1))
    assert factor_terms(Interval(0, 1)) == Interval(0, 1)
    e = 1/sqrt(a/2 + 1)
    assert factor_terms(e, clear=False) == 1/sqrt(a/2 + 1)
    assert factor_terms(e, clear=True) == sqrt(2)/sqrt(a + 2)

    eq = x/(x + 1/x) + 1/(x**2 + 1)
    assert factor_terms(eq, fraction=False) == eq
    assert factor_terms(eq, fraction=True) == 1

    assert factor_terms((1/(x**3 + x**2) + 2/x**2)*y) == \
        y*(2 + 1/(x + 1))/x**2

    # if not True, then processesing for this in factor_terms is not necessary
    assert gcd_terms(-x - y) == -x - y
    assert factor_terms(-x - y) == Mul(-1, x + y, evaluate=False)

    # if not True, then "special" processesing in factor_terms is not necessary
    assert gcd_terms(exp(Mul(-1, x + 1))) == exp(-x - 1)
    e = exp(-x - 2) + x
    assert factor_terms(e) == exp(Mul(-1, x + 2, evaluate=False)) + x
    assert factor_terms(e, sign=False) == e
    assert factor_terms(exp(-4*x - 2) - x) == -x + exp(Mul(-2, 2*x + 1, evaluate=False))
Beispiel #9
0
def test_sympyissue_21466():
    a, b, k, m, n = symbols('a b k m n', cls=Wild)
    pattern = x**n * (a + b * x**k)**m
    expr = root(x, 3) * root(3 - x**2, 3)
    assert expr.match(pattern) == {
        k: 2,
        b: -1,
        a: 3,
        m: Rational(1, 3),
        n: Rational(1, 3)
    }
Beispiel #10
0
def test_issue_6208():
    from diofant import root, Rational
    I = S.ImaginaryUnit
    assert sqrt(33**(9 * I / 10)) == -33**(9 * I / 20)
    assert root((6 * I)**(2 * I),
                3).as_base_exp()[1] == Rational(1, 3)  # != 2*I/3
    assert root((6 * I)**(I / 3), 3).as_base_exp()[1] == I / 9
    assert sqrt(exp(3 * I)) == exp(3 * I / 2)
    assert sqrt(-sqrt(3) * (1 + 2 * I)) == sqrt(sqrt(3)) * sqrt(-1 - 2 * I)
    assert sqrt(exp(5 * I)) == -exp(5 * I / 2)
    assert root(exp(5 * I), 3).exp == Rational(1, 3)
Beispiel #11
0
def test_gruntz_other():
    assert gruntz(sqrt(log(x + 1)) - sqrt(log(x)), x) == 0  # p12, 2.5
    y = Symbol('y')
    assert gruntz(((1 + 1 / x)**y - 1) * x, x) == y  # p12, 2.6
    n = Symbol('n', integer=True)
    assert gruntz(x**n / exp(x), x) == 0  # p14, 2.9
    assert gruntz((1 + 1 / x) * x - 1 / log(1 + 1 / x),
                  x) == Rational(1, 2)  # p15, 2.10
    m = Symbol('m', integer=True)
    assert gruntz((root(1 + 1 / x, n) - 1) / (root(1 + 1 / x, m) - 1),
                  x) == m / n  # p13, 2.7
Beispiel #12
0
def test_airyai():
    z = Symbol('z', extended_real=False)
    r = Symbol('r', extended_real=True)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyai(z), airyai)

    assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3)))
    assert airyai(oo) == 0
    assert airyai(-oo) == 0

    assert diff(airyai(z), z) == airyaiprime(z)

    assert series(airyai(z), z, 0, 3) == (
        3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))

    l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0)
    assert l.doit() == l  # cover _airyais._eval_aseries

    assert airyai(z).rewrite(hyper) == (
        -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) +
        cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))

    assert isinstance(airyai(z).rewrite(besselj), airyai)
    assert airyai(t).rewrite(besselj) == (
        sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) +
                  besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
    assert airyai(z).rewrite(besseli) == (
        -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) +
        cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3)
    assert airyai(p).rewrite(besseli) == (
        sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) -
                 besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)

    assert expand_func(airyai(2*cbrt(3*z**5))) == (
        -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 +
        (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airyai(x*y)) == airyai(x*y)
    assert expand_func(airyai(log(x))) == airyai(log(x))
    assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5))

    assert (airyai(r).as_real_imag() ==
            airyai(r).as_real_imag(deep=False) == (airyai(r), 0))
    assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False)
    assert (airyai(x).as_real_imag() ==
            (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
             airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
             I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
                airyai(re(x) + I*re(x)*abs(im(x))/Abs(re(x)))) *
             re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))

    assert airyai(x).taylor_term(-1, x) == 0
Beispiel #13
0
def test_airyai():
    z = Symbol('z', extended_real=False)
    r = Symbol('r', extended_real=True)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyai(z), airyai)

    assert airyai(0) == cbrt(3)/(3*gamma(Rational(2, 3)))
    assert airyai(oo) == 0
    assert airyai(-oo) == 0

    assert diff(airyai(z), z) == airyaiprime(z)

    assert airyai(z).series(z, 0, 3) == (
        3**Rational(5, 6)*gamma(Rational(1, 3))/(6*pi) - root(3, 6)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))

    l = Limit(airyai(I/x)/(exp(-Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))/2), x, 0)
    assert l.doit() == l  # cover _airyais._eval_aseries

    assert airyai(z).rewrite(hyper) == (
        -3**Rational(2, 3)*z*hyper((), (Rational(4, 3),), z**3/9)/(3*gamma(Rational(1, 3))) +
        cbrt(3)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))

    assert isinstance(airyai(z).rewrite(besselj), airyai)
    assert airyai(t).rewrite(besselj) == (
        sqrt(-t)*(besselj(-Rational(1, 3), 2*(-t)**Rational(3, 2)/3) +
                  besselj(Rational(1, 3), 2*(-t)**Rational(3, 2)/3))/3)
    assert airyai(z).rewrite(besseli) == (
        -z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/(3*cbrt(z**Rational(3, 2))) +
        cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3)
    assert airyai(p).rewrite(besseli) == (
        sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) -
                 besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)

    assert expand_func(airyai(2*cbrt(3*z**5))) == (
        -sqrt(3)*(-1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/6 +
        (1 + cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airyai(x*y)) == airyai(x*y)
    assert expand_func(airyai(log(x))) == airyai(log(x))
    assert expand_func(airyai(2*root(3*z**5, 5))) == airyai(2*root(3*z**5, 5))

    assert (airyai(r).as_real_imag() ==
            airyai(r).as_real_imag(deep=False) == (airyai(r), 0))
    assert airyai(x).as_real_imag() == airyai(x).as_real_imag(deep=False)
    assert (airyai(x).as_real_imag() ==
            (airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x)))/2 +
             airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))/2,
             I*(airyai(re(x) - I*re(x)*abs(im(x))/abs(re(x))) -
                airyai(re(x) + I*re(x)*abs(im(x))/abs(re(x)))) *
             re(x)*abs(im(x))/(2*im(x)*abs(re(x)))))

    assert airyai(x).taylor_term(-1, x) == 0
Beispiel #14
0
def test_sqrtdenest():
    d = {sqrt(5 + 2 * r6): r2 + r3,
         sqrt(5. + 2 * r6): sqrt(5. + 2 * r6),
         sqrt(5. + 4*sqrt(5 + 2 * r6)): sqrt(5.0 + 4*r2 + 4*r3),
         sqrt(r2): sqrt(r2),
         sqrt(5 + r7): sqrt(5 + r7),
         sqrt(3 + sqrt(5 + 2*r7)):
         3*r2*root(5 + 2*r7, 4)/(2*sqrt(6 + 3*r7)) +
         r2*sqrt(6 + 3*r7)/(2*root(5 + 2*r7, 4)),
         sqrt(3 + 2*r3): 3**Rational(3, 4)*(r6/2 + 3*r2/2)/3}
    for i in d:
        assert sqrtdenest(i) == d[i]
Beispiel #15
0
def test_airybiprime():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airybiprime(z), airybiprime)

    assert airybiprime(0) == root(3, 6) / gamma(Rational(1, 3))
    assert airybiprime(oo) == oo
    assert airybiprime(-oo) == 0

    assert diff(airybiprime(z), z) == z * airybi(z)

    assert series(airybiprime(z), z, 0,
                  3) == (root(3, 6) / gamma(Rational(1, 3)) +
                         3**Rational(5, 6) * z**2 /
                         (6 * gamma(Rational(2, 3))) + O(z**3))

    assert airybiprime(z).rewrite(hyper) == (
        3**Rational(5, 6) * z**2 * hyper((), (Rational(5, 3), ), z**3 / 9) /
        (6 * gamma(Rational(2, 3))) + root(3, 6) * hyper(
            (), (Rational(1, 3), ), z**3 / 9) / gamma(Rational(1, 3)))

    assert isinstance(airybiprime(z).rewrite(besselj), airybiprime)
    assert (airybiprime(t).rewrite(besselj) == -sqrt(3) * t *
            (besselj(-Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3) +
             besselj(Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3)
    assert airybiprime(z).rewrite(besseli) == (
        sqrt(3) * (z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) /
                   (z**Rational(3, 2))**Rational(2, 3) +
                   (z**Rational(3, 2))**Rational(2, 3) *
                   besseli(-Rational(2, 3), 2 * z**Rational(3, 2) / 3)) / 3)
    assert airybiprime(p).rewrite(besseli) == (
        sqrt(3) * p * (besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) +
                       besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3)
    assert airybiprime(p).rewrite(besselj) == airybiprime(p)

    assert expand_func(airybiprime(
        2 *
        cbrt(3 * z**5))) == (sqrt(3) * (z**Rational(5, 3) / cbrt(z**5) - 1) *
                             airyaiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2 +
                             (z**Rational(5, 3) / cbrt(z**5) + 1) *
                             airybiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2)
    assert expand_func(airybiprime(x * y)) == airybiprime(x * y)
    assert expand_func(airybiprime(log(x))) == airybiprime(log(x))
    assert expand_func(airybiprime(2 * root(3 * z**5, 5))) == airybiprime(
        2 * root(3 * z**5, 5))

    assert airybiprime(-2).evalf(50) == Float(
        '0.27879516692116952268509756941098324140300059345163131', dps=50)
Beispiel #16
0
def test_airyaiprime():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyaiprime(z), airyaiprime)

    assert airyaiprime(0) == -3**Rational(2, 3) / (3 * gamma(Rational(1, 3)))
    assert airyaiprime(oo) == 0

    assert diff(airyaiprime(z), z) == z * airyai(z)

    assert series(airyaiprime(z), z, 0,
                  3) == (-3**Rational(2, 3) / (3 * gamma(Rational(1, 3))) +
                         cbrt(3) * z**2 / (6 * gamma(Rational(2, 3))) +
                         O(z**3))

    assert airyaiprime(z).rewrite(hyper) == (
        cbrt(3) * z**2 * hyper((), (Rational(5, 3), ), z**3 / 9) /
        (6 * gamma(Rational(2, 3))) - 3**Rational(2, 3) * hyper(
            (), (Rational(1, 3), ), z**3 / 9) / (3 * gamma(Rational(1, 3))))

    assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime)
    assert (airyaiprime(t).rewrite(besselj) == t *
            (besselj(-Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3) -
             besselj(Rational(2, 3), 2 * (-t)**Rational(3, 2) / 3)) / 3)
    assert airyaiprime(z).rewrite(besseli) == (
        z**2 * besseli(Rational(2, 3), 2 * z**Rational(3, 2) / 3) /
        (3 * (z**Rational(3, 2))**Rational(2, 3)) -
        (z**Rational(3, 2))**Rational(2, 3) *
        besseli(-Rational(1, 3), 2 * z**Rational(3, 2) / 3) / 3)
    assert airyaiprime(p).rewrite(besseli) == (
        p * (-besseli(-Rational(2, 3), 2 * p**Rational(3, 2) / 3) +
             besseli(Rational(2, 3), 2 * p**Rational(3, 2) / 3)) / 3)
    assert airyaiprime(p).rewrite(besselj) == airyaiprime(p)

    assert expand_func(airyaiprime(
        2 *
        cbrt(3 * z**5))) == (sqrt(3) * (z**Rational(5, 3) / cbrt(z**5) - 1) *
                             airybiprime(2 * cbrt(3) * z**Rational(5, 3)) / 6 +
                             (z**Rational(5, 3) / cbrt(z**5) + 1) *
                             airyaiprime(2 * cbrt(3) * z**Rational(5, 3)) / 2)
    assert expand_func(airyaiprime(x * y)) == airyaiprime(x * y)
    assert expand_func(airyaiprime(log(x))) == airyaiprime(log(x))
    assert expand_func(airyaiprime(2 * root(3 * z**5, 5))) == airyaiprime(
        2 * root(3 * z**5, 5))

    assert airyaiprime(-2).evalf(50) == Float(
        '0.61825902074169104140626429133247528291577794512414753', dps=50)
Beispiel #17
0
def test_sympyissue_4956():
    v = ((-27 * cbrt(12) * sqrt(31) * I +
          27 * 2**Rational(2, 3) * cbrt(3) * sqrt(31) * I) /
         (-2511 * 2**Rational(2, 3) * cbrt(3) +
          (29 * cbrt(18) + 9 * cbrt(2) * 3**Rational(2, 3) * sqrt(31) * I +
           87 * cbrt(2) * root(3, 6) * I)**2))
    assert NS(v, 1, strict=False) == '0.e-198 - 0.e-198*I'
Beispiel #18
0
def test_trig_split():
    assert trig_split(cos(x), cos(y)) == (1, 1, 1, x, y, True)
    assert trig_split(2*cos(x), -2*cos(y)) == (2, 1, -1, x, y, True)
    assert trig_split(cos(x)*sin(y), cos(y)*sin(y)) == \
        (sin(y), 1, 1, x, y, True)

    assert trig_split(cos(x), -sqrt(3)*sin(x), two=True) == \
        (2, 1, -1, x, pi/6, False)
    assert trig_split(cos(x), sin(x), two=True) == \
        (sqrt(2), 1, 1, x, pi/4, False)
    assert trig_split(cos(x), -sin(x), two=True) == \
        (sqrt(2), 1, -1, x, pi/4, False)
    assert trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True) == \
        (2*sqrt(2), 1, -1, x, pi/6, False)
    assert trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True) == \
        (-2*sqrt(2), 1, 1, x, pi/3, False)
    assert trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True) == \
        (sqrt(6)/3, 1, 1, x, pi/6, False)
    assert trig_split(-sqrt(6)*cos(x)*sin(y),
                      -sqrt(2)*sin(x)*sin(y), two=True) == \
        (-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False)

    assert trig_split(cos(x), sin(x)) is None
    assert trig_split(cos(x), sin(z)) is None
    assert trig_split(2*cos(x), -sin(x)) is None
    assert trig_split(cos(x), -sqrt(3)*sin(x)) is None
    assert trig_split(cos(x)*cos(y), sin(x)*sin(z)) is None
    assert trig_split(cos(x)*cos(y), sin(x)*sin(y)) is None
    assert trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True) is \
        None

    assert trig_split(sqrt(3)*sqrt(x), cos(3), two=True) is None
    assert trig_split(sqrt(3)*root(x, 3), sin(3)*cos(2), two=True) is None
    assert trig_split(cos(5)*cos(6), cos(7)*sin(5), two=True) is None
Beispiel #19
0
def test_python_functions():
    # Simple
    assert python((2*x + exp(x))) in "x = Symbol('x')\ne = E**x + 2*x"
    assert python(sqrt(2)) == 'e = sqrt(2)'
    assert python(cbrt(2)) == 'e = 2**Rational(1, 3)'
    assert python(sqrt(2 + pi)) == 'e = sqrt(2 + pi)'
    assert python(cbrt(2 + pi)) == 'e = (2 + pi)**Rational(1, 3)'
    assert python(root(2, 4)) == 'e = 2**Rational(1, 4)'
    assert python(Abs(x)) == "x = Symbol('x')\ne = Abs(x)"
    assert python(
        Abs(x/(x**2 + 1))) in ["x = Symbol('x')\ne = Abs(x/(1 + x**2))",
                               "x = Symbol('x')\ne = Abs(x/(x**2 + 1))"]

    # Univariate/Multivariate functions
    f = Function('f')
    assert python(f(x)) == "x = Symbol('x')\nf = Function('f')\ne = f(x)"
    assert python(f(x, y)) == "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x, y)"
    assert python(f(x/(y + 1), y)) in [
        "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(1 + y), y)",
        "x = Symbol('x')\ny = Symbol('y')\nf = Function('f')\ne = f(x/(y + 1), y)"]

    # Nesting of square roots
    assert python(sqrt((sqrt(x + 1)) + 1)) in [
        "x = Symbol('x')\ne = sqrt(1 + sqrt(1 + x))",
        "x = Symbol('x')\ne = sqrt(sqrt(x + 1) + 1)"]

    # Nesting of powers
    assert python(cbrt(cbrt(x + 1) + 1)) in [
        "x = Symbol('x')\ne = (1 + (1 + x)**Rational(1, 3))**Rational(1, 3)",
        "x = Symbol('x')\ne = ((x + 1)**Rational(1, 3) + 1)**Rational(1, 3)"]

    # Function powers
    assert python(sin(x)**2) == "x = Symbol('x')\ne = sin(x)**2"
Beispiel #20
0
def test_Limits_simple_1():
    assert limit((n + 1) * (n + 2) * (n + 3) / n**3, n, oo) == 1  # 172
    assert limit(sqrt(n + 1) - sqrt(n), n, oo) == 0  # 179
    assert limit((2 * x - 3) * (3 * x + 5) * (4 * x - 6) /
                 (3 * x**3 + x - 1), x, oo) == 8  # Example 1
    assert limit(x / root(x**3 + 10, 3), x, oo) == 1  # Example 2
    assert limit((x + 1)**2 / (x**2 + 1), x, oo) == 1  # 181
Beispiel #21
0
def test_Limits_simple_4a():
    assert limit((sqrt(x) - sqrt(a)) / (x - a), x,
                 a) == 1 / (2 * sqrt(a))  # Example 5
    assert limit((sqrt(x) - 1) / (root(x, 3) - 1), x, 1) == Rational(3,
                                                                     2)  # 205
    assert limit((sqrt(1 + x) - sqrt(1 - x)) / x, x, 0) == 1  # 207
    assert limit(sqrt(x**2 - 5 * x + 6) - x, x, oo) == -Rational(5, 2)  # 213
Beispiel #22
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(a, "AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])" % repr(QQ))
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(a, "AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])" % repr(QQ))
Beispiel #23
0
def test_trig_split():
    assert trig_split(cos(x), cos(y)) == (1, 1, 1, x, y, True)
    assert trig_split(2 * cos(x), -2 * cos(y)) == (2, 1, -1, x, y, True)
    assert trig_split(cos(x)*sin(y), cos(y)*sin(y)) == \
        (sin(y), 1, 1, x, y, True)

    assert trig_split(cos(x), -sqrt(3)*sin(x), two=True) == \
        (2, 1, -1, x, pi/6, False)
    assert trig_split(cos(x), sin(x), two=True) == \
        (sqrt(2), 1, 1, x, pi/4, False)
    assert trig_split(cos(x), -sin(x), two=True) == \
        (sqrt(2), 1, -1, x, pi/4, False)
    assert trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True) == \
        (2*sqrt(2), 1, -1, x, pi/6, False)
    assert trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True) == \
        (-2*sqrt(2), 1, 1, x, pi/3, False)
    assert trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True) == \
        (sqrt(6)/3, 1, 1, x, pi/6, False)
    assert trig_split(-sqrt(6)*cos(x)*sin(y),
                      -sqrt(2)*sin(x)*sin(y), two=True) == \
        (-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False)

    assert trig_split(cos(x), sin(x)) is None
    assert trig_split(cos(x), sin(z)) is None
    assert trig_split(2 * cos(x), -sin(x)) is None
    assert trig_split(cos(x), -sqrt(3) * sin(x)) is None
    assert trig_split(cos(x) * cos(y), sin(x) * sin(z)) is None
    assert trig_split(cos(x) * cos(y), sin(x) * sin(y)) is None
    assert trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True) is \
        None

    assert trig_split(sqrt(3) * sqrt(x), cos(3), two=True) is None
    assert trig_split(sqrt(3) * root(x, 3), sin(3) * cos(2), two=True) is None
    assert trig_split(cos(5) * cos(6), cos(7) * sin(5), two=True) is None
Beispiel #24
0
def test_sympyissue_12578():
    s = root(1 - ((x - 1 / x) / 2)**(-4), 8)
    assert s.series(
        x, n=17) == (1 - 2 * x**4 - 8 * x**6 - 34 * x**8 - 152 * x**10 -
                     714 * x**12 - 3472 * x**14 - 17318 * x**16 + O(x**17))
    d10 = s.diff(x, 10)
    assert d10.limit(x, 0) == -551577600
Beispiel #25
0
def test_unrad0():
    s = symbols('s', cls=Dummy)

    # checkers to deal with possibility of answer coming
    # back with a sign change (cf issue sympy/sympy#5203)
    def check(rv, ans):
        assert bool(rv[1]) == bool(ans[1])
        if ans[1]:
            return s_check(rv, ans)
        e = rv[0].expand()
        a = ans[0].expand()
        return e in [a, -a] and rv[1] == ans[1]

    def s_check(rv, ans):
        # get the dummy
        rv = list(rv)
        d = rv[0].atoms(Dummy)
        reps = list(zip(d, [s] * len(d)))
        # replace s with this dummy
        rv = (rv[0].subs(reps).expand(),
              [rv[1][0].subs(reps), rv[1][1].subs(reps)])
        ans = (ans[0].subs(reps).expand(),
               [ans[1][0].subs(reps), ans[1][1].subs(reps)])
        return str(rv[0]) in [str(ans[0]), str(-ans[0])] and \
            str(rv[1]) == str(ans[1])

    assert check(unrad(sqrt(x) - root(x + 1, 3) * sqrt(x + 2) + 2),
                 (s**10 + 8 * s**8 + 24 * s**6 - 12 * s**5 - 22 * s**4 -
                  160 * s**3 - 212 * s**2 - 192 * s - 56, [s, s**2 - x]))
Beispiel #26
0
def test_unrad0():
    s = symbols('s', cls=Dummy)

    # checkers to deal with possibility of answer coming
    # back with a sign change (cf issue sympy/sympy#5203)
    def check(rv, ans):
        assert bool(rv[1]) == bool(ans[1])
        if ans[1]:
            return s_check(rv, ans)
        e = rv[0].expand()
        a = ans[0].expand()
        return e in [a, -a] and rv[1] == ans[1]

    def s_check(rv, ans):
        # get the dummy
        rv = list(rv)
        d = rv[0].atoms(Dummy)
        reps = list(zip(d, [s]*len(d)))
        # replace s with this dummy
        rv = (rv[0].subs(reps).expand(), [rv[1][0].subs(reps), rv[1][1].subs(reps)])
        ans = (ans[0].subs(reps).expand(), [ans[1][0].subs(reps), ans[1][1].subs(reps)])
        return str(rv[0]) in [str(ans[0]), str(-ans[0])] and \
            str(rv[1]) == str(ans[1])

    assert check(unrad(sqrt(x) - root(x + 1, 3)*sqrt(x + 2) + 2),
                 (s**10 + 8*s**8 + 24*s**6 - 12*s**5 - 22*s**4 - 160*s**3 - 212*s**2 -
                  192*s - 56, [s, s**2 - x]))
Beispiel #27
0
def test_AlgebraicNumber():
    a = AlgebraicNumber(sqrt(2))
    sT(a, "AlgebraicNumber(Pow(Integer(2), Rational(1, 2)), Tuple(Integer(1), Integer(0)))")
    a = AlgebraicNumber(sqrt(2), alias='a')
    sT(a, "AlgebraicNumber(Pow(Integer(2), Rational(1, 2)), Tuple(Integer(1), Integer(0)), Symbol('a'))")
    a = AlgebraicNumber(root(-2, 3))
    sT(a, "AlgebraicNumber(Pow(Integer(-2), Rational(1, 3)), Tuple(Integer(1), Integer(0)))")
Beispiel #28
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(a, 'AlgebraicField(%s, Pow(Integer(2), Rational(1, 2)))([Integer(1), Integer(0)])' % repr(QQ))
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(a, 'AlgebraicField(%s, Pow(Integer(-2), Rational(1, 3)))([Integer(1), Integer(0)])' % repr(QQ))
Beispiel #29
0
def test_expressions():
    assert residue(1 / (x + 1), x, 0) == 0
    assert residue(1 / (x + 1), x, -1) == 1
    assert residue(1 / (x**2 + 1), x, -1) == 0
    assert residue(1 / (x**2 + 1), x, I) == -I / 2
    assert residue(1 / (x**2 + 1), x, -I) == I / 2
    assert residue(1 / (x**4 + 1), x, 0) == 0
    assert residue(1 / (x**4 + 1), x, exp(I * pi / 4)) == -root(-1, 4) / 4
    assert residue(1 / (x**2 + a**2)**2, x, a * I) == -I / 4 / a**3
Beispiel #30
0
def test_expressions():
    assert residue(1/(x + 1), x, 0) == 0
    assert residue(1/(x + 1), x, -1) == 1
    assert residue(1/(x**2 + 1), x, -1) == 0
    assert residue(1/(x**2 + 1), x, I) == -I/2
    assert residue(1/(x**2 + 1), x, -I) == I/2
    assert residue(1/(x**4 + 1), x, 0) == 0
    assert residue(1/(x**4 + 1), x, exp(I*pi/4)) == -root(-1, 4)/4
    assert residue(1/(x**2 + a**2)**2, x, a*I) == -I/4/a**3
Beispiel #31
0
def test_roots_cyclotomic():
    assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1]
    assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1]
    assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True)) == [
        -Rational(1, 2) - I * sqrt(3) / 2, -Rational(1, 2) + I * sqrt(3) / 2
    ]
    assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I]
    assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True)) == [
        Rational(1, 2) - I * sqrt(3) / 2,
        Rational(1, 2) + I * sqrt(3) / 2
    ]

    assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [
        -cos(pi / 7) - I * sin(pi / 7),
        -cos(pi / 7) + I * sin(pi / 7),
        -cos(3 * pi / 7) - I * sin(3 * pi / 7),
        -cos(3 * pi / 7) + I * sin(3 * pi / 7),
        cos(2 * pi / 7) - I * sin(2 * pi / 7),
        cos(2 * pi / 7) + I * sin(2 * pi / 7),
    ]

    assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [
        -sqrt(2) / 2 - I * sqrt(2) / 2,
        -sqrt(2) / 2 + I * sqrt(2) / 2,
        sqrt(2) / 2 - I * sqrt(2) / 2,
        sqrt(2) / 2 + I * sqrt(2) / 2,
    ]

    assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [
        -sqrt(3) / 2 - I / 2,
        -sqrt(3) / 2 + I / 2,
        sqrt(3) / 2 - I / 2,
        sqrt(3) / 2 + I / 2,
    ]

    assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True),
                            factor=True) == [1]
    assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True),
                            factor=True) == [-1]

    assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \
        [-root(-1, 3), -1 + root(-1, 3)]
    assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \
        [-I, I]
    assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \
        [-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3]

    assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \
        [1 - root(-1, 3), root(-1, 3)]
Beispiel #32
0
def test_Rational():
    n1 = Rational(1, 4)
    n2 = Rational(1, 3)
    n3 = Rational(2, 4)
    n4 = Rational(2, -4)
    n5 = Integer(0)
    n7 = Integer(3)
    n8 = Integer(-3)
    assert str(n1 * n2) == "1/12"
    assert str(n1 * n2) == "1/12"
    assert str(n3) == "1/2"
    assert str(n1 * n3) == "1/8"
    assert str(n1 + n3) == "3/4"
    assert str(n1 + n2) == "7/12"
    assert str(n1 + n4) == "-1/4"
    assert str(n4 * n4) == "1/4"
    assert str(n4 + n2) == "-1/6"
    assert str(n4 + n5) == "-1/2"
    assert str(n4 * n5) == "0"
    assert str(n3 + n4) == "0"
    assert str(n1**n7) == "1/64"
    assert str(n2**n7) == "1/27"
    assert str(n2**n8) == "27"
    assert str(n7**n8) == "1/27"
    assert str(Rational("-25")) == "-25"
    assert str(Rational("1.25")) == "5/4"
    assert str(Rational("-2.6e-2")) == "-13/500"
    assert str(Rational(25, 7)) == "25/7"
    assert str(Rational(-123, 569)) == "-123/569"

    assert str(sqrt(Rational(1, 4))) == "1/2"
    assert str(sqrt(Rational(1, 36))) == "1/6"

    assert str(root(123**25, 25)) == "123"
    assert str(root(123**25 + 1, 25)) != "123"
    assert str(root(123**25 - 1, 25)) != "123"
    assert str(root(123**25 - 1, 25)) != "122"

    assert str(sqrt(Rational(81, 36))**3) == "27/8"
    assert str(1 / sqrt(Rational(81, 36))**3) == "8/27"

    assert str(sqrt(-4)) == str(2 * I)
    assert str(root(2, 10**10)) == "2**(1/10000000000)"
Beispiel #33
0
def test_airybi():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airybi(z), airybi)

    assert airybi(0) == 3**Rational(5, 6)/(3*gamma(Rational(2, 3)))
    assert airybi(oo) == oo
    assert airybi(-oo) == 0

    assert diff(airybi(z), z) == airybiprime(z)

    assert series(airybi(z), z, 0, 3) == (
        cbrt(3)*gamma(Rational(1, 3))/(2*pi) + 3**Rational(2, 3)*z*gamma(Rational(2, 3))/(2*pi) + O(z**3))
    l = Limit(airybi(I/x)/(exp(Rational(2, 3)*(I/x)**Rational(3, 2))*sqrt(pi*sqrt(I/x))), x, 0)
    assert l.doit() == l

    assert airybi(z).rewrite(hyper) == (
        root(3, 6)*z*hyper((), (Rational(4, 3),), z**3/9)/gamma(Rational(1, 3)) +
        3**Rational(5, 6)*hyper((), (Rational(2, 3),), z**3/9)/(3*gamma(Rational(2, 3))))

    assert isinstance(airybi(z).rewrite(besselj), airybi)
    assert (airybi(t).rewrite(besselj) ==
            sqrt(3)*sqrt(-t)*(besselj(-1/3, 2*(-t)**Rational(3, 2)/3) -
                              besselj(Rational(1, 3),
                                      2*(-t)**Rational(3, 2)/3))/3)
    assert airybi(z).rewrite(besseli) == (
        sqrt(3)*(z*besseli(Rational(1, 3), 2*z**Rational(3, 2)/3)/cbrt(z**Rational(3, 2)) +
                 cbrt(z**Rational(3, 2))*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3))/3)
    assert airybi(p).rewrite(besseli) == (
        sqrt(3)*sqrt(p)*(besseli(-Rational(1, 3), 2*p**Rational(3, 2)/3) +
                         besseli(Rational(1, 3), 2*p**Rational(3, 2)/3))/3)
    assert airybi(p).rewrite(besselj) == airybi(p)

    assert expand_func(airybi(2*cbrt(3*z**5))) == (
        sqrt(3)*(1 - cbrt(z**5)/z**Rational(5, 3))*airyai(2*cbrt(3)*z**Rational(5, 3))/2 +
        (1 + cbrt(z**5)/z**Rational(5, 3))*airybi(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airybi(x*y)) == airybi(x*y)
    assert expand_func(airybi(log(x))) == airybi(log(x))
    assert expand_func(airybi(2*root(3*z**5, 5))) == airybi(2*root(3*z**5, 5))

    assert airybi(x).taylor_term(-1, x) == 0
Beispiel #34
0
def test_Rational():
    n1 = Rational(1, 4)
    n2 = Rational(1, 3)
    n3 = Rational(2, 4)
    n4 = Rational(2, -4)
    n5 = Integer(0)
    n7 = Integer(3)
    n8 = Integer(-3)
    assert str(n1*n2) == "1/12"
    assert str(n1*n2) == "1/12"
    assert str(n3) == "1/2"
    assert str(n1*n3) == "1/8"
    assert str(n1 + n3) == "3/4"
    assert str(n1 + n2) == "7/12"
    assert str(n1 + n4) == "-1/4"
    assert str(n4*n4) == "1/4"
    assert str(n4 + n2) == "-1/6"
    assert str(n4 + n5) == "-1/2"
    assert str(n4*n5) == "0"
    assert str(n3 + n4) == "0"
    assert str(n1**n7) == "1/64"
    assert str(n2**n7) == "1/27"
    assert str(n2**n8) == "27"
    assert str(n7**n8) == "1/27"
    assert str(Rational("-25")) == "-25"
    assert str(Rational("1.25")) == "5/4"
    assert str(Rational("-2.6e-2")) == "-13/500"
    assert str(Rational(25, 7)) == "25/7"
    assert str(Rational(-123, 569)) == "-123/569"

    assert str(sqrt(Rational(1, 4))) == "1/2"
    assert str(sqrt(Rational(1, 36))) == "1/6"

    assert str(root(123**25, 25)) == "123"
    assert str(root(123**25 + 1, 25)) != "123"
    assert str(root(123**25 - 1, 25)) != "123"
    assert str(root(123**25 - 1, 25)) != "122"

    assert str(sqrt(Rational(81, 36))**3) == "27/8"
    assert str(1/sqrt(Rational(81, 36))**3) == "8/27"

    assert str(sqrt(-4)) == str(2*I)
    assert str(root(2, 10**10)) == "2**(1/10000000000)"
Beispiel #35
0
def test_Rational():
    n1 = Rational(1, 4)
    n2 = Rational(1, 3)
    n3 = Rational(2, 4)
    n4 = Rational(2, -4)
    n5 = Integer(0)
    n7 = Integer(3)
    n8 = Integer(-3)
    assert str(n1 * n2) == '1/12'
    assert str(n1 * n2) == '1/12'
    assert str(n3) == '1/2'
    assert str(n1 * n3) == '1/8'
    assert str(n1 + n3) == '3/4'
    assert str(n1 + n2) == '7/12'
    assert str(n1 + n4) == '-1/4'
    assert str(n4 * n4) == '1/4'
    assert str(n4 + n2) == '-1/6'
    assert str(n4 + n5) == '-1/2'
    assert str(n4 * n5) == '0'
    assert str(n3 + n4) == '0'
    assert str(n1**n7) == '1/64'
    assert str(n2**n7) == '1/27'
    assert str(n2**n8) == '27'
    assert str(n7**n8) == '1/27'
    assert str(Rational('-25')) == '-25'
    assert str(Rational('1.25')) == '5/4'
    assert str(Rational('-2.6e-2')) == '-13/500'
    assert str(Rational(25, 7)) == '25/7'
    assert str(Rational(-123, 569)) == '-123/569'

    assert str(sqrt(Rational(1, 4))) == '1/2'
    assert str(sqrt(Rational(1, 36))) == '1/6'

    assert str(root(123**25, 25)) == '123'
    assert str(root(123**25 + 1, 25)) != '123'
    assert str(root(123**25 - 1, 25)) != '123'
    assert str(root(123**25 - 1, 25)) != '122'

    assert str(sqrt(Rational(81, 36))**3) == '27/8'
    assert str(1 / sqrt(Rational(81, 36))**3) == '8/27'

    assert str(sqrt(-4)) == str(2 * I)
    assert str(root(2, 10**10)) == '2**(1/10000000000)'
Beispiel #36
0
def test_intractable():
    assert gruntz(1/gamma(x), x) == 0
    assert gruntz(1/loggamma(x), x) == 0
    assert gruntz(gamma(x)/loggamma(x), x) == oo
    assert gruntz(exp(gamma(x))/gamma(x), x) == oo
    assert gruntz(gamma(3 + 1/x), x) == 2
    assert gruntz(gamma(Rational(1, 7) + 1/x), x) == gamma(Rational(1, 7))
    assert gruntz(log(x**x)/log(gamma(x)), x) == 1
    assert gruntz(log(gamma(gamma(x)))/exp(x), x) == oo

    # issue sympy/sympy#10804
    assert gruntz(2*airyai(x)*root(x, 4) *
                  exp(2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airybi(x)*root(x, 4) *
                  exp(-2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airyai(1/x), x) == (3**Rational(5, 6) *
                                      gamma(Rational(1, 3))/(6*pi))
    assert gruntz(airybi(1/x), x) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert gruntz(airyai(2 + 1/x), x) == airyai(2)
    assert gruntz(airybi(2 + 1/x), x) == airybi(2)
Beispiel #37
0
def test_sqrtdenest():
    d = {
        sqrt(5 + 2 * r6):
        r2 + r3,
        sqrt(5. + 2 * r6):
        sqrt(5. + 2 * r6),
        sqrt(5. + 4 * sqrt(5 + 2 * r6)):
        sqrt(5.0 + 4 * r2 + 4 * r3),
        sqrt(r2):
        sqrt(r2),
        sqrt(5 + r7):
        sqrt(5 + r7),
        sqrt(3 + sqrt(5 + 2 * r7)):
        3 * r2 * root(5 + 2 * r7, 4) / (2 * sqrt(6 + 3 * r7)) +
        r2 * sqrt(6 + 3 * r7) / (2 * root(5 + 2 * r7, 4)),
        sqrt(3 + 2 * r3):
        3**Rational(3, 4) * (r6 / 2 + 3 * r2 / 2) / 3
    }
    for i in d:
        assert sqrtdenest(i) == d[i]
Beispiel #38
0
def test_intractable():
    assert gruntz(1/gamma(x), x) == 0
    assert gruntz(1/loggamma(x), x) == 0
    assert gruntz(gamma(x)/loggamma(x), x) == oo
    assert gruntz(exp(gamma(x))/gamma(x), x) == oo
    assert gruntz(gamma(3 + 1/x), x) == 2
    assert gruntz(gamma(Rational(1, 7) + 1/x), x) == gamma(Rational(1, 7))
    assert gruntz(log(x**x)/log(gamma(x)), x) == 1
    assert gruntz(log(gamma(gamma(x)))/exp(x), x) == oo

    # issue sympy/sympy#10804
    assert gruntz(2*airyai(x)*root(x, 4) *
                  exp(2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airybi(x)*root(x, 4) *
                  exp(-2*x**Rational(3, 2)/3), x) == 1/sqrt(pi)
    assert gruntz(airyai(1/x), x) == (3**Rational(5, 6) *
                                      gamma(Rational(1, 3))/(6*pi))
    assert gruntz(airybi(1/x), x) == cbrt(3)*gamma(Rational(1, 3))/(2*pi)
    assert gruntz(airyai(2 + 1/x), x) == airyai(2)
    assert gruntz(airybi(2 + 1/x), x) == airybi(2)
Beispiel #39
0
def test_roots_cyclotomic():
    assert roots_cyclotomic(cyclotomic_poly(1, x, polys=True)) == [1]
    assert roots_cyclotomic(cyclotomic_poly(2, x, polys=True)) == [-1]
    assert roots_cyclotomic(cyclotomic_poly(
        3, x, polys=True)) == [-Rational(1, 2) - I*sqrt(3)/2, -Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True)) == [-I, I]
    assert roots_cyclotomic(cyclotomic_poly(
        6, x, polys=True)) == [Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]

    assert roots_cyclotomic(cyclotomic_poly(7, x, polys=True)) == [
        -cos(pi/7) - I*sin(pi/7),
        -cos(pi/7) + I*sin(pi/7),
        -cos(3*pi/7) - I*sin(3*pi/7),
        -cos(3*pi/7) + I*sin(3*pi/7),
        cos(2*pi/7) - I*sin(2*pi/7),
        cos(2*pi/7) + I*sin(2*pi/7),
    ]

    assert roots_cyclotomic(cyclotomic_poly(8, x, polys=True)) == [
        -sqrt(2)/2 - I*sqrt(2)/2,
        -sqrt(2)/2 + I*sqrt(2)/2,
        sqrt(2)/2 - I*sqrt(2)/2,
        sqrt(2)/2 + I*sqrt(2)/2,
    ]

    assert roots_cyclotomic(cyclotomic_poly(12, x, polys=True)) == [
        -sqrt(3)/2 - I/2,
        -sqrt(3)/2 + I/2,
        sqrt(3)/2 - I/2,
        sqrt(3)/2 + I/2,
    ]

    assert roots_cyclotomic(
        cyclotomic_poly(1, x, polys=True), factor=True) == [1]
    assert roots_cyclotomic(
        cyclotomic_poly(2, x, polys=True), factor=True) == [-1]

    assert roots_cyclotomic(cyclotomic_poly(3, x, polys=True), factor=True) == \
        [-root(-1, 3), -1 + root(-1, 3)]
    assert roots_cyclotomic(cyclotomic_poly(4, x, polys=True), factor=True) == \
        [-I, I]
    assert roots_cyclotomic(cyclotomic_poly(5, x, polys=True), factor=True) == \
        [-root(-1, 5), -root(-1, 5)**3, root(-1, 5)**2, -1 - root(-1, 5)**2 + root(-1, 5) + root(-1, 5)**3]

    assert roots_cyclotomic(cyclotomic_poly(6, x, polys=True), factor=True) == \
        [1 - root(-1, 3), root(-1, 3)]
Beispiel #40
0
def test_roots_cubic():
    assert roots_cubic(Poly(2*x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3*x**2 + 3*x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2*x**2 + 3*x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic(Poly(x**3 + 2*a/27, x))
    assert res == [-root(a + sqrt(a**2), 3)/3,
                   Mul(Rational(-1, 3), Rational(-1, 2) + sqrt(3)*I/2,
                       root(a + sqrt(a**2), 3), evaluate=False),
                   Mul(Rational(-1, 3), Rational(-1, 2) - sqrt(3)*I/2,
                       root(a + sqrt(a**2), 3), evaluate=False)]
Beispiel #41
0
def test_as_real_imag():
    n = pi**1000
    # the special code for working out the real
    # and complex parts of a power with Integer exponent
    # should not run if there is no imaginary part, hence
    # this should not hang
    assert n.as_real_imag() == (n, 0)

    # issue sympy/sympy#6261
    assert sqrt(x).as_real_imag() == \
        (root(re(x)**2 + im(x)**2, 4)*cos(arg(re(x) + I*im(x))/2),
         root(re(x)**2 + im(x)**2, 4)*sin(arg(re(x) + I*im(x))/2))

    # issue sympy/sympy#3853
    a, b = symbols('a,b', extended_real=True)
    assert (((1 + sqrt(a + b*I))/2).as_real_imag() ==
            (root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2),
             root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2))

    assert sqrt(a**2).as_real_imag() == (sqrt(a**2), 0)
    i = symbols('i', imaginary=True)
    assert sqrt(i**2).as_real_imag() == (0, abs(i))
Beispiel #42
0
def test_AlgebraicElement():
    K = QQ.algebraic_field(sqrt(2))
    a = K.unit
    sT(
        a,
        f'AlgebraicField({QQ!r}, Pow(Integer(2), Rational(1, 2)))([Integer(0), Integer(1)])'
    )
    K = QQ.algebraic_field(root(-2, 3))
    a = K.unit
    sT(
        a,
        f'AlgebraicField({QQ!r}, Pow(Integer(-2), Rational(1, 3)))([Integer(0), Integer(1)])'
    )
Beispiel #43
0
def test_as_real_imag():
    n = pi**1000
    # the special code for working out the real
    # and complex parts of a power with Integer exponent
    # should not run if there is no imaginary part, hence
    # this should not hang
    assert n.as_real_imag() == (n, 0)

    # issue sympy/sympy#6261
    assert sqrt(x).as_real_imag() == \
        (root(re(x)**2 + im(x)**2, 4)*cos(arg(re(x) + I*im(x))/2),
         root(re(x)**2 + im(x)**2, 4)*sin(arg(re(x) + I*im(x))/2))

    # issue sympy/sympy#3853
    a, b = symbols('a,b', extended_real=True)
    assert (((1 + sqrt(a + b*I))/2).as_real_imag() ==
            (root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2),
             root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2))

    assert sqrt(a**2).as_real_imag() == (sqrt(a**2), 0)
    i = symbols('i', imaginary=True)
    assert sqrt(i**2).as_real_imag() == (0, abs(i))
Beispiel #44
0
def test_airybiprime():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airybiprime(z), airybiprime)

    assert airybiprime(0) == root(3, 6)/gamma(Rational(1, 3))
    assert airybiprime(oo) == oo
    assert airybiprime(-oo) == 0

    assert diff(airybiprime(z), z) == z*airybi(z)

    assert series(airybiprime(z), z, 0, 3) == (
        root(3, 6)/gamma(Rational(1, 3)) + 3**Rational(5, 6)*z**2/(6*gamma(Rational(2, 3))) + O(z**3))

    assert airybiprime(z).rewrite(hyper) == (
        3**Rational(5, 6)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) +
        root(3, 6)*hyper((), (Rational(1, 3),), z**3/9)/gamma(Rational(1, 3)))

    assert isinstance(airybiprime(z).rewrite(besselj), airybiprime)
    assert (airybiprime(t).rewrite(besselj) ==
            -sqrt(3)*t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) +
                        besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3)
    assert airybiprime(z).rewrite(besseli) == (
        sqrt(3)*(z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(z**Rational(3, 2))**Rational(2, 3) +
                 (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(2, 3), 2*z**Rational(3, 2)/3))/3)
    assert airybiprime(p).rewrite(besseli) == (
        sqrt(3)*p*(besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3)
    assert airybiprime(p).rewrite(besselj) == airybiprime(p)

    assert expand_func(airybiprime(2*cbrt(3*z**5))) == (
        sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2 +
        (z**Rational(5, 3)/cbrt(z**5) + 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airybiprime(x*y)) == airybiprime(x*y)
    assert expand_func(airybiprime(log(x))) == airybiprime(log(x))
    assert expand_func(airybiprime(2*root(3*z**5, 5))) == airybiprime(2*root(3*z**5, 5))

    assert airybiprime(-2).evalf(50) == Float('0.27879516692116952268509756941098324140300059345163131', dps=50)
Beispiel #45
0
def test_pow_eval():
    # XXX Pow does not fully support conversion of negative numbers
    #     to their complex equivalent

    assert sqrt(-1) == I

    assert sqrt(-4) == 2*I
    assert sqrt(+4) == 2
    assert cbrt(+8) == 2
    assert cbrt(-8) == 2*cbrt(-1)

    assert sqrt(-2) == I*sqrt(2)
    assert cbrt(-1) != I
    assert cbrt(-10) != I*cbrt(10)
    assert root(-2, 4) != root(2, 4)

    assert cbrt(64) == 4
    assert 64**Rational(2, 3) == 16
    assert 24/sqrt(64) == 3
    assert cbrt(-27) == 3*cbrt(-1)

    assert (cos(2) / tan(2))**2 == (cos(2) / tan(2))**2
Beispiel #46
0
def test_airyaiprime():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyaiprime(z), airyaiprime)

    assert airyaiprime(0) == -3**Rational(2, 3)/(3*gamma(Rational(1, 3)))
    assert airyaiprime(oo) == 0

    assert diff(airyaiprime(z), z) == z*airyai(z)

    assert series(airyaiprime(z), z, 0, 3) == (
        -3**Rational(2, 3)/(3*gamma(Rational(1, 3))) + cbrt(3)*z**2/(6*gamma(Rational(2, 3))) + O(z**3))

    assert airyaiprime(z).rewrite(hyper) == (
        cbrt(3)*z**2*hyper((), (Rational(5, 3),), z**3/9)/(6*gamma(Rational(2, 3))) -
        3**Rational(2, 3)*hyper((), (Rational(1, 3),), z**3/9)/(3*gamma(Rational(1, 3))))

    assert isinstance(airyaiprime(z).rewrite(besselj), airyaiprime)
    assert (airyaiprime(t).rewrite(besselj) ==
            t*(besselj(-Rational(2, 3), 2*(-t)**Rational(3, 2)/3) -
               besselj(Rational(2, 3), 2*(-t)**Rational(3, 2)/3))/3)
    assert airyaiprime(z).rewrite(besseli) == (
        z**2*besseli(Rational(2, 3), 2*z**Rational(3, 2)/3)/(3*(z**Rational(3, 2))**Rational(2, 3)) -
        (z**Rational(3, 2))**Rational(2, 3)*besseli(-Rational(1, 3), 2*z**Rational(3, 2)/3)/3)
    assert airyaiprime(p).rewrite(besseli) == (
        p*(-besseli(-Rational(2, 3), 2*p**Rational(3, 2)/3) + besseli(Rational(2, 3), 2*p**Rational(3, 2)/3))/3)
    assert airyaiprime(p).rewrite(besselj) == airyaiprime(p)

    assert expand_func(airyaiprime(2*cbrt(3*z**5))) == (
        sqrt(3)*(z**Rational(5, 3)/cbrt(z**5) - 1)*airybiprime(2*cbrt(3)*z**Rational(5, 3))/6 +
        (z**Rational(5, 3)/cbrt(z**5) + 1)*airyaiprime(2*cbrt(3)*z**Rational(5, 3))/2)
    assert expand_func(airyaiprime(x*y)) == airyaiprime(x*y)
    assert expand_func(airyaiprime(log(x))) == airyaiprime(log(x))
    assert expand_func(airyaiprime(2*root(3*z**5, 5))) == airyaiprime(2*root(3*z**5, 5))

    assert airyaiprime(-2).evalf(50) == Float('0.61825902074169104140626429133247528291577794512414753', dps=50)
Beispiel #47
0
def test_roots_cubic():
    assert roots_cubic(Poly(2 * x**3, x)) == [0, 0, 0]
    assert roots_cubic(Poly(x**3 - 3 * x**2 + 3 * x - 1, x)) == [1, 1, 1]

    assert roots_cubic(Poly(x**3 + 1, x)) == \
        [-1, Rational(1, 2) - I*sqrt(3)/2, Rational(1, 2) + I*sqrt(3)/2]
    assert roots_cubic(Poly(2*x**3 - 3*x**2 - 3*x - 1, x))[0] == \
        Rational(1, 2) + cbrt(3)/2 + 3**Rational(2, 3)/2
    eq = -x**3 + 2 * x**2 + 3 * x - 2
    assert roots(eq, trig=True, multiple=True) == \
        roots_cubic(Poly(eq, x), trig=True) == [
        Rational(2, 3) + 2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3,
        -2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + Rational(2, 3),
        -2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + Rational(2, 3),
    ]
    res = roots_cubic(Poly(x**3 + 2 * a / 27, x))
    assert res == [
        -root(a + sqrt(a**2), 3) / 3,
        Mul(Rational(-1, 3),
            Rational(-1, 2) + sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False),
        Mul(Rational(-1, 3),
            Rational(-1, 2) - sqrt(3) * I / 2,
            root(a + sqrt(a**2), 3),
            evaluate=False)
    ]

    # issue sympy/sympy#8438
    p = Poly([1, y, -2, -3], x).as_expr()
    croots = roots_cubic(Poly(p, x), x)
    z = -Rational(3,
                  2) - 7 * I / 2  # this will fail in code given in commit msg
    post = [r.subs({y: z}) for r in croots]
    assert set(post) == set(roots_cubic(Poly(p.subs({y: z}), x)))
    # /!\ if p is not made an expression, this is *very* slow
    assert all(p.subs({y: z, x: i}).evalf(2, chop=True) == 0 for i in post)
Beispiel #48
0
def test_as_content_primitive():
    # although the _as_content_primitive methods do not alter the underlying structure,
    # the as_content_primitive function will touch up the expression and join
    # bases that would otherwise have not been joined.
    assert ((x*(2 + 2*x)*(3*x + 3)**2)).as_content_primitive() == \
        (18, x*(x + 1)**3)
    assert (2 + 2*x + 2*y*(3 + 3*y)).as_content_primitive() == \
        (2, x + 3*y*(y + 1) + 1)
    assert ((2 + 6 * x)**2).as_content_primitive() == (4, (3 * x + 1)**2)
    assert ((2 + 6*x)**(2*y)).as_content_primitive() == \
        (1, (_keep_coeff(Integer(2), (3*x + 1)))**(2*y))
    assert (5 + 10*x + 2*y*(3 + 3*y)).as_content_primitive() == \
        (1, 10*x + 6*y*(y + 1) + 5)
    assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))).as_content_primitive() == \
        (11, x*(y + 1))
    assert ((5*(x*(1 + y)) + 2*x*(3 + 3*y))**2).as_content_primitive() == \
        (121, x**2*(y + 1)**2)
    assert (y**2).as_content_primitive() == (1, y**2)
    assert oo.as_content_primitive() == (1, oo)
    eq = x**(2 + y)
    assert (eq).as_content_primitive() == (1, eq)
    assert (Rational(1, 2)**(2 + x)).as_content_primitive() == (Rational(1, 4),
                                                                2**-x)
    assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \
           (Rational(1, 4), Rational(-1, 2)**x)
    assert (Rational(-1, 2)**(2 + x)).as_content_primitive() == \
           (Rational(1, 4), Rational(-1, 2)**x)
    assert (4**((1 + y) / 2)).as_content_primitive() == (2, 4**(y / 2))
    assert (3**((1 + y)/2)).as_content_primitive() == \
           (1, 3**(Mul(Rational(1, 2), 1 + y, evaluate=False)))
    assert (5**Rational(3, 4)).as_content_primitive() == (1, 5**Rational(3, 4))
    assert (5**Rational(7, 4)).as_content_primitive() == (5, 5**Rational(3, 4))
    assert Add(5*z/7, 0.5*x, 3*y/2, evaluate=False).as_content_primitive() == \
              (Rational(1, 14), 7.0*x + 21*y + 10*z)
    assert (2**Rational(3, 4) + root(2, 4)*sqrt(3)).as_content_primitive(radical=True) == \
           (1, root(2, 4)*(sqrt(2) + sqrt(3)))
Beispiel #49
0
def test_besselsimp():
    assert besselsimp(exp(-I*pi*y/2)*besseli(y, z*exp_polar(I*pi/2))) == \
        besselj(y, z)
    assert besselsimp(exp(-I*pi*a/2)*besseli(a, 2*sqrt(x)*exp_polar(I*pi/2))) == \
        besselj(a, 2*sqrt(x))
    assert besselsimp(sqrt(2)*sqrt(pi)*root(x, 4)*exp(I*pi/4)*exp(-I*pi*a/2) *
                      besseli(-Rational(1, 2), sqrt(x)*exp_polar(I*pi/2)) *
                      besseli(a, sqrt(x)*exp_polar(I*pi/2))/2) == \
        besselj(a, sqrt(x)) * cos(sqrt(x))
    assert besselsimp(besseli(Rational(-1, 2), z)) == \
        sqrt(2)*cosh(z)/(sqrt(pi)*sqrt(z))
    assert besselsimp(besseli(a, z*exp_polar(-I*pi/2))) == \
        exp(-I*pi*a/2)*besselj(a, z)
    assert cosine_transform(1/t*sin(a/t), t, y) == \
        sqrt(2)*sqrt(pi)*besselj(0, 2*sqrt(a)*sqrt(y))/2
Beispiel #50
0
def test_sympyissue_5204():
    x0, x1, x2, x3, x4, x5, x6, x7, x8, x9 = symbols('x:10')
    v = ((-18873261792 * x0 + 3110400000 * I * x1 * x5 + 1239810624 * x1 * x8 -
          97043832 * x1 * x9 + 304403832 * x2 * x6 *
          (4 * x0 + 1422)**Rational(2, 3) - 56619785376 * x2 -
          41281887168 * x5 - 1274950152 * x6 * x7 - 13478400000 * I * x8 +
          5276370456 * I * x9 - 357587765856 - 108755765856 * sqrt(3) * I) /
         ((25596 * x0 + 76788 * x2 + 1106028)**2 + 175732658352))
    v = v.subs(
        ((x9, 2**Rational(2, 3) * root(3, 6) * x7),
         (x8, cbrt(2) * 3**Rational(5, 6) * x4), (x7, x3**Rational(2, 3)),
         (x6, 6**Rational(2, 3)), (x5, cbrt(6) * x4), (x4, cbrt(x3)),
         (x3, 54 * x0 + 1422), (x2, I * x1), (x1, sqrt(83)), (x0, sqrt(249))))

    assert NS(v, 5) == '0.077284 + 1.1104*I'
    assert NS(v, 1) == '0.08 + 1.*I'
Beispiel #51
0
def test_Limits_simple_3b():
    assert limit(((x + h)**3 - x**3) / h, h, 0) == 3 * x**2  # 197
    assert limit(1 / (1 - x) - 3 / (1 - x**3), x, 1) == -1  # 198
    assert limit((sqrt(1 + x) - 1) / (root(1 + x, 3) - 1), x,
                 0) == Rational(3, 2)  # Example 4
    assert limit((sqrt(x) - 1) / (x - 1), x, 1) == Rational(1, 2)  # 199
    assert limit((sqrt(x) - 8) / (root(x, 3) - 4), x, 64) == 3  # 200
    assert limit((root(x, 3) - 1) / (root(x, 4) - 1), x,
                 1) == Rational(4, 3)  # 201
    assert limit((root(x**2, 3) - 2 * root(x, 3) + 1) / (x - 1)**2, x,
                 1) == Rational(1, 9)  # 202
Beispiel #52
0
def test_components():
    assert components(x*y, x) == {x}
    assert components(1/(x + y), x) == {x}
    assert components(sin(x), x) == {sin(x), x}
    assert components(sin(x)*sqrt(log(x)), x) == \
        {log(x), sin(x), sqrt(log(x)), x}
    assert components(x*sin(exp(x)*y), x) == \
        {sin(y*exp(x)), x, exp(x)}
    assert components(x**Rational(17, 54)/sqrt(sin(x)), x) == \
        {sin(x), root(x, 54), sqrt(sin(x)), x}

    assert components(f(x), x) == \
        {x, f(x)}
    assert components(Derivative(f(x), x), x) == \
        {x, f(x), Derivative(f(x), x)}
    assert components(f(x)*diff(f(x), x), x) == \
        {x, f(x), Derivative(f(x), x), Derivative(f(x), x)}
Beispiel #53
0
def test_unrad2():
    assert solve(root(x**3 - 3*x**2, 3) + 1 - x) == []
    assert solve(root(x + 1, 3) + root(x**2 - 2, 5) + 1) == []
Beispiel #54
0
def test_unrad1_fail():
    assert solve(sqrt(x + root(x, 3)) + root(x - y, 5), y) != []
Beispiel #55
0
def test_complex():
    a = Symbol("a", extended_real=True)
    b = Symbol("b", extended_real=True)
    e = (a + I*b)*(a - I*b)
    assert e.expand() == a**2 + b**2
    assert sqrt(I) == root(-1, 4)
Beispiel #56
0
def test_unrad1():
    pytest.raises(NotImplementedError, lambda:
                  unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x)) + 3))
    pytest.raises(NotImplementedError, lambda:
                  unrad(sqrt(x) + cbrt(x + 1) + 2*sqrt(y)))

    s = symbols('s', cls=Dummy)

    # checkers to deal with possibility of answer coming
    # back with a sign change (cf issue sympy/sympy#5203)
    def check(rv, ans):
        assert bool(rv[1]) == bool(ans[1])
        if ans[1]:
            return s_check(rv, ans)
        e = rv[0].expand()
        a = ans[0].expand()
        return e in [a, -a] and rv[1] == ans[1]

    def s_check(rv, ans):
        # get the dummy
        rv = list(rv)
        d = rv[0].atoms(Dummy)
        reps = list(zip(d, [s]*len(d)))
        # replace s with this dummy
        rv = (rv[0].subs(reps).expand(), [rv[1][0].subs(reps), rv[1][1].subs(reps)])
        ans = (ans[0].subs(reps).expand(), [ans[1][0].subs(reps), ans[1][1].subs(reps)])
        return str(rv[0]) in [str(ans[0]), str(-ans[0])] and \
            str(rv[1]) == str(ans[1])

    assert check(unrad(sqrt(x)),
                 (x, []))
    assert check(unrad(sqrt(x) + 1),
                 (x - 1, []))
    assert check(unrad(sqrt(x) + root(x, 3) + 2),
                 (s**3 + s**2 + 2, [s, s**6 - x]))
    assert check(unrad(sqrt(x)*root(x, 3) + 2),
                 (x**5 - 64, []))
    assert check(unrad(sqrt(x) + cbrt(x + 1)),
                 (x**3 - (x + 1)**2, []))
    assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(2*x)),
                 (-2*sqrt(2)*x - 2*x + 1, []))
    assert check(unrad(sqrt(x) + sqrt(x + 1) + 2),
                 (16*x - 9, []))
    assert check(unrad(sqrt(x) + sqrt(x + 1) + sqrt(1 - x)),
                 (5*x**2 - 4*x, []))
    assert check(unrad(a*sqrt(x) + b*sqrt(x) + c*sqrt(y) + d*sqrt(y)),
                 ((a*sqrt(x) + b*sqrt(x))**2 - (c*sqrt(y) + d*sqrt(y))**2, []))
    assert check(unrad(sqrt(x) + sqrt(1 - x)),
                 (2*x - 1, []))
    assert check(unrad(sqrt(x) + sqrt(1 - x) - 3),
                 (x**2 - x + 16, []))
    assert check(unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x)),
                 (5*x**2 - 2*x + 1, []))
    assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - 3) in [
        (25*x**4 + 376*x**3 + 1256*x**2 - 2272*x + 784, []),
        (25*x**8 - 476*x**6 + 2534*x**4 - 1468*x**2 + 169, [])]
    assert unrad(sqrt(x) + sqrt(1 - x) + sqrt(2 + x) - sqrt(1 - 2*x)) == \
        (41*x**4 + 40*x**3 + 232*x**2 - 160*x + 16, [])  # orig root at 0.487
    assert check(unrad(sqrt(x) + sqrt(x + 1)), (Integer(1), []))

    eq = sqrt(x) + sqrt(x + 1) + sqrt(1 - sqrt(x))
    assert check(unrad(eq),
                 (16*x**2 - 9*x, []))
    assert {s[x] for s in solve(eq, check=False)} == {0, Rational(9, 16)}
    assert solve(eq) == []
    # but this one really does have those solutions
    assert ({s[x] for s in solve(sqrt(x) - sqrt(x + 1) +
                                 sqrt(1 - sqrt(x)))} ==
            {0, Rational(9, 16)})

    assert check(unrad(sqrt(x) + root(x + 1, 3) + 2*sqrt(y), y),
                 (2*sqrt(x)*cbrt(x + 1) + x - 4*y +
                     (x + 1)**Rational(2, 3), []))
    assert check(unrad(sqrt(x/(1 - x)) + cbrt(x + 1)),
                 (x**5 - x**4 - x**3 + 2*x**2 + x - 1, []))
    assert check(unrad(sqrt(x/(1 - x)) + 2*sqrt(y), y),
                 (4*x*y + x - 4*y, []))
    assert check(unrad(sqrt(x)*sqrt(1 - x) + 2, x),
                 (x**2 - x + 4, []))

    # http://tutorial.math.lamar.edu/
    #        Classes/Alg/SolveRadicalEqns.aspx#Solve_Rad_Ex2_a
    assert solve(Eq(x, sqrt(x + 6))) == [{x: 3}]
    assert solve(Eq(x + sqrt(x - 4), 4)) == [{x: 4}]
    assert solve(Eq(1, x + sqrt(2*x - 3))) == []
    assert {s[x] for s in solve(Eq(sqrt(5*x + 6) - 2, x))} == {-1, 2}
    assert {s[x] for s in solve(Eq(sqrt(2*x - 1) - sqrt(x - 4), 2))} == {5, 13}
    assert solve(Eq(sqrt(x + 7) + 2, sqrt(3 - x))) == [{x: -6}]
    # http://www.purplemath.com/modules/solverad.htm
    assert solve(cbrt(2*x - 5) - 3) == [{x: 16}]
    assert {s[x] for s in solve(x + 1 - root(x**4 + 4*x**3 - x, 4))} == {-Rational(1, 2),
                                                                         -Rational(1, 3)}
    assert {s[x] for s in solve(sqrt(2*x**2 - 7) - (3 - x))} == {-8, 2}
    assert solve(sqrt(2*x + 9) - sqrt(x + 1) - sqrt(x + 4)) == [{x: 0}]
    assert solve(sqrt(x + 4) + sqrt(2*x - 1) - 3*sqrt(x - 1)) == [{x: 5}]
    assert solve(sqrt(x)*sqrt(x - 7) - 12) == [{x: 16}]
    assert solve(sqrt(x - 3) + sqrt(x) - 3) == [{x: 4}]
    assert solve(sqrt(9*x**2 + 4) - (3*x + 2)) == [{x: 0}]
    assert solve(sqrt(x) - 2 - 5) == [{x: 49}]
    assert solve(sqrt(x - 3) - sqrt(x) - 3) == []
    assert solve(sqrt(x - 1) - x + 7) == [{x: 10}]
    assert solve(sqrt(x - 2) - 5) == [{x: 27}]
    assert solve(sqrt(17*x - sqrt(x**2 - 5)) - 7) == [{x: 3}]
    assert solve(sqrt(x) - sqrt(x - 1) + sqrt(sqrt(x))) == []

    # don't posify the expression in unrad and do use _mexpand
    z = sqrt(2*x + 1)/sqrt(x) - sqrt(2 + 1/x)
    p = posify(z)[0]
    assert solve(p) == []
    assert solve(z) == []
    assert solve(z + 6*I) == [{x: -Rational(1, 11)}]
    assert solve(p + 6*I) == []
    # issue sympy/sympy#8622
    assert unrad((root(x + 1, 5) - root(x, 3))) == (
        x**5 - x**3 - 3*x**2 - 3*x - 1, [])
    # issue sympy/sympy#8679
    assert check(unrad(x + root(x, 3) + root(x, 3)**2 + sqrt(y), x),
                 (s**3 + s**2 + s + sqrt(y), [s, s**3 - x]))

    # for coverage
    assert check(unrad(sqrt(x) + root(x, 3) + y),
                 (s**3 + s**2 + y, [s, s**6 - x]))
    assert solve(sqrt(x) + root(x, 3) - 2) == [{x: 1}]
    pytest.raises(NotImplementedError, lambda:
                  solve(sqrt(x) + root(x, 3) + root(x + 1, 5) - 2))
    # fails through a different code path
    pytest.raises(NotImplementedError, lambda: solve(-sqrt(2) + cosh(x)/x))
    # unrad some
    e = root(x + 1, 3) + root(x, 3)
    assert unrad(e) == (2*x + 1, [])
    eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5)
    assert check(unrad(eq),
                 (15625*x**4 + 173000*x**3 + 355600*x**2 - 817920*x + 331776, []))
    assert check(unrad(root(x, 4) + root(x, 4)**3 - 1),
                 (s**3 + s - 1, [s, s**4 - x]))
    assert check(unrad(root(x, 2) + root(x, 2)**3 - 1),
                 (x**3 + 2*x**2 + x - 1, []))
    assert unrad(x**0.5) is None
    assert check(unrad(t + root(x + y, 5) + root(x + y, 5)**3),
                 (s**3 + s + t, [s, s**5 - x - y]))
    assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, y),
                 (s**3 + s + x, [s, s**5 - x - y]))
    assert check(unrad(x + root(x + y, 5) + root(x + y, 5)**3, x),
                 (s**5 + s**3 + s - y, [s, s**5 - x - y]))
    assert check(unrad(root(x - 1, 3) + root(x + 1, 5) + root(2, 5)),
                 (s**5 + 5*root(2, 5)*s**4 + s**3 + 10*2**Rational(2, 5)*s**3 +
                  10*2**Rational(3, 5)*s**2 + 5*2**Rational(4, 5)*s + 4, [s, s**3 - x + 1]))
    pytest.raises(NotImplementedError,
                  lambda: unrad((root(x, 2) + root(x, 3) +
                                 root(x, 4)).subs({x: x**5 - x + 1})))

    # the simplify flag should be reset to False for unrad results;
    # if it's not then this next test will take a long time
    assert solve(root(x, 3) + root(x, 5) - 2) == [{x: 1}]
    eq = (sqrt(x) + sqrt(x + 1) + sqrt(1 - x) - 6*sqrt(5)/5)
    assert check(unrad(eq),
                 ((5*x - 4)*(3125*x**3 + 37100*x**2 + 100800*x - 82944), []))
    ans = [{x: Rational(4, 5)},
           {x: Rational(-1484, 375) + 172564/(140625*cbrt(114*sqrt(12657)/78125 +
                                                          Rational(12459439, 52734375))) +
               4*cbrt(114*sqrt(12657)/78125 +
                      Rational(12459439, 52734375))}]
    assert solve(eq) == ans
    # duplicate radical handling
    assert check(unrad(sqrt(x + root(x + 1, 3)) - root(x + 1, 3) - 2),
                 (s**3 - s**2 - 3*s - 5, [s, s**3 - x - 1]))
    # cov post-processing
    e = root(x**2 + 1, 3) - root(x**2 - 1, 5) - 2
    assert check(unrad(e),
                 (s**5 - 10*s**4 + 39*s**3 - 80*s**2 + 80*s - 30,
                  [s, s**3 - x**2 - 1]))

    e = sqrt(x + root(x + 1, 2)) - root(x + 1, 3) - 2
    assert check(unrad(e),
                 (s**6 - 2*s**5 - 7*s**4 - 3*s**3 + 26*s**2 + 40*s + 25,
                  [s, s**3 - x - 1]))
    assert check(unrad(e, _reverse=True),
                 (s**6 - 14*s**5 + 73*s**4 - 187*s**3 + 276*s**2 - 228*s + 89,
                  [s, s**2 - x - sqrt(x + 1)]))
    # this one needs r0, r1 reversal to work
    assert check(unrad(sqrt(x + sqrt(root(x, 3) - 1)) - root(x, 6) - 2),
                 (s**12 - 2*s**8 - 8*s**7 - 8*s**6 + s**4 + 8*s**3 + 23*s**2 +
                  32*s + 17, [s, s**6 - x]))

    # is this needed?
    # assert unrad(root(cosh(x), 3)/x*root(x + 1, 5) - 1) == (
    #    x**15 - x**3*cosh(x)**5 - 3*x**2*cosh(x)**5 - 3*x*cosh(x)**5 - cosh(x)**5, [])
    pytest.raises(NotImplementedError, lambda:
                  unrad(sqrt(cosh(x)/x) + root(x + 1, 3)*sqrt(x) - 1))
    assert unrad((x+y)**(2*y/3) + cbrt(x+y) + 1) is None
    assert check(unrad((x+y)**(2*y/3) + cbrt(x+y) + 1, x),
                 (s**(2*y) + s + 1, [s, s**3 - x - y]))

    # This tests two things: that if full unrad is attempted and fails
    # the solution should still be found; also it tests that the use of
    # composite
    assert len(solve(sqrt(y)*x + x**3 - 1, x)) == 3
    assert len(solve(-512*y**3 + 1344*cbrt(x + 2)*y**2 -
                     1176*(x + 2)**Rational(2, 3)*y -
                     169*x + 686, y, _unrad=False)) == 3

    # watch out for when the cov doesn't involve the symbol of interest
    eq = -x + (7*y/8 - cbrt(27*x/2 + 27*sqrt(x**2)/2)/3)**3 - 1
    assert solve(eq, y) == [
        {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) +
                   2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) -
                   768*sqrt(x**2) - 1536, y, 0, evaluate=False)},
        {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) +
                   2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) -
                   768*sqrt(x**2) - 1536, y, 1, evaluate=False)},
        {y: RootOf(-2304*x + 1029*y**3 - 1764*cbrt(4)*y**2*cbrt(x + sqrt(x**2)) +
                   2016*cbrt(2)*y*(x + sqrt(x**2))**Rational(2, 3) -
                   768*sqrt(x**2) - 1536, y, 2, evaluate=False)}]

    eq = root(x + 1, 3) - (root(x, 3) + root(x, 5))
    assert check(unrad(eq),
                 (3*s**13 + 3*s**11 + s**9 - 1, [s, s**15 - x]))
    assert check(unrad(eq - 2),
                 (3*s**13 + 3*s**11 + 6*s**10 + s**9 + 12*s**8 + 6*s**6 + 12*s**5 +
                  12*s**3 + 7, [s, s**15 - x]))
    assert check(unrad(root(x, 3) - root(x + 1, 4)/2 + root(x + 2, 3)),
                 (4096*s**13 + 960*s**12 + 48*s**11 - s**10 - 1728*s**4,
                  [s, s**4 - x - 1]))  # orig expr has two real roots: -1, -.389
    assert check(unrad(root(x, 3) + root(x + 1, 4) - root(x + 2, 3)/2),
                 (343*s**13 + 2904*s**12 + 1344*s**11 + 512*s**10 - 1323*s**9 -
                  3024*s**8 - 1728*s**7 + 1701*s**5 + 216*s**4 - 729*s, [s, s**4 - x -
                                                                         1]))  # orig expr has one real root: -0.048
    assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3)),
                 (729*s**13 - 216*s**12 + 1728*s**11 - 512*s**10 + 1701*s**9 -
                  3024*s**8 + 1344*s**7 + 1323*s**5 - 2904*s**4 + 343*s, [s, s**4 - x -
                                                                          1]))  # orig expr has 2 real roots: -0.91, -0.15

    # orig expr has 1 real root: 19.53
    assert check(unrad(root(x, 3)/2 - root(x + 1, 4) + root(x + 2, 3) - 2),
                 (729*s**13 + 1242*s**12 + 18496*s**10 + 129701*s**9 + 388602*s**8 +
                  453312*s**7 - 612864*s**6 - 3337173*s**5 - 6332418*s**4 - 7134912*s**3
                  - 5064768*s**2 - 2111913*s - 398034, [s, s**4 - x - 1]))

    ans = solve(sqrt(x) + sqrt(x + 1) -
                sqrt(1 - x) - sqrt(2 + x))
    assert len(ans) == 1 and NS(ans[0][x])[:4] == '0.73'
    # the fence optimization problem
    # https://github.com/sympy/sympy/issues/4793#issuecomment-36994519
    eq = F - (2*x + 2*y + sqrt(x**2 + y**2))
    ans = 2*F/7 - sqrt(2)*F/14
    X = solve(eq, x, check=False)
    for xi in reversed(X):  # reverse since currently, ans is the 2nd one
        Y = solve((x*y).subs(xi).diff(y), y,
                  simplify=False, check=False)
        if any((a[y] - ans).expand().is_zero for a in Y):
            break
    else:
        assert None  # no answer was found
    assert (solve(sqrt(x + 1) + root(x, 3) - 2) ==
            [{x: (-11/(9*cbrt(Rational(47, 54) + sqrt(93)/6)) +
                  Rational(1, 3) + cbrt(Rational(47, 54) +
                                        sqrt(93)/6))**3}])
    assert (solve(sqrt(sqrt(x + 1)) + cbrt(x) - 2) ==
            [{x: (-sqrt(-2*cbrt(Rational(-1, 16) + sqrt(6913)/16) +
                        6/cbrt(Rational(-1, 16) + sqrt(6913)/16) +
                        Rational(17, 2) +
                        121/(4*sqrt(-6/cbrt(Rational(-1, 16) +
                                            sqrt(6913)/16) +
                                    2*cbrt(Rational(-1, 16) +
                                           sqrt(6913)/16) +
                                    Rational(17, 4))))/2 +
                  sqrt(-6/cbrt(Rational(-1, 16) + sqrt(6913)/16) +
                       2*cbrt(Rational(-1, 16) + sqrt(6913)/16) +
                       Rational(17, 4))/2 + Rational(9, 4))**3}])
    assert (solve(sqrt(x) + root(sqrt(x) + 1, 3) - 2) ==
            [{x: (-cbrt(Rational(81, 2) + 3*sqrt(741)/2)/3 +
                  (Rational(81, 2) + 3*sqrt(741)/2)**Rational(-1, 3) + 2)**2}])
    eq = (-x + (Rational(1, 2) - sqrt(3)*I/2)*cbrt(3*x**3/2 - x*(3*x**2 - 34)/2 +
                                                   sqrt((-3*x**3 + x*(3*x**2 - 34) + 90)**2/4 - Rational(39304, 27)) -
                                                   45) + 34/(3*(Rational(1, 2) - sqrt(3)*I/2)*cbrt(3*x**3/2 -
                                                                                                   x*(3*x**2 - 34)/2 + sqrt((-3*x**3 + x*(3*x**2 - 34) + 90)**2/4 -
                                                                                                                            Rational(39304, 27)) - 45)))
    assert check(unrad(eq),
                 (s**6 - sqrt(3)*s**6*I + 102*cbrt(12)*s**4 +
                  102*2**Rational(2, 3)*3**Rational(5, 6)*s**4*I + 1620*s**3 - 1620*sqrt(3)*s**3*I -
                  13872*cbrt(18)*s**2 + 471648 - 471648*sqrt(3)*I, [s, s**3 - 306*x
                                                                    - sqrt(3)*sqrt(31212*x**2 - 165240*x + 61484) + 810]))
    assert solve(eq, x, check=False) != []  # not other code errors
Beispiel #57
0
def test_RootOf___new__():
    assert RootOf(x, 0) == 0
    assert RootOf(x, -1) == 0

    assert RootOf(x - 1, 0) == 1
    assert RootOf(x - 1, -1) == 1

    assert RootOf(x + 1, 0) == -1
    assert RootOf(x + 1, -1) == -1

    assert RootOf(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2)
    assert RootOf(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2)

    r = RootOf(x**2 + 2*x + 3, 0, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, 1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, -1, radicals=False)
    assert isinstance(r, RootOf) is True

    r = RootOf(x**2 + 2*x + 3, -2, radicals=False)
    assert isinstance(r, RootOf) is True

    assert RootOf((x - 1)*(x + 1), 0, radicals=False) == -1
    assert RootOf((x - 1)*(x + 1), 1, radicals=False) == 1
    assert RootOf((x - 1)*(x + 1), -1, radicals=False) == 1
    assert RootOf((x - 1)*(x + 1), -2, radicals=False) == -1

    assert RootOf((x - 1)*(x + 1), 0, radicals=True) == -1
    assert RootOf((x - 1)*(x + 1), 1, radicals=True) == 1
    assert RootOf((x - 1)*(x + 1), -1, radicals=True) == 1
    assert RootOf((x - 1)*(x + 1), -2, radicals=True) == -1

    assert RootOf((x - 1)*(x**3 + x + 3), 0) == RootOf(x**3 + x + 3, 0)
    assert RootOf((x - 1)*(x**3 + x + 3), 1) == 1
    assert RootOf((x - 1)*(x**3 + x + 3), 2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1)*(x**3 + x + 3), 3) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1)*(x**3 + x + 3), -1) == RootOf(x**3 + x + 3, 2)
    assert RootOf((x - 1)*(x**3 + x + 3), -2) == RootOf(x**3 + x + 3, 1)
    assert RootOf((x - 1)*(x**3 + x + 3), -3) == 1
    assert RootOf((x - 1)*(x**3 + x + 3), -4) == RootOf(x**3 + x + 3, 0)

    assert RootOf(x**4 + 3*x**3, 0) == -3
    assert RootOf(x**4 + 3*x**3, 1) == 0
    assert RootOf(x**4 + 3*x**3, 2) == 0
    assert RootOf(x**4 + 3*x**3, 3) == 0

    pytest.raises(GeneratorsNeeded, lambda: RootOf(0, 0))
    pytest.raises(GeneratorsNeeded, lambda: RootOf(1, 0))

    pytest.raises(PolynomialError, lambda: RootOf(Poly(0, x), 0))
    pytest.raises(PolynomialError, lambda: RootOf(Poly(1, x), 0))

    pytest.raises(PolynomialError, lambda: RootOf(x - y, 0))

    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -4))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, -3))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 2))
    pytest.raises(IndexError, lambda: RootOf(x**2 - 1, 3))
    pytest.raises(ValueError, lambda: RootOf(x**2 - 1, x))
    pytest.raises(NotImplementedError,
                  lambda: RootOf(Symbol('a', nonzero=False)*x**5 +
                                 2*x - 1, x, 0))
    pytest.raises(NotImplementedError,
                  lambda: Poly(Symbol('a', nonzero=False)*x**5 +
                               2*x - 1, x).all_roots())

    assert RootOf(Poly(x - y, x), 0) == y

    assert RootOf(Poly(x**2 - y, x), 0) == -sqrt(y)
    assert RootOf(Poly(x**2 - y, x), 1) == sqrt(y)

    assert isinstance(RootOf(x**3 - y, x, 0), RootOf)
    p = Symbol('p', positive=True)
    assert RootOf(x**3 - p, x, 0) == root(p, 3)*RootOf(x**3 - 1, 0)

    assert RootOf(y*x**3 + y*x + 2*y, x, 0) == -1

    assert RootOf(x**3 + x + 1, 0).is_commutative is True

    e = RootOf(x**2 - 4, x, 1, evaluate=False)
    assert isinstance(e, RootOf)
    assert e.doit() == 2
    assert e.args == (x**2 - 4, x, 1)
    assert e.poly == PurePoly(x**2 - 4, x)
    assert e.index == 1

    assert RootOf(x**7 - 0.1*x + 1, 0) == RootOf(10*x**7 - x + 10, 0)
Beispiel #58
0
def test_re():
    a, b = symbols('a,b', extended_real=True)

    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)

    assert re(nan) == nan

    assert re(oo) == oo
    assert re(-oo) == -oo

    assert re(0) == 0

    assert re(1) == 1
    assert re(-1) == -1

    assert re(E) == E
    assert re(-E) == -E

    assert re(x) == re(x)
    assert re(x*I) == -im(x)
    assert re(r*I) == 0
    assert re(r) == r
    assert re(i*I) == I * i
    assert re(i) == 0

    assert re(x + y) == re(x + y)
    assert re(x + r) == re(x) + r

    assert re(re(x)) == re(x)

    assert re(2 + I) == 2
    assert re(x + I) == re(x)

    assert re(x + y*I) == re(x) - im(y)
    assert re(x + r*I) == re(x)

    assert re(log(2*I)) == log(2)

    assert re((2 + I)**2).expand(complex=True) == 3

    assert re(conjugate(x)) == re(x)
    assert conjugate(re(x)) == re(x)

    assert re(x).as_real_imag() == (re(x), 0)

    assert re(i*r*x).diff(r) == re(i*x)
    assert re(i*r*x).diff(i) == I*r*im(x)

    assert re(sqrt(a + b*I)) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)
    assert re(a * (2 + b*I)) == 2*a

    assert re((1 + sqrt(a + b*I))/2) == root(a**2 + b**2, 4)*cos(arg(a + I*b)/2)/2 + Rational(1, 2)

    assert re(x).rewrite(im) == x - I*im(x)  # issue sympy/sympy#10897
    assert (x + re(y)).rewrite(re, im) == x + y - I*im(y)

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert re(zoo) == nan
Beispiel #59
0
def test_im():
    a, b = symbols('a,b', extended_real=True)

    r = Symbol('r', extended_real=True)
    i = Symbol('i', imaginary=True)

    assert im(nan) == nan

    assert im(oo*I) == oo
    assert im(-oo*I) == -oo

    assert im(0) == 0

    assert im(1) == 0
    assert im(-1) == 0

    assert im(E*I) == E
    assert im(-E*I) == -E

    assert im(x) == im(x)
    assert im(x*I) == re(x)
    assert im(r*I) == r
    assert im(r) == 0
    assert im(i*I) == 0
    assert im(i) == -I * i

    assert im(x + y) == im(x + y)
    assert im(x + r) == im(x)
    assert im(x + r*I) == im(x) + r

    assert im(im(x)*I) == im(x)

    assert im(2 + I) == 1
    assert im(x + I) == im(x) + 1

    assert im(x + y*I) == im(x) + re(y)
    assert im(x + r*I) == im(x) + r

    assert im(log(2*I)) == pi/2

    assert im((2 + I)**2).expand(complex=True) == 4

    assert im(conjugate(x)) == -im(x)
    assert conjugate(im(x)) == im(x)

    assert im(x).as_real_imag() == (im(x), 0)

    assert im(i*r*x).diff(r) == im(i*x)
    assert im(i*r*x).diff(i) == -I * re(r*x)

    assert im(sqrt(a + b*I)) == root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)
    assert im(a * (2 + b*I)) == a*b

    assert im((1 + sqrt(a + b*I))/2) == root(a**2 + b**2, 4)*sin(arg(a + I*b)/2)/2

    assert im(x).rewrite(re) == -I*(x - re(x))  # sympy/sympy#10897
    assert (x + im(y)).rewrite(im, re) == x - I*(y - re(y))

    a = Symbol('a', algebraic=True)
    t = Symbol('t', transcendental=True)
    assert re(a).is_algebraic
    assert re(x).is_algebraic is None
    assert re(t).is_algebraic is False

    assert re(zoo) == nan