Beispiel #1
0
def test_issue_7231():
    from sympy.abc import a
    ans1 = f(x).series(x, a)
    res = (f(a) + (-a + x) * Subs(Derivative(f(y), y), y, a) +
           (-a + x)**2 * Subs(Derivative(f(y), y, y), y, a) / 2 +
           (-a + x)**3 * Subs(Derivative(f(y), y, y, y), y, a) / 6 +
           (-a + x)**4 * Subs(Derivative(f(y), y, y, y, y), y, a) / 24 +
           (-a + x)**5 * Subs(Derivative(f(y), y, y, y, y, y), y, a) / 120 + O(
               (-a + x)**6, (x, a)))
    assert res == ans1
    ans2 = f(x).series(x, a)
    assert res == ans2
Beispiel #2
0
def test_issue_7068():
    from sympy.abc import a, b, f
    y1 = Dummy('y')
    y2 = Dummy('y')
    func1 = f(a + y1 * b)
    func2 = f(a + y2 * b)
    func1_y = func1.diff(y1)
    func2_y = func2.diff(y2)
    assert func1_y != func2_y
    z1 = Subs(f(a), a, y1)
    z2 = Subs(f(a), a, y2)
    assert z1 != z2
Beispiel #3
0
def test_deriv1():
    # These all requre derivatives evaluated at a point (issue 1620) to work.
    # See issue 1525
    assert f(2*x).diff(x) == 2*Subs(Derivative(f(x), x), Tuple(x), Tuple(2*x))
    assert (f(x)**3).diff(x) == 3*f(x)**2*f(x).diff(x)
    assert (f(2*x)**3).diff(x) == 6*f(2*x)**2*Subs(Derivative(f(x), x), Tuple(x),
            Tuple(2*x))

    assert f(2+x).diff(x) == Subs(Derivative(f(x), x), Tuple(x), Tuple(x + 2))
    assert f(2+3*x).diff(x) == 3*Subs(Derivative(f(x), x), Tuple(x),
            Tuple(3*x + 2))
    assert f(3*sin(x)).diff(x) == 3*cos(x)*Subs(Derivative(f(x), x),
            Tuple(x), Tuple(3*sin(x)))
Beispiel #4
0
def test_Subs_subs():
    assert Subs(x * y, x, x).subs(x, y) == Subs(x * y, x, y)
    assert Subs(x*y, x, x + 1).subs(x, y) == \
        Subs(x*y, x, y + 1)
    assert Subs(x*y, y, x + 1).subs(x, y) == \
        Subs(y**2, y, y + 1)
    a = Subs(x * y * z, (y, x, z), (x + 1, x + z, x))
    b = Subs(x * y * z, (y, x, z), (x + 1, y + z, y))
    assert a.subs(x, y) == b and \
        a.doit().subs(x, y) == a.subs(x, y).doit()
    f = Function('f')
    g = Function('g')
    assert Subs(2 * f(x, y) + g(x), f(x, y),
                1).subs(y, 2) == Subs(2 * f(x, y) + g(x), (f(x, y), y), (1, 2))
Beispiel #5
0
def test_issue_7231():
    from sympy.abc import a
    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
Beispiel #6
0
def check_solutions(eq):
    """
    Determines whether solutions returned by diophantine() satisfy the original
    equation. Hope to generalize this so we can remove functions like check_ternay_quadratic,
    check_solutions_normal, check_solutions()
    """
    s = diophantine(eq)

    terms = factor_list(eq)[1]

    var = list(eq.free_symbols)
    var.sort(key=default_sort_key)

    okay = True

    while len(s) and okay:
        solution = s.pop()

        okay = False

        for term in terms:
            subeq = term[0]

            if simplify(_mexpand(Subs(subeq, var, solution).doit())) == 0:
                okay = True
                break

    return okay
Beispiel #7
0
def is_pell_transformation_ok(eq):
    """
    Test whether X*Y, X, or Y terms are present in the equation
    after transforming the equation using the transformation returned
    by transformation_to_pell(). If they are not present we are good.
    Moreover, coefficient of X**2 should be a divisor of coefficient of
    Y**2 and the constant term.
    """
    A, B = transformation_to_DN(eq)
    u = (A * Matrix([X, Y]) + B)[0]
    v = (A * Matrix([X, Y]) + B)[1]
    simplified = _mexpand(Subs(eq, (x, y), (u, v)).doit())

    coeff = dict(
        [reversed(t.as_independent(*[X, Y])) for t in simplified.args])

    for term in [X * Y, X, Y]:
        if term in coeff.keys():
            return False

    for term in [X**2, Y**2, Integer(1)]:
        if term not in coeff.keys():
            coeff[term] = Integer(0)

    if coeff[X**2] != 0:
        return isinstance(S(coeff[Y**2]) / coeff[X**2],
                          Integer) and isinstance(
                              S(coeff[Integer(1)]) / coeff[X**2], Integer)

    return True
def get_gamma(k,alpha):
    f49 =  N(Subs(f49Coeff, [_j,_alpha] , [k-1,alpha]))
    j = int(np.ceil(k/2))
    f27,f31 = get_fCoeffs(j,alpha)
    # Eq. 58, Hadden & Lithwick '16
    gamma = f49 * (f27*f27 + f31*f31) / f27 / f31 / 2
    return gamma
Beispiel #9
0
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)
Beispiel #10
0
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) == \
        Derivative(g(x), x)*Derivative(f(g(x)), g(x))
    assert diff(f(g(x), h(y)), x) == \
        Derivative(g(x), x)*Derivative(f(g(x), h(y)), g(x))
    assert diff(f(g(x), h(x)),
                x) == (Derivative(f(g(x), h(x)), g(x)) * Derivative(g(x), x) +
                       Derivative(f(g(x), h(x)), h(x)) * Derivative(h(x), x))
    assert f(sin(x)).diff(x) == cos(x) * Subs(Derivative(f(x), x), x, sin(x))

    assert diff(f(g(x)), g(x)) == Derivative(f(g(x)), g(x))
Beispiel #11
0
def test_series_of_Subs():
    from sympy.abc import x, y, z

    subs1 = Subs(sin(x), x, y)
    subs2 = Subs(sin(x) * cos(z), x, y)
    subs3 = Subs(sin(x * z), (x, z), (y, x))

    assert subs1.series(x) == subs1
    assert subs1.series(y) == Subs(x, x, y) + Subs(-x**3 / 6, x, y) + Subs(
        x**5 / 120, x, y) + O(y**6)
    assert subs1.series(z) == subs1
    assert subs2.series(z) == Subs(z**4 * sin(x) / 24, x, y) + Subs(
        -z**2 * sin(x) / 2, x, y) + Subs(sin(x), x, y) + O(z**6)
    assert subs3.series(x).doit() == subs3.doit().series(x)
    assert subs3.series(z).doit() == sin(x * y)
Beispiel #12
0
def is_solution_quad(var, coeff, u, v):
    """
    Check whether (u, v) is solution to the quadratic diophantine equation.
    """
    x = var[0]
    y = var[1]

    eq = x**2*coeff[x**2] + x*y*coeff[x*y] + y**2*coeff[y**2] + x*coeff[x] + y*coeff[y] + coeff[Integer(1)]

    return simplify(Subs(eq, (x, y), (u, v)).doit()) == 0
Beispiel #13
0
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)
Beispiel #14
0
def is_normal_transformation_ok(eq):
    A = transformation_to_normal(eq)
    X, Y, Z = A * Matrix([x, y, z])
    simplified = _mexpand(Subs(eq, (x, y, z), (X, Y, Z)).doit())

    coeff = dict(
        [reversed(t.as_independent(*[X, Y, Z])) for t in simplified.args])
    for term in [X * Y, Y * Z, X * Z]:
        if term in coeff.keys():
            return False

    return True
Beispiel #15
0
def test_issue_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) == S.Zero
    e5 = Subs(Derivative(f(x), x), (y, z), (y, 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),))
Beispiel #16
0
def test_subs_in_derivative():
    expr = sin(x*exp(y))
    u = Function('u')
    v = Function('v')
    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))
    assert Derivative(f(x, y), y).subs(x, g(x, y)) == Subs(Derivative(f(x, y), y), x, g(x, 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))
    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))
    # 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
    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.replace(f, F).doit() - concrete).simplify() == 0
Beispiel #17
0
def test_complicated_derivative_with_Indexed():
    x, y = symbols("x,y", cls=IndexedBase)
    sigma = symbols("sigma")
    i, j, k = symbols("i,j,k")
    m0, m1, m2, m3, m4, m5 = symbols("m0:6")
    f = Function("f")

    expr = f((x[i] - y[i]) ** 2 / sigma)
    _xi_1 = symbols("xi_1", cls=Dummy)
    assert expr.diff(x[m0]).dummy_eq(
        (x[i] - y[i])
        * KroneckerDelta(i, m0)
        * 2
        * Subs(Derivative(f(_xi_1), _xi_1), (_xi_1,), ((x[i] - y[i]) ** 2 / sigma,))
        / sigma
    )
    assert (
        expr.diff(x[m0])
        .diff(x[m1])
        .dummy_eq(
            2
            * KroneckerDelta(i, m0)
            * KroneckerDelta(i, m1)
            * Subs(Derivative(f(_xi_1), _xi_1), (_xi_1,), ((x[i] - y[i]) ** 2 / sigma,))
            / sigma
            + 4
            * (x[i] - y[i]) ** 2
            * KroneckerDelta(i, m0)
            * KroneckerDelta(i, m1)
            * Subs(
                Derivative(f(_xi_1), _xi_1, _xi_1),
                (_xi_1,),
                ((x[i] - y[i]) ** 2 / sigma,),
            )
            / sigma ** 2
        )
    )
Beispiel #18
0
def test_deriv1():
    # These all requre derivatives evaluated at a point (issue 4719) to work.
    # See issue 4624
    assert f(2*x).diff(x) == 2*Subs(Derivative(f(x), x), Tuple(x), Tuple(2*x))
    assert (f(x)**3).diff(x) == 3*f(x)**2*f(x).diff(x)
    assert (
        f(2*x)**3).diff(x) == 6*f(2*x)**2*Subs(Derivative(f(x), x), Tuple(x),
            Tuple(2*x))

    assert f(2 + x).diff(x) == Subs(Derivative(f(x), x), Tuple(x), Tuple(x + 2))
    assert f(2 + 3*x).diff(x) == 3*Subs(Derivative(f(x), x), Tuple(x),
            Tuple(3*x + 2))
    assert f(3*sin(x)).diff(x) == 3*cos(x)*Subs(Derivative(f(x), x),
            Tuple(x), Tuple(3*sin(x)))

    # See issue 8510
    assert f(x, x + z).diff(x) == Subs(Derivative(f(y, x + z), y), Tuple(y), Tuple(x)) \
            + Subs(Derivative(f(x, y), y), Tuple(y), Tuple(x + z))
    assert f(x, x**2).diff(x) == Subs(Derivative(f(y, x**2), y), Tuple(y), Tuple(x)) \
            + 2*x*Subs(Derivative(f(x, y), y), Tuple(y), Tuple(x**2))
Beispiel #19
0
def solutions_ok_quadratic(eq):
    """
    Determines whether solutions returned by diop_solve() satisfy the original
    equation.
    """
    s = diop_solve(eq)
    x, y = symbols("x, y", Integer=True)
    ok = True

    while len(s) and ok:
        u, v = s.pop()

        if simplify(simplify(Subs(eq, (x, y), (u, v)).doit())) != 0:
            ok = False
    return ok
Beispiel #20
0
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))
Beispiel #21
0
def test_dont_cse_tuples():
    from sympy import Subs
    f = Function("f")
    g = Function("g")

    name_val, (expr, ) = cse(
        Subs(f(x, y), (x, y), (0, 1)) + Subs(g(x, y), (x, y), (0, 1)))

    assert name_val == []
    assert expr == (Subs(f(x, y), (x, y),
                         (0, 1)) + Subs(g(x, y), (x, y), (0, 1)))

    name_val, (expr, ) = cse(
        Subs(f(x, y), (x, y), (0, x + y)) + Subs(g(x, y), (x, y), (0, x + y)))

    assert name_val == [(x0, x + y)]
    assert expr == Subs(f(x, y), (x, y), (0, x0)) + \
        Subs(g(x, y), (x, y), (0, x0))
Beispiel #22
0
def test_deriv1():
    # These all require derivatives evaluated at a point (issue 4719) to work.
    # See issue 4624
    assert f(2 * x).diff(x) == 2 * Subs(Derivative(f(x), x), x, 2 * x)
    assert (f(x)**3).diff(x) == 3 * f(x)**2 * f(x).diff(x)
    assert (f(
        2 *
        x)**3).diff(x) == 6 * f(2 * x)**2 * Subs(Derivative(f(x), x), x, 2 * x)

    assert f(2 + x).diff(x) == Subs(Derivative(f(x), x), x, x + 2)
    assert f(2 + 3 * x).diff(x) == 3 * Subs(Derivative(f(x), x), x, 3 * x + 2)
    assert f(3 * sin(x)).diff(x) == 3 * cos(x) * Subs(Derivative(f(x), x), x,
                                                      3 * sin(x))

    # See issue 8510
    assert f(x, x + z).diff(x) == (Subs(Derivative(f(y, x + z), y), y, x) +
                                   Subs(Derivative(f(x, y), y), y, x + z))
    assert f(x,
             x**2).diff(x) == (2 * x * Subs(Derivative(f(x, y), y), y, x**2) +
                               Subs(Derivative(f(y, x**2), y), y, x))
    # but Subs is not always necessary
    assert f(x, g(y)).diff(g(y)) == Derivative(f(x, g(y)), g(y))
Beispiel #23
0
def test_879():
    f = Function('f')
    assert f(x).series(x, 0, 3, dir='-') == \
            f(0) + x*Subs(Derivative(f(x), x), (x,), (0,)) + \
            x**2*Subs(Derivative(f(x), x, x), (x,), (0,))/2 + O(x**3)
    assert f(x).series(x, 0, 3) == \
            f(0) + x*Subs(Derivative(f(x), x), (x,), (0,)) + \
            x**2*Subs(Derivative(f(x), x, x), (x,), (0,))/2 + O(x**3)
    assert f(x**2).series(x, 0, 3) == \
            f(0) + x**2*Subs(Derivative(f(x), x), (x,), (0,)) + O(x**3)
    assert f(x**2+1).series(x, 0, 3) == \
            f(1) + x**2*Subs(Derivative(f(x), x), (x,), (1,)) + O(x**3)

    class TestF(Function):
        pass

    assert TestF(x).series(x, 0, 3) ==  TestF(0) + \
            x*Subs(Derivative(TestF(x), x), (x,), (0,)) + \
            x**2*Subs(Derivative(TestF(x), x, x), (x,), (0,))/2 + O(x**3)
Beispiel #24
0
def test_issue_3978():
    f = Function("f")
    assert f(x).series(x, 0, 3, dir="-") == f(0) + x * Subs(
        Derivative(f(x), x), x, 0
    ) + x ** 2 * Subs(Derivative(f(x), x, x), x, 0) / 2 + O(x ** 3)
    assert f(x).series(x, 0, 3) == f(0) + x * Subs(
        Derivative(f(x), x), x, 0
    ) + x ** 2 * Subs(Derivative(f(x), x, x), x, 0) / 2 + O(x ** 3)
    assert f(x ** 2).series(x, 0, 3) == f(0) + x ** 2 * Subs(
        Derivative(f(x), x), x, 0
    ) + O(x ** 3)
    assert f(x ** 2 + 1).series(x, 0, 3) == f(1) + x ** 2 * Subs(
        Derivative(f(x), x), x, 1
    ) + O(x ** 3)

    class TestF(Function):
        pass

    assert TestF(x).series(x, 0, 3) == TestF(0) + x * Subs(
        Derivative(TestF(x), x), x, 0
    ) + x ** 2 * Subs(Derivative(TestF(x), x, x), x, 0) / 2 + O(x ** 3)
Beispiel #25
0
def _find_DN(var, coeff):

    x = var[0]
    y = var[1]
    X, Y = symbols("X, Y", integer=True)
    A , B = _transformation_to_pell(var, coeff)

    u = (A*Matrix([X, Y]) + B)[0]
    v = (A*Matrix([X, Y]) + B)[1]
    eq = x**2*coeff[x**2] + x*y*coeff[x*y] + y**2*coeff[y**2] + x*coeff[x] + y*coeff[y] + coeff[Integer(1)]

    simplified = simplify(Subs(eq, (x, y), (u, v)).doit())

    coeff = dict([reversed(t.as_independent(*[X, Y])) for t in simplified.args])

    for term in [X**2, Y**2, Integer(1)]:
        if term not in coeff.keys():
            coeff[term] = Integer(0)

    return -coeff[Y**2]/coeff[X**2], -coeff[Integer(1)]/coeff[X**2]
Beispiel #26
0
def test_dsolve_all_hint():
    eq = f(x).diff(x)
    output = dsolve(eq, hint='all')

    # Match the Dummy variables:
    sol1 = output['separable_Integral']
    _y = sol1.lhs.args[1][0]
    sol1 = output['1st_homogeneous_coeff_subs_dep_div_indep_Integral']
    _u1 = sol1.rhs.args[1].args[1][0]

    expected = {'Bernoulli_Integral': Eq(f(x), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_best': Eq(f(x), C1),
        'Bernoulli': Eq(f(x), C1),
        'nth_algebraic': Eq(f(x), C1),
        'nth_linear_euler_eq_homogeneous': Eq(f(x), C1),
        'nth_linear_constant_coeff_homogeneous': Eq(f(x), C1),
        'separable': Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_indep_div_dep': Eq(f(x), C1),
        'nth_algebraic_Integral': Eq(f(x), C1),
        '1st_linear': Eq(f(x), C1),
        '1st_linear_Integral': Eq(f(x), C1 + Integral(0, x)),
        '1st_exact': Eq(f(x), C1),
        '1st_exact_Integral': Eq(Subs(Integral(0, x) + Integral(1, _y), _y, f(x)), C1),
        'lie_group': Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep': Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral': Eq(log(x), C1 + Integral(-1/_u1, (_u1, f(x)/x))),
        '1st_power_series': Eq(f(x), C1),
        'separable_Integral': Eq(Integral(1, (_y, f(x))), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral': Eq(f(x), C1),
        'best': Eq(f(x), C1),
        'best_hint': 'nth_algebraic',
        'default': 'nth_algebraic',
        'order': 1}
    assert output == expected

    assert dsolve(eq, hint='best') == Eq(f(x), C1)
def test_deriv1():
    # These all requre derivatives evaluated at a point (issue 1620) to work.
    # See issue 1525
    f = Function('f')
    g = Function('g')
    x = Symbol('x')

    assert f(g(x)).diff(x) == Derivative(g(x), x) * Subs(
        Derivative(f(x), x), Tuple(x), Tuple(g(x)))
    assert f(
        2 * x).diff(x) == 2 * Subs(Derivative(f(x), x), Tuple(x), Tuple(2 * x))
    assert (f(x)**3).diff(x) == 3 * f(x)**2 * f(x).diff(x)
    assert (f(2 * x)**3).diff(x) == 6 * f(2 * x)**2 * Subs(
        Derivative(f(x), x), Tuple(x), Tuple(2 * x))

    assert f(2 + x).diff(x) == Subs(Derivative(f(x), x), Tuple(x),
                                    Tuple(x + 2))
    assert f(2 + 3 * x).diff(x) == 3 * Subs(Derivative(f(x), x), Tuple(x),
                                            Tuple(3 * x + 2))
    assert f(sin(x)).diff(x) == cos(x) * Subs(Derivative(f(x), x), Tuple(x),
                                              Tuple(sin(x)))
    assert f(3 * sin(x)).diff(x) == 3 * cos(x) * Subs(Derivative(
        f(x), x), Tuple(x), Tuple(3 * sin(x)))
Beispiel #28
0
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)
Beispiel #29
0
def test_Subs2():
    # this reflects a limitation of subs(), probably won't fix
    assert Subs(f(x), x**2, x).doit() == f(sqrt(x))
Beispiel #30
0
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)