Example #1
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) == \
        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)))
Example #2
0
def test_trigsimp2():
    assert trigsimp(cos(x)**2*sin(y)**2 + cos(x)**2*cos(y)**2 + sin(x)**2,
            recursive=True) == 1
    assert trigsimp(sin(x)**2*sin(y)**2 + sin(x)**2*cos(y)**2 + cos(x)**2,
            recursive=True) == 1
    assert trigsimp(
        Subs(x, x, sin(y)**2 + cos(y)**2)) == Subs(x, x, 1)
Example #3
0
def test_mul():
    x, y, z, a, b, c = symbols('x y z a b c')
    A, B, C = symbols('A B C', commutative=0)
    assert (x * y * z).subs(z * x, y) == y**2
    assert (z * x).subs(1 / x, z) == z * x
    assert (x * y / z).subs(1 / z, a) == a * x * y
    assert (x * y / z).subs(x / z, a) == a * y
    assert (x * y / z).subs(y / z, a) == a * x
    assert (x * y / z).subs(x / z, 1 / a) == y / a
    assert (x * y / z).subs(x, 1 / a) == y / (z * a)
    assert (2 * x * y).subs(5 * x * y, z) != 2 * z / 5
    assert (x * y * A).subs(x * y, a) == a * A
    assert Subs(x * y * A, x * y, a).is_commutative is False
    assert (x**2 * y**(3 * x / 2)).subs(x * y**(x / 2), 2) == 4 * y**(x / 2)
    assert (x * exp(x * 2)).subs(x * exp(x), 2) == 2 * exp(x)
    assert ((x**(2 * y))**3).subs(x**y, 2) == 64
    assert (x * A * B).subs(x * A, y) == y * B
    assert (x * y * (1 + x) * (1 + x * y)).subs(x * y, 2) == 6 * (1 + x)
    assert ((1 + A * B) * A * B).subs(A * B, x * A * B)
    assert (x * a / z).subs(x / z, A) == a * A
    assert Subs(x * a / z, x / z, A).is_commutative is False
    assert (x**3 * A).subs(x**2 * A, a) == a * x
    assert (x**2 * A * B).subs(x**2 * B, a) == a * A
    assert (x**2 * A * B).subs(x**2 * A, a) == a * B
    assert (b*A**3/(a**3*c**3)).subs(a**4*c**3*A**3/b**4, z) == \
        b*A**3/(a**3*c**3)
    assert (6 * x).subs(2 * x, y) == 3 * y
    assert (y * exp(3 * x / 2)).subs(y * exp(x), 2) == 2 * exp(x / 2)
    assert (y * exp(3 * x / 2)).subs(y * exp(x), 2) == 2 * exp(x / 2)
    assert (A**2 * B * A**2 * B * A**2).subs(A * B * A, C) == A * C**2 * A
    assert (x * A**3).subs(x * A, y) == y * A**2
    assert (x**2 * A**3).subs(x * A, y) == y**2 * A
    assert (x * A**3).subs(x * A, B) == B * A**2
    assert (x * A * B * A * exp(x * A * B)).subs(x * A,
                                                 B) == B**2 * A * exp(B * B)
    assert (x**2 * A * B * A * exp(x * A * B)).subs(x * A,
                                                    B) == B**3 * exp(B**2)
    assert (x**3*A*exp(x*A*B)*A*exp(x*A*B)).subs(x*A, B) == \
        x*B*exp(B**2)*B*exp(B**2)
    assert (x * A * B * C * A * B).subs(x * A * B, C) == C**2 * A * B
    assert (-I * a * b).subs(a * b, 2) == -2 * I

    # issue sympy/sympy#6361
    assert (-8 * I * a).subs(-2 * a, 1) == 4 * I
    assert (-I * a).subs(-a, 1) == I

    # issue sympy/sympy#6441
    assert (4 * x**2).subs(2 * x, y) == y**2
    assert (2 * 4 * x**2).subs(2 * x, y) == 2 * y**2
    assert (-x**3 / 9).subs(-x / 3, z) == -z**2 * x
    assert (-x**3 / 9).subs(x / 3, z) == -z**2 * x
    assert (-2 * x**3 / 9).subs(x / 3, z) == -2 * x * z**2
    assert (-2 * x**3 / 9).subs(-x / 3, z) == -2 * x * z**2
    assert (-2 * x**3 / 9).subs(-2 * x, z) == z * x**2 / 9
    assert (-2 * x**3 / 9).subs(2 * x, z) == -z * x**2 / 9
    assert (2 * (3 * x / 5 / 7)**2).subs(3 * x / 5,
                                         z) == 2 * (Rational(1, 7))**2 * z**2
    assert (4 * x).subs(-2 * x, z) == 4 * x  # try keep subs literal
Example #4
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
Example #5
0
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)))
Example #6
0
def test_sympyissue_7068():
    f = Function('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
Example #7
0
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
Example #8
0
def test_sympyissue_7068():
    from diofant.abc import a, b
    f = Function('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
Example #9
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
Example #10
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 = {val: key for key, val in (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(coeff[Y**2]/coeff[X**2], Integer) and isinstance(coeff[Integer(1)]/coeff[X**2], Integer)

    return True
Example #11
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
Example #12
0
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
Example #13
0
def test_deriv1():
    # These all requre derivatives evaluated at a point (issue sympy/sympy#4719) to work.
    # See issue sympy/sympy#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 sympy/sympy#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) == Subs(Derivative(f(y, x**2), y), (y, x)) \
        + 2*x*Subs(Derivative(f(x, y), y), (y, x**2))
Example #14
0
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, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y, z), (x, 0), (y, 0), (z, 1))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) == \
        Subs(f(x, y), (x, 0), (y, 1), (z, 2))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y) + z, (x, 0), (y, 1), (z, 0))
    assert Subs(f(x, y), (x, 0), (y, 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))
    pytest.raises(ValueError, lambda: Subs(f(x, y), (x, 0), (x, 0), (y, 1)))

    assert len(Subs(f(x, y), (x, 0), (y, 1)).variables) == 2
    assert Subs(f(x, y), (x, 0), (y, 1)).point == Tuple(0, 1)

    assert Subs(f(x), (x, 0)) == Subs(f(y), (y, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)) == Subs(f(x, y), (y, 1), (x, 0))
    assert Subs(f(x)*y, (x, 0), (y, 1)) == Subs(f(y)*x, (y, 0), (x, 1))
    assert Subs(f(x)*y, (x, 1), (y, 1)) == Subs(f(y)*x, (x, 1), (y, 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, 0), (y, 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, 0), (y, pi/3)).evalf(2, strict=False) == \
        Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        z + Rational('1/2').evalf(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})
Example #15
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
Example #16
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))
Example #17
0
def test_sympyissue_11799():
    n = 2
    M = Manifold('M', n)
    P = Patch('P', M)

    coord = CoordSystem('coord', P, ['x%s' % i for i in range(n)])
    x = coord.coord_functions()
    dx = coord.base_oneforms()

    f = Function('f')
    g = [[f(x[0], x[1])**2, 0], [0, f(x[0], x[1])**2]]
    metric = sum(g[i][j] * TP(dx[i], dx[j]) for i in range(n)
                 for j in range(n))

    R = metric_to_Riemann_components(metric)
    d = Symbol('d')

    assert (R[0, 1, 0, 1] == -Subs(Derivative(f(d, x[1]), d, d),
                                   (d, x[0])) / f(x[0], x[1]) -
            Subs(Derivative(f(x[0], d), d, d), (d, x[1])) / f(x[0], x[1]) +
            Subs(Derivative(f(d, x[1]), d), (d, x[0]))**2 / f(x[0], x[1])**2 +
            Subs(Derivative(f(x[0], d), d), (d, x[1]))**2 / f(x[0], x[1])**2)
Example #18
0
def test_dont_cse_tuples():
    f = Function("f")
    g = Function("g")

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

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

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

    assert name_val == [(x0, x + y)]
    assert expr == Subs(f(x, y), (x, 0), (y, x0)) + Subs(g(x, y), (x, 0), (y, x0))
Example #19
0
def test_dont_cse_tuples():
    from diofant 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))
Example #20
0
def test_sympyissue_3978():
    assert f(x).series(x, 0, 3, dir=+1) == \
        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)
Example #21
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)
Example #22
0
def test_underscores():
    _0, _1 = symbols('_0 _1')
    e = Subs(_0 + _1, (_0, 1), (_1, 0))
    assert e._expr == Symbol('__0') + Symbol('__1')
Example #23
0
def test_mul():
    A, B, C = symbols('A B C', commutative=False)
    assert (x * y * z).subs({z * x: y}) == y**2
    assert (z * x).subs({1 / x: z}) == z * x
    assert (x * y / z).subs({1 / z: a}) == a * x * y
    assert (x * y / z).subs({x / z: a}) == a * y
    assert (x * y / z).subs({y / z: a}) == a * x
    assert (x * y / z).subs({x / z: 1 / a}) == y / a
    assert (x * y / z).subs({x: 1 / a}) == y / (z * a)
    assert (2 * x * y).subs({5 * x * y: z}) != 2 * z / 5
    assert (x * y * A).subs({x * y: a}) == a * A
    assert Subs(x * y * A, (x * y, a)).is_commutative is False
    assert (x**2 * y**(3 * x / 2)).subs({x * y**(x / 2): 2}) == 4 * y**(x / 2)
    assert (x * exp(x * 2)).subs({x * exp(x): 2}) == 2 * exp(x)
    assert ((x**(2 * y))**3).subs({x**y: 2}) == 64
    assert (x * A * B).subs({x * A: y}) == y * B
    assert (x * y * (1 + x) * (1 + x * y)).subs({x * y: 2}) == 6 * (1 + x)
    assert ((1 + A * B) * A * B).subs({A * B: x * A * B})
    assert (x * a / z).subs({x / z: A}) == a * A
    assert Subs(x * a / z, (x / z, A)).is_commutative is False
    assert (x**3 * A).subs({x**2 * A: a}) == a * x
    assert (x**2 * A * B).subs({x**2 * B: a}) == a * A
    assert (x**2 * A * B).subs({x**2 * A: a}) == a * B
    assert (b*A**3/(a**3*c**3)).subs({a**4*c**3*A**3/b**4: z}) == \
        b*A**3/(a**3*c**3)
    assert (6 * x).subs({2 * x: y}) == 3 * y
    assert (y * exp(3 * x / 2)).subs({y * exp(x): 2}) == 2 * exp(x / 2)
    assert (y * exp(3 * x / 2)).subs({y * exp(x): 2}) == 2 * exp(x / 2)
    assert (A**2 * B * A**2 * B * A**2).subs({A * B * A: C}) == A * C**2 * A
    assert (x * A**3).subs({x * A: y}) == y * A**2
    assert (x**2 * A**3).subs({x * A: y}) == y**2 * A
    assert (x * A**3).subs({x * A: B}) == B * A**2
    assert (x * A * B * A * exp(x * A * B)).subs({x * A:
                                                  B}) == B**2 * A * exp(B * B)
    assert (x**2 * A * B * A * exp(x * A * B)).subs({x * A:
                                                     B}) == B**3 * exp(B**2)
    assert (x**3*A*exp(x*A*B)*A*exp(x*A*B)).subs({x*A: B}) == \
        x*B*exp(B**2)*B*exp(B**2)
    assert (x * A * B * C * A * B).subs({x * A * B: C}) == C**2 * A * B
    assert (-I * a * b).subs({a * b: 2}) == -2 * I

    # issue sympy/sympy#6361
    assert (-8 * I * a).subs({-2 * a: 1}) == 4 * I
    assert (-I * a).subs({-a: 1}) == I

    # issue sympy/sympy#6441
    assert (4 * x**2).subs({2 * x: y}) == y**2
    assert (2 * 4 * x**2).subs({2 * x: y}) == 2 * y**2
    assert (-x**3 / 9).subs({-x / 3: z}) == -z**2 * x
    assert (-x**3 / 9).subs({x / 3: z}) == -z**2 * x
    assert (-2 * x**3 / 9).subs({x / 3: z}) == -2 * x * z**2
    assert (-2 * x**3 / 9).subs({-x / 3: z}) == -2 * x * z**2
    assert (-2 * x**3 / 9).subs({-2 * x: z}) == z * x**2 / 9
    assert (-2 * x**3 / 9).subs({2 * x: z}) == -z * x**2 / 9
    assert (2 * (3 * x / 5 / 7)**2).subs({3 * x / 5:
                                          z}) == 2 * Rational(1, 7)**2 * z**2
    assert (4 * x).subs({-2 * x: z}) == 4 * x  # try keep subs literal

    assert (A**2 * B**2).subs({A * B**3: C}) == A**2 * B**2
    assert (A**Rational(5, 3) * B**3).subs({sqrt(A) * B:
                                            C}) == A**Rational(5, 3) * B**3
    assert (A**2 * B**2 * A).subs({A**2 * B * A: C}) == A**2 * B**2 * A
Example #24
0
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) == 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(Function('f')(y), y, y),
                                            (y, ), (g(x), ))
Example #25
0
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)))
Example #26
0
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, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y, z), (x, 0), (y, 0), (z, 1))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) == \
        Subs(f(x, y), (x, 0), (y, 1), (z, 2))
    assert Subs(f(x, y), (x, 0), (y, 1), (z, 1)) != \
        Subs(f(x, y) + z, (x, 0), (y, 1), (z, 0))
    assert Subs(f(x, y), (x, 0), (y, 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))
    pytest.raises(ValueError, lambda: Subs(f(x, y), (x, 0), (x, 0), (y, 1)))

    assert len(Subs(f(x, y), (x, 0), (y, 1)).variables) == 2
    assert Subs(f(x, y), (x, 0), (y, 1)).point == Tuple(0, 1)

    assert Subs(f(x), (x, 0)) == Subs(f(y), (y, 0))
    assert Subs(f(x, y), (x, 0), (y, 1)) == Subs(f(x, y), (y, 1), (x, 0))
    assert Subs(f(x) * y, (x, 0), (y, 1)) == Subs(f(y) * x, (y, 0), (x, 1))
    assert Subs(f(x) * y, (x, 1), (y, 1)) == Subs(f(y) * x, (x, 1), (y, 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, 0), (y, 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, 0), (y, pi/3)).evalf(2, strict=False) == \
        Subs(f(x)*cos(y) + z, (x, 0), (y, pi/3)).evalf(2, strict=False) == \
        z + Rational('1/2').evalf(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})
Example #27
0
def test_sign():
    assert sign(1.2) == 1
    assert sign(-1.2) == -1
    assert sign(3 * I) == I
    assert sign(-3 * I) == -I
    assert sign(0) == 0
    assert sign(nan) == nan
    assert sign(2 + 2 * I).doit() == sqrt(2) * (2 + 2 * I) / 4
    assert sign(2 + 3 * I).simplify() == sign(2 + 3 * I)
    assert sign(2 + 2 * I).simplify() == sign(1 + I)
    assert sign(im(sqrt(1 - sqrt(3)))) == 1
    assert sign(sqrt(1 - sqrt(3))) == I

    x = Symbol('x')
    assert sign(x).is_finite is True
    assert sign(x).is_complex is True
    assert sign(x).is_imaginary is None
    assert sign(x).is_integer is None
    assert sign(x).is_extended_real is None
    assert sign(x).is_zero is None
    assert sign(x).doit() == sign(x)
    assert sign(1.2 * x) == sign(x)
    assert sign(2 * x) == sign(x)
    assert sign(I * x) == I * sign(x)
    assert sign(-2 * I * x) == -I * sign(x)
    assert sign(conjugate(x)) == conjugate(sign(x))

    p = Symbol('p', positive=True)
    n = Symbol('n', negative=True)
    m = Symbol('m', negative=True)
    assert sign(2 * p * x) == sign(x)
    assert sign(n * x) == -sign(x)
    assert sign(n * m * x) == sign(x)

    x = Symbol('x', imaginary=True)
    xn = Symbol('xn', imaginary=True, nonzero=True)
    assert sign(x).is_imaginary is True
    assert sign(x).is_integer is None
    assert sign(x).is_extended_real is None
    assert sign(x).is_zero is None
    assert sign(x).diff(x) == 2 * DiracDelta(-I * x)
    assert sign(xn).doit() == xn / abs(xn)
    assert conjugate(sign(x)) == -sign(x)

    x = Symbol('x', extended_real=True)
    assert sign(x).is_imaginary is None
    assert sign(x).is_integer is True
    assert sign(x).is_extended_real is True
    assert sign(x).is_zero is None
    assert sign(x).diff(x) == 2 * DiracDelta(x)
    assert sign(x).doit() == sign(x)
    assert conjugate(sign(x)) == sign(x)

    assert sign(sin(x)).series(x) == 1
    y = Symbol('y')
    assert sign(x * y).series(x).removeO() == sign(y)
    assert sign(I + x).series(
        x, n=2) == I + x * Subs(sign(x + I).diff(x), (x, 0)) + O(x**2)

    x = Symbol('x', nonzero=True)
    assert sign(x).is_imaginary is None
    assert sign(x).is_integer is None
    assert sign(x).is_extended_real is None
    assert sign(x).is_nonzero is True
    assert sign(x).doit() == x / abs(x)
    assert sign(abs(x)) == 1
    assert abs(sign(x)) == 1

    x = Symbol('x', positive=True)
    assert sign(x).is_imaginary is False
    assert sign(x).is_integer is True
    assert sign(x).is_extended_real is True
    assert sign(x).is_nonzero is True
    assert sign(x).doit() == x / abs(x)
    assert sign(abs(x)) == 1
    assert abs(sign(x)) == 1

    x = 0
    assert sign(x).is_imaginary is True
    assert sign(x).is_integer is True
    assert sign(x).is_extended_real is True
    assert sign(x).is_zero is True
    assert sign(x).doit() == 0
    assert sign(abs(x)) == 0
    assert abs(sign(x)) == 0

    nz = Symbol('nz', nonzero=True, integer=True)
    assert sign(nz).is_imaginary is False
    assert sign(nz).is_integer is True
    assert sign(nz).is_extended_real is True
    assert sign(nz).is_nonzero is True
    assert sign(nz)**2 == 1
    assert (sign(nz)**3).args == (sign(nz), 3)

    assert sign(Symbol('x', nonnegative=True)).is_nonnegative
    assert sign(Symbol('x', nonnegative=True)).is_nonpositive is None
    assert sign(Symbol('x', nonpositive=True)).is_nonnegative is None
    assert sign(Symbol('x', nonpositive=True)).is_nonpositive
    assert sign(Symbol('x', extended_real=True)).is_nonnegative is None
    assert sign(Symbol('x', extended_real=True)).is_nonpositive is None
    assert sign(Symbol('x', extended_real=True,
                       zero=False)).is_nonpositive is None

    x, y = Symbol('x', extended_real=True), Symbol('y')
    assert sign(x).rewrite(Piecewise) == \
        Piecewise((1, x > 0), (-1, x < 0), (0, True))
    assert sign(y).rewrite(Piecewise) == sign(y)
    assert sign(x).rewrite(Heaviside) == 2 * Heaviside(x) - 1
    assert sign(y).rewrite(Heaviside) == sign(y)
    assert sign(1 + I).rewrite(exp) == exp(I * pi / 4)

    # evaluate what can be evaluated
    assert sign(exp_polar(I * pi) * pi) is Integer(-1)

    eq = -sqrt(10 + 6 * sqrt(3)) + sqrt(1 + sqrt(3)) + sqrt(3 + 3 * sqrt(3))
    # if there is a fast way to know when and when you cannot prove an
    # expression like this is zero then the equality to zero is ok
    assert sign(eq) == 0
    q = 1 + sqrt(2) - 2 * sqrt(3) + 1331 * sqrt(6)
    p = cbrt(expand(q**3))
    d = p - q
    assert sign(d) == 0

    assert abs(sign(z)) == Abs(sign(z), evaluate=False)
Example #28
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))
Example #29
0
def test_series_of_Subs():
    subs1 = Subs(sin(x), (x, y))
    subs2 = Subs(sin(x) * cos(z), (x, y))
    subs3 = Subs(sin(x * z), (x, z), (y, x))
    subs4 = Subs(x, (x, z))

    res1 = Subs(x, (x, y)) + Subs(-x**3 / 6,
                                  (x, y)) + Subs(x**5 / 120, (x, y)) + O(y**6)
    res2 = Subs(z**4 * sin(x) / 24,
                (x, y)) + Subs(-z**2 * sin(x) / 2,
                               (x, y)) + Subs(sin(x), (x, y)) + O(z**6)
    res3 = Subs(x * z, (x, z), (y, x)) + O(z**6)

    assert subs1.series(x) == subs1
    assert subs1.series(y) == res1
    assert subs1.series(z) == subs1
    assert subs2.series(z) == res2
    assert subs3.series(x) == subs3
    assert subs3.series(z) == res3
    assert subs4.series(z) == subs4

    assert subs1.doit().series(x) == subs1.doit()
    assert subs1.doit().series(y) == res1.doit()
    assert subs1.doit().series(z) == subs1.doit()
    assert subs2.doit().series(z) == res2.doit()
    assert subs3.doit().series(x) == subs3.doit()
    assert subs3.doit().series(z) == res3.doit()
    assert subs4.doit().series(z) == subs4.doit()
Example #30
0
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)