def test_deriv_sub_bug3(): x = Symbol("x") 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_deriv_sub_bug3(): x = Symbol('x') 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 _main(expr): _new = [] for a in expr.args: is_V = False if isinstance(a, V): is_V = True a = a.expr if a.is_Function: name = a.__class__.__name__ for i in a.args: if i.is_Function: func = i break if ',' in name: variables = [eval(i) for i in name.split(',')[1]] a = Derivative(func, *variables) if 'V' in name: #TODO remove this V and use class a = V(a) a.function = func #TODO add more, maybe all that have args elif a.is_Add or a.is_Mul or a.is_Pow: a = _main(a) if is_V: a = V(a) a.function = func _new.append( a ) return expr.func(*tuple(_new))
def test_subs_in_derivative(): expr = sin(x*exp(y)) u = Function('u') v = Function('v') assert Derivative(expr, y).subs(expr, y) == Derivative(y, y) assert Derivative(expr, y).subs(y, x).doit() == \ Derivative(expr, y).doit().subs(y, x) assert Derivative(f(x, y), y).subs(y, x) == Subs(Derivative(f(x, y), y), y, x) assert Derivative(f(x, y), y).subs(x, y) == Subs(Derivative(f(x, y), y), x, y) assert Derivative(f(x, y), y).subs(y, g(x, y)) == Subs(Derivative(f(x, y), y), y, g(x, y)).doit() assert Derivative(f(x, y), y).subs(x, g(x, y)) == Subs(Derivative(f(x, y), y), x, g(x, y)) assert Derivative(f(x, y), g(y)).subs(x, g(x, y)) == Derivative(f(g(x, y), y), g(y)) assert Derivative(f(u(x), h(y)), h(y)).subs(h(y), g(x, y)) == \ Subs(Derivative(f(u(x), h(y)), h(y)), h(y), g(x, y)).doit() assert Derivative(f(x, y), y).subs(y, z) == Derivative(f(x, z), z) assert Derivative(f(x, y), y).subs(y, g(y)) == Derivative(f(x, g(y)), g(y)) assert Derivative(f(g(x), h(y)), h(y)).subs(h(y), u(y)) == \ Derivative(f(g(x), u(y)), u(y)) assert Derivative(f(x, f(x, x)), f(x, x)).subs( f, Lambda((x, y), x + y)) == Subs( Derivative(z + x, z), z, 2*x) assert Subs(Derivative(f(f(x)), x), f, cos).doit() == sin(x)*sin(cos(x)) assert Subs(Derivative(f(f(x)), f(x)), f, cos).doit() == -sin(cos(x)) # Issue 13791. No comparison (it's a long formula) but this used to raise an exception. assert isinstance(v(x, y, u(x, y)).diff(y).diff(x).diff(y), Expr) # This is also related to issues 13791 and 13795; issue 15190 F = Lambda((x, y), exp(2*x + 3*y)) abstract = f(x, f(x, x)).diff(x, 2) concrete = F(x, F(x, x)).diff(x, 2) assert (abstract.subs(f, F).doit() - concrete).simplify() == 0 # don't introduce a new symbol if not necessary assert x in f(x).diff(x).subs(x, 0).atoms() # case (4) assert Derivative(f(x,f(x,y)), x, y).subs(x, g(y) ) == Subs(Derivative(f(x, f(x, y)), x, y), x, g(y)) assert Derivative(f(x, x), x).subs(x, 0 ) == Subs(Derivative(f(x, x), x), x, 0) # issue 15194 assert Derivative(f(y, g(x)), (x, z)).subs(z, x ) == Derivative(f(y, g(x)), (x, x)) df = f(x).diff(x) assert df.subs(df, 1) is S.One assert df.diff(df) is S.One dxy = Derivative(f(x, y), x, y) dyx = Derivative(f(x, y), y, x) assert dxy.subs(Derivative(f(x, y), y, x), 1) is S.One assert dxy.diff(dyx) is S.One assert Derivative(f(x, y), x, 2, y, 3).subs( dyx, g(x, y)) == Derivative(g(x, y), x, 1, y, 2) assert Derivative(f(x, x - y), y).subs(x, x + y) == Subs( Derivative(f(x, x - y), y), x, x + y)
def test_ODE_1(): l = Function('l') r = Symbol('r') e = Derivative(l(r),r)/r+Derivative(l(r),r,r)/2- \ Derivative(l(r),r)**2/2 sol = dsolve(e, [l(r)]) assert (e.subs(l(r), sol)).expand() == 0 e = e*exp(-l(r))/exp(l(r)) sol = dsolve(e, [l(r)]) assert (e.subs(l(r), sol)).expand() == 0
def test_derivative1(): x, y = map(Symbol, 'xy') 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(): x,y = map(Symbol, 'xy') 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) != None p = Wild("p", exclude=[x]) q = Wild("q", exclude=[x]) assert (3*fd-1).match(p*fd + q) == {p: 3, q: -1}
def test_derivative_subs(): f = Function('f') g = Function('g') assert Derivative(f(x), x).subs(f(x), y) != 0 # need xreplace to put the function back, see #13803 assert Derivative(f(x), x).subs(f(x), y).xreplace({y: f(x)}) == \ Derivative(f(x), x) # issues 5085, 5037 assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative) assert cse(Derivative(f(x, y), x) + Derivative(f(x, y), y))[1][0].has(Derivative) eq = Derivative(g(x), g(x)) assert eq.subs(g, f) == Derivative(f(x), f(x)) assert eq.subs(g(x), f(x)) == Derivative(f(x), f(x)) assert eq.subs(g, cos) == Subs(Derivative(y, y), y, cos(x))
class invalid_units_derivative(Equation): expr = Eq(demo_g, Derivative(demo_d, demo_fall.definition.t))
def test_classify_ode_ics(): # Dummy eq = f(x).diff(x, x) - f(x) # Not f(0) or f'(0) ics = {x: 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) ############################ # f(0) type (AppliedUndef) # ############################ # Wrong function ics = {g(0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Contains x ics = {f(x): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Too many args ics = {f(0, 0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # point contains f # XXX: Should be NotImplementedError ics = {f(0): f(1)} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Does not raise ics = {f(0): 1} classify_ode(eq, f(x), ics=ics) ##################### # f'(0) type (Subs) # ##################### # Wrong function ics = {g(x).diff(x).subs(x, 0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Contains x ics = {f(y).diff(y).subs(y, x): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Wrong variable ics = {f(y).diff(y).subs(y, 0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Too many args ics = {f(x, y).diff(x).subs(x, 0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Derivative wrt wrong vars ics = {Derivative(f(x), x, y).subs(x, 0): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # point contains f # XXX: Should be NotImplementedError ics = {f(x).diff(x).subs(x, 0): f(0)} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Does not raise ics = {f(x).diff(x).subs(x, 0): 1} classify_ode(eq, f(x), ics=ics) ########################### # f'(y) type (Derivative) # ########################### # Wrong function ics = {g(x).diff(x).subs(x, y): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Contains x ics = {f(y).diff(y).subs(y, x): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Too many args ics = {f(x, y).diff(x).subs(x, y): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Derivative wrt wrong vars ics = {Derivative(f(x), x, z).subs(x, y): 1} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # point contains f # XXX: Should be NotImplementedError ics = {f(x).diff(x).subs(x, y): f(0)} raises(ValueError, lambda: classify_ode(eq, f(x), ics=ics)) # Does not raise ics = {f(x).diff(x).subs(x, y): 1} classify_ode(eq, f(x), ics=ics)
def test_deriv_sub_bug3(): 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_issue_15893(): f = Function('f', real=True) x = Symbol('x', real=True) eq = Derivative(Abs(f(x)), f(x)) assert eq.doit() == sign(f(x))
def test_Subs(): assert Subs(1, (), ()) is S.One # check null subs influence on hashing assert Subs(x, y, z) != Subs(x, y, 1) # neutral subs works assert Subs(x, x, 1).subs(x, y).has(y) # self mapping var/point assert Subs(Derivative(f(x), (x, 2)), x, x).doit() == f(x).diff(x, x) assert Subs(x, x, 0).has(x) # it's a structural answer assert not Subs(x, x, 0).free_symbols assert Subs(Subs(x + y, x, 2), y, 1) == Subs(x + y, (x, y), (2, 1)) assert Subs(x, (x, ), (0, )) == Subs(x, x, 0) 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(x, y, 2).subs(x, y).doit() == 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) raises(ValueError, lambda: Subs(f(x, y), (x, y), (0, 0, 1))) 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)) == Derivative(f(g(x)), g(x)) assert Derivative(f(x), x).subs(x, x + y) == Subs(Derivative(f(x), x), x, x + y) 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) assert Subs(Derivative(g(x)**2, g(x), x), g(x), exp(x)).doit() == 2 * exp(x) assert Subs(Derivative(g(x)**2, g(x), x), g(x), exp(x)).doit(deep=False) == 2 * Derivative(exp(x), x) assert Derivative( f(x, g(x)), x).doit() == Derivative(f(x, g(x)), g(x)) * Derivative(g(x), x) + Subs( Derivative(f(y, g(x)), y), y, x)
def test_issue_7027(): for wrt in (cos(x), re(x), Derivative(cos(x), x)): raises(ValueError, lambda: diff(f(x), wrt))
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) == Derivative(f(x, y, z), (x, 3)) assert diff(f(x, y, z), x, 3) == Derivative(f(x, y, z), x, 3) # issue 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) raises(TypeError, lambda: cos(x).diff((x, y)).variables) assert cos(x).diff((x, y))._wrt_variables == [x]
def _visit_eval_derivative_array(self, x): if x.has(self): return _matrix_derivative(x, self) else: from sympy import Derivative return Derivative(x, self)
def test_neq_nth_linear_constant_coeff_match(): x, y, z, w = symbols('x, y, z, w', cls=Function) t = Symbol('t') x1 = diff(x(t), t) y1 = diff(y(t), t) z1 = diff(z(t), t) w1 = diff(w(t), t) x2 = diff(x(t), t, t) funcs = [x(t), y(t)] funcs_2 = funcs + [z(t), w(t)] eqs_1 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * t * x(t) + 3 * y(t) + t)) assert neq_nth_linear_constant_coeff_match(eqs_1, funcs, t) is None # NOTE: Raises TypeError eqs_2 = (5 * (x1**2) + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * t * x(t) + 3 * y(t) + t)) assert neq_nth_linear_constant_coeff_match(eqs_2, funcs, t) is None eqs_3 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * x(t) + 3 * y(t)), (5 * w1 + z(t)), (z1 + w(t))) answer_3 = { 'no_of_equation': 4, 'eq': (12 * x(t) - 6 * y(t) + 5 * Derivative(x(t), t), -11 * x(t) + 3 * y(t) + 2 * Derivative(y(t), t), z(t) + 5 * Derivative(w(t), t), w(t) + Derivative(z(t), t)), 'func': [x(t), y(t), z(t), w(t)], 'order': { x(t): 1, y(t): 1, z(t): 1, w(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[Rational(12, 5), Rational(-6, 5), 0, 0], [Rational(-11, 2), Rational(3, 2), 0, 0], [0, 0, 0, 1], [0, 0, Rational(1, 5), 0]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_3, funcs_2, t) == answer_3 eqs_4 = (5 * x1 + 12 * x(t) - 6 * (y(t)), (2 * y1 - 11 * x(t) + 3 * y(t)), (z1 - w(t)), (w1 - z(t))) answer_4 = { 'no_of_equation': 4, 'eq': (12 * x(t) - 6 * y(t) + 5 * Derivative(x(t), t), -11 * x(t) + 3 * y(t) + 2 * Derivative(y(t), t), -w(t) + Derivative(z(t), t), -z(t) + Derivative(w(t), t)), 'func': [x(t), y(t), z(t), w(t)], 'order': { x(t): 1, y(t): 1, z(t): 1, w(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[Rational(12, 5), Rational(-6, 5), 0, 0], [Rational(-11, 2), Rational(3, 2), 0, 0], [0, 0, 0, -1], [0, 0, -1, 0]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_4, funcs_2, t) == answer_4 eqs_5 = (5 * x1 + 12 * x(t) - 6 * (y(t)) + x2, (2 * y1 - 11 * x(t) + 3 * y(t)), (z1 - w(t)), (w1 - z(t))) assert neq_nth_linear_constant_coeff_match(eqs_5, funcs_2, t) is None eqs_6 = (Eq(x1, 3 * y(t) - 11 * z(t)), Eq(y1, 7 * z(t) - 3 * x(t)), Eq(z1, 11 * x(t) - 7 * y(t))) answer_6 = { 'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 3 * y(t) - 11 * z(t)), Eq(Derivative(y(t), t), -3 * x(t) + 7 * z(t)), Eq(Derivative(z(t), t), 11 * x(t) - 7 * y(t))), 'func': [x(t), y(t), z(t)], 'order': { x(t): 1, y(t): 1, z(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[0, -3, 11], [3, 0, -7], [-11, 7, 0]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_6, funcs_2[:-1], t) == answer_6 eqs_7 = (Eq(x1, y(t)), Eq(y1, x(t))) answer_7 = { 'no_of_equation': 2, 'eq': (Eq(Derivative(x(t), t), y(t)), Eq(Derivative(y(t), t), x(t))), 'func': [x(t), y(t)], 'order': { x(t): 1, y(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[0, -1], [-1, 0]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_7, funcs, t) == answer_7 eqs_8 = (Eq(x1, 21 * x(t)), Eq(y1, 17 * x(t) + 3 * y(t)), Eq(z1, 5 * x(t) + 7 * y(t) + 9 * z(t))) answer_8 = { 'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 21 * x(t)), Eq(Derivative(y(t), t), 17 * x(t) + 3 * y(t)), Eq(Derivative(z(t), t), 5 * x(t) + 7 * y(t) + 9 * z(t))), 'func': [x(t), y(t), z(t)], 'order': { x(t): 1, y(t): 1, z(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[-21, 0, 0], [-17, -3, 0], [-5, -7, -9]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_8, funcs_2[:-1], t) == answer_8 eqs_9 = (Eq(x1, 4 * x(t) + 5 * y(t) + 2 * z(t)), Eq(y1, x(t) + 13 * y(t) + 9 * z(t)), Eq(z1, 32 * x(t) + 41 * y(t) + 11 * z(t))) answer_9 = { 'no_of_equation': 3, 'eq': (Eq(Derivative(x(t), t), 4 * x(t) + 5 * y(t) + 2 * z(t)), Eq(Derivative(y(t), t), x(t) + 13 * y(t) + 9 * z(t)), Eq(Derivative(z(t), t), 32 * x(t) + 41 * y(t) + 11 * z(t))), 'func': [x(t), y(t), z(t)], 'order': { x(t): 1, y(t): 1, z(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[-4, -5, -2], [-1, -13, -9], [-32, -41, -11]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_9, funcs_2[:-1], t) == answer_9 eqs_10 = (Eq(3 * x1, 4 * 5 * (y(t) - z(t))), Eq(4 * y1, 3 * 5 * (z(t) - x(t))), Eq(5 * z1, 3 * 4 * (x(t) - y(t)))) answer_10 = { 'no_of_equation': 3, 'eq': (Eq(3 * Derivative(x(t), t), 20 * y(t) - 20 * z(t)), Eq(4 * Derivative(y(t), t), -15 * x(t) + 15 * z(t)), Eq(5 * Derivative(z(t), t), 12 * x(t) - 12 * y(t))), 'func': [x(t), y(t), z(t)], 'order': { x(t): 1, y(t): 1, z(t): 1 }, 'is_linear': True, 'is_constant': True, 'is_homogeneous': True, 'func_coeff': Matrix([[0, Rational(-20, 3), Rational(20, 3)], [Rational(15, 4), 0, Rational(-15, 4)], [Rational(-12, 5), Rational(12, 5), 0]]), 'type_of_equation': 'type1' } assert neq_nth_linear_constant_coeff_match(eqs_10, funcs_2[:-1], t) == answer_10
def test_sysode_linear_neq_order1(): f, g, x, y, h = symbols('f g x y h', cls=Function) a, b, c, t = symbols('a b c t') eq1 = [Eq(x(t).diff(t), x(t)), Eq(y(t).diff(t), y(t))] sol1 = [Eq(x(t), C1 * exp(t)), Eq(y(t), C2 * exp(t))] assert dsolve(eq1) == sol1 assert checksysodesol(eq1, sol1) == (True, [0, 0]) eq2 = [Eq(x(t).diff(t), 2 * x(t)), Eq(y(t).diff(t), 3 * y(t))] #sol2 = [Eq(x(t), C1*exp(2*t)), Eq(y(t), C2*exp(3*t))] sol2 = [Eq(x(t), C1 * exp(2 * t)), Eq(y(t), C2 * exp(3 * t))] assert dsolve(eq2) == sol2 assert checksysodesol(eq2, sol2) == (True, [0, 0]) eq3 = [Eq(x(t).diff(t), a * x(t)), Eq(y(t).diff(t), a * y(t))] sol3 = [Eq(x(t), C1 * exp(a * t)), Eq(y(t), C2 * exp(a * t))] assert dsolve(eq3) == sol3 assert checksysodesol(eq3, sol3) == (True, [0, 0]) # Regression test case for issue #15474 # https://github.com/sympy/sympy/issues/15474 eq4 = [Eq(x(t).diff(t), a * x(t)), Eq(y(t).diff(t), b * y(t))] sol4 = [Eq(x(t), C1 * exp(a * t)), Eq(y(t), C2 * exp(b * t))] assert dsolve(eq4) == sol4 assert checksysodesol(eq4, sol4) == (True, [0, 0]) eq5 = [Eq(x(t).diff(t), -y(t)), Eq(y(t).diff(t), x(t))] sol5 = [ Eq(x(t), -C1 * sin(t) - C2 * cos(t)), Eq(y(t), C1 * cos(t) - C2 * sin(t)) ] assert dsolve(eq5) == sol5 assert checksysodesol(eq5, sol5) == (True, [0, 0]) eq6 = [Eq(x(t).diff(t), -2 * y(t)), Eq(y(t).diff(t), 2 * x(t))] sol6 = [ Eq(x(t), -C1 * sin(2 * t) - C2 * cos(2 * t)), Eq(y(t), C1 * cos(2 * t) - C2 * sin(2 * t)) ] assert dsolve(eq6) == sol6 assert checksysodesol(eq6, sol6) == (True, [0, 0]) eq7 = [Eq(x(t).diff(t), I * y(t)), Eq(y(t).diff(t), I * x(t))] sol7 = [ Eq(x(t), -C1 * exp(-I * t) + C2 * exp(I * t)), Eq(y(t), C1 * exp(-I * t) + C2 * exp(I * t)) ] assert dsolve(eq7) == sol7 assert checksysodesol(eq7, sol7) == (True, [0, 0]) eq8 = [Eq(x(t).diff(t), -a * y(t)), Eq(y(t).diff(t), a * x(t))] sol8 = [ Eq(x(t), -I * C1 * exp(-I * a * t) + I * C2 * exp(I * a * t)), Eq(y(t), C1 * exp(-I * a * t) + C2 * exp(I * a * t)) ] assert dsolve(eq8) == sol8 assert checksysodesol(eq8, sol8) == (True, [0, 0]) eq9 = [Eq(x(t).diff(t), x(t) + y(t)), Eq(y(t).diff(t), x(t) - y(t))] sol9 = [ Eq( x(t), C1 * (1 - sqrt(2)) * exp(-sqrt(2) * t) + C2 * (1 + sqrt(2)) * exp(sqrt(2) * t)), Eq(y(t), C1 * exp(-sqrt(2) * t) + C2 * exp(sqrt(2) * t)) ] assert dsolve(eq9) == sol9 assert checksysodesol(eq9, sol9) == (True, [0, 0]) eq10 = [Eq(x(t).diff(t), x(t) + y(t)), Eq(y(t).diff(t), x(t) + y(t))] sol10 = [Eq(x(t), -C1 + C2 * exp(2 * t)), Eq(y(t), C1 + C2 * exp(2 * t))] assert dsolve(eq10) == sol10 assert checksysodesol(eq10, sol10) == (True, [0, 0]) eq11 = [ Eq(x(t).diff(t), 2 * x(t) + y(t)), Eq(y(t).diff(t), -x(t) + 2 * y(t)) ] sol11 = [ Eq(x(t), (C1 * sin(t) + C2 * cos(t)) * exp(2 * t)), Eq(y(t), (C1 * cos(t) - C2 * sin(t)) * exp(2 * t)) ] assert dsolve(eq11) == sol11 assert checksysodesol(eq11, sol11) == (True, [0, 0]) eq12 = [ Eq(x(t).diff(t), x(t) + 2 * y(t)), Eq(y(t).diff(t), 2 * x(t) + y(t)) ] sol12 = [ Eq(x(t), -C1 * exp(-t) + C2 * exp(3 * t)), Eq(y(t), C1 * exp(-t) + C2 * exp(3 * t)) ] assert dsolve(eq12) == sol12 assert checksysodesol(eq12, sol12) == (True, [0, 0]) eq13 = [ Eq(x(t).diff(t), 4 * x(t) + y(t)), Eq(y(t).diff(t), -x(t) + 2 * y(t)) ] sol13 = [ Eq(x(t), (C1 + C2 * t + C2) * exp(3 * t)), Eq(y(t), (-C1 - C2 * t) * exp(3 * t)) ] assert dsolve(eq13) == sol13 assert checksysodesol(eq13, sol13) == (True, [0, 0]) eq14 = [Eq(x(t).diff(t), a * y(t)), Eq(y(t).diff(t), a * x(t))] sol14 = [ Eq(x(t), -C1 * exp(-a * t) + C2 * exp(a * t)), Eq(y(t), C1 * exp(-a * t) + C2 * exp(a * t)) ] assert dsolve(eq14) == sol14 assert checksysodesol(eq14, sol14) == (True, [0, 0]) eq15 = [Eq(x(t).diff(t), a * y(t)), Eq(y(t).diff(t), b * x(t))] sol15 = [ Eq( x(t), -C1 * a * exp(-t * sqrt(a * b)) / sqrt(a * b) + C2 * a * exp(t * sqrt(a * b)) / sqrt(a * b)), Eq(y(t), C1 * exp(-t * sqrt(a * b)) + C2 * exp(t * sqrt(a * b))) ] assert dsolve(eq15) == sol15 assert checksysodesol(eq15, sol15) == (True, [0, 0]) eq16 = [Eq(x(t).diff(t), a * x(t) + b * y(t)), Eq(y(t).diff(t), c * x(t))] sol16 = [ Eq( x(t), -2 * C1 * b * exp(t * (a / 2 - sqrt(a**2 + 4 * b * c) / 2)) / (a + sqrt(a**2 + 4 * b * c)) - 2 * C2 * b * exp(t * (a / 2 + sqrt(a**2 + 4 * b * c) / 2)) / (a - sqrt(a**2 + 4 * b * c))), Eq( y(t), C1 * exp(t * (a / 2 - sqrt(a**2 + 4 * b * c) / 2)) + C2 * exp(t * (a / 2 + sqrt(a**2 + 4 * b * c) / 2))) ] assert dsolve(eq16) == sol16 assert checksysodesol(eq16, sol16) == (True, [0, 0]) Z0 = Function('Z0') Z1 = Function('Z1') Z2 = Function('Z2') Z3 = Function('Z3') k01, k10, k20, k21, k23, k30 = symbols('k01 k10 k20 k21 k23 k30') eq1 = (Eq(Derivative(Z0(t), t), -k01 * Z0(t) + k10 * Z1(t) + k20 * Z2(t) + k30 * Z3(t)), Eq(Derivative(Z1(t), t), k01 * Z0(t) - k10 * Z1(t) + k21 * Z2(t)), Eq(Derivative(Z2(t), t), -(k20 + k21 + k23) * Z2(t)), Eq(Derivative(Z3(t), t), k23 * Z2(t) - k30 * Z3(t))) sol1 = [ Eq( Z0(t), C1 * k10 / k01 + C2 * (-k10 + k30) * exp(-k30 * t) / (k01 + k10 - k30) - C3 * exp(t * (-k01 - k10)) + C4 * (-k10 * k20 - k10 * k21 + k10 * k30 + k20**2 + k20 * k21 + k20 * k23 - k20 * k30 - k23 * k30) * exp(t * (-k20 - k21 - k23)) / (k23 * (-k01 - k10 + k20 + k21 + k23))), Eq( Z1(t), C1 - C2 * k01 * exp(-k30 * t) / (k01 + k10 - k30) + C3 * exp(t * (-k01 - k10)) + C4 * (-k01 * k20 - k01 * k21 + k01 * k30 + k20 * k21 + k21**2 + k21 * k23 - k21 * k30) * exp(t * (-k20 - k21 - k23)) / (k23 * (-k01 - k10 + k20 + k21 + k23))), Eq(Z2(t), C4 * (-k20 - k21 - k23 + k30) * exp(t * (-k20 - k21 - k23)) / k23), Eq(Z3(t), C2 * exp(-k30 * t) + C4 * exp(t * (-k20 - k21 - k23))) ] assert dsolve(eq1, simplify=False) == sol1 assert checksysodesol(eq1, sol1) == (True, [0, 0, 0, 0]) x, y, z = symbols('x y z', cls=Function) k2, k3 = symbols('k2 k3') eq2 = (Eq(Derivative(z(t), t), k2 * y(t)), Eq(Derivative(x(t), t), k3 * y(t)), Eq(Derivative(y(t), t), (-k2 - k3) * y(t))) sol2 = { Eq(z(t), C1 - C3 * k2 * exp(t * (-k2 - k3)) / (k2 + k3)), Eq(x(t), C2 - C3 * k3 * exp(t * (-k2 - k3)) / (k2 + k3)), Eq(y(t), C3 * exp(t * (-k2 - k3))) } assert set(dsolve(eq2)) == sol2 assert checksysodesol(eq2, sol2) == (True, [0, 0, 0]) u, v, w = symbols('u v w', cls=Function) eq3 = [ 4 * u(t) - v(t) - 2 * w(t) + Derivative(u(t), t), 2 * u(t) + v(t) - 2 * w(t) + Derivative(v(t), t), 5 * u(t) + v(t) - 3 * w(t) + Derivative(w(t), t) ] sol3 = [ Eq( u(t), C1 * exp(-2 * t) + C2 * cos(sqrt(3) * t) / 2 - C3 * sin(sqrt(3) * t) / 2 + sqrt(3) * (C2 * sin(sqrt(3) * t) + C3 * cos(sqrt(3) * t)) / 6), Eq( v(t), C2 * cos(sqrt(3) * t) / 2 - C3 * sin(sqrt(3) * t) / 2 + sqrt(3) * (C2 * sin(sqrt(3) * t) + C3 * cos(sqrt(3) * t)) / 6), Eq(w(t), C1 * exp(-2 * t) + C2 * cos(sqrt(3) * t) - C3 * sin(sqrt(3) * t)) ] assert dsolve(eq3) == sol3 assert checksysodesol(eq3, sol3) == (True, [0, 0, 0]) tw = Rational(2, 9) eq4 = [ Eq(x(t).diff(t), 2 * x(t) + y(t) - tw * 4 * z(t) - tw * w(t)), Eq(y(t).diff(t), 2 * y(t) + 8 * tw * z(t) + 2 * tw * w(t)), Eq(z(t).diff(t), Rational(37, 9) * z(t) - tw * w(t)), Eq(w(t).diff(t), 22 * tw * w(t) - 2 * tw * z(t)) ] sol4 = [ Eq(x(t), (C1 + C2 * t) * exp(2 * t)), Eq(y(t), C2 * exp(2 * t) + 2 * C3 * exp(4 * t)), Eq(z(t), 2 * C3 * exp(4 * t) - C4 * exp(5 * t) / 4), Eq(w(t), C3 * exp(4 * t) + C4 * exp(5 * t)) ] assert dsolve(eq4) == sol4 assert checksysodesol(eq4, sol4) == (True, [0, 0, 0, 0]) # Regression test case for issue #15574 # https://github.com/sympy/sympy/issues/15574 eq5 = [ Eq(x(t).diff(t), x(t)), Eq(y(t).diff(t), y(t)), Eq(z(t).diff(t), z(t)), Eq(w(t).diff(t), w(t)) ] sol5 = [ Eq(x(t), C1 * exp(t)), Eq(y(t), C2 * exp(t)), Eq(z(t), C3 * exp(t)), Eq(w(t), C4 * exp(t)) ] assert dsolve(eq5) == sol5 assert checksysodesol(eq5, sol5) == (True, [0, 0, 0, 0]) eq6 = [ Eq(x(t).diff(t), x(t) + y(t)), Eq(y(t).diff(t), y(t) + z(t)), Eq(z(t).diff(t), z(t) + Rational(-1, 8) * w(t)), Eq(w(t).diff(t), Rational(1, 2) * (w(t) + z(t))) ] sol6 = [ Eq(x(t), (C3 + C4 * t) * exp(t) + (4 * C1 + 4 * C2 * t + 48 * C2) * exp(3 * t / 4)), Eq(y(t), C4 * exp(t) + (-C1 - C2 * t - 8 * C2) * exp(3 * t / 4)), Eq(z(t), (C1 / 4 + C2 * t / 4 + C2) * exp(3 * t / 4)), Eq(w(t), (C1 / 2 + C2 * t / 2) * exp(3 * t / 4)) ] assert dsolve(eq6) == sol6 assert checksysodesol(eq6, sol6) == (True, [0, 0, 0, 0]) # Regression test case for issue #15574 # https://github.com/sympy/sympy/issues/15574 eq7 = [ Eq(x(t).diff(t), x(t)), Eq(y(t).diff(t), y(t)), Eq(z(t).diff(t), z(t)), Eq(w(t).diff(t), w(t)), Eq(u(t).diff(t), u(t)) ] sol7 = [ Eq(x(t), C1 * exp(t)), Eq(y(t), C2 * exp(t)), Eq(z(t), C3 * exp(t)), Eq(w(t), C4 * exp(t)), Eq(u(t), C5 * exp(t)) ] assert dsolve(eq7) == sol7 assert checksysodesol(eq7, sol7) == (True, [0, 0, 0, 0, 0]) eq8 = [ Eq(x(t).diff(t), 2 * x(t) + y(t)), Eq(y(t).diff(t), 2 * y(t)), Eq(z(t).diff(t), 4 * z(t)), Eq(w(t).diff(t), 5 * w(t) + u(t)), Eq(u(t).diff(t), 5 * u(t)) ] sol8 = [ Eq(x(t), (C1 + C2 * t) * exp(2 * t)), Eq(y(t), C2 * exp(2 * t)), Eq(z(t), C3 * exp(4 * t)), Eq(w(t), (C4 + C5 * t) * exp(5 * t)), Eq(u(t), C5 * exp(5 * t)) ] assert dsolve(eq8) == sol8 assert checksysodesol(eq8, sol8) == (True, [0, 0, 0, 0, 0]) # Regression test case for issue #15574 # https://github.com/sympy/sympy/issues/15574 eq9 = [ Eq(x(t).diff(t), x(t)), Eq(y(t).diff(t), y(t)), Eq(z(t).diff(t), z(t)) ] sol9 = [ Eq(x(t), C1 * exp(t)), Eq(y(t), C2 * exp(t)), Eq(z(t), C3 * exp(t)) ] assert dsolve(eq9) == sol9 assert checksysodesol(eq9, sol9) == (True, [0, 0, 0]) # Regression test case for issue #15407 # https://github.com/sympy/sympy/issues/15407 a_b, a_c = symbols('a_b a_c', real=True) eq10 = [ Eq(x(t).diff(t), (-a_b - a_c) * x(t)), Eq(y(t).diff(t), a_b * y(t)), Eq(z(t).diff(t), a_c * x(t)) ] sol10 = [ Eq(x(t), -C3 * (a_b + a_c) * exp(t * (-a_b - a_c)) / a_c), Eq(y(t), C2 * exp(a_b * t)), Eq(z(t), C1 + C3 * exp(t * (-a_b - a_c))) ] assert dsolve(eq10) == sol10 assert checksysodesol(eq10, sol10) == (True, [0, 0, 0]) # Regression test case for issue #14312 # https://github.com/sympy/sympy/issues/14312 eq11 = (Eq(Derivative(x(t), t), k3 * y(t)), Eq(Derivative(y(t), t), -(k3 + k2) * y(t)), Eq(Derivative(z(t), t), k2 * y(t))) sol11 = [ Eq(x(t), C1 + C3 * k3 * exp(t * (-k2 - k3)) / k2), Eq(y(t), -C3 * (k2 + k3) * exp(t * (-k2 - k3)) / k2), Eq(z(t), C2 + C3 * exp(t * (-k2 - k3))) ] assert dsolve(eq11) == sol11 assert checksysodesol(eq11, sol11) == (True, [0, 0, 0]) # Regression test case for issue #14312 # https://github.com/sympy/sympy/issues/14312 eq12 = (Eq(Derivative(z(t), t), k2 * y(t)), Eq(Derivative(x(t), t), k3 * y(t)), Eq(Derivative(y(t), t), -(k3 + k2) * y(t))) sol12 = [ Eq(z(t), C1 - C3 * k2 * exp(t * (-k2 - k3)) / (k2 + k3)), Eq(x(t), C2 - C3 * k3 * exp(t * (-k2 - k3)) / (k2 + k3)), Eq(y(t), C3 * exp(t * (-k2 - k3))) ] assert dsolve(eq12) == sol12 assert checksysodesol(eq12, sol12) == (True, [0, 0, 0]) # Regression test case for issue #15474 # https://github.com/sympy/sympy/issues/15474 eq13 = [Eq(diff(f(t), t), 2 * f(t) + g(t)), Eq(diff(g(t), t), a * f(t))] sol13 = [ Eq( f(t), -C1 * exp(t * (1 - sqrt(a + 1))) / (sqrt(a + 1) + 1) + C2 * exp(t * (sqrt(a + 1) + 1)) / (sqrt(a + 1) - 1)), Eq(g(t), C1 * exp(t * (1 - sqrt(a + 1))) + C2 * exp(t * (sqrt(a + 1) + 1))) ] assert dsolve(eq13) == sol13 assert checksysodesol(eq13, sol13) == (True, [0, 0]) eq14 = [ Eq(f(t).diff(t), 2 * g(t) - 3 * h(t)), Eq(g(t).diff(t), 4 * h(t) - 2 * f(t)), Eq(h(t).diff(t), 3 * f(t) - 4 * g(t)) ] sol14 = [ Eq( f(t), 2 * C1 - 8 * C2 * cos(sqrt(29) * t) / 25 + 8 * C3 * sin(sqrt(29) * t) / 25 - 3 * sqrt(29) * (C2 * sin(sqrt(29) * t) + C3 * cos(sqrt(29) * t)) / 25), Eq( g(t), 3 * C1 / 2 - 6 * C2 * cos(sqrt(29) * t) / 25 + 6 * C3 * sin(sqrt(29) * t) / 25 + 4 * sqrt(29) * (C2 * sin(sqrt(29) * t) + C3 * cos(sqrt(29) * t)) / 25), Eq(h(t), C1 + C2 * cos(sqrt(29) * t) - C3 * sin(sqrt(29) * t)) ] assert dsolve(eq14) == sol14 assert checksysodesol(eq14, sol14) == (True, [0, 0, 0]) eq15 = [ Eq(2 * f(t).diff(t), 3 * 4 * (g(t) - h(t))), Eq(3 * g(t).diff(t), 2 * 4 * (h(t) - f(t))), Eq(4 * h(t).diff(t), 2 * 3 * (f(t) - g(t))) ] sol15 = [ Eq( f(t), C1 - 16 * C2 * cos(sqrt(29) * t) / 13 + 16 * C3 * sin(sqrt(29) * t) / 13 - 6 * sqrt(29) * (C2 * sin(sqrt(29) * t) + C3 * cos(sqrt(29) * t)) / 13), Eq( g(t), C1 - 16 * C2 * cos(sqrt(29) * t) / 13 + 16 * C3 * sin(sqrt(29) * t) / 13 + 8 * sqrt(29) * (C2 * sin(sqrt(29) * t) + C3 * cos(sqrt(29) * t)) / 39), Eq(h(t), C1 + C2 * cos(sqrt(29) * t) - C3 * sin(sqrt(29) * t)) ] assert dsolve(eq15) == sol15 assert checksysodesol(eq15, sol15) == (True, [0, 0, 0]) eq16 = (Eq(diff(x(t), t), 21 * x(t)), Eq(diff(y(t), t), 17 * x(t) + 3 * y(t)), Eq(diff(z(t), t), 5 * x(t) + 7 * y(t) + 9 * z(t))) sol16 = [ Eq(x(t), 216 * C3 * exp(21 * t) / 209), Eq(y(t), -6 * C1 * exp(3 * t) / 7 + 204 * C3 * exp(21 * t) / 209), Eq(z(t), C1 * exp(3 * t) + C2 * exp(9 * t) + C3 * exp(21 * t)) ] assert dsolve(eq16) == sol16 assert checksysodesol(eq16, sol16) == (True, [0, 0, 0]) eq17 = (Eq(diff(x(t), t), 3 * y(t) - 11 * z(t)), Eq(diff(y(t), t), 7 * z(t) - 3 * x(t)), Eq(diff(z(t), t), 11 * x(t) - 7 * y(t))) sol17 = [ Eq( x(t), 7 * C1 / 3 - 21 * C2 * cos(sqrt(179) * t) / 170 + 21 * C3 * sin(sqrt(179) * t) / 170 - 11 * sqrt(179) * (C2 * sin(sqrt(179) * t) + C3 * cos(sqrt(179) * t)) / 170), Eq( y(t), 11 * C1 / 3 - 33 * C2 * cos(sqrt(179) * t) / 170 + 33 * C3 * sin(sqrt(179) * t) / 170 + 7 * sqrt(179) * (C2 * sin(sqrt(179) * t) + C3 * cos(sqrt(179) * t)) / 170), Eq(z(t), C1 + C2 * cos(sqrt(179) * t) - C3 * sin(sqrt(179) * t)) ] assert dsolve(eq17) == sol17 assert checksysodesol(eq17, sol17) == (True, [0, 0, 0]) eq18 = (Eq(3 * diff(x(t), t), 4 * 5 * (y(t) - z(t))), Eq(4 * diff(y(t), t), 3 * 5 * (z(t) - x(t))), Eq(5 * diff(z(t), t), 3 * 4 * (x(t) - y(t)))) sol18 = [ Eq( x(t), C1 - C2 * cos(5 * sqrt(2) * t) + C3 * sin(5 * sqrt(2) * t) - 4 * sqrt(2) * (C2 * sin(5 * sqrt(2) * t) + C3 * cos(5 * sqrt(2) * t)) / 3), Eq( y(t), C1 - C2 * cos(5 * sqrt(2) * t) + C3 * sin(5 * sqrt(2) * t) + 3 * sqrt(2) * (C2 * sin(5 * sqrt(2) * t) + C3 * cos(5 * sqrt(2) * t)) / 4), Eq(z(t), C1 + C2 * cos(5 * sqrt(2) * t) - C3 * sin(5 * sqrt(2) * t)) ] assert dsolve(eq18) == sol18 assert checksysodesol(eq18, sol18) == (True, [0, 0, 0]) eq19 = (Eq(diff(x(t), t), 4 * x(t) - z(t)), Eq(diff(y(t), t), 2 * x(t) + 2 * y(t) - z(t)), Eq(diff(z(t), t), 3 * x(t) + y(t))) sol19 = [ Eq(x(t), (C1 + C2 * t + 2 * C2 + C3 * t**2 / 2 + 2 * C3 * t + C3) * exp(2 * t)), Eq(y(t), (C1 + C2 * t + 2 * C2 + C3 * t**2 / 2 + 2 * C3 * t) * exp(2 * t)), Eq(z(t), (2 * C1 + 2 * C2 * t + 3 * C2 + C3 * t**2 + 3 * C3 * t) * exp(2 * t)) ] assert dsolve(eq19) == sol19 assert checksysodesol(eq19, sol19) == (True, [0, 0, 0]) eq20 = (Eq(diff(x(t), t), 4 * x(t) - y(t) - 2 * z(t)), Eq(diff(y(t), t), 2 * x(t) + y(t) - 2 * z(t)), Eq(diff(z(t), t), 5 * x(t) - 3 * z(t))) sol20 = [ Eq( x(t), C1 * exp(2 * t) - C2 * sin(t) / 5 + 3 * C2 * cos(t) / 5 - 3 * C3 * sin(t) / 5 - C3 * cos(t) / 5), Eq( y(t), -C2 * sin(t) / 5 + 3 * C2 * cos(t) / 5 - 3 * C3 * sin(t) / 5 - C3 * cos(t) / 5), Eq(z(t), C1 * exp(2 * t) + C2 * cos(t) - C3 * sin(t)) ] assert dsolve(eq20) == sol20 assert checksysodesol(eq20, sol20) == (True, [0, 0, 0]) eq21 = (Eq(diff(x(t), t), 9 * y(t)), Eq(diff(y(t), t), 12 * x(t))) sol21 = [ Eq( x(t), -sqrt(3) * C1 * exp(-6 * sqrt(3) * t) / 2 + sqrt(3) * C2 * exp(6 * sqrt(3) * t) / 2), Eq(y(t), C1 * exp(-6 * sqrt(3) * t) + C2 * exp(6 * sqrt(3) * t)) ] assert dsolve(eq21) == sol21 assert checksysodesol(eq21, sol21) == (True, [0, 0]) eq22 = (Eq(diff(x(t), t), 2 * x(t) + 4 * y(t)), Eq(diff(y(t), t), 12 * x(t) + 41 * y(t))) sol22 = [Eq(x(t), C1*(-sqrt(1713)/24 + Rational(-13, 8))*exp(t*(Rational(43, 2) - sqrt(1713)/2)) \ + C2*(Rational(-13, 8) + sqrt(1713)/24)*exp(t*(sqrt(1713)/2 + Rational(43, 2)))), Eq(y(t), C1*exp(t*(Rational(43, 2) - sqrt(1713)/2)) + C2*exp(t*(sqrt(1713)/2 + Rational(43, 2))))] assert dsolve(eq22) == sol22 assert checksysodesol(eq22, sol22) == (True, [0, 0]) eq23 = (Eq(diff(x(t), t), x(t) + y(t)), Eq(diff(y(t), t), -2 * x(t) + 2 * y(t))) sol23 = [ Eq(x(t), (C1 * cos(sqrt(7) * t / 2) / 4 - C2 * sin(sqrt(7) * t / 2) / 4 + sqrt(7) * (C1 * sin(sqrt(7) * t / 2) + C2 * cos(sqrt(7) * t / 2)) / 4) * exp(3 * t / 2)), Eq(y(t), (C1 * cos(sqrt(7) * t / 2) - C2 * sin(sqrt(7) * t / 2)) * exp(3 * t / 2)) ] assert dsolve(eq23) == sol23 assert checksysodesol(eq23, sol23) == (True, [0, 0]) # Regression test case for issue #15474 # https://github.com/sympy/sympy/issues/15474 a = Symbol("a", real=True) eq24 = [x(t).diff(t) - a * y(t), y(t).diff(t) + a * x(t)] sol24 = [ Eq(x(t), C1 * sin(a * t) + C2 * cos(a * t)), Eq(y(t), C1 * cos(a * t) - C2 * sin(a * t)) ] assert dsolve(eq24) == sol24 assert checksysodesol(eq24, sol24) == (True, [0, 0]) # Regression test case for issue #19150 # https://github.com/sympy/sympy/issues/19150 eq25 = [ Eq(Derivative(f(t), t), 0), Eq(Derivative(g(t), t), 1 / (c * b) * (-2 * g(t) + x(t) + f(t))), Eq(Derivative(x(t), t), 1 / (c * b) * (-2 * x(t) + g(t) + y(t))), Eq(Derivative(y(t), t), 1 / (c * b) * (-2 * y(t) + x(t) + h(t))), Eq(Derivative(h(t), t), 0) ] sol25 = [ Eq(f(t), 4 * C1 - 3 * C2), Eq( g(t), 3 * C1 - 2 * C2 - C3 * exp(-2 * t / (b * c)) + C4 * exp(t * (-2 - sqrt(2)) / (b * c)) + C5 * exp(t * (-2 + sqrt(2)) / (b * c))), Eq( x(t), 2 * C1 - C2 - sqrt(2) * C4 * exp(t * (-2 - sqrt(2)) / (b * c)) + sqrt(2) * C5 * exp(t * (-2 + sqrt(2)) / (b * c))), Eq( y(t), C1 + C3 * exp(-2 * t / (b * c)) + C4 * exp(t * (-2 - sqrt(2)) / (b * c)) + C5 * exp(t * (-2 + sqrt(2)) / (b * c))), Eq(h(t), C2) ] assert dsolve(eq25) == sol25 assert checksysodesol(eq25, sol25)
def _eval_rewrite_as_pde(self, *args): from sympy import Function mu, epsilon, x, t = symbols('mu, epsilon, x, t') E = Function('E') return Derivative(E(x, t), x, 2) + mu * epsilon * Derivative(E(x, t), t, 2)
return out def make_derivatives(derivatives): return np.array([[eval(str(d))] for d in derivatives]) function = raw_input("\nEscribe tu funcion: ") matched_variable = re.findall(r"x\d", function) variables = sorted(list(set(matched_variable))) derivatives = [] for var in variables: x = Symbol(var) partialderiv= Derivative(function, x) derivatives.append(partialderiv.doit()) exec('{} = 0.1'.format(var)) f2 = 1 alfa = 0 gradient = None S = [[0] for d in derivatives] counter = 0 while not (sqrt(f2) <= 0.0001): counter += 1 for i in range(len(variables)): exec("x{} += alfa * S[{}][0]".format(i+1, i))
def _eval_derivative(self, symbol): new_expr = Derivative(self.expr, symbol) return DifferentialOperator(new_expr, self.args[-1])
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol('m') k = Symbol('k') x = Function('x') y = Function('y') t = Symbol('t') L = m*Derivative(x(t), t)**2/2 + m*Derivative(y(t), t)**2/2 - \ k*Derivative(x(t), t)*Derivative(y(t), t, t) + \ k*Derivative(y(t), t)*Derivative(x(t), t, t) assert euler_equations(L, [x(t), y(t)]) == \ set([Eq(-2*k*Derivative(x(t), t, t, t) - \ m*Derivative(y(t), t, t), 0), Eq(2*k*Derivative(y(t), t, t, t) - \ m*Derivative(x(t), t, t), 0)]) w = Symbol('w') L = x(t, w).diff(t, w)**2 / 2 assert euler_equations(L) == \ set([Eq(Derivative(x(t, w), t, t, w, w), 0)])
def test_big_expr(): f = Function('f') x = symbols('x') e1 = Dagger( AntiCommutator( Operator('A') + Operator('B'), Pow(DifferentialOperator(Derivative(f(x), x), f(x)), 3)) * TensorProduct(Jz**2, Operator('A') + Operator('B'))) * (JzBra(1, 0) + JzBra( 1, 1)) * (JzKet(0, 0) + JzKet(1, -1)) e2 = Commutator(Jz**2, Operator('A') + Operator('B')) * AntiCommutator( Dagger(Operator('C') * Operator('D')), Operator('E').inv()**2) * Dagger(Commutator(Jz, J2)) e3 = Wigner3j(1, 2, 3, 4, 5, 6) * TensorProduct( Commutator( Operator('A') + Dagger(Operator('B')), Operator('C') + Operator('D')), Jz - J2) * Dagger( OuterProduct(Dagger(JzBra(1, 1)), JzBra( 1, 0))) * TensorProduct( JzKetCoupled(1, 1, (1, 1)) + JzKetCoupled(1, 0, (1, 1)), JzKetCoupled(1, -1, (1, 1))) e4 = (ComplexSpace(1) * ComplexSpace(2) + FockSpace()**2) * (L2(Interval(0, oo)) + HilbertSpace()) assert str( e1 ) == '(Jz**2)x(Dagger(A) + Dagger(B))*{Dagger(DifferentialOperator(Derivative(f(x), x),f(x)))**3,Dagger(A) + Dagger(B)}*(<1,0| + <1,1|)*(|0,0> + |1,-1>)' ascii_str = \ """\ / 3 \\ \n\ |/ +\\ | \n\ 2 / + +\\ <| /d \\ | + +> \n\ /J \\ x \\A + B /*||DifferentialOperator|--(f(x)),f(x)| | ,A + B |*(<1,0| + <1,1|)*(|0,0> + |1,-1>)\n\ \\ z/ \\\\ \\dx / / / \ """ ucode_str = \ """\ ⎧ 3 ⎫ \n\ ⎪⎛ †⎞ ⎪ \n\ 2 ⎛ † †⎞ ⎨⎜ ⎛d ⎞ ⎟ † †⎬ \n\ ⎛J ⎞ ⨂ ⎝A + B ⎠⋅⎪⎜DifferentialOperator⎜──(f(x)),f(x)⎟ ⎟ ,A + B ⎪⋅(⟨1,0❘ + ⟨1,1❘)⋅(❘0,0⟩ + ❘1,-1⟩)\n\ ⎝ z⎠ ⎩⎝ ⎝dx ⎠ ⎠ ⎭ \ """ assert pretty(e1) == ascii_str assert upretty(e1) == ucode_str assert latex(e1) == \ r'{J_z^{2}}\otimes \left({A^{\dagger} + B^{\dagger}}\right) \left\{\left(DifferentialOperator\left(\frac{d}{d x} f{\left(x \right)},f{\left(x \right)}\right)^{\dagger}\right)^{3},A^{\dagger} + B^{\dagger}\right\} \left({\left\langle 1,0\right|} + {\left\langle 1,1\right|}\right) \left({\left|0,0\right\rangle } + {\left|1,-1\right\rangle }\right)' sT( e1, "Mul(TensorProduct(Pow(JzOp(Symbol('J')), Integer(2)), Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), AntiCommutator(Pow(Dagger(DifferentialOperator(Derivative(Function('f')(Symbol('x')), Tuple(Symbol('x'), Integer(1))),Function('f')(Symbol('x')))), Integer(3)),Add(Dagger(Operator(Symbol('A'))), Dagger(Operator(Symbol('B'))))), Add(JzBra(Integer(1),Integer(0)), JzBra(Integer(1),Integer(1))), Add(JzKet(Integer(0),Integer(0)), JzKet(Integer(1),Integer(-1))))" ) assert str(e2) == '[Jz**2,A + B]*{E**(-2),Dagger(D)*Dagger(C)}*[J2,Jz]' ascii_str = \ """\ [ 2 ] / -2 + +\\ [ 2 ]\n\ [/J \\ ,A + B]*<E ,D *C >*[J ,J ]\n\ [\\ z/ ] \\ / [ z]\ """ ucode_str = \ """\ ⎡ 2 ⎤ ⎧ -2 † †⎫ ⎡ 2 ⎤\n\ ⎢⎛J ⎞ ,A + B⎥⋅⎨E ,D ⋅C ⎬⋅⎢J ,J ⎥\n\ ⎣⎝ z⎠ ⎦ ⎩ ⎭ ⎣ z⎦\ """ assert pretty(e2) == ascii_str assert upretty(e2) == ucode_str assert latex(e2) == \ r'\left[J_z^{2},A + B\right] \left\{E^{-2},D^{\dagger} C^{\dagger}\right\} \left[J^2,J_z\right]' sT( e2, "Mul(Commutator(Pow(JzOp(Symbol('J')), Integer(2)),Add(Operator(Symbol('A')), Operator(Symbol('B')))), AntiCommutator(Pow(Operator(Symbol('E')), Integer(-2)),Mul(Dagger(Operator(Symbol('D'))), Dagger(Operator(Symbol('C'))))), Commutator(J2Op(Symbol('J')),JzOp(Symbol('J'))))" ) assert str(e3) == \ "Wigner3j(1, 2, 3, 4, 5, 6)*[Dagger(B) + A,C + D]x(-J2 + Jz)*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x|1,-1,j1=1,j2=1>" ascii_str = \ """\ [ + ] / 2 \\ \n\ /1 3 5\\*[B + A,C + D]x |- J + J |*|1,0><1,1|*(|1,0,j1=1,j2=1> + |1,1,j1=1,j2=1>)x |1,-1,j1=1,j2=1>\n\ | | \\ z/ \n\ \\2 4 6/ \ """ ucode_str = \ """\ ⎡ † ⎤ ⎛ 2 ⎞ \n\ ⎛1 3 5⎞⋅⎣B + A,C + D⎦⨂ ⎜- J + J ⎟⋅❘1,0⟩⟨1,1❘⋅(❘1,0,j₁=1,j₂=1⟩ + ❘1,1,j₁=1,j₂=1⟩)⨂ ❘1,-1,j₁=1,j₂=1⟩\n\ ⎜ ⎟ ⎝ z⎠ \n\ ⎝2 4 6⎠ \ """ assert pretty(e3) == ascii_str assert upretty(e3) == ucode_str assert latex(e3) == \ r'\left(\begin{array}{ccc} 1 & 3 & 5 \\ 2 & 4 & 6 \end{array}\right) {\left[B^{\dagger} + A,C + D\right]}\otimes \left({- J^2 + J_z}\right) {\left|1,0\right\rangle }{\left\langle 1,1\right|} \left({{\left|1,0,j_{1}=1,j_{2}=1\right\rangle } + {\left|1,1,j_{1}=1,j_{2}=1\right\rangle }}\right)\otimes {{\left|1,-1,j_{1}=1,j_{2}=1\right\rangle }}' sT( e3, "Mul(Wigner3j(Integer(1), Integer(2), Integer(3), Integer(4), Integer(5), Integer(6)), TensorProduct(Commutator(Add(Dagger(Operator(Symbol('B'))), Operator(Symbol('A'))),Add(Operator(Symbol('C')), Operator(Symbol('D')))), Add(Mul(Integer(-1), J2Op(Symbol('J'))), JzOp(Symbol('J')))), OuterProduct(JzKet(Integer(1),Integer(0)),JzBra(Integer(1),Integer(1))), TensorProduct(Add(JzKetCoupled(Integer(1),Integer(0),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1)))), JzKetCoupled(Integer(1),Integer(1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))), JzKetCoupled(Integer(1),Integer(-1),Tuple(Integer(1), Integer(1)),Tuple(Tuple(Integer(1), Integer(2), Integer(1))))))" ) assert str(e4) == '(C(1)*C(2)+F**2)*(L2(Interval(0, oo))+H)' ascii_str = \ """\ // 1 2\\ x2\\ / 2 \\\n\ \\\\C x C / + F / x \\L + H/\ """ ucode_str = \ """\ ⎛⎛ 1 2⎞ ⨂2⎞ ⎛ 2 ⎞\n\ ⎝⎝C ⨂ C ⎠ ⊕ F ⎠ ⨂ ⎝L ⊕ H⎠\ """ assert pretty(e4) == ascii_str assert upretty(e4) == ucode_str assert latex(e4) == \ r'\left(\left(\mathcal{C}^{1}\otimes \mathcal{C}^{2}\right)\oplus {\mathcal{F}}^{\otimes 2}\right)\otimes \left({\mathcal{L}^2}\left( \left[0, \infty\right) \right)\oplus \mathcal{H}\right)' sT( e4, "TensorProductHilbertSpace((DirectSumHilbertSpace(TensorProductHilbertSpace(ComplexSpace(Integer(1)),ComplexSpace(Integer(2))),TensorPowerHilbertSpace(FockSpace(),Integer(2)))),(DirectSumHilbertSpace(L2(Interval(Integer(0), oo, false, true)),HilbertSpace())))" )
def test_issue_15226(): assert Subs(Derivative(f(y), x, y), y, g(x)).doit() != 0
init_printing(use_unicode=True) assert 10**10 < oo x, y = symbols('x, y') f = Integral(x**2, x) print(f) assert f.doit() == x**3 / 3 assert limit(x**2, x, 2) == 4 print(sin(1) / cos(1)) print(exp(x)) assert integrate(x, (x, 0, 2)) == 2 z = Integral(x**2, (x, 0, 2)) print(z) assert z.doit() == 8 / 3 assert diff(x**2, x) == 2 * x assert diff(x**3, x, 2) == 6 * x assert diff(x * y, x) == y z = Derivative((x**2) * y, x) print(z) assert z.doit() == 2 * x * y if __name__ == '__main__': pass
def test_issue_15084_13166(): eq = f(x, g(x)) assert eq.diff((g(x), y)) == Derivative(f(x, g(x)), (g(x), y)) # issue 13166 assert eq.diff( x, 2).doit() == ((Derivative(f(x, g(x)), (g(x), 2)) * Derivative(g(x), x) + Subs(Derivative(f(x, _xi_2), _xi_2, x), _xi_2, g(x))) * Derivative(g(x), x) + Derivative(f(x, g(x)), g(x)) * Derivative(g(x), (x, 2)) + Derivative(g(x), x) * Subs(Derivative(f(_xi_1, g(x)), _xi_1, g(x)), _xi_1, x) + Subs(Derivative(f(_xi_1, g(x)), (_xi_1, 2)), _xi_1, x)) # issue 6681 assert diff(f(x, t, g(x, t)), x).doit() == ( Derivative(f(x, t, g(x, t)), g(x, t)) * Derivative(g(x, t), x) + Subs(Derivative(f(_xi_1, t, g(x, t)), _xi_1), _xi_1, x)) # make sure the order doesn't matter when using diff assert eq.diff(x, g(x)) == eq.diff(g(x), x)
def test_meijer(): raises(TypeError, lambda: meijerg(1, z)) raises(TypeError, lambda: meijerg(((1, ), (2, )), (3, ), (4, ), z)) assert meijerg(((1, 2), (3,)), ((4,), (5,)), z) == \ meijerg(Tuple(1, 2), Tuple(3), Tuple(4), Tuple(5), z) g = meijerg((1, 2), (3, 4, 5), (6, 7, 8, 9), (10, 11, 12, 13, 14), z) assert g.an == Tuple(1, 2) assert g.ap == Tuple(1, 2, 3, 4, 5) assert g.aother == Tuple(3, 4, 5) assert g.bm == Tuple(6, 7, 8, 9) assert g.bq == Tuple(6, 7, 8, 9, 10, 11, 12, 13, 14) assert g.bother == Tuple(10, 11, 12, 13, 14) assert g.argument == z assert g.nu == 75 assert g.delta == -1 assert g.is_commutative is True assert g.is_number is False #issue 13071 assert meijerg([[], []], [[S(1) / 2], [0]], 1).is_number is True assert meijerg([1, 2], [3], [4], [5], z).delta == S(1) / 2 # just a few checks to make sure that all arguments go where they should assert tn(meijerg(Tuple(), Tuple(), Tuple(0), Tuple(), -z), exp(z), z) assert tn( sqrt(pi) * meijerg(Tuple(), Tuple(), Tuple(0), Tuple(S(1) / 2), z**2 / 4), cos(z), z) assert tn(meijerg(Tuple(1, 1), Tuple(), Tuple(1), Tuple(0), z), log(1 + z), z) # test exceptions raises(ValueError, lambda: meijerg(((3, 1), (2, )), ((oo, ), (2, 0)), x)) raises(ValueError, lambda: meijerg(((3, 1), (2, )), ((1, ), (2, 0)), x)) # differentiation g = meijerg((randcplx(), ), (randcplx() + 2 * I, ), Tuple(), (randcplx(), randcplx()), z) assert td(g, z) g = meijerg(Tuple(), (randcplx(), ), Tuple(), (randcplx(), randcplx()), z) assert td(g, z) g = meijerg(Tuple(), Tuple(), Tuple(randcplx()), Tuple(randcplx(), randcplx()), z) assert td(g, z) a1, a2, b1, b2, c1, c2, d1, d2 = symbols('a1:3, b1:3, c1:3, d1:3') assert meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z).diff(z) == \ (meijerg((a1 - 1, a2), (b1, b2), (c1, c2), (d1, d2), z) + (a1 - 1)*meijerg((a1, a2), (b1, b2), (c1, c2), (d1, d2), z))/z assert meijerg([z, z], [], [], [], z).diff(z) == \ Derivative(meijerg([z, z], [], [], [], z), z) # meijerg is unbranched wrt parameters from sympy import polar_lift as pl assert meijerg([pl(a1)], [pl(a2)], [pl(b1)], [pl(b2)], pl(z)) == \ meijerg([a1], [a2], [b1], [b2], pl(z)) # integrand from sympy.abc import a, b, c, d, s assert meijerg([a], [b], [c], [d], z).integrand(s) == \ z**s*gamma(c - s)*gamma(-a + s + 1)/(gamma(b - s)*gamma(-d + s + 1))
def test_doitdoit(): done = Derivative(f(x, g(x)), x, g(x)).doit() assert done == done.doit()
def calc_cumulant(self, _fun, direction, order): cgf = ln(_fun) deriv = Derivative(cgf, (direction, order)) all_terms = deriv.doit() all_terms = simplify(all_terms) return all_terms
def test_ode_order(): f = Function('f') g = Function('g') x = Symbol('x') assert ode_order(3 * x * exp(f(x)), f(x)) == 0 assert ode_order(x * diff(f(x), x) + 3 * x * f(x) - sin(x) / x, f(x)) == 1 assert ode_order(x**2 * f(x).diff(x, x) + x * diff(f(x), x) - f(x), f(x)) == 2 assert ode_order(diff(x * exp(f(x)), x, x), f(x)) == 2 assert ode_order(diff(x * diff(x * exp(f(x)), x, x), x), f(x)) == 3 assert ode_order(diff(f(x), x, x), g(x)) == 0 assert ode_order(diff(f(x), x, x) * diff(g(x), x), f(x)) == 2 assert ode_order(diff(f(x), x, x) * diff(g(x), x), g(x)) == 1 assert ode_order(diff(x * diff(x * exp(f(x)), x, x), x), g(x)) == 0 # issue 5835: ode_order has to also work for unevaluated derivatives # (ie, without using doit()). assert ode_order(Derivative(x * f(x), x), f(x)) == 1 assert ode_order(x * sin(Derivative(x * f(x)**2, x, x)), f(x)) == 2 assert ode_order(Derivative(x * Derivative(x * exp(f(x)), x, x), x), g(x)) == 0 assert ode_order(Derivative(f(x), x, x), g(x)) == 0 assert ode_order(Derivative(x * exp(f(x)), x, x), f(x)) == 2 assert ode_order(Derivative(f(x), x, x) * Derivative(g(x), x), g(x)) == 1 assert ode_order(Derivative(x * Derivative(f(x), x, x), x), f(x)) == 3 assert ode_order(x * sin(Derivative(x * Derivative(f(x), x)**2, x, x)), f(x)) == 3
def test_integrate_derivatives(): assert integrate(Derivative(f(x), x), x) == f(x) assert integrate(Derivative(f(y), y), x) == x*Derivative(f(y), y)
def test_differential_operator(): x = Symbol('x') f = Function('f') d = DifferentialOperator(Derivative(f(x), x), f(x)) g = Wavefunction(x**2, x) assert qapply(d * g) == Wavefunction(2 * x, x) assert d.expr == Derivative(f(x), x) assert d.function == f(x) assert d.variables == (x, ) assert diff(d, x) == DifferentialOperator(Derivative(f(x), x, 2), f(x)) d = DifferentialOperator(Derivative(f(x), x, 2), f(x)) g = Wavefunction(x**3, x) assert qapply(d * g) == Wavefunction(6 * x, x) assert d.expr == Derivative(f(x), x, 2) assert d.function == f(x) assert d.variables == (x, ) assert diff(d, x) == DifferentialOperator(Derivative(f(x), x, 3), f(x)) d = DifferentialOperator(1 / x * Derivative(f(x), x), f(x)) assert d.expr == 1 / x * Derivative(f(x), x) assert d.function == f(x) assert d.variables == (x, ) assert diff(d, x) == \ DifferentialOperator(Derivative(1/x*Derivative(f(x), x), x), f(x)) assert qapply(d * g) == Wavefunction(3 * x, x) # 2D cartesian Laplacian y = Symbol('y') d = DifferentialOperator( Derivative(f(x, y), x, 2) + Derivative(f(x, y), y, 2), f(x, y)) w = Wavefunction(x**3 * y**2 + y**3 * x**2, x, y) assert d.expr == Derivative(f(x, y), x, 2) + Derivative(f(x, y), y, 2) assert d.function == f(x, y) assert d.variables == (x, y) assert diff(d, x) == \ DifferentialOperator(Derivative(d.expr, x), f(x, y)) assert diff(d, y) == \ DifferentialOperator(Derivative(d.expr, y), f(x, y)) assert qapply(d * w) == Wavefunction( 2 * x**3 + 6 * x * y**2 + 6 * x**2 * y + 2 * y**3, x, y) # 2D polar Laplacian (th = theta) r, th = symbols('r th') d = DifferentialOperator( 1 / r * Derivative(r * Derivative(f(r, th), r), r) + 1 / (r**2) * Derivative(f(r, th), th, 2), f(r, th)) w = Wavefunction(r**2 * sin(th), r, (th, 0, pi)) assert d.expr == \ 1/r*Derivative(r*Derivative(f(r, th), r), r) + \ 1/(r**2)*Derivative(f(r, th), th, 2) assert d.function == f(r, th) assert d.variables == (r, th) assert diff(d, r) == \ DifferentialOperator(Derivative(d.expr, r), f(r, th)) assert diff(d, th) == \ DifferentialOperator(Derivative(d.expr, th), f(r, th)) assert qapply(d * w) == Wavefunction(3 * sin(th), r, (th, 0, pi))
def test_derivative2(): f = Function("f") x = Symbol("x") 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)) == None e = Derivative(f(x), x, x) assert e.match(Derivative(f(x), x)) == 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) == None e = Derivative(f(x), x, x)+x**2 assert e.match(a*Derivative(f(x), x) + b) == None assert e.match(a*Derivative(f(x), x, x) + b) == {a: 1, b: x**2}
class valid_units(Equation): expr = Eq(demo_v, Derivative(demo_d, demo_fall.definition.t))
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_derivative_subs2(): f_func, g_func = symbols('f g', cls=Function) f, g = f_func(x, y, z), g_func(x, y, z) assert Derivative(f, x, y).subs(Derivative(f, x, y), g) == g assert Derivative(f, y, x).subs(Derivative(f, x, y), g) == g assert Derivative(f, x, y).subs(Derivative(f, x), g) == Derivative(g, y) assert Derivative(f, x, y).subs(Derivative(f, y), g) == Derivative(g, x) assert (Derivative(f, x, y, z).subs(Derivative(f, x, z), g) == Derivative(g, y)) assert (Derivative(f, x, y, z).subs(Derivative(f, z, y), g) == Derivative(g, x)) assert (Derivative(f, x, y, z).subs(Derivative(f, z, y, x), g) == g) # Issue 9135 assert (Derivative(f, x, x, y).subs(Derivative(f, y, y), g) == Derivative(f, x, x, y)) assert (Derivative(f, x, y, y, z).subs(Derivative(f, x, y, y, y), g) == Derivative(f, x, y, y, z)) assert Derivative(f, x, y).subs(Derivative(f_func(x), x, y), g) == Derivative(f, x, y)
def test_derivative_subs3(): 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_euler_pendulum(): x = Function('x') t = Symbol('t') L = (x(t).diff(t))**2 / 2 + cos(x(t)) assert euler_equations(L, x(t), t) == \ set([Eq(-sin(x(t)) - Derivative(x(t), t, t), 0)])
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))
#!/usr/bin/env python # coding: utf-8 # In[1]: from sympy import init_printing, symbols, Integral, Derivative, sin, exp, Eq from IPython.display import display init_printing(use_latex=True, latex_mode='equation*') x = symbols('x') # In[2]: int_x = Integral(sin(x)*exp(x), x) display(Eq(int_x, int_x.doit())) derv_x = Derivative(sin(x)*exp(x), x) display(Eq(derv_x, derv_x.doit()))