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)
def test_func_deriv(): assert f(x).diff(x) == Derivative(f(x), x) # issue sympy/sympy#4534 assert f(x, y).diff(x, y) - f(x, y).diff(y, x) == 0 assert Derivative(f(x, y), x, y).args[1:] == (x, y) assert Derivative(f(x, y), y, x).args[1:] == (y, x) assert (Derivative(f(x, y), x, y) - Derivative(f(x, y), y, x)).doit() == 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)
def test_functional_diffgeom_ch3(): x0, y0 = symbols('x0, y0', extended_real=True) x, y, t = symbols('x, y, t', extended_real=True) f = Function('f') b1 = Function('b1') b2 = Function('b2') p_r = R2_r.point([x0, y0]) s_field = f(R2.x, R2.y) v_field = b1(R2.x) * R2.e_x + b2(R2.y) * R2.e_y assert v_field.rcall(s_field).rcall(p_r).doit() == b1(x0) * Derivative( f(x0, y0), x0) + b2(y0) * Derivative(f(x0, y0), y0) assert R2.e_x(R2.r**2).rcall(p_r) == 2 * x0 v = R2.e_x + 2 * R2.e_y s = R2.r**2 + 3 * R2.x assert v.rcall(s).rcall(p_r).doit() == 2 * x0 + 4 * y0 + 3 circ = -R2.y * R2.e_x + R2.x * R2.e_y series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True) series_x, series_y = zip(*series) assert all(term == cos(t).taylor_term(i, t) for i, term in enumerate(series_x)) assert all(term == sin(t).taylor_term(i, t) for i, term in enumerate(series_y))
def test_sympyissue_5183(): assert abs(x + x**2).series(n=1) == O(x) assert abs(x + x**2).series(n=2) == x + O(x**2) assert ((1 + x)**2).series(x, n=6) == 1 + 2 * x + x**2 assert (1 + 1 / x).series() == 1 + 1 / x assert Derivative(exp(x).series(), x).doit() == \ 1 + x + x**2/2 + x**3/6 + x**4/24 + Derivative(O(x**6), x)
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)
def test_Subs(): assert Subs(x, x, 0) == Subs(y, y, 0) assert Subs(x, x, 0).subs(x, 1) == Subs(x, x, 0) assert Subs(y, x, 0).subs(y, 1) == Subs(1, x, 0) assert Subs(f(x), x, 0).doit() == f(0) assert Subs(f(x**2), x**2, 0).doit() == f(0) assert Subs(f(x, y, z), (x, y, z), (0, 1, 1)) != \ Subs(f(x, y, z), (x, y, z), (0, 0, 1)) assert Subs(f(x, y), (x, y, z), (0, 1, 1)) == \ Subs(f(x, y), (x, y, z), (0, 1, 2)) assert Subs(f(x, y), (x, y, z), (0, 1, 1)) != \ Subs(f(x, y) + z, (x, y, z), (0, 1, 0)) assert Subs(f(x, y), (x, y), (0, 1)).doit() == f(0, 1) assert Subs(Subs(f(x, y), x, 0), y, 1).doit() == f(0, 1) pytest.raises(ValueError, lambda: Subs(f(x, y), (x, y), (0, 0, 1))) pytest.raises(ValueError, lambda: Subs(f(x, y), (x, x, y), (0, 0, 1))) assert len(Subs(f(x, y), (x, y), (0, 1)).variables) == 2 assert Subs(f(x, y), (x, y), (0, 1)).point == Tuple(0, 1) assert Subs(f(x), x, 0) == Subs(f(y), y, 0) assert Subs(f(x, y), (x, y), (0, 1)) == Subs(f(x, y), (y, x), (1, 0)) assert Subs(f(x) * y, (x, y), (0, 1)) == Subs(f(y) * x, (y, x), (0, 1)) assert Subs(f(x) * y, (x, y), (1, 1)) == Subs(f(y) * x, (x, y), (1, 1)) assert Subs(f(x), x, 0).subs(x, 1).doit() == f(0) assert Subs(f(x), x, y).subs(y, 0) == Subs(f(x), x, 0) assert Subs(y * f(x), x, y).subs(y, 2) == Subs(2 * f(x), x, 2) assert (2 * Subs(f(x), x, 0)).subs(Subs(f(x), x, 0), y) == 2 * y assert Subs(f(x), x, 0).free_symbols == set() assert Subs(f(x, y), x, z).free_symbols == {y, z} assert Subs(f(x).diff(x), x, 0).doit(), Subs(f(x).diff(x), x, 0) assert Subs(1 + f(x).diff(x), x, 0).doit(), 1 + Subs(f(x).diff(x), x, 0) assert Subs(y*f(x, y).diff(x), (x, y), (0, 2)).doit() == \ 2*Subs(Derivative(f(x, 2), x), x, 0) assert Subs(y**2 * f(x), x, 0).diff(y) == 2 * y * f(0) e = Subs(y**2 * f(x), x, y) assert e.diff(y) == e.doit().diff( y) == y**2 * Derivative(f(y), y) + 2 * y * f(y) assert Subs(f(x), x, 0) + Subs(f(x), x, 0) == 2 * Subs(f(x), x, 0) e1 = Subs(z * f(x), x, 1) e2 = Subs(z * f(y), y, 1) assert e1 + e2 == 2 * e1 assert e1.__hash__() == e2.__hash__() assert Subs(z * f(x + 1), x, 1) not in [e1, e2] assert Derivative(f(x), x).subs(x, g(x)) == Subs(Derivative(f(x), x), (x, ), (g(x), )) assert Subs(f(x)*cos(y) + z, (x, y), (0, pi/3)).n(2) == \ Subs(f(x)*cos(y) + z, (x, y), (0, pi/3)).evalf(2) == \ z + Rational('1/2').n(2)*f(0) assert f(x).diff(x).subs(x, 0).subs(x, y) == f(x).diff(x).subs(x, 0) assert (x * f(x).diff(x).subs(x, 0)).subs( x, y) == y * f(x).diff(x).subs(x, 0)
def test_diofantissue_376(): f = symbols('f', cls=Function) e1 = Subs(Derivative(f(x), x), (x, y * z)) e2 = Subs(Derivative(f(t), t), (t, y * z)) assert (x * e1).subs({e2: y}) == x * y e3 = Subs(Derivative(f(t), t), (t, y * z**2)) assert e3.subs({e2: y}) == e3 e4 = Subs(Derivative(f(t), t, t), (t, y * z)) assert e4.subs({e2: y}) == e4
def test_derivative_bug1(): f = Function("f") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = expr.match(pattern.xreplace(d1)) assert d2 is None
def test_derivative_bug1(): f = Function('f') a = Wild('a', exclude=[f, x]) b = Wild('b', exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = expr.match(pattern.xreplace(d1)) assert d2 is None
def test_unhandled(): class MyExpr(Expr): def _eval_derivative(self, s): if not s.name.startswith('xi'): return self 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)
def test_derivative_bug1(): f = Function("f") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = pattern.xreplace(d1).matches(expr, d1) assert d2 is None
def test_vector_diff_integrate(): f = Function('f') v = f(a)*C.i + a**2*C.j - C.k 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)
def test_diofantissue_376(): x, y, z, t = symbols('x y z t') f = symbols('f', cls=Function) e1 = Subs(Derivative(f(x), x), x, y * z) e2 = Subs(Derivative(f(t), t), t, y * z) assert (x * e1).subs(e2, y) == x * y e3 = Subs(Derivative(f(t), t), t, y * z**2) assert e3.subs(e2, y) == e3 e4 = Subs(Derivative(f(t), t, t), t, y * z) assert e4.subs(e2, y) == e4
def test_simultaneous_subs(): reps = {x: 0, y: 0} assert (x / y).subs(reps) != (y / x).subs(reps) assert (x/y).subs(reps, simultaneous=True) == \ (y/x).subs(reps, simultaneous=True) reps = reps.items() assert (x / y).subs(reps) != (y / x).subs(reps) assert (x/y).subs(reps, simultaneous=True) == \ (y/x).subs(reps, simultaneous=True) assert Derivative(x, y, z).subs(reps, simultaneous=True) == \ Subs(Derivative(0, y, z), (y, 0))
def test_derivative1(): p, q = map(Wild, 'pq') f = Function('f', nargs=1) fd = Derivative(f(x), x) assert fd.match(p) == {p: fd} assert (fd + 1).match(p + 1) == {p: fd} assert fd.match(fd) == {} assert (3 * fd).match(p * fd) is not None assert (3 * fd - 1).match(p * fd + q) == {p: 3, q: -1}
def test_derivative1(): p, q = map(Wild, 'pq') f = Function('f', nargs=1) fd = Derivative(f(x), x) assert fd.match(p) == {p: fd} assert (fd + 1).match(p + 1) == {p: fd} assert fd.match(fd) == {} assert (3*fd).match(p*fd) is not None assert (3*fd - 1).match(p*fd + q) == {p: 3, q: -1}
def test_sympyissue_7231(): ans1 = f(x).series(x, a) _xi_1 = ans1.atoms(Dummy).pop() res = (f(a) + (-a + x)*Subs(Derivative(f(_xi_1), _xi_1), (_xi_1, a)) + (-a + x)**2*Subs(Derivative(f(_xi_1), _xi_1, _xi_1), (_xi_1, a))/2 + (-a + x)**3*Subs(Derivative(f(_xi_1), _xi_1, _xi_1, _xi_1), (_xi_1, a))/6 + (-a + x)**4*Subs(Derivative(f(_xi_1), _xi_1, _xi_1, _xi_1, _xi_1), (_xi_1, a))/24 + (-a + x)**5*Subs(Derivative(f(_xi_1), _xi_1, _xi_1, _xi_1, _xi_1, _xi_1), (_xi_1, a))/120 + O((-a + x)**6, (x, a))) assert res == ans1 ans2 = f(x).series(x, a) assert res == ans2
def test_simple_1(): assert x.series(x, n=5) == x assert y.series(x, n=5) == y assert (1/(x*y)).series(y, n=5) == 1/(x*y) assert Rational(3, 4).series(x, n=5) == Rational(3, 4) assert x.series(x) == x # issue sympy/sympy#5183 assert ((1 + x)**2).series(x) == 1 + 2*x + x**2 assert (1 + 1/x).series() == 1 + 1/x assert (Derivative(exp(x).series(), x).doit() == 1 + x + x**2/2 + x**3/6 + x**4/24 + Derivative(O(x**6), x))
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)
def test_diff_wrt(): fx = f(x) dfx = diff(f(x), x) ddfx = diff(f(x), x, x) assert diff(sin(fx) + fx**2, fx) == cos(fx) + 2 * fx assert diff(sin(dfx) + dfx**2, dfx) == cos(dfx) + 2 * dfx assert diff(sin(ddfx) + ddfx**2, ddfx) == cos(ddfx) + 2 * ddfx assert diff(fx**2, dfx) == 0 assert diff(fx**2, ddfx) == 0 assert diff(dfx**2, fx) == 0 assert diff(dfx**2, ddfx) == 0 assert diff(ddfx**2, dfx) == 0 assert diff(fx * dfx * ddfx, fx) == dfx * ddfx assert diff(fx * dfx * ddfx, dfx) == fx * ddfx assert diff(fx * dfx * ddfx, ddfx) == fx * dfx assert diff(f(x), x).diff(f(x)) == 0 assert (sin(f(x)) - cos(diff(f(x), x))).diff(f(x)) == cos(f(x)) assert diff(sin(fx), fx, x) == diff(sin(fx), x, fx) # Chain rule cases assert f(g(x)).diff(x) == \ Subs(Derivative(f(x), x), (x, g(x)))*Derivative(g(x), x) assert diff(f(g(x), h(x)), x) == \ Subs(Derivative(f(y, h(x)), y), (y, g(x)))*Derivative(g(x), x) + \ Subs(Derivative(f(g(x), y), y), (y, h(x)))*Derivative(h(x), x) assert f(sin(x)).diff(x) == Subs(Derivative(f(x), x), (x, sin(x))) * cos(x) assert diff(f(g(x)), g(x)) == Subs(Derivative(f(x), x), (x, g(x)))
def test_derivatives_sympyissue_4757(): x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert re(f(x)).diff(x) == re(f(x).diff(x)) assert im(f(x)).diff(x) == im(f(x).diff(x)) assert re(f(y)).diff(y) == -I*im(f(y).diff(y)) assert im(f(y)).diff(y) == -I*re(f(y).diff(y)) assert re(f(z)).diff(z) == Derivative(re(f(z)), z) assert im(f(z)).diff(z) == Derivative(im(f(z)), z) assert Abs(f(x)).diff(x).subs(f(x), 1 + I*x).doit() == x/sqrt(1 + x**2) assert arg(f(x)).diff(x).subs(f(x), 1 + I*x**2).doit() == 2*x/(1 + x**4) assert Abs(f(y)).diff(y).subs(f(y), 1 + y).doit() == -y/sqrt(1 - y**2) assert arg(f(y)).diff(y).subs(f(y), I + y**2).doit() == 2*y/(1 + y**4)
def test_sympyissue_12005(): e1 = Subs(Derivative(f(x), x), (x, x)) assert e1.diff(x) == Derivative(f(x), x, x) e2 = Subs(Derivative(f(x), x), (x, x**2 + 1)) assert e2.diff(x) == 2 * x * Subs(Derivative(f(x), x, x), (x, x**2 + 1)) e3 = Subs(Derivative(f(x) + y**2 - y, y), (y, y**2)) assert e3.diff(y) == 4 * y e4 = Subs(Derivative(f(x + y), y), (y, x**2)) assert e4.diff(y) == 0 e5 = Subs(Derivative(f(x), x), (y, y), (z, z)) assert e5.diff(x) == Derivative(f(x), x, x) assert f(g(x)).diff(g(x), g(x)) == Subs(Derivative(f(y), y, y), (y, g(x)))
def test_conjugate(): a = Symbol('a', extended_real=True) b = Symbol('b', imaginary=True) assert conjugate(a) == a assert conjugate(I * a) == -I * a assert conjugate(b) == -b assert conjugate(I * b) == I * b assert conjugate(a * b) == -a * b assert conjugate(I * a * b) == I * a * b x = Symbol('x') y = Symbol('y') assert conjugate(conjugate(x)) == x assert conjugate(x + y) == conjugate(x) + conjugate(y) assert conjugate(x - y) == conjugate(x) - conjugate(y) assert conjugate(x * y) == conjugate(x) * conjugate(y) assert conjugate(x / y) == conjugate(x) / conjugate(y) assert conjugate(-x) == -conjugate(x) 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 conjugate(z).diff(z) == Derivative(conjugate(z), z) # issue sympy/sympy#4754 x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert (f(x).conjugate()).diff(x) == (f(x).diff(x)).conjugate() assert (f(y).conjugate()).diff(y) == -(f(y).diff(y)).conjugate()
def test_simple(): # issue sympy/sympy#5223 assert Integer(1).series(x) == 1 pytest.raises(ValueError, lambda: cos(x + y).series()) pytest.raises(ValueError, lambda: x.series(dir='')) pytest.raises(ValueError, lambda: x.series(dir=0)) assert Derivative(x**2 + x**3 * y**2, (x, 2), (y, 1)).series(x).simplify() == 12 * x * y + O(x**6) assert (1 + x).getn() is None # issue sympy/sympy#8805 assert Integer(1).series(n=8) == 1 # issue sympy/sympy#5223 assert (cos(x).series(x, 1) - cos(x + 1).series(x).subs({x: x - 1})).removeO() == 0 assert abs(x).series(x, oo, n=5, dir=-1) == x assert abs(x).series(x, -oo, n=5, dir=+1) == -x assert abs(-x).series(x, oo, n=5, dir=-1) == x assert abs(-x).series(x, -oo, n=5, dir=+1) == -x # issue sympy/sympy#7203 assert cos(x).series(x, pi, 3) == -1 + (x - pi)**2 / 2 + O((x - pi)**3, (x, pi))
def test_hyper(): pytest.raises(TypeError, lambda: hyper(1, 2, z)) assert hyper((1, 2), (1, ), z) == hyper(Tuple(1, 2), Tuple(1), z) h = hyper((1, 2), (3, 4, 5), z) assert h.ap == Tuple(1, 2) assert h.bq == Tuple(3, 4, 5) assert h.argument == z assert h.is_commutative is True # just a few checks to make sure that all arguments go where they should assert tn(hyper(Tuple(), Tuple(), z), exp(z), z) assert tn(z * hyper((1, 1), Tuple(2), -z), log(1 + z), z) # differentiation h = hyper((randcplx(), randcplx(), randcplx()), (randcplx(), randcplx()), z) assert td(h, z) a1, a2, b1, b2, b3 = symbols('a1:3, b1:4') assert hyper((a1, a2), (b1, b2, b3), z).diff(z) == \ a1*a2/(b1*b2*b3) * hyper((a1 + 1, a2 + 1), (b1 + 1, b2 + 1, b3 + 1), z) # differentiation wrt parameters is not supported assert hyper([z], [], z).diff(z) == Derivative(hyper([z], [], z), z) # hyper is unbranched wrt parameters assert hyper([polar_lift(z)], [polar_lift(k)], polar_lift(x)) == \ hyper([z], [k], polar_lift(x)) assert hyper((1, 2, 3), [3, 4], 1).is_number assert not hyper((1, 2, 3), [3, x], 1).is_number
def test_derivative_by_array(): bexpr = x * y**2 * exp(z) * log(t) sexpr = sin(bexpr) cexpr = cos(bexpr) a = Array([sexpr]) assert derive_by_array( sexpr, t) == x * y**2 * exp(z) * cos(x * y**2 * exp(z) * log(t)) / t assert derive_by_array(sexpr, [x, y, z]) == Array( [bexpr / x * cexpr, 2 * y * bexpr / y**2 * cexpr, bexpr * cexpr]) assert derive_by_array(a, [x, y, z]) == Array([[bexpr / x * cexpr], [2 * y * bexpr / y**2 * cexpr], [bexpr * cexpr]]) assert derive_by_array(sexpr, [[x, y], [z, t]]) == Array( [[bexpr / x * cexpr, 2 * y * bexpr / y**2 * cexpr], [bexpr * cexpr, bexpr / log(t) / t * cexpr]]) assert derive_by_array(a, [[x, y], [z, t]]) == Array( [[[bexpr / x * cexpr], [2 * y * bexpr / y**2 * cexpr]], [[bexpr * cexpr], [bexpr / log(t) / t * cexpr]]]) assert derive_by_array([[x, y], [z, t]], [x, y]) == Array([[[1, 0], [0, 0]], [[0, 1], [0, 0]]]) assert derive_by_array([[x, y], [z, t]], [[x, y], [z, t]]) == Array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]]) pytest.raises(ValueError, lambda: derive_by_array(x, [Derivative(x**2, x)]))
def test_Derivative_bug1(): f = Function("f") x = abc.x a = Wild("a", exclude=[f(x)]) b = Wild("b", exclude=[f(x)]) eq = f(x).diff(x) assert eq.match(a * Derivative(f(x), x) + b) == {a: 1, b: 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) - S.Half) 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, S.Half, S.Half, x) == RisingFactorial(Rational( 3, 2), n) * chebyshevu(n, x) / factorial(n + 1) assert jacobi(n, -S.Half, -S.Half, 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 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))
def test_derivatives(): assert zeta(x, a).diff(x) == Derivative(zeta(x, a), x) assert zeta(x, a).diff(a) == -x * zeta(x + 1, a) assert zeta(z).diff(z) == Derivative(zeta(z), z) assert lerchphi( z, s, a).diff(z) == (lerchphi(z, s - 1, a) - a * lerchphi(z, s, a)) / z pytest.raises(ArgumentIndexError, lambda: lerchphi(z, s, a).fdiff(4)) assert lerchphi(z, s, a).diff(a) == -s * lerchphi(z, s + 1, a) assert polylog(s, z).diff(z) == polylog(s - 1, z) / z pytest.raises(ArgumentIndexError, lambda: polylog(s, z).fdiff(3)) b = randcplx() c = randcplx() assert td(zeta(b, x), x) assert td(polylog(b, z), z) assert td(lerchphi(c, b, x), x) assert td(lerchphi(x, b, c), x)
def test_eval_derivative(): assert Sum(x, (x, 1, 2)).diff(x) == 0 assert Sum(x * y, (x, 1, 2)).diff(x) == 0 assert Sum(x * y, (y, 1, 2)).diff(x) == Sum(y, (y, 1, 2)) e = Sum(x * y, (x, 1, a)) assert e.diff(a) == Derivative(e, a) assert Sum(x*y, (x, 1, 3), (a, 2, 5)).diff(y) == \ Sum(x*y, (x, 1, 3), (a, 2, 5)).doit().diff(y) == 24
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)}
def test_derivative2(): f = Function("f") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) e = Derivative(f(x), x) assert e.match(Derivative(f(x), x)) == {} assert e.match(Derivative(f(x), x, x)) is None e = Derivative(f(x), x, x) assert e.match(Derivative(f(x), x)) is None assert e.match(Derivative(f(x), x, x)) == {} e = Derivative(f(x), x) + x**2 assert e.match(a*Derivative(f(x), x) + b) == {a: 1, b: x**2} assert e.match(a*Derivative(f(x), x, x) + b) is None e = Derivative(f(x), x, x) + x**2 assert e.match(a*Derivative(f(x), x) + b) is None assert e.match(a*Derivative(f(x), x, x) + b) == {a: 1, b: x**2}
def test_sympyissue_11313(): # test Derivative series & as_leading_term assert Derivative(x**3 + x**4, x).as_leading_term(x).doit() == 3*x**2 s = Derivative(sin(x), x).series(x, n=3) assert s == Derivative(-x**3/6, x) + Derivative(x, x) + O(x**3) assert s.doit() == 1 - x**2/2 + O(x**3)
def test_derivative_subs3(): x = Symbol('x') dex = Derivative(exp(x), x) assert Derivative(dex, x).subs({dex: exp(x)}) == dex assert dex.subs({exp(x): dex}) == Derivative(exp(x), x, x)
def test_deriv_sub_bug3(): y = Symbol('y') f = Function('f') pat = Derivative(f(x), x, x) assert pat.subs({y: y**2}) == Derivative(f(x), x, x) assert pat.subs({y: y**2}) != Derivative(f(x), x)
def test_doit(): n = Symbol('n', integer=True) f = Sum(2 * n * x, (n, 1, 3)) d = Derivative(f, x) assert d.doit() == 12 assert d.doit(deep=False) == Sum(2*n, (n, 1, 3))