Repository: sympy/sympy
File: sympy/core/tests/test_function.py (Python)

Code before:
    class X(Expr):
        _diff_wrt = True
    assert X().diff(X()) == 1
    assert (2*X()).diff(X()) == 2


def test_noncommutative_issue_15131():
    x = Symbol('x', commutative=False)
    t = Symbol('t', commutative=False)
    fx = Function('Fx', commutative=False)(x)
    ft = Function('Ft', commutative=False)(t)
    A = Symbol('A', commutative=False)
    eq = fx * A * ft
    eqdt = eq.diff(t)
    assert eqdt.args[-1] == ft.diff(t)


def test_noncommutative_derivative():
    t = symbols('t')
    S = Function('S', commutative=False)(t)
    T = Function('T', commutative=False)(t)

    dS = Derivative(S, t)
    dT = Derivative(T, t)

    assert isinstance(diff(S**2, t), Derivative)
    assert isinstance(diff(S**3, t), Derivative)
    assert isinstance(diff(S**-1, t), Derivative)
    assert isinstance(diff(S**-2, t), Derivative)

    assert diff(S*T, t) == dS*T + S*dT

    U = Function('U')(t)
    dU = Derivative(U, t)
    assert diff(U**2, t) == 2*U*dU

    n = symbols('n')
    assert isinstance(diff(S**n, t), Derivative)


def test_Subs_Derivative():
    a = Derivative(f(g(x), h(x)), g(x), h(x),x)
    b = Derivative(Derivative(f(g(x), h(x)), g(x), h(x)),x)
    c = f(g(x), h(x)).diff(g(x), h(x), x)
    d = f(g(x), h(x)).diff(g(x), h(x)).diff(x)
    e = Derivative(f(g(x), h(x)), x)
    eqs = (a, b, c, d, e)
    subs = lambda arg: arg.subs(f, Lambda((x, y), exp(x + y))
        ).subs(g(x), 1/x).subs(h(x), x**3)
    ans = 3*x**2*exp(1/x)*exp(x**3) - exp(1/x)*exp(x**3)/x**2
    assert all(subs(i).doit().expand() == ans for i in eqs)

Review comment (suggestion):
It would be better to write
```
assert diff(S**2, t) == Derivative(S**2, t)
```
i.e. to assert exactly what the output should be.

Code after:
    class X(Expr):
        _diff_wrt = True
    assert X().diff(X()) == 1
    assert (2*X()).diff(X()) == 2


def test_noncommutative_issue_15131():
    x = Symbol('x', commutative=False)
    t = Symbol('t', commutative=False)
    fx = Function('Fx', commutative=False)(x)
    ft = Function('Ft', commutative=False)(t)
    A = Symbol('A', commutative=False)
    eq = fx * A * ft
    eqdt = eq.diff(t)
    assert eqdt.args[-1] == ft.diff(t)


def test_noncommutative_derivative():
    t = symbols('t')
    S = Function('S', commutative=False)(t)
    T = Function('T', commutative=False)(t)

    dS = Derivative(S, t)
    dT = Derivative(T, t)

    assert diff(S**2, t) == Derivative(S**2, t)
    assert diff(S**3, t) == Derivative(S**3, t)
    assert diff(S**-1, t) == Derivative(S**-1, t)
    assert diff(S**-2, t) == Derivative(S**-2, t)

    assert diff(S*T, t) == dS*T + S*dT

    U = Function('U')(t)
    dU = Derivative(U, t)
    assert diff(U**2, t) == 2*U*dU

    n = symbols('n')
    assert diff(S**n, t) == Derivative(S**n, t)


def test_Subs_Derivative():
    a = Derivative(f(g(x), h(x)), g(x), h(x),x)
    b = Derivative(Derivative(f(g(x), h(x)), g(x), h(x)),x)
    c = f(g(x), h(x)).diff(g(x), h(x), x)
    d = f(g(x), h(x)).diff(g(x), h(x)).diff(x)
    e = Derivative(f(g(x), h(x)), x)
    eqs = (a, b, c, d, e)
    subs = lambda arg: arg.subs(f, Lambda((x, y), exp(x + y))
        ).subs(g(x), 1/x).subs(h(x), x**3)
    ans = 3*x**2*exp(1/x)*exp(x**3) - exp(1/x)*exp(x**3)/x**2
    assert all(subs(i).doit().expand() == ans for i in eqs)