Пример #1
0
def test_euler_sineg():
    psi = Function('psi')
    t = Symbol('t')
    x = Symbol('x')
    L = diff(psi(t, x), t)**2/2 - diff(psi(t, x), x)**2/2 + cos(psi(t, x))
    assert euler(L, psi(t, x), [t, x]) == [Eq(-sin(psi(t, x)) -
                                              diff(psi(t, x), t, t) +
                                              diff(psi(t, x), x, x))]
Пример #2
0
def test_deriv2():
    assert (x**3).diff(x) == 3*x**2
    assert (x**3).diff(x, evaluate=False) != 3*x**2
    assert (x**3).diff(x, evaluate=False) == Derivative(x**3, x)

    assert diff(x**3, x) == 3*x**2
    assert diff(x**3, x, evaluate=False) != 3*x**2
    assert diff(x**3, x, evaluate=False) == Derivative(x**3, x)
Пример #3
0
def test_jacobi():
    assert jacobi(0, a, b, x) == 1
    assert jacobi(1, a, b, x) == a/2 - b/2 + x*(a/2 + b/2 + 1)
    assert (jacobi(2, a, b, x) == a**2/8 - a*b/4 - a/8 + b**2/8 - b/8 +
            x**2*(a**2/8 + a*b/4 + 7*a/8 + b**2/8 + 7*b/8 + Rational(3, 2)) +
            x*(a**2/4 + 3*a/4 - b**2/4 - 3*b/4) - Rational(1, 2))

    assert jacobi(n, a, a, x) == RisingFactorial(
        a + 1, n)*gegenbauer(n, a + Rational(1, 2), x)/RisingFactorial(2*a + 1, n)
    assert jacobi(n, a, -a, x) == ((-1)**a*(-x + 1)**(-a/2)*(x + 1)**(a/2)*assoc_legendre(n, a, x) *
                                   factorial(-a + n)*gamma(a + n + 1)/(factorial(a + n)*gamma(n + 1)))
    assert jacobi(n, -b, b, x) == ((-x + 1)**(b/2)*(x + 1)**(-b/2)*assoc_legendre(n, b, x) *
                                   gamma(-b + n + 1)/gamma(n + 1))
    assert jacobi(n, 0, 0, x) == legendre(n, x)
    assert jacobi(n, Rational(1, 2), Rational(1, 2), x) == RisingFactorial(
        Rational(3, 2), n)*chebyshevu(n, x)/factorial(n + 1)
    assert jacobi(n, Rational(-1, 2), Rational(-1, 2), x) == RisingFactorial(
        Rational(1, 2), n)*chebyshevt(n, x)/factorial(n)

    X = jacobi(n, a, b, x)
    assert isinstance(X, jacobi)

    assert jacobi(n, a, b, -x) == (-1)**n*jacobi(n, b, a, x)
    assert jacobi(n, a, b, 0) == 2**(-n)*gamma(a + n + 1)*hyper(
        (-b - n, -n), (a + 1,), -1)/(factorial(n)*gamma(a + 1))
    assert jacobi(n, a, b, 1) == RisingFactorial(a + 1, n)/factorial(n)

    m = Symbol("m", positive=True)
    assert jacobi(m, a, b, oo) == oo*RisingFactorial(a + b + m + 1, m)
    assert jacobi(n, a, b, oo) == jacobi(n, a, b, oo, evaluate=False)

    assert conjugate(jacobi(m, a, b, x)) == \
        jacobi(m, conjugate(a), conjugate(b), conjugate(x))

    assert diff(jacobi(n, a, b, x), n) == Derivative(jacobi(n, a, b, x), n)
    assert diff(jacobi(n, a, b, x), x) == \
        (a/2 + b/2 + n/2 + Rational(1, 2))*jacobi(n - 1, a + 1, b + 1, x)

    # XXX see issue sympy/sympy#5539
    assert str(jacobi(n, a, b, x).diff(a)) == \
        ("Sum((jacobi(n, a, b, x) + (a + b + 2*_k + 1)*RisingFactorial(b + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)))/(a + b + n + _k + 1), (_k, 0, n - 1))")
    assert str(jacobi(n, a, b, x).diff(b)) == \
        ("Sum(((-1)**(n - _k)*(a + b + 2*_k + 1)*RisingFactorial(a + "
         "_k + 1, n - _k)*jacobi(_k, a, b, x)/((n - _k)*RisingFactorial(a + "
         "b + _k + 1, n - _k)) + jacobi(n, a, b, x))/(a + b + n + "
         "_k + 1), (_k, 0, n - 1))")

    assert jacobi_normalized(n, a, b, x) == \
        (jacobi(n, a, b, x)/sqrt(2**(a + b + 1)*gamma(a + n + 1)*gamma(b + n + 1)
                                 / ((a + b + 2*n + 1)*factorial(n)*gamma(a + b + n + 1))))

    pytest.raises(ValueError, lambda: jacobi(-2.1, a, b, x))
    pytest.raises(ValueError, lambda: jacobi(Dummy(positive=True, integer=True), 1, 2, oo))
    pytest.raises(ArgumentIndexError, lambda: jacobi(n, a, b, x).fdiff(5))
Пример #4
0
def test_euler_henonheiles():
    x = Function('x')
    y = Function('y')
    t = Symbol('t')
    L = sum(diff(z(t), t)**2/2 - z(t)**2/2 for z in [x, y])
    L += -x(t)**2*y(t) + y(t)**3/3
    assert euler(L, [x(t), y(t)], t) == [Eq(-2*x(t)*y(t) - x(t) -
                                            diff(x(t), t, t)),
                                         Eq(-x(t)**2 + y(t)**2 -
                                            y(t) - diff(y(t), t, t))]
Пример #5
0
def test_unhandled():
    class MyExpr(Expr):
        def _eval_derivative(self, s):
            if not s.name.startswith('xi'):
                return self
            else:
                return

    expr = MyExpr(x, y, z)
    assert diff(expr, x, y, f(x), z) == Derivative(expr, f(x), z)
    assert diff(expr, f(x), x) == Derivative(expr, f(x), x)
Пример #6
0
def test_pde_separate():
    f, X, Y = map(Function, 'fXY')
    pytest.raises(ValueError,
                  lambda: pde_separate(f(x, y).diff(x), f(x, y),
                                       [X(x), Y(y)], strategy='spam'))

    eq = diff(f(x, y), x) - exp(f(x, y))*diff(f(x, y), y)
    assert pde_separate(eq, f(x, y),
                        [X(x), Y(y)],
                        strategy='add') == [exp(-X(x))*diff(X(x), x),
                                            exp(Y(y))*diff(Y(y), y)]
Пример #7
0
def test_chebyshev():
    assert chebyshevt(0, x) == 1
    assert chebyshevt(1, x) == x
    assert chebyshevt(2, x) == 2*x**2 - 1
    assert chebyshevt(3, x) == 4*x**3 - 3*x
    assert chebyshevt(-2, x) == 2*x**2 - 1

    for n in range(1, 4):
        for k in range(n):
            z = chebyshevt_root(n, k)
            assert chebyshevt(n, z) == 0
        pytest.raises(ValueError, lambda: chebyshevt_root(n, n))

    for n in range(1, 4):
        for k in range(n):
            z = chebyshevu_root(n, k)
            assert chebyshevu(n, z) == 0
        pytest.raises(ValueError, lambda: chebyshevu_root(n, n))

    n = Symbol("n")
    X = chebyshevt(n, x)
    assert isinstance(X, chebyshevt)
    assert chebyshevt(n, -x) == (-1)**n*chebyshevt(n, x)
    assert chebyshevt(-n, x) == chebyshevt(n, x)

    assert chebyshevt(n, oo) == oo
    assert chebyshevt(n, 0) == cos(pi*n/2)
    assert chebyshevt(n, 1) == 1

    assert conjugate(chebyshevt(n, x)) == chebyshevt(n, conjugate(x))

    assert diff(chebyshevt(n, x), x) == n*chebyshevu(n - 1, x)

    pytest.raises(ArgumentIndexError, lambda: chebyshevt(n, x).fdiff(1))

    X = chebyshevu(n, x)
    assert isinstance(X, chebyshevu)

    assert chebyshevu(n, -x) == (-1)**n*chebyshevu(n, x)
    assert chebyshevu(-n, x) == -chebyshevu(n - 2, x)

    assert chebyshevu(n, oo) == oo
    assert chebyshevu(n, 0) == cos(pi*n/2)
    assert chebyshevu(n, 1) == n + 1
    assert chebyshevu(-1, x) == 0
    assert chebyshevu(-2, x) == -1

    assert conjugate(chebyshevu(n, x)) == chebyshevu(n, conjugate(x))

    assert diff(chebyshevu(n, x), x) == \
        (-x*chebyshevu(n, x) + (n + 1)*chebyshevt(n + 1, x))/(x**2 - 1)

    pytest.raises(ArgumentIndexError, lambda: chebyshevu(n, x).fdiff(1))
Пример #8
0
def test_klein_gordon_lagrangian():
    m = Symbol('m')
    phi = f(x, t)

    L = -(diff(phi, t)**2 - diff(phi, x)**2 - m**2*phi**2)/2
    eqna = Eq(
        diff(L, phi) - diff(L, diff(phi, x), x) - diff(L, diff(phi, t), t), 0)
    eqnb = Eq(diff(phi, t, t) - diff(phi, x, x) + m**2*phi, 0)
    assert eqna == eqnb
Пример #9
0
def test_beta():
    assert isinstance(beta(x, y), beta)

    assert expand_func(beta(x, y)) == gamma(x)*gamma(y)/gamma(x + y)
    assert expand_func(beta(x, y) - beta(y, x)) == 0  # Symmetric
    assert expand_func(beta(x, y)) == expand_func(beta(x, y + 1) +
                                                  beta(x + 1, y)).simplify()

    assert diff(beta(x, y), x) == beta(x, y)*(digamma(x) - digamma(x + y))
    assert diff(beta(x, y), y) == beta(x, y)*(digamma(y) - digamma(x + y))
    pytest.raises(ArgumentIndexError, lambda: beta(x, y).fdiff(3))

    assert conjugate(beta(x, y)) == beta(conjugate(x), conjugate(y))
Пример #10
0
def test_fcode_Piecewise():
    expr = Piecewise((x, x < 1), (x**2, True))
    # Check that inline conditional (merge) fails if standard isn't 95+
    pytest.raises(NotImplementedError, lambda: fcode(expr))
    code = fcode(expr, standard=95)
    expected = "      merge(x, x**2, x < 1)"
    assert code == expected
    assert fcode(Piecewise((x, x < 1), (x**2, True)), assign_to="var") == (
        "      if (x < 1) then\n"
        "         var = x\n"
        "      else\n"
        "         var = x**2\n"
        "      end if"
    )
    a = cos(x)/x
    b = sin(x)/x
    for i in range(10):
        a = diff(a, x)
        b = diff(b, x)
    expected = (
        "      if (x < 0) then\n"
        "         weird_name = -cos(x)/x + 10*sin(x)/x**2 + 90*cos(x)/x**3 - 720*\n"
        "     @ sin(x)/x**4 - 5040*cos(x)/x**5 + 30240*sin(x)/x**6 + 151200*cos(x\n"
        "     @ )/x**7 - 604800*sin(x)/x**8 - 1814400*cos(x)/x**9 + 3628800*sin(x\n"
        "     @ )/x**10 + 3628800*cos(x)/x**11\n"
        "      else\n"
        "         weird_name = -sin(x)/x - 10*cos(x)/x**2 + 90*sin(x)/x**3 + 720*\n"
        "     @ cos(x)/x**4 - 5040*sin(x)/x**5 - 30240*cos(x)/x**6 + 151200*sin(x\n"
        "     @ )/x**7 + 604800*cos(x)/x**8 - 1814400*sin(x)/x**9 - 3628800*cos(x\n"
        "     @ )/x**10 + 3628800*sin(x)/x**11\n"
        "      end if"
    )
    code = fcode(Piecewise((a, x < 0), (b, True)), assign_to="weird_name")
    assert code == expected
    code = fcode(Piecewise((x, x < 1), (x**2, x > 1), (sin(x), True)), standard=95)
    expected = "      merge(x, merge(x**2, sin(x), x > 1), x < 1)"
    assert code == expected
    # Check that Piecewise without a True (default) condition error
    expr = Piecewise((x, x < 1), (x**2, x > 1), (sin(x), x > 0))
    pytest.raises(ValueError, lambda: fcode(expr))

    assert (fcode(Piecewise((0, x < -1), (1, And(x >= -1, x < 0)),
                            (-1, True)), assign_to="var") ==
            '      if (x < -1) then\n'
            '         var = 0\n'
            '      else if (x >= -1 .and. x < 0) then\n'
            '         var = 1\n'
            '      else\n'
            '         var = -1\n'
            '      end if')
Пример #11
0
def test_general_function():
    nu = Function('nu')

    e = nu(x)
    edx = e.diff(x)
    edy = e.diff(y)
    edxdx = e.diff(x).diff(x)
    edxdy = e.diff(x).diff(y)
    assert e == nu(x)
    assert edx != nu(x)
    assert edx == diff(nu(x), x)
    assert edy == 0
    assert edxdx == diff(diff(nu(x), x), x)
    assert edxdy == 0
Пример #12
0
def test_collect_D():
    D = Derivative
    f = Function('f')
    fx = D(f(x), x)
    fxx = D(f(x), x, x)

    assert collect(a*fx + b*fx, fx) == (a + b)*fx
    assert collect(a*D(fx, x) + b*D(fx, x), fx) == (a + b)*D(fx, x)
    assert collect(a*fxx + b*fxx, fx) == (a + b)*D(fx, x)
    # issue sympy/sympy#4784
    assert collect(5*f(x) + 3*fx, fx) == 5*f(x) + 3*fx
    assert collect(f(x) + f(x)*diff(f(x), x) + x*diff(f(x), x)*f(x), f(x).diff(x), exact=True) == \
        (x*f(x) + f(x))*D(f(x), x) + f(x)
    assert collect(1/f(x) + 1/f(x)*diff(f(x), x) + x*diff(f(x), x)/f(x), f(x).diff(x), exact=True) == \
        (1/f(x) + x/f(x))*D(f(x), x) + 1/f(x)
Пример #13
0
def test_catalan():
    n = Symbol('n', integer=True)
    m = Symbol('n', integer=True, positive=True)

    catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
    for i, c in enumerate(catalans):
        assert catalan(i) == c
        assert catalan(n).rewrite(factorial).subs({n: i}) == c
        assert catalan(n).rewrite(Product).subs({n: i}).doit() == c

    assert catalan(x) == catalan(x)
    assert catalan(2*x).rewrite(binomial) == binomial(4*x, 2*x)/(2*x + 1)
    assert catalan(Rational(1, 2)).rewrite(gamma) == 8/(3*pi)
    assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\
        8 / (3 * pi)
    assert catalan(3*x).rewrite(gamma) == 4**(
        3*x)*gamma(3*x + Rational(1, 2))/(sqrt(pi)*gamma(3*x + 2))
    assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2,), 1)

    assert catalan(n).rewrite(factorial) == factorial(2*n) / (factorial(n + 1)
                                                              * factorial(n))
    assert isinstance(catalan(n).rewrite(Product), catalan)
    assert isinstance(catalan(m).rewrite(Product), Product)

    assert diff(catalan(x), x) == (polygamma(
        0, x + Rational(1, 2)) - polygamma(0, x + 2) + log(4))*catalan(x)

    assert catalan(x).evalf() == catalan(x)
    c = catalan(Rational(1, 2)).evalf()
    assert str(c) == '0.848826363156775'
    c = catalan(I).evalf(3)
    assert sstr((re(c), im(c))) == '(0.398, -0.0209)'
Пример #14
0
def test_laguerre():
    assert laguerre(0, x) == 1
    assert laguerre(1, x) == -x + 1
    assert laguerre(2, x) == x**2/2 - 2*x + 1
    assert laguerre(3, x) == -x**3/6 + 3*x**2/2 - 3*x + 1

    assert laguerre(n, oo) == (-1)**n*oo
    assert laguerre(n, -oo) == oo

    assert laguerre(-n, x) == exp(x)*laguerre(n - 1, -x)

    X = laguerre(n, x)
    assert isinstance(X, laguerre)

    assert laguerre(n, 0) == 1

    assert conjugate(laguerre(n, x)) == laguerre(n, conjugate(x))

    assert diff(laguerre(n, x), x) == -assoc_laguerre(n - 1, 1, x)

    pytest.raises(ArgumentIndexError, lambda: laguerre(n, x).fdiff(1))

    pytest.raises(ValueError, lambda: laguerre(-2.1, x))

    # issue sympy/sympy#10961
    X = laguerre(Rational(5, 2), x)
    assert isinstance(X, laguerre)
Пример #15
0
def test_derivative_subs_bug():
    e = diff(g(x), x)
    assert e.subs({g(x): +f(x)}) != e
    assert e.subs({g(x): +f(x)}) == Derivative(f(x), x)
    assert e.subs({g(x): -f(x)}) == Derivative(-f(x), x)

    assert e.subs({x: y}) == Derivative(g(y), y)
Пример #16
0
def test_sympyissue_11526():
    df = diff(1/(a*log((x - b)/(x - c))), x)
    res = -1/(-a*c + a*b)
    assert limit(df, x, oo) == res
    assert limit(simplify(df), x, oo) == res

    e = log((1/x - b)/(1/x - c))
    assert e.as_leading_term(x) == x*(c - b)
Пример #17
0
def test_mathml_functions():
    mml_1 = mp._print(sin(x))
    assert mml_1.nodeName == 'apply'
    assert mml_1.childNodes[0].nodeName == 'sin'
    assert mml_1.childNodes[1].nodeName == 'ci'

    mml_2 = mp._print(diff(sin(x), x, evaluate=False))
    assert mml_2.nodeName == 'apply'
    assert mml_2.childNodes[0].nodeName == 'diff'
    assert mml_2.childNodes[1].nodeName == 'bvar'
    assert mml_2.childNodes[1].childNodes[
        0].nodeName == 'ci'  # below bvar there's <ci>x/ci>

    mml_3 = mp._print(diff(cos(x*y), x, evaluate=False))
    assert mml_3.nodeName == 'apply'
    assert mml_3.childNodes[0].nodeName == 'partialdiff'
    assert mml_3.childNodes[1].nodeName == 'bvar'
    assert mml_3.childNodes[1].childNodes[
        0].nodeName == 'ci'  # below bvar there's <ci>x/ci>
Пример #18
0
def test_vector_diff_integrate():
    f = Function('f')
    v = f(a)*C.i + a**2*C.j - C.k
    pytest.raises(TypeError, lambda: v.diff(v))
    assert Derivative(v, a) == Derivative((f(a))*C.i +
                                          a**2*C.j + (-1)*C.k, a)
    assert (diff(v, a) == v.diff(a) == Derivative(v, a).doit() ==
            (Derivative(f(a), a))*C.i + 2*a*C.j)
    assert (Integral(v, a) == (Integral(f(a), a))*C.i +
            (Integral(a**2, a))*C.j + (Integral(-1, a))*C.k)
Пример #19
0
def test_sympyissue_4418():
    a, b, c = symbols('a b c', cls=Wild, exclude=(x,))
    f, g = symbols('f g', cls=Function)

    eq = diff(g(x)*f(x).diff(x), x)

    assert eq.match(
        g(x).diff(x)*f(x).diff(x) + g(x)*f(x).diff(x, x) + c) == {c: 0}
    assert eq.match(a*g(x).diff(
        x)*f(x).diff(x) + b*g(x)*f(x).diff(x, x) + c) == {a: 1, b: 1, c: 0}
Пример #20
0
def test_trigsimp_issues():
    # issue sympy/sympy#4625 - factor_terms works, too
    assert trigsimp(sin(x)**3 + cos(x)**2*sin(x)) == sin(x)

    # issue sympy/sympy#5948
    assert trigsimp(diff(integrate(cos(x)/sin(x)**3, x), x)) == \
        cos(x)/sin(x)**3
    assert trigsimp(diff(integrate(sin(x)/cos(x)**3, x), x)) == \
        sin(x)/cos(x)**3

    # check integer exponents
    e = sin(x)**y/cos(x)**y
    assert trigsimp(e) == e
    assert trigsimp(e.subs({y: 2})) == tan(x)**2
    assert trigsimp(e.subs({x: 1})) == tan(1)**y

    # check for multiple patterns
    assert (cos(x)**2/sin(x)**2*cos(y)**2/sin(y)**2).trigsimp() == \
        1/tan(x)**2/tan(y)**2
    assert trigsimp(cos(x)/sin(x)*cos(x+y)/sin(x+y)) == \
        1/(tan(x)*tan(x + y))

    eq = cos(2)*(cos(3) + 1)**2/(cos(3) - 1)**2
    assert trigsimp(eq) == eq.factor()  # factor makes denom (-1 + cos(3))**2
    assert trigsimp(cos(2)*(cos(3) + 1)**2*(cos(3) - 1)**2) == \
        cos(2)*sin(3)**4

    # issue sympy/sympy#6789; this generates an expression that formerly caused
    # trigsimp to hang
    assert cot(x).equals(tan(x)) is False

    # nan or the unchanged expression is ok, but not sin(1)
    z = cos(x)**2 + sin(x)**2 - 1
    z1 = tan(x)**2 - 1/cot(x)**2
    n = (1 + z1/z)
    assert trigsimp(sin(n)) != sin(1)
    eq = x*(n - 1) - x*n
    assert trigsimp(eq) is nan
    assert trigsimp(eq, recursive=True) is nan
    assert trigsimp(1).is_Integer

    assert trigsimp(-sin(x)**4 - 2*sin(x)**2*cos(x)**2 - cos(x)**4) == -1
Пример #21
0
def test_hermite():
    assert hermite(0, x) == 1
    assert hermite(1, x) == 2*x
    assert hermite(2, x) == 4*x**2 - 2
    assert hermite(3, x) == 8*x**3 - 12*x
    assert hermite(4, x) == 16*x**4 - 48*x**2 + 12
    assert hermite(6, x) == 64*x**6 - 480*x**4 + 720*x**2 - 120

    assert hermite(n, x) == hermite(n, x)
    assert hermite(n, -x) == (-1)**n*hermite(n, x)
    assert hermite(-n, x) == hermite(-n, x)
    assert hermite(n, 0) == 2**n*sqrt(pi)/gamma((1 - n)/2)
    assert hermite(n, oo) == oo

    pytest.raises(ValueError, lambda: hermite(-1, x))

    assert conjugate(hermite(n, x)) == hermite(n, conjugate(x))

    assert diff(hermite(n, x), x) == 2*n*hermite(n - 1, x)
    assert diff(hermite(n, x), n) == Derivative(hermite(n, x), n)
Пример #22
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
Пример #23
0
def test_Li():
    assert Li(2) == 0
    assert Li(oo) == oo

    assert isinstance(Li(z), Li)

    assert diff(Li(z), z) == 1/log(z)
    pytest.raises(ArgumentIndexError, lambda: Li(z).fdiff(2))

    assert Li(z).rewrite(li) == li(z) - li(2)

    assert Li(4).evalf(30) == Float('1.92242131492155809316615998937961', dps=30)
Пример #24
0
def test_gegenbauer():
    assert gegenbauer(0, a, x) == 1
    assert gegenbauer(1, a, x) == 2*a*x
    assert gegenbauer(2, a, x) == -a + x**2*(2*a**2 + 2*a)
    assert gegenbauer(3, a, x) == \
        x**3*(4*a**3/3 + 4*a**2 + 8*a/3) + x*(-2*a**2 - 2*a)

    assert gegenbauer(-1, a, x) == 0
    assert gegenbauer(n, Rational(1, 2), x) == legendre(n, x)
    assert gegenbauer(n, 1, x) == chebyshevu(n, x)
    assert gegenbauer(n, -1, x) == 0

    assert gegenbauer(n, -2, -1) == gegenbauer(n, -2, -1, evaluate=False)

    X = gegenbauer(n, a, x)
    assert isinstance(X, gegenbauer)

    assert gegenbauer(n, a, -x) == (-1)**n*gegenbauer(n, a, x)
    assert gegenbauer(n, a, 0) == 2**n*sqrt(pi) * \
        gamma(a + n/2)/(gamma(a)*gamma(-n/2 + Rational(1, 2))*gamma(n + 1))
    assert gegenbauer(n, a, 1) == gamma(2*a + n)/(gamma(2*a)*gamma(n + 1))

    assert gegenbauer(n, Rational(3, 4), -1) == zoo

    m = Symbol("m", positive=True)
    assert gegenbauer(m, a, oo) == oo*RisingFactorial(a, m)
    assert gegenbauer(n, a, oo) == gegenbauer(n, a, oo, evaluate=False)

    assert conjugate(gegenbauer(n, a, x)) == gegenbauer(n, conjugate(a), conjugate(x))

    assert diff(gegenbauer(n, a, x), n) == Derivative(gegenbauer(n, a, x), n)
    assert diff(gegenbauer(n, a, x), x) == 2*a*gegenbauer(n - 1, a + 1, x)

    pytest.raises(ArgumentIndexError, lambda: gegenbauer(n, a, x).fdiff(4))

    # XXX see issue sympy/sympy#5539
    assert str(gegenbauer(n, a, x).diff(a)) == \
        ("Sum((2*(-1)**(n - _k) + 2)*(a + _k)*gegenbauer(_k, a, x)/((n - "
         "_k)*(2*a + n + _k)) + (2/(2*a + n + _k) + (2*_k + 2)/((2*a + "
         "_k)*(2*a + 2*_k + 1)))*gegenbauer(n, a, x), (_k, 0, n - 1))")
Пример #25
0
def test_legendre():
    pytest.raises(ValueError, lambda: legendre(-1, x))
    assert legendre(0, x) == 1
    assert legendre(1, x) == x
    assert legendre(2, x) == ((3*x**2 - 1)/2).expand()
    assert legendre(3, x) == ((5*x**3 - 3*x)/2).expand()
    assert legendre(4, x) == ((35*x**4 - 30*x**2 + 3)/8).expand()
    assert legendre(5, x) == ((63*x**5 - 70*x**3 + 15*x)/8).expand()
    assert legendre(6, x) == ((231*x**6 - 315*x**4 + 105*x**2 - 5)/16).expand()

    assert legendre(10, -1) == 1
    assert legendre(11, -1) == -1
    assert legendre(10, 1) == 1
    assert legendre(11, 1) == 1
    assert legendre(10, 0) != 0
    assert legendre(11, 0) == 0

    assert roots(legendre(4, x), x) == {
        sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1,
        -sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1,
        sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1,
        -sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1,
    }

    X = legendre(n, x)
    assert isinstance(X, legendre)

    assert legendre(-n, x) == legendre(n - 1, x)
    assert legendre(n, -x) == (-1)**n*legendre(n, x)

    assert legendre(n, 0) == sqrt(pi)/(gamma(Rational(1, 2) - n/2)*gamma(1 + n/2))
    assert legendre(n, 1) == 1
    assert legendre(n, oo) == oo

    assert conjugate(legendre(n, x)) == legendre(n, conjugate(x))

    assert diff(legendre(n, x), x) == \
        n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1)
    assert diff(legendre(n, x), n) == Derivative(legendre(n, x), n)
Пример #26
0
def test_euler_interface():
    x = Function('x')
    y = Symbol('y')
    t = Symbol('t')
    pytest.raises(TypeError, lambda: euler())
    pytest.raises(TypeError, lambda: euler(diff(x(t), t)*y(t), [x(t), y]))
    pytest.raises(ValueError, lambda: euler(diff(x(t), t)*x(y), [x(t), x(y)]))
    pytest.raises(TypeError, lambda: euler(diff(x(t), t)**2, x(0)))
    pytest.raises(TypeError, lambda: euler(1, y))
    assert euler(diff(x(t), t)**2/2, {x(t)}) == [Eq(-diff(x(t), t, t))]
    assert euler(diff(x(t), t)**2/2, x(t), {t}) == [Eq(-diff(x(t), t, t))]
Пример #27
0
def test_legendre():
    pytest.raises(ValueError, lambda: legendre(-1, x))
    assert legendre(0, x) == 1
    assert legendre(1, x) == x
    assert legendre(2, x) == ((3*x**2 - 1)/2).expand()
    assert legendre(3, x) == ((5*x**3 - 3*x)/2).expand()
    assert legendre(4, x) == ((35*x**4 - 30*x**2 + 3)/8).expand()
    assert legendre(5, x) == ((63*x**5 - 70*x**3 + 15*x)/8).expand()
    assert legendre(6, x) == ((231*x**6 - 315*x**4 + 105*x**2 - 5)/16).expand()

    assert legendre(10, -1) == 1
    assert legendre(11, -1) == -1
    assert legendre(10, 1) == 1
    assert legendre(11, 1) == 1
    assert legendre(10, 0) != 0
    assert legendre(11, 0) == 0

    assert roots(legendre(4, x), x) == {
        sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1,
        -sqrt(Rational(3, 7) - Rational(2, 35)*sqrt(30)): 1,
        sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1,
        -sqrt(Rational(3, 7) + Rational(2, 35)*sqrt(30)): 1,
    }

    X = legendre(n, x)
    assert isinstance(X, legendre)

    assert legendre(-n, x) == legendre(n - 1, x)
    assert legendre(n, -x) == (-1)**n*legendre(n, x)

    assert legendre(n, 0) == sqrt(pi)/(gamma(Rational(1, 2) - n/2)*gamma(1 + n/2))
    assert legendre(n, 1) == 1
    assert legendre(n, oo) == oo

    assert conjugate(legendre(n, x)) == legendre(n, conjugate(x))

    assert diff(legendre(n, x), x) == \
        n*(x*legendre(n, x) - legendre(n - 1, x))/(x**2 - 1)
    assert diff(legendre(n, x), n) == Derivative(legendre(n, x), n)
Пример #28
0
def test_euler_interface():
    x = Function('x')
    y = Symbol('y')
    t = Symbol('t')
    pytest.raises(TypeError, lambda: euler())
    pytest.raises(TypeError, lambda: euler(diff(x(t), t) * y(t), [x(t), y]))
    pytest.raises(ValueError,
                  lambda: euler(diff(x(t), t) * x(y), [x(t), x(y)]))
    pytest.raises(TypeError, lambda: euler(diff(x(t), t)**2, x(0)))
    pytest.raises(TypeError, lambda: euler(1, y))
    assert euler(diff(x(t), t)**2 / 2, {x(t)}) == [Eq(-diff(x(t), t, t))]
    assert euler(diff(x(t), t)**2 / 2, x(t), {t}) == [Eq(-diff(x(t), t, t))]
Пример #29
0
def test_Ynm():
    # http://en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi", extended_real=True)
    from diofant.abc import n, m

    assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
    assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                    + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
    assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                   + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))

    assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
                                           + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
    assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) ==
            Ynm(n, m, th, ph).rewrite(cos) ==
            exp(I*m*ph)*sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n)) *
            assoc_legendre(n, m, cos(th))/(2*sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() ==
            (sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*cos(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi)),
             sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*sin(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi))))
Пример #30
0
def test_gegenbauer():
    assert gegenbauer(0, a, x) == 1
    assert gegenbauer(1, a, x) == 2*a*x
    assert gegenbauer(2, a, x) == -a + x**2*(2*a**2 + 2*a)
    assert gegenbauer(3, a, x) == \
        x**3*(4*a**3/3 + 4*a**2 + 8*a/3) + x*(-2*a**2 - 2*a)

    assert gegenbauer(-1, a, x) == 0
    assert gegenbauer(n, Rational(1, 2), x) == legendre(n, x)
    assert gegenbauer(n, 1, x) == chebyshevu(n, x)
    assert gegenbauer(n, -1, x) == 0

    assert gegenbauer(n, -2, -1) == gegenbauer(n, -2, -1, evaluate=False)

    X = gegenbauer(n, a, x)
    assert isinstance(X, gegenbauer)

    assert gegenbauer(n, a, -x) == (-1)**n*gegenbauer(n, a, x)
    assert gegenbauer(n, a, 0) == 2**n*sqrt(pi) * \
        gamma(a + n/2)/(gamma(a)*gamma(-n/2 + Rational(1, 2))*gamma(n + 1))
    assert gegenbauer(n, a, 1) == gamma(2*a + n)/(gamma(2*a)*gamma(n + 1))

    assert gegenbauer(n, Rational(3, 4), -1) == zoo

    m = Symbol('m', positive=True)
    assert gegenbauer(m, a, oo) == oo*RisingFactorial(a, m)
    assert gegenbauer(n, a, oo) == gegenbauer(n, a, oo, evaluate=False)

    assert conjugate(gegenbauer(n, a, x)) == gegenbauer(n, conjugate(a), conjugate(x))

    assert diff(gegenbauer(n, a, x), n) == Derivative(gegenbauer(n, a, x), n)
    assert diff(gegenbauer(n, a, x), x) == 2*a*gegenbauer(n - 1, a + 1, x)

    pytest.raises(ArgumentIndexError, lambda: gegenbauer(n, a, x).fdiff(4))

    assert (gegenbauer(n, a, x).diff(a) ==
            Sum((2*(-1)**(n - k) + 2)*(a + k)*gegenbauer(k, a, x)/((n - k)*(2*a + n + k)) +
                (2/(2*a + n + k) + (2*k + 2)/((2*a + k)*(2*a + 2*k + 1)))*gegenbauer(n, a, x), (k, 0, n - 1)))
Пример #31
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)
Пример #32
0
def test_Ynm():
    # https//en.wikipedia.org/wiki/Spherical_harmonics
    th, ph = Symbol("theta", extended_real=True), Symbol("phi", extended_real=True)

    assert Ynm(0, 0, th, ph).expand(func=True) == 1/(2*sqrt(pi))
    assert Ynm(1, -1, th, ph) == -exp(-2*I*ph)*Ynm(1, 1, th, ph)
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, -1, th, ph).expand(func=True) == sqrt(6)*sin(th)*exp(-I*ph)/(4*sqrt(pi))
    assert Ynm(1, 0, th, ph).expand(func=True) == sqrt(3)*cos(th)/(2*sqrt(pi))
    assert Ynm(1, 1, th, ph).expand(func=True) == -sqrt(6)*sin(th)*exp(I*ph)/(4*sqrt(pi))
    assert Ynm(2, 0, th, ph).expand(func=True) == 3*sqrt(5)*cos(th)**2/(4*sqrt(pi)) - sqrt(5)/(4*sqrt(pi))
    assert Ynm(2, 1, th, ph).expand(func=True) == -sqrt(30)*sin(th)*exp(I*ph)*cos(th)/(4*sqrt(pi))
    assert Ynm(2, -2, th, ph).expand(func=True) == (-sqrt(30)*exp(-2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                    + sqrt(30)*exp(-2*I*ph)/(8*sqrt(pi)))
    assert Ynm(2, 2, th, ph).expand(func=True) == (-sqrt(30)*exp(2*I*ph)*cos(th)**2/(8*sqrt(pi))
                                                   + sqrt(30)*exp(2*I*ph)/(8*sqrt(pi)))

    assert diff(Ynm(n, m, th, ph), th) == (m*cot(th)*Ynm(n, m, th, ph)
                                           + sqrt((-m + n)*(m + n + 1))*exp(-I*ph)*Ynm(n, m + 1, th, ph))
    assert diff(Ynm(n, m, th, ph), ph) == I*m*Ynm(n, m, th, ph)
    pytest.raises(ArgumentIndexError, lambda: Ynm(n, m, th, ph).fdiff(1))

    assert conjugate(Ynm(n, m, th, ph)) == (-1)**(2*m)*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert Ynm(n, m, -th, ph) == Ynm(n, m, th, ph)
    assert Ynm(n, m, th, -ph) == exp(-2*I*m*ph)*Ynm(n, m, th, ph)
    assert Ynm(n, -m, th, ph) == (-1)**m*exp(-2*I*m*ph)*Ynm(n, m, th, ph)

    assert (Ynm(n, m, th, ph).rewrite(sin) ==
            Ynm(n, m, th, ph).rewrite(cos) ==
            exp(I*m*ph)*sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n)) *
            assoc_legendre(n, m, cos(th))/(2*sqrt(pi)))
    assert (Ynm(n, m, th, ph).as_real_imag() ==
            (sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*cos(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi)),
             sqrt((2*n + 1)*factorial(-m + n)/factorial(m + n))*sin(m*ph) *
             assoc_legendre(n, m, cos(th))/(2*sqrt(pi))))
Пример #33
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)
Пример #34
0
def scalar_potential(field, coord_sys):
    """
    Returns the scalar potential function of a field in a given
    coordinate system (without the added integration constant).

    Parameters
    ==========

    field : Vector
        The vector field whose scalar potential function is to be
        calculated

    coord_sys : CoordSysCartesian
        The coordinate system to do the calculation in

    Examples
    ========

    >>> from diofant.vector import CoordSysCartesian
    >>> from diofant.vector import scalar_potential, gradient
    >>> R = CoordSysCartesian('R')
    >>> scalar_potential(R.k, R) == R.z
    True
    >>> scalar_field = 2*R.x**2*R.y*R.z
    >>> grad_field = gradient(scalar_field, R)
    >>> scalar_potential(grad_field, R)
    2*R.x**2*R.y*R.z

    """

    # Check whether field is conservative
    if not is_conservative(field):
        raise ValueError("Field is not conservative")
    if field == Vector.zero:
        return Integer(0)
    # Express the field exntirely in coord_sys
    # Subsitute coordinate variables also
    if not isinstance(coord_sys, CoordSysCartesian):
        raise TypeError("coord_sys must be a CoordSysCartesian")
    field = express(field, coord_sys, variables=True)
    dimensions = coord_sys.base_vectors()
    scalars = coord_sys.base_scalars()
    # Calculate scalar potential function
    temp_function = integrate(field.dot(dimensions[0]), scalars[0])
    for i, dim in enumerate(dimensions[1:]):
        partial_diff = diff(temp_function, scalars[i + 1])
        partial_diff = field.dot(dim) - partial_diff
        temp_function += integrate(partial_diff, scalars[i + 1])
    return temp_function
Пример #35
0
def test_sympyissue_4418():
    a, b, c = symbols('a b c', cls=Wild, exclude=(x, ))
    f, g = symbols('f g', cls=Function)

    eq = diff(g(x) * f(x).diff(x), x)

    assert eq.match(g(x).diff(x) * f(x).diff(x) + g(x) * f(x).diff(x, x) +
                    c) == {
                        c: 0
                    }
    assert eq.match(a * g(x).diff(x) * f(x).diff(x) +
                    b * g(x) * f(x).diff(x, x) + c) == {
                        a: 1,
                        b: 1,
                        c: 0
                    }
Пример #36
0
def test_sympyissue_19453():
    beta = Symbol('beta', real=True, positive=True)
    h = Symbol('h', real=True, positive=True)
    m = Symbol('m', real=True, positive=True)
    w = Symbol('omega', real=True, positive=True)
    g = Symbol('g', real=True, positive=True)

    q = 3*h**2*beta*g*exp(h*beta*w/2)
    p = m**2*w**2
    s = exp(h*beta*w) - 1
    z = (-q/(4*p*s) - q/(2*p*s**2) -
         q*(exp(h*beta*w) + 1)/(2*p*s**3) + exp(h*beta*w/2)/s)
    e = -diff(log(z), beta)

    assert limit(e - h*w/2, beta, oo) == 0
    assert limit(e.simplify() - h*w/2, beta, oo) == 0
Пример #37
0
def test_pde_separate_add():
    x, y, z, t = symbols("x,y,z,t")
    F, T, X, Y, Z, u = map(Function, 'FTXYZu')

    eq = Eq(diff(u(x, t), x), diff(u(x, t), t)*exp(u(x, t)))
    res = pde_separate_add(eq, u(x, t), [X(x), T(t)])
    assert res == [diff(X(x), x)*exp(-X(x)), diff(T(t), t)*exp(T(t))]

    eq = Eq(u(x, y).diff(x), -exp(u(x, y).diff(y)))
    res = pde_separate_add(eq, u(x, y), [X(x), Y(y)])
    assert res == [diff(X(x), x), -exp(diff(Y(y), y))]
Пример #38
0
def test_pde_separate_add():
    x, y, z, t = symbols('x,y,z,t')
    F, T, X, Y, Z, u = map(Function, 'FTXYZu')

    eq = Eq(diff(u(x, t), x), diff(u(x, t), t) * exp(u(x, t)))
    res = pde_separate_add(eq, u(x, t), [X(x), T(t)])
    assert res == [diff(X(x), x) * exp(-X(x)), diff(T(t), t) * exp(T(t))]

    eq = Eq(u(x, y).diff(x), -exp(u(x, y).diff(y)))
    res = pde_separate_add(eq, u(x, y), [X(x), Y(y)])
    assert res == [diff(X(x), x), -exp(diff(Y(y), y))]
Пример #39
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)}
Пример #40
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)}
Пример #41
0
def test_match_deriv_bug1():
    n = Function('n')
    l = Function('l')

    p = Wild('p')

    e = diff(l(x), x)/x - diff(diff(n(x), x), x)/2 - \
        diff(n(x), x)**2/4 + diff(n(x), x)*diff(l(x), x)/4
    e = e.subs({n(x): -l(x)}).doit()
    t = x*exp(-l(x))
    t2 = t.diff(x, x)/t
    assert e.match( (p*t2).expand() ) == {p: -Rational(1, 2)}
Пример #42
0
def test_match_deriv_bug1():
    n = Function('n')
    l = Function('l')

    p = Wild('p')

    e = diff(l(x), x)/x - diff(diff(n(x), x), x)/2 - \
        diff(n(x), x)**2/4 + diff(n(x), x)*diff(l(x), x)/4
    e = e.subs({n(x): -l(x)}).doit()
    t = x * exp(-l(x))
    t2 = t.diff(x, x) / t
    assert e.match((p * t2).expand()) == {p: -Rational(1, 2)}
Пример #43
0
def test_airyai():
    z = Symbol('z', extended_real=False)
    t = Symbol('t', negative=True)
    p = Symbol('p', positive=True)

    assert isinstance(airyai(z), airyai)

    assert airyai(0) == 3**Rational(1, 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) -
                         3**Rational(1, 6) * z * gamma(Rational(2, 3)) /
                         (2 * pi) + O(z**3))

    assert airyai(z).rewrite(hyper) == (-3**Rational(2, 3) * z * hyper(
        (), (Rational(4, 3), ),
        z**Integer(3) / 9) / (3 * gamma(Rational(1, 3))) +
                                        3**Rational(1, 3) * hyper(
                                            (), (Rational(2, 3), ),
                                            z**Integer(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 * (z**Rational(3, 2))**Rational(1, 3)) +
        (z**Rational(3, 2))**Rational(1, 3) *
        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 * (3 * z**5)**Rational(1, 3))) == (
        -sqrt(3) * (-1 + (z**5)**Rational(1, 3) / z**Rational(5, 3)) *
        airybi(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 6 +
        (1 + (z**5)**Rational(1, 3) / z**Rational(5, 3)) *
        airyai(2 * 3**Rational(1, 3) * z**Rational(5, 3)) / 2)
Пример #44
0
def test_catalan():
    n = Symbol('n', integer=True)
    m = Symbol('n', integer=True, positive=True)

    catalans = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786]
    for i, c in enumerate(catalans):
        assert catalan(i) == c
        assert catalan(n).rewrite(factorial).subs({n: i}) == c
        assert catalan(n).rewrite(Product).subs({n: i}).doit() == c

    assert catalan(x) == catalan(x)
    assert catalan(2 *
                   x).rewrite(binomial) == binomial(4 * x, 2 * x) / (2 * x + 1)
    assert catalan(Rational(1, 2)).rewrite(gamma) == 8 / (3 * pi)
    assert catalan(Rational(1, 2)).rewrite(factorial).rewrite(gamma) ==\
        8 / (3 * pi)
    assert catalan(3 * x).rewrite(gamma) == 4**(
        3 * x) * gamma(3 * x + Rational(1, 2)) / (sqrt(pi) * gamma(3 * x + 2))
    assert catalan(x).rewrite(hyper) == hyper((-x + 1, -x), (2, ), 1)

    assert catalan(n).rewrite(factorial) == factorial(
        2 * n) / (factorial(n + 1) * factorial(n))
    assert isinstance(catalan(n).rewrite(Product), catalan)
    assert isinstance(catalan(m).rewrite(Product), Product)

    assert diff(catalan(x), x) == (polygamma(0, x + Rational(1, 2)) -
                                   polygamma(0, x + 2) + log(4)) * catalan(x)

    assert catalan(x).evalf() == catalan(x)
    c = catalan(Rational(1, 2)).evalf()
    assert str(c) == '0.848826363156775'
    c = catalan(I).evalf(3)
    assert sstr((re(c), im(c))) == '(0.398, -0.0209)'

    # issue sympy/sympy#8601
    n = Symbol('n', integer=True, negative=True)

    assert catalan(n - 1) == 0
    assert catalan(Rational(-1, 2)) == zoo
    assert catalan(-1) == Rational(-1, 2)
    c1 = catalan(-5.6).evalf(strict=False)
    assert str(c1) == '6.93334070531408e-5'
    c2 = catalan(-35.4).evalf(strict=False)
    assert str(c2) == '-4.14189164517449e-24'
Пример #45
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 airybi(z).series(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
Пример #46
0
def test_diff_symbols():
    assert diff(f(x, y, z), x, y, z) == Derivative(f(x, y, z), x, y, z)
    assert diff(f(x, y, z), x, x, x) == Derivative(f(x, y, z), x, x, x)
    assert diff(f(x, y, z), x, 3) == Derivative(f(x, y, z), x, 3)

    # issue sympy/sympy#5028
    assert [diff(-z + x/y, sym) for sym in (z, x, y)] == [-1, 1/y, -x/y**2]
    assert diff(f(x, y, z), x, y, z, 2) == Derivative(f(x, y, z), x, y, z, z)
    assert diff(f(x, y, z), x, y, z, 2, evaluate=False) == \
        Derivative(f(x, y, z), x, y, z, z)
    assert Derivative(f(x, y, z), x, y, z)._eval_derivative(z) == \
        Derivative(f(x, y, z), x, y, z, z)
    assert Derivative(Derivative(f(x, y, z), x), y)._eval_derivative(z) == \
        Derivative(f(x, y, z), x, y, z)
Пример #47
0
def test_li():
    z = Symbol('z')
    zr = Symbol('z', extended_real=True)
    zp = Symbol('z', positive=True)
    zn = Symbol('z', negative=True)

    assert li(0) == 0
    assert li(1) == -oo
    assert li(oo) == oo

    assert isinstance(li(z), li)

    assert diff(li(z), z) == 1 / log(z)
    pytest.raises(ArgumentIndexError, lambda: li(z).fdiff(2))

    assert conjugate(li(z)) == li(conjugate(z))
    assert conjugate(li(-zr)) == li(-zr)
    assert conjugate(li(-zp)) == conjugate(li(-zp))
    assert conjugate(li(zn)) == conjugate(li(zn))

    assert li(z).rewrite(Li) == Li(z) + li(2)
    assert li(z).rewrite(Ei) == Ei(log(z))
    assert li(z).rewrite(uppergamma) == (-log(1 / log(z)) / 2 - log(-log(z)) +
                                         log(log(z)) / 2 - expint(1, -log(z)))
    assert li(z).rewrite(Si) == (-log(I * log(z)) - log(1 / log(z)) / 2 +
                                 log(log(z)) / 2 + Ci(I * log(z)) +
                                 Shi(log(z)))
    assert li(z).rewrite(Ci) == (-log(I * log(z)) - log(1 / log(z)) / 2 +
                                 log(log(z)) / 2 + Ci(I * log(z)) +
                                 Shi(log(z)))
    assert li(z).rewrite(Shi) == (-log(1 / log(z)) / 2 + log(log(z)) / 2 +
                                  Chi(log(z)) - Shi(log(z)))
    assert li(z).rewrite(Chi) == (-log(1 / log(z)) / 2 + log(log(z)) / 2 +
                                  Chi(log(z)) - Shi(log(z)))
    assert li(z).rewrite(hyper) == (log(z) * hyper(
        (1, 1),
        (2, 2), log(z)) - log(1 / log(z)) / 2 + log(log(z)) / 2 + EulerGamma)
    assert li(z).rewrite(meijerg) == (-log(1 / log(z)) / 2 - log(-log(z)) +
                                      log(log(z)) / 2 - meijerg(
                                          ((), (1, )), ((0, 0), ()), -log(z)))
Пример #48
0
def main():
    t = ReferenceSimplex(2)
    fe = Lagrange(2, 2)

    u = 0
    # compute u = sum_i u_i N_i
    us = []
    for i in range(0, fe.nbf()):
        ui = Symbol("u_%d" % i)
        us.append(ui)
        u += ui * fe.N[i]

    J = zeros(fe.nbf())
    for i in range(0, fe.nbf()):
        Fi = u * fe.N[i]
        print(Fi)
        for j in range(0, fe.nbf()):
            uj = us[j]
            integrands = diff(Fi, uj)
            print(integrands)
            J[j, i] = t.integrate(integrands)

    pprint(J)
Пример #49
0
def test_assoc_laguerre():
    # generalized Laguerre polynomials:
    assert assoc_laguerre(0, alpha, x) == 1
    assert assoc_laguerre(1, alpha, x) == -x + alpha + 1
    assert assoc_laguerre(2, alpha, x).expand() == \
        (x**2/2 - (alpha + 2)*x + (alpha + 2)*(alpha + 1)/2).expand()
    assert assoc_laguerre(3, alpha, x).expand() == \
        (-x**3/6 + (alpha + 3)*x**2/2 - (alpha + 2)*(alpha + 3)*x/2 +
        (alpha + 1)*(alpha + 2)*(alpha + 3)/6).expand()

    # Test the lowest 10 polynomials with laguerre_poly, to make sure it works:
    for i in range(10):
        assert assoc_laguerre(i, 0, x).expand() == laguerre_poly(i, x)

    X = assoc_laguerre(n, m, x)
    assert isinstance(X, assoc_laguerre)

    assert assoc_laguerre(n, 0, x) == laguerre(n, x)
    assert assoc_laguerre(n, alpha, 0) == binomial(alpha + n, alpha)

    p = Symbol('p', positive=True)
    assert assoc_laguerre(p, alpha, oo) == (-1)**p * oo
    assert assoc_laguerre(p, alpha, -oo) == oo

    assert diff(assoc_laguerre(n, alpha, x), x) == \
        -assoc_laguerre(n - 1, alpha + 1, x)

    pytest.raises(ArgumentIndexError, lambda: assoc_laguerre(n, m, x).fdiff(1))

    assert conjugate(assoc_laguerre(n, alpha, x)) == \
        assoc_laguerre(n, conjugate(alpha), conjugate(x))

    pytest.raises(ValueError, lambda: assoc_laguerre(-2.1, alpha, x))

    # issue sympy/sympy#10961
    X = assoc_laguerre(Rational(5, 2), alpha, x)
    assert isinstance(X, assoc_laguerre)
Пример #50
0
def test_heurisch_symbolic_coeffs():
    assert heurisch(1 / (x + y), x) == log(x + y)
    assert heurisch(1 / (x + sqrt(2)), x) == log(x + sqrt(2))
    assert simplify(diff(heurisch(log(x + y + z), y), y)) == log(x + y + z)
Пример #51
0
def test_derivative_subs_self_bug():
    d = diff(f(x), x)

    assert d.subs({d: y}) == y
Пример #52
0
def test_laguerre_2():
    # This fails due to issue for Sum, like issue sympy/sympy#5539
    assert diff(assoc_laguerre(n, alpha, x), alpha) == \
        Sum(assoc_laguerre(k, alpha, x)/(-alpha + n), (k, 0, n - 1))
Пример #53
0
def test_pde_separate_mul():
    x, y, z, t = symbols('x,y,z,t')
    c = Symbol('C', extended_real=True)
    Phi = Function('Phi')
    F, R, T, X, Y, Z, u = map(Function, 'FRTXYZu')
    r, theta, z = symbols('r,theta,z')

    # Something simple :)
    eq = Eq(diff(F(x, y, z), x) + diff(F(x, y, z), y) + diff(F(x, y, z), z), 0)

    # Duplicate arguments in functions
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(x), u(z, z)]))
    # Wrong number of arguments
    pytest.raises(ValueError,
                  lambda: pde_separate_mul(eq, F(x, y, z), [X(x), Y(y)]))
    # Wrong variables: [x, y] -> [x, z]
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(t), Y(x, y)]))

    assert pde_separate_mul(eq, F(x, y, z), [Y(y), u(x, z)]) == \
        [diff(Y(y), y)/Y(y), -diff(u(x, z), x)/u(x, z) - diff(u(x, z), z)/u(x, z)]
    assert pde_separate_mul(eq, F(x, y, z), [X(x), Y(y), Z(z)]) == \
        [diff(X(x), x)/X(x), -diff(Z(z), z)/Z(z) - diff(Y(y), y)/Y(y)]

    # wave equation
    wave = Eq(diff(u(x, t), t, t), c**2 * diff(u(x, t), x, x))
    res = pde_separate_mul(wave, u(x, t), [X(x), T(t)])
    assert res == [diff(X(x), x, x) / X(x), diff(T(t), t, t) / (c**2 * T(t))]

    # Laplace equation in cylindrical coords
    eq = Eq(
        1 / r * diff(Phi(r, theta, z), r) + diff(Phi(r, theta, z), r, 2) +
        1 / r**2 * diff(Phi(r, theta, z), theta, 2) +
        diff(Phi(r, theta, z), z, 2), 0)
    # Separate z
    res = pde_separate_mul(eq, Phi(r, theta, z), [Z(z), u(theta, r)])
    assert res == [
        diff(Z(z), z, z) / Z(z), -diff(u(theta, r), r, r) / u(theta, r) -
        diff(u(theta, r), r) / (r * u(theta, r)) -
        diff(u(theta, r), theta, theta) / (r**2 * u(theta, r))
    ]
    # Lets use the result to create a new equation...
    eq = Eq(res[1], c)
    # ...and separate theta...
    res = pde_separate_mul(eq, u(theta, r), [T(theta), R(r)])
    assert res == [
        diff(T(theta), theta, theta) / T(theta),
        -r * diff(R(r), r) / R(r) - r**2 * diff(R(r), r, r) / R(r) - c * r**2
    ]
    # ...or r...
    res = pde_separate_mul(eq, u(theta, r), [R(r), T(theta)])
    assert res == [
        r * diff(R(r), r) / R(r) + r**2 * diff(R(r), r, r) / R(r) + c * r**2,
        -diff(T(theta), theta, theta) / T(theta)
    ]

    # Laplace eq in spherical coordinates
    r, phi, theta, C1 = symbols('r,phi,theta,C1')
    Xi = Function('Xi')
    R, Phi, Theta, u = map(Function, ['R', 'Phi', 'Theta', 'u'])
    eq = Eq(
        diff(Xi(r, phi, theta), r, 2) + 2 / r * diff(Xi(r, phi, theta), r) +
        1 / (r**2 * sin(phi)**2) * diff(Xi(r, phi, theta), theta, 2) +
        cos(phi) / (r**2 * sin(phi)) * diff(Xi(r, phi, theta), phi) +
        1 / r**2 * diff(Xi(r, phi, theta), phi, 2), 0)
    res_theta = pde_separate(eq, Xi(r, phi, theta), [Theta(theta), u(r, phi)])
    eq_left = Eq(res_theta[1], -C1)
    res_theta = pde_separate(eq_left, u(r, phi), [Phi(phi), R(r)])
    assert (res_theta == [
        C1 / sin(phi)**2 - diff(Phi(phi), phi, phi) / Phi(phi) -
        diff(Phi(phi), phi) / (Phi(phi) * tan(phi)),
        r**2 * diff(R(r), r, r) / R(r) + 2 * r * diff(R(r), r) / R(r)
    ])

    # coverage tests
    assert pde_separate_mul(Eq(u(x, t).diff(x),
                               u(x, t).diff(x, t)), u(x, t),
                            [X(x), T(t)]) == [-1, -T(t) / diff(T(t), t)]
    assert pde_separate(Eq((x + t) * u(x, t).diff(x),
                           u(x, t).diff(t)),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x),
                           u(x, t).diff(t) + t),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x), exp(u(x, t).diff(t))),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
Пример #54
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
Пример #55
0
def test_derivative_evaluate():
    assert Derivative(sin(x), x) != diff(sin(x), x)
    assert Derivative(sin(x), x).doit() == diff(sin(x), x)

    assert Derivative(Derivative(f(x), x), x) == diff(f(x), x, x)
    assert Derivative(sin(x), x, 0) == sin(x)
Пример #56
0
def test_sympyissue_3740():
    f = 4 * log(x) - 2 * log(x)**2
    fid = diff(integrate(f, x), x)
    assert abs(f.subs(x, 42).evalf() - fid.subs(x, 42).evalf()) < 1e-10
Пример #57
0
def test_integrate_functions_1():
    assert integrate(f(x) * diff(f(x), x), x) == f(x)**2 / 2
    assert integrate(diff(f(x), x) / f(x), x) == log(f(x))
Пример #58
0
def test_sympyissue_5948():
    assert trigsimp(diff(integrate(cos(x) / sin(x)**7, x),
                         x)) == cos(x) / sin(x)**7
Пример #59
0
 def vdiff(f, y):
     return diff(f, y)
Пример #60
0
def test_basics():
    assert Integral(0, x) != 0
    assert Integral(x, (x, 1, 1)) != 0
    assert Integral(oo, x) != oo
    assert Integral(nan, x) == nan

    assert diff(Integral(y, y), x) == 0
    assert diff(Integral(x, (x, 0, 1)), x) == 0
    assert diff(Integral(x, x), x) == x
    assert diff(Integral(t, (t, 0, x)), x) == x + Integral(0, (t, 0, x))

    e = (t + 1)**2
    assert diff(integrate(e, (t, 0, x)), x) == \
        diff(Integral(e, (t, 0, x)), x).doit().expand() == \
        ((1 + x)**2).expand()
    assert diff(integrate(e, (t, 0, x)), t) == \
        diff(Integral(e, (t, 0, x)), t) == 0
    assert diff(integrate(e, (t, 0, x)), a) == \
        diff(Integral(e, (t, 0, x)), a) == 0
    assert diff(integrate(e, t), a) == diff(Integral(e, t), a) == 0

    assert integrate(e, (t, a, x)).diff(x) == \
        Integral(e, (t, a, x)).diff(x).doit().expand()
    assert Integral(e, (t, a, x)).diff(x).doit() == ((1 + x)**2)
    assert integrate(e, (t, x, a)).diff(x).doit() == (-(1 + x)**2).expand()

    assert integrate(t**2, (t, x, 2 * x)).diff(x) == 7 * x**2

    assert Integral(x, x).atoms() == {x}
    assert Integral(f(x), (x, 0, 1)).atoms() == {0, 1, x}

    assert diff_test(Integral(x, (x, 3 * y))) == {y}
    assert diff_test(Integral(x, (a, 3 * y))) == {x, y}

    assert integrate(x, (x, oo, oo)) == 0  # issue sympy/sympy#8171
    assert integrate(x, (x, -oo, -oo)) == 0

    # sum integral of terms
    assert integrate(y + x + exp(x), x) == x * y + x**2 / 2 + exp(x)

    assert Integral(x).is_commutative
    n = Symbol('n', commutative=False)
    assert Integral(n + x, x).is_commutative is False