def test_diff(): f = Function('f') g = Function('g') assert simplify(g(x).diff(x)*f(x).diff(x) - f(x).diff(x)*g(x).diff(x)) == 0 assert simplify(2*f(x)*f(x).diff(x) - diff(f(x)**2, x)) == 0 assert simplify(diff(1/f(x), x) + f(x).diff(x)/f(x)**2) == 0 assert simplify(f(x).diff(x, y) - f(x).diff(y, x)) == 0
def test_simplify_expr(): A = Symbol('A') f = Function('f') assert all(simplify(tmp) == tmp for tmp in [I, E, oo, x, -x, -oo, -E, -I]) e = 1 / x + 1 / y assert e != (x + y) / (x * y) assert simplify(e) == (x + y) / (x * y) e = A**2 * s**4 / (4 * pi * k * m**3) assert simplify(e) == e e = (4 + 4 * x - 2 * (2 + 2 * x)) / (2 + 2 * x) assert simplify(e) == 0 e = (-4 * x * y**2 - 2 * y**3 - 2 * x**2 * y) / (x + y)**2 assert simplify(e) == -2 * y e = -x - y - (x + y)**(-1) * y**2 + (x + y)**(-1) * x**2 assert simplify(e) == -2 * y e = (x + x * y) / x assert simplify(e) == 1 + y e = (f(x) + y * f(x)) / f(x) assert simplify(e) == 1 + y e = (2 * (1 / n - cos(n * pi) / n)) / pi assert simplify(e) == (-cos(pi * n) + 1) / (pi * n) * 2 e = integrate(1 / (x**3 + 1), x).diff(x) assert simplify(e) == 1 / (x**3 + 1) e = integrate(x / (x**2 + 3 * x + 1), x).diff(x) assert simplify(e) == x / (x**2 + 3 * x + 1) f = Symbol('f') A = Matrix([[2 * k - m * w**2, -k], [-k, k - m * w**2]]).inv() assert simplify((A*Matrix([0, f]))[1]) == \ -f*(2*k - m*w**2)/(k**2 - (k - m*w**2)*(2*k - m*w**2)) f = -x + y / (z + t) + z * x / (z + t) + z * a / (z + t) + t * x / (z + t) assert simplify(f) == (y + a * z) / (z + t) A, B = symbols('A,B', commutative=False) assert simplify(A * B - B * A) == A * B - B * A assert simplify(A / (1 + y / x)) == x * A / (x + y) assert simplify(A * (1 / x + 1 / y)) == A / x + A / y # (x + y)*A/(x*y) assert simplify(log(2) + log(3)) == log(6) assert simplify(log(2 * x) - log(2)) == log(x) assert simplify(hyper([], [], x)) == exp(x)
def test_separatevars(): n = Symbol('n') assert separatevars(2 * n * x * z + 2 * x * y * z) == 2 * x * z * (n + y) assert separatevars(x * z + x * y * z) == x * z * (1 + y) assert separatevars(pi * x * z + pi * x * y * z) == pi * x * z * (1 + y) assert separatevars(x*y**2*sin(x) + x*sin(x)*sin(y)) == \ x*(sin(y) + y**2)*sin(x) assert separatevars(x * exp(x + y) + x * exp(x)) == x * (1 + exp(y)) * exp(x) assert separatevars((x * (y + 1))**z).is_Pow # != x**z*(1 + y)**z assert separatevars(1 + x + y + x * y) == (x + 1) * (y + 1) assert separatevars(y/pi*exp(-(z - x)/cos(n))) == \ y*exp(x/cos(n))*exp(-z/cos(n))/pi assert separatevars((x + y) * (x - y) + y**2 + 2 * x + 1) == (x + 1)**2 # issue sympy/sympy#4858 p = Symbol('p', positive=True) assert separatevars(sqrt(p**2 + x * p**2)) == p * sqrt(1 + x) assert separatevars(sqrt(y * (p**2 + x * p**2))) == p * sqrt(y * (1 + x)) assert separatevars(sqrt(y*(p**2 + x*p**2)), force=True) == \ p*sqrt(y)*sqrt(1 + x) # issue sympy/sympy#4865 assert separatevars(sqrt(x * y)).is_Pow assert separatevars(sqrt(x * y), force=True) == sqrt(x) * sqrt(y) # issue sympy/sympy#4957 # any type sequence for symbols is fine assert separatevars(((2*x + 2)*y), dict=True, symbols=()) == \ {'coeff': 1, x: 2*x + 2, y: y} # separable assert separatevars(((2*x + 2)*y), dict=True, symbols=[x]) == \ {'coeff': y, x: 2*x + 2} assert separatevars(((2*x + 2)*y), dict=True, symbols=[]) == \ {'coeff': 1, x: 2*x + 2, y: y} assert separatevars(((2*x + 2)*y), dict=True) == \ {'coeff': 1, x: 2*x + 2, y: y} assert separatevars(((2*x + 2)*y), dict=True, symbols=None) == \ {'coeff': y*(2*x + 2)} # not separable assert separatevars(3, dict=True) is None assert separatevars(2 * x + y, dict=True, symbols=()) is None assert separatevars(2 * x + y, dict=True) is None assert separatevars(2 * x + y, dict=True, symbols=None) == { 'coeff': 2 * x + y } # issue sympy/sympy#4808 n, m = symbols('n,m', commutative=False) assert separatevars(m + n * m) == (1 + n) * m assert separatevars(x + x * n) == x * (1 + n) # issue sympy/sympy#4910 f = Function('f') assert separatevars(f(x) + x * f(x)) == f(x) + x * f(x) # a noncommutable object present eq = x * (1 + hyper((), (), y * z)) assert separatevars(eq) == eq
def test_sympyissue_6920(): e = [cos(x) + I*sin(x), cos(x) - I*sin(x), cosh(x) - sinh(x), cosh(x) + sinh(x)] ok = [exp(I*x), exp(-I*x), exp(-x), exp(x)] # wrap in f to show that the change happens wherever ei occurs f = Function('f') assert [simplify(f(ei)).args[0] for ei in e] == ok
def test_collect_D_0(): D = Derivative f = Function('f') fxx = D(f(x), x, x) # collect does not distinguish nested derivatives, so it returns # -- (a + b)*D(D(f, x), x) assert collect(a * fxx + b * fxx, fxx) == (a + b) * fxx
def test_collect_3(): """Collect with respect to a product""" f = Function('f') assert collect(-x / 8 + x * y, -x) == x * (y - Rational(1, 8)) assert collect(1 + x * (y**2), x * y) == 1 + x * (y**2) assert collect(x * y + a * x * y, x * y) == x * y * (1 + a) assert collect(1 + x * y + a * x * y, x * y) == 1 + x * y * (1 + a) assert collect(a * x * f(x) + b * (x * f(x)), x * f(x)) == x * (a + b) * f(x) assert collect(a * x * log(x) + b * (x * log(x)), x * log(x)) == x * (a + b) * log(x) assert collect(a*x**2*log(x)**2 + b*(x*log(x))**2, x*log(x)) == \ x**2*log(x)**2*(a + b) # with respect to a product of three symbols assert collect(y * x * z + a * x * y * z, x * y * z) == (1 + a) * x * y * z
def test_sympyissue_4194(): # simplify should call cancel f = Function('f') assert simplify((4 * x + 6 * f(y)) / (2 * x + 3 * f(y))) == 2
def test_collect_Wild(): """Collect with respect to functions with Wild argument""" f = Function('f') w1 = Wild('.1') w2 = Wild('.2') assert collect(f(x) + a * f(x), f(w1)) == (1 + a) * f(x) assert collect(f(x, y) + a * f(x, y), f(w1)) == f(x, y) + a * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w2)) == (1 + a) * f(x, y) assert collect(f(x, y) + a * f(x, y), f(w1, w1)) == f(x, y) + a * f(x, y) assert collect(f(x, x) + a * f(x, x), f(w1, w1)) == (1 + a) * f(x, x) assert collect(a * (x + 1)**y + (x + 1)**y, w1**y) == (1 + a) * (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, w1**b) == \ a*(x + 1)**y + (x + 1)**y assert collect(a*(x + 1)**y + (x + 1)**y, (x + 1)**w2) == \ (1 + a)*(x + 1)**y assert collect(a * (x + 1)**y + (x + 1)**y, w1**w2) == (1 + a) * (x + 1)**y
def test_collect_issues(): D = Derivative f = Function('f') e = (1 + x * D(f(x), x) + D(f(x), x)) / f(x) assert collect(e.expand(), f(x).diff(x)) != e
def test_collect_D(): D = Derivative f = Function('f') fx = D(f(x), x) fxx = D(f(x), x, x) assert collect(a * fx + b * fx, fx) == (a + b) * fx assert collect(a * D(fx, x) + b * D(fx, x), fx) == (a + b) * D(fx, x) assert collect(a * fxx + b * fxx, fx) == (a + b) * D(fx, x) # issue sympy/sympy#4784 assert collect(5 * f(x) + 3 * fx, fx) == 5 * f(x) + 3 * fx assert collect(f(x) + f(x)*diff(f(x), x) + x*diff(f(x), x)*f(x), f(x).diff(x), exact=True) == \ (x*f(x) + f(x))*D(f(x), x) + f(x) assert collect(1/f(x) + 1/f(x)*diff(f(x), x) + x*diff(f(x), x)/f(x), f(x).diff(x), exact=True) == \ (1/f(x) + x/f(x))*D(f(x), x) + 1/f(x)