def test_Lambda(): e = Lambda(x, x**2) assert e(4) == 16 assert e(x) == x**2 assert e(y) == y**2 assert Lambda((), 42)() == 42 assert Lambda((), 42) == Lambda((), 42) assert Lambda((), 42) != Lambda((), 43) assert Lambda((), f(x))() == f(x) assert Lambda((), 42).nargs == FiniteSet(0) assert Lambda(x, x**2) == Lambda(x, x**2) assert Lambda(x, x**2) == Lambda(y, y**2) assert Lambda(x, x**2) != Lambda(y, y**2 + 1) assert Lambda((x, y), x**y) == Lambda((y, x), y**x) assert Lambda((x, y), x**y) != Lambda((x, y), y**x) assert Lambda((x, y), x**y)(x, y) == x**y assert Lambda((x, y), x**y)(3, 3) == 3**3 assert Lambda((x, y), x**y)(x, 3) == x**3 assert Lambda((x, y), x**y)(3, y) == 3**y assert Lambda(x, f(x))(x) == f(x) assert Lambda(x, x**2)(e(x)) == x**4 assert e(e(x)) == x**4 assert Lambda((x, y), x + y).nargs == FiniteSet(2) p = x, y, z, t assert Lambda(p, t*(x + y + z))(*p) == t * (x + y + z) assert Lambda(x, 2*x) + Lambda(y, 2*y) == 2*Lambda(x, 2*x) assert Lambda(x, 2*x) not in [ Lambda(x, x) ] raises(TypeError, lambda: Lambda(1, x)) assert Lambda(x, 1)(1) is S.One
def test_f_expand_complex(): x = Symbol("x", real=True) assert f(x).expand(complex=True) == I * im(f(x)) + re(f(x)) assert exp(x).expand(complex=True) == exp(x) assert exp(I * x).expand(complex=True) == cos(x) + I * sin(x) assert exp(z).expand(complex=True) == cos(im(z)) * exp(re(z)) + I * sin(im(z)) * exp(re(z))
def test_derivative_subs_bug(): e = diff(g(x), x) assert e.subs(g(x), f(x)) != e assert e.subs(g(x), f(x)) == Derivative(f(x), x) assert e.subs(g(x), -f(x)) == Derivative(-f(x), x) assert e.subs(x, y) == Derivative(g(y), y)
def test_diff(): x = Symbol("x") y = Symbol("y") 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_is_commutative(): from sympy.physics.secondquant import NO, F, Fd m = Symbol('m', commutative=False) for f in (Sum, Product, Integral): assert f(z, (z, 1, 1)).is_commutative is True assert f(z*y, (z, 1, 6)).is_commutative is True assert f(m*x, (x, 1, 2)).is_commutative is False assert f(NO(Fd(x)*F(y))*z, (z, 1, 2)).is_commutative is False
def test_function_complex(): x = Symbol('x', complex=True) assert f(x).is_commutative is True assert sin(x).is_commutative is True assert exp(x).is_commutative is True assert log(x).is_commutative is True assert f(x).is_complex is True assert sin(x).is_complex is True assert exp(x).is_complex is True assert log(x).is_complex is True
def test_function_non_commutative(): x = Symbol('x', commutative=False) assert f(x).is_commutative is False assert sin(x).is_commutative is False assert exp(x).is_commutative is False assert log(x).is_commutative is False assert f(x).is_complex is False assert sin(x).is_complex is False assert exp(x).is_complex is False assert log(x).is_complex is False
def test_unhandled(): class MyExpr(Expr): def _eval_derivative(self, s): if not s.name.startswith('xi'): return self else: return None expr = MyExpr(x, y, z) assert diff(expr, x, y, f(x), z) == Derivative(expr, f(x), z) assert diff(expr, f(x), x) == Derivative(expr, f(x), x)
def test_simplify_expr(): x, y, z, k, n, m, w, f, s, A = symbols("x,y,z,k,n,m,w,f,s,A") 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) 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_simplify_function_inverse(): x, y = symbols('x, y') g = Function('g') class f(Function): def inverse(self, argindex=1): return g assert simplify(f(g(x))) == x assert simplify(f(g(sin(x)**2 + cos(x)**2))) == 1 assert simplify(f(g(x, y))) == f(g(x, y))
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
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)) == Derivative(f(g(x)), g(x), g(x))
def test_func_deriv(): assert f(x).diff(x) == Derivative(f(x), x) # issue 4534 assert f(x, y).diff(x, y) - f(x, y).diff(y, x) == 0 assert Derivative(f(x, y), x, y).args[1:] == ((x, 1), (y, 1)) assert Derivative(f(x, y), y, x).args[1:] == ((y, 1), (x, 1)) assert (Derivative(f(x, y), x, y) - Derivative(f(x, y), y, x)).doit() == 0
def test_Sum_doit(): assert Sum(n*Integral(a**2), (n, 0, 2)).doit() == a**3 assert Sum(n*Integral(a**2), (n, 0, 2)).doit(deep=False) == \ 3*Integral(a**2) assert summation(n*Integral(a**2), (n, 0, 2)) == 3*Integral(a**2) # test nested sum evaluation s = Sum( Sum( Sum(2,(z,1,n+1)), (y,x+1,n)), (x,1,n)) assert 0 == (s.doit() - n*(n+1)*(n-1)).factor() assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3 assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == \ 3*Piecewise((1, And(S(1) <= k, k <= 3)), (0, True)) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == \ f(1) + f(2) + f(3) assert Sum(f(n)*Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == \ Sum(Piecewise((f(n), n >= 0), (0, True)), (n, 1, oo)) l = Symbol('l', integer=True, positive=True) assert Sum(f(l)*Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == \ Sum(f(l), (l, 1, oo)) # issue 2597 nmax = symbols('N', integer=True, positive=True) pw = Piecewise((1, And(S(1) <= n, n <= nmax)), (0, True)) assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax))
def test_separatevars(): x, y, z, n = symbols('x,y,z,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 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 4865 assert separatevars(sqrt(x*y)).is_Pow assert separatevars(sqrt(x*y), force=True) == sqrt(x)*sqrt(y) # issue 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 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 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_function_assumptions(): x = Symbol('x') f = Function('f') f_real = Function('f', real=True) assert f != f_real assert f(x) != f_real(x) assert f(x).is_real is None assert f_real(x).is_real is True # Can also do it this way, but it won't be equal to f_real because of the # way UndefinedFunction.__new__ works. f_real2 = Function('f', is_real=True) assert f_real2(x).is_real is True
def test_issue_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_klein_gordon_lagrangian(): m = Symbol("m") phi = f(x, t) L = -(diff(phi, t) ** 2 - diff(phi, x) ** 2 - m ** 2 * phi ** 2) / 2 eqna = Eq(diff(L, phi) - diff(L, diff(phi, x), x) - diff(L, diff(phi, t), t), 0) eqnb = Eq(diff(phi, t, t) - diff(phi, x, x) + m ** 2 * phi, 0) assert eqna == eqnb
def test_collect_D_0(): D = Derivative f = Function('f') x, a, b = symbols('x,a,b') 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""" a, b, c = symbols('a,b,c') f = Function('f') x, y, z, n = symbols('x,y,z,n') assert collect(-x/8 + x*y, -x) == x*(y - S(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_Sum_doit(): assert Sum(n * Integral(a ** 2), (n, 0, 2)).doit() == a ** 3 assert Sum(n * Integral(a ** 2), (n, 0, 2)).doit(deep=False) == 3 * Integral(a ** 2) assert summation(n * Integral(a ** 2), (n, 0, 2)) == 3 * Integral(a ** 2) # test nested sum evaluation s = Sum(Sum(Sum(2, (z, 1, n + 1)), (y, x + 1, n)), (x, 1, n)) assert 0 == (s.doit() - n * (n + 1) * (n - 1)).factor() assert Sum(KroneckerDelta(m, n), (m, -oo, oo)).doit() == Piecewise((1, And(-oo < n, n < oo)), (0, True)) assert Sum(x * KroneckerDelta(m, n), (m, -oo, oo)).doit() == Piecewise((x, And(-oo < n, n < oo)), (0, True)) assert Sum(Sum(KroneckerDelta(m, n), (m, 1, 3)), (n, 1, 3)).doit() == 3 assert Sum(Sum(KroneckerDelta(k, m), (m, 1, 3)), (n, 1, 3)).doit() == 3 * Piecewise( (1, And(S(1) <= k, k <= 3)), (0, True) ) assert Sum(f(n) * Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, 3)).doit() == f(1) + f(2) + f(3) assert Sum(f(n) * Sum(KroneckerDelta(m, n), (m, 0, oo)), (n, 1, oo)).doit() == Sum( Piecewise((f(n), And(Le(0, n), n < oo)), (0, True)), (n, 1, oo) ) l = Symbol("l", integer=True, positive=True) assert Sum(f(l) * Sum(KroneckerDelta(m, l), (m, 0, oo)), (l, 1, oo)).doit() == Sum(f(l), (l, 1, oo)) # issue 2597 nmax = symbols("N", integer=True, positive=True) pw = Piecewise((1, And(S(1) <= n, n <= nmax)), (0, True)) assert Sum(pw, (n, 1, nmax)).doit() == Sum(pw, (n, 1, nmax)) q, s = symbols("q, s") assert summation(1 / n ** (2 * s), (n, 1, oo)) == Piecewise( (zeta(2 * s), 2 * s > 1), (Sum(n ** (-2 * s), (n, 1, oo)), True) ) assert summation(1 / (n + 1) ** s, (n, 0, oo)) == Piecewise( (zeta(s), s > 1), (Sum((n + 1) ** (-s), (n, 0, oo)), True) ) assert summation(1 / (n + q) ** s, (n, 0, oo)) == Piecewise( (zeta(s, q), And(q > 0, s > 1)), (Sum((n + q) ** (-s), (n, 0, oo)), True) ) assert summation(1 / (n + q) ** s, (n, q, oo)) == Piecewise( (zeta(s, 2 * q), And(2 * q > 0, s > 1)), (Sum((n + q) ** (-s), (n, q, oo)), True) ) assert summation(1 / n ** 2, (n, 1, oo)) == zeta(2) assert summation(1 / n ** s, (n, 0, oo)) == Sum(n ** (-s), (n, 0, oo))
def test_nargs(): f = Function('f') assert f.nargs == S.Naturals0 assert f(1).nargs == S.Naturals0 assert Function('f', nargs=2)(1, 2).nargs == FiniteSet(2) assert sin.nargs == FiniteSet(1) assert sin(2).nargs == FiniteSet(1) assert log.nargs == FiniteSet(1, 2) assert log(2).nargs == FiniteSet(1, 2) assert Function('f', nargs=2).nargs == FiniteSet(2) assert Function('f', nargs=0).nargs == FiniteSet(0)
def test_sho_lagrangian(): m = Symbol('m') k = Symbol('k') x = f(t) L = m*diff(x, t)**2/2 - k*x**2/2 eqna = Eq(diff(L, x), diff(L, diff(x, t), t)) eqnb = Eq(-k*x, m*diff(x, t, t)) assert eqna == eqnb assert diff(L, x, t) == diff(L, t, x) assert diff(L, diff(x, t), t) == m*diff(x, t, 2) assert diff(L, t, diff(x, t)) == -k*x + m*diff(x, t, 2)
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
def test_issue_13843(): x = symbols('x') f = Function('f') m, n = symbols('m n', integer=True) assert Derivative(Derivative(f(x), (x, m)), (x, n)) == Derivative(f(x), (x, m + n)) assert Derivative(Derivative(f(x), (x, m+5)), (x, n+3)) == Derivative(f(x), (x, m + n + 8)) assert Derivative(f(x), (x, n)).doit() == Derivative(f(x), (x, n))
def test_nargs(): f = Function('f') assert f.nargs == S.Naturals0 assert f(1).nargs == S.Naturals0 assert Function('f', nargs=2)(1, 2).nargs == FiniteSet(2) assert sin.nargs == FiniteSet(1) assert sin(2).nargs == FiniteSet(1) assert log.nargs == FiniteSet(1, 2) assert log(2).nargs == FiniteSet(1, 2) assert Function('f', nargs=2).nargs == FiniteSet(2) assert Function('f', nargs=0).nargs == FiniteSet(0) assert Function('f', nargs=(0, 1)).nargs == FiniteSet(0, 1) assert Function('f', nargs=None).nargs == S.Naturals0 raises(ValueError, lambda: Function('f', nargs=()))
def test_function_subs(): f = Function("f") S = Sum(x*f(y),(x,0,oo),(y,0,oo)) assert S.subs(f(y),y) == Sum(x*y,(x,0,oo),(y,0,oo)) assert S.subs(f(x),x) == S raises(ValueError, lambda: S.subs(f(y),x+y) ) S = Sum(x*log(y),(x,0,oo),(y,0,oo)) assert S.subs(log(y),y) == S S = Sum(x*f(y),(x,0,oo),(y,0,oo)) assert S.subs(f(y),y) == Sum(x*y,(x,0,oo),(y,0,oo))
def test_telescopic_sums(): # checks also input 2 of comment 1 issue 1028 assert Sum(1 / k - 1 / (k + 1), (k, 1, n)).doit() == 1 - 1 / (1 + n) f = Function("f") assert Sum(f(k) - f(k + 2), (k, m, n)).doit() == -f(1 + n) - f(2 + n) + f(m) + f(1 + m) assert Sum(cos(k) - cos(k + 3), (k, 1, n)).doit() == -cos(1 + n) - cos(2 + n) - cos(3 + n) + cos(1) + cos(2) + cos( 3 ) # dummy variable shouldn't matter assert telescopic(1 / m, -m / (1 + m), (m, n - 1, n)) == telescopic(1 / k, -k / (1 + k), (k, n - 1, n)) assert Sum(1 / x / (x - 1), (x, a, b)).doit() == -((a - b - 1) / (b * (a - 1)))
def test_simplify_function_inverse(): # "inverse" attribute does not guarantee that f(g(x)) is x # so this simplification should not happen automatically. # See issue #12140 x, y = symbols('x, y') g = Function('g') class f(Function): def inverse(self, argindex=1): return g assert simplify(f(g(x))) == f(g(x)) assert inversecombine(f(g(x))) == x assert simplify(f(g(x)), inverse=True) == x assert simplify(f(g(sin(x)**2 + cos(x)**2)), inverse=True) == 1 assert simplify(f(g(x, y)), inverse=True) == f(g(x, y)) assert simplify(2*asin(sin(3*x)), inverse=True) == 6*x
def test_deriv_wrt_function(): x = f(t) xd = diff(x, t) xdd = diff(xd, t) y = g(t) yd = diff(y, t) assert diff(x, t) == xd assert diff(2 * x + 4, t) == 2 * xd assert diff(2 * x + 4 + y, t) == 2 * xd + yd assert diff(2 * x + 4 + y * x, t) == 2 * xd + x * yd + xd * y assert diff(2 * x + 4 + y * x, x) == 2 + y assert diff(4 * x ** 2 + 3 * x + x * y, t) == 3 * xd + x * yd + xd * y + 8 * x * xd assert diff(4 * x ** 2 + 3 * xd + x * y, t) == 3 * xdd + x * yd + xd * y + 8 * x * xd assert diff(4 * x ** 2 + 3 * xd + x * y, xd) == 3 assert diff(4 * x ** 2 + 3 * xd + x * y, xdd) == 0 assert diff(sin(x), t) == xd * cos(x) assert diff(exp(x), t) == xd * exp(x) assert diff(sqrt(x), t) == xd / (2 * sqrt(x))
def test_distribution_over_equality(): assert Product(Eq(x * 2, f(x)), (x, 1, 3)).doit() == Eq(48, f(1) * f(2) * f(3)) assert Sum(Eq(f(x), x**2), (x, 0, y)) == \ Eq(Sum(f(x), (x, 0, y)), Sum(x**2, (x, 0, y)))
def test_sort_variable(): vsort = Derivative._sort_variables assert vsort((x, y, z)) == [x, y, z] assert vsort((h(x), g(x), f(x))) == [f(x), g(x), h(x)] assert vsort((z, y, x, h(x), g(x), f(x))) == [x, y, z, f(x), g(x), h(x)] assert vsort((x, f(x), y, f(y))) == [x, f(x), y, f(y)] assert vsort((y, x, g(x), f(x), z, h(x), y, x)) == \ [x, y, f(x), g(x), z, h(x), x, y] assert vsort((z, y, f(x), x, f(x), g(x))) == [y, z, f(x), x, f(x), g(x)] assert vsort((z, y, f(x), x, f(x), g(x), z, z, y, x)) == \ [y, z, f(x), x, f(x), g(x), x, y, z, z]
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_issue_13143(): fx = f(x).diff(x) e = f(x) + fx + f(x) * fx # collect function before derivative assert collect(e, Wild('w')) == f(x) * (fx + 1) + fx e = f(x) + f(x) * fx + x * fx * f(x) assert collect(e, fx) == (x * f(x) + f(x)) * fx + f(x) assert collect(e, f(x)) == (x * fx + fx + 1) * f(x) e = f(x) + fx + f(x) * fx assert collect(e, [f(x), fx]) == f(x) * (1 + fx) + fx assert collect(e, [fx, f(x)]) == fx * (1 + f(x)) + f(x)
def test_derivative_evaluate(): assert Derivative(sin(x), x) != diff(sin(x), x) assert Derivative(sin(x), x).doit() == diff(sin(x), x) assert Derivative(Derivative(f(x), x), x) == diff(f(x), x, x) assert Derivative(sin(x), x, 0) == sin(x)
def test_derivative_subs_self_bug(): d = diff(f(x), x) assert d.subs(d, y) == y
def test_karr_convention(): # Test the Karr summation convention that we want to hold. # See his paper "Summation in Finite Terms" for a detailed # reasoning why we really want exactly this definition. # The convention is described on page 309 and essentially # in section 1.4, definition 3: # # \sum_{m <= i < n} f(i) 'has the obvious meaning' for m < n # \sum_{m <= i < n} f(i) = 0 for m = n # \sum_{m <= i < n} f(i) = - \sum_{n <= i < m} f(i) for m > n # # It is important to note that he defines all sums with # the upper limit being *exclusive*. # In contrast, sympy and the usual mathematical notation has: # # sum_{i = a}^b f(i) = f(a) + f(a+1) + ... + f(b-1) + f(b) # # with the upper limit *inclusive*. So translating between # the two we find that: # # \sum_{m <= i < n} f(i) = \sum_{i = m}^{n-1} f(i) # # where we intentionally used two different ways to typeset the # sum and its limits. i = Symbol("i", integer=True) k = Symbol("k", integer=True) j = Symbol("j", integer=True) # A simple example with a concrete summand and symbolic limits. # The normal sum: m = k and n = k + j and therefore m < n: m = k n = k + j a = m b = n - 1 S1 = Sum(i**2, (i, a, b)).doit() # The reversed sum: m = k + j and n = k and therefore m > n: m = k + j n = k a = m b = n - 1 S2 = Sum(i**2, (i, a, b)).doit() assert simplify(S1 + S2) == 0 # Test the empty sum: m = k and n = k and therefore m = n: m = k n = k a = m b = n - 1 Sz = Sum(i**2, (i, a, b)).doit() assert Sz == 0 # Another example this time with an unspecified summand and # numeric limits. (We can not do both tests in the same example.) f = Function("f") # The normal sum with m < n: m = 2 n = 11 a = m b = n - 1 S1 = Sum(f(i), (i, a, b)).doit() # The reversed sum with m > n: m = 11 n = 2 a = m b = n - 1 S2 = Sum(f(i), (i, a, b)).doit() assert simplify(S1 + S2) == 0 # Test the empty sum with m = n: m = 5 n = 5 a = m b = n - 1 Sz = Sum(f(i), (i, a, b)).doit() assert Sz == 0
def test_diff_wrt_func_subs(): assert f(g(x)).diff(x).subs(g, Lambda(x, 2 * x)).doit() == f(2 * x).diff(x)
def test_simplify_expr(): x, y, z, k, n, m, w, s, A = symbols('x,y,z,k,n,m,w,s,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)))) == 0 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) # issue 10347 expr = -x * (y**2 - 1) * ( 2 * y**2 * (x**2 - 1) / (a * (x**2 - y**2)**2) + (x**2 - 1) / (a * (x**2 - y**2))) / (a * (x**2 - y**2)) + x * ( -2 * x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2)**2) - x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - 1) * (x**2 - y**2)) + (x**2 * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (x**2 - 1) + sqrt( (-x**2 + 1) * (y**2 - 1)) * (x * (-x * y**2 + x) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1) + sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) * sin(z)) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / ( a * (x**2 - y**2)) + x * ( -2 * x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2)**2) - x**2 * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - 1) * (x**2 - y**2)) + (x**2 * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (x**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x * y**2 + x) * cos(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1) + sqrt( (-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z)) / (a * sqrt((-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / ( a * (x**2 - y**2)) - y * sqrt((-x**2 + 1) * (y**2 - 1)) * ( -x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2) * (y**2 - 1)) + 2 * x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (a * (x**2 - y**2)**2) + (x * y * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin(z) / (y**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x**2 * y + y) * sin(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) ) * sin(z) / (a * (x**2 - y**2)) + y * (x**2 - 1) * ( -2 * x * y * (x**2 - 1) / (a * (x**2 - y**2)**2) + 2 * x * y / (a * (x**2 - y**2)) ) / (a * (x**2 - y**2)) + y * (x**2 - 1) * (y**2 - 1) * ( -x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2) * (y**2 - 1)) + 2 * x * y * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (a * (x**2 - y**2)**2) + (x * y * sqrt((-x**2 + 1) * (y**2 - 1)) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * cos(z) / (y**2 - 1) + x * sqrt( (-x**2 + 1) * (y**2 - 1)) * (-x**2 * y + y) * cos(z) / sqrt(-x**2 * y**2 + x**2 + y**2 - 1)) / (a * sqrt((-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2))) * cos(z) / (a * sqrt( (-x**2 + 1) * (y**2 - 1)) * (x**2 - y**2)) - x * sqrt( (-x**2 + 1) * (y**2 - 1) ) * sqrt(-x**2 * y**2 + x**2 + y**2 - 1) * sin( z)**2 / (a**2 * (x**2 - 1) * (x**2 - y**2) * (y**2 - 1)) - x * sqrt( (-x**2 + 1) * (y**2 - 1)) * sqrt( -x**2 * y**2 + x**2 + y**2 - 1) * cos(z)**2 / ( a**2 * (x**2 - 1) * (x**2 - y**2) * (y**2 - 1)) assert simplify(expr) == 2 * x / (a**2 * (x**2 - y**2)) #issue 17631 assert simplify('((-1/2)*Boole(True)*Boole(False)-1)*Boole(True)') == \ Mul(sympify('(2 + Boole(True)*Boole(False))'), sympify('-Boole(True)/2')) 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_collect_D(): D = Derivative f = Function('f') x, a, b = symbols('x,a,b') 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 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)) == \ (x*f(x) + f(x))*D(f(x), x) + f(x) 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)
def test_issue_4711(): x = Symbol("x") assert Symbol('f')(x) == f(x)
def test_evalf_symbolic(): f, g = symbols('f g', cls=Function) # issue 6328 expr = Sum(f(x), (x, 1, 3)) + Sum(g(x), (x, 1, 3)) assert expr.evalf() == expr
def test_derivative_linearity(): assert diff(-f(x), x) == -diff(f(x), x) assert diff(8 * f(x), x) == 8 * diff(f(x), x) assert diff(8 * f(x), x) != 7 * diff(f(x), x) assert diff(8 * f(x) * x, x) == 8 * f(x) + 8 * x * diff(f(x), x) assert diff(8 * f(x) * y * x, x) == 8 * y * f(x) + 8 * y * x * diff(f(x), x)
def test_powsimp(): x, y, z, n = symbols('x,y,z,n') f = Function('f') assert powsimp(4**x * 2**(-x) * 2**(-x)) == 1 assert powsimp((-4)**x * (-2)**(-x) * 2**(-x)) == 1 assert powsimp(f(4**x * 2**(-x) * 2**(-x))) == f(4**x * 2**(-x) * 2**(-x)) assert powsimp(f(4**x * 2**(-x) * 2**(-x)), deep=True) == f(1) assert exp(x) * exp(y) == exp(x) * exp(y) assert powsimp(exp(x) * exp(y)) == exp(x + y) assert powsimp(exp(x) * exp(y) * 2**x * 2**y) == (2 * E)**(x + y) assert powsimp(exp(x)*exp(y)*2**x*2**y, combine='exp') == \ exp(x + y)*2**(x + y) assert powsimp(exp(x)*exp(y)*exp(2)*sin(x) + sin(y) + 2**x*2**y) == \ exp(2 + x + y)*sin(x) + sin(y) + 2**(x + y) assert powsimp(sin(exp(x) * exp(y))) == sin(exp(x) * exp(y)) assert powsimp(sin(exp(x) * exp(y)), deep=True) == sin(exp(x + y)) assert powsimp(x**2 * x**y) == x**(2 + y) # This should remain factored, because 'exp' with deep=True is supposed # to act like old automatic exponent combining. assert powsimp((1 + E*exp(E))*exp(-E), combine='exp', deep=True) == \ (1 + exp(1 + E))*exp(-E) assert powsimp((1 + E*exp(E))*exp(-E), deep=True) == \ (1 + exp(1 + E))*exp(-E) assert powsimp((1 + E * exp(E)) * exp(-E)) == (1 + exp(1 + E)) * exp(-E) assert powsimp((1 + E*exp(E))*exp(-E), combine='exp') == \ (1 + exp(1 + E))*exp(-E) assert powsimp((1 + E*exp(E))*exp(-E), combine='base') == \ (1 + E*exp(E))*exp(-E) x, y = symbols('x,y', nonnegative=True) n = Symbol('n', real=True) assert powsimp(y**n * (y / x)**(-n)) == x**n assert powsimp(x**(x**(x*y)*y**(x*y))*y**(x**(x*y)*y**(x*y)), deep=True) \ == (x*y)**(x*y)**(x*y) assert powsimp(2**(2**(2 * x) * x), deep=False) == 2**(2**(2 * x) * x) assert powsimp(2**(2**(2 * x) * x), deep=True) == 2**(x * 4**x) assert powsimp( exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \ exp(-x + exp(-x)*exp(-x*log(x))) assert powsimp( exp(-x + exp(-x)*exp(-x*log(x))), deep=False, combine='exp') == \ exp(-x + exp(-x)*exp(-x*log(x))) assert powsimp((x + y) / (3 * z), deep=False, combine='exp') == (x + y) / (3 * z) assert powsimp((x / 3 + y / 3) / z, deep=True, combine='exp') == (x / 3 + y / 3) / z assert powsimp(exp(x)/(1 + exp(x)*exp(y)), deep=True) == \ exp(x)/(1 + exp(x + y)) assert powsimp(x * y**(z**x * z**y), deep=True) == x * y**(z**(x + y)) assert powsimp((z**x * z**y)**x, deep=True) == (z**(x + y))**x assert powsimp(x * (z**x * z**y)**x, deep=True) == x * (z**(x + y))**x p = symbols('p', positive=True) assert powsimp((1 / x)**log(2) / x) == (1 / x)**(1 + log(2)) assert powsimp((1 / p)**log(2) / p) == p**(-1 - log(2)) # coefficient of exponent can only be simplified for positive bases assert powsimp(2**(2 * x)) == 4**x assert powsimp((-1)**(2 * x)) == (-1)**(2 * x) i = symbols('i', integer=True) assert powsimp((-1)**(2 * i)) == 1 assert powsimp((-1)**(-x)) != (-1)**x # could be 1/((-1)**x), but is not # force=True overrides assumptions assert powsimp((-1)**(2 * x), force=True) == 1 # rational exponents allow combining of negative terms w, n, m = symbols('w n m', negative=True) e = i / a # not a rational exponent if `a` is unknown ex = w**e * n**e * m**e assert powsimp(ex) == m**(i / a) * n**(i / a) * w**(i / a) e = i / 3 ex = w**e * n**e * m**e assert powsimp(ex) == (-1)**i * (-m * n * w)**(i / 3) e = (3 + i) / i ex = w**e * n**e * m**e assert powsimp(ex) == (-1)**(3 * e) * (-m * n * w)**e eq = x**(2 * a / 3) # eq != (x**a)**(2/3) (try x = -1 and a = 3 to see) assert powsimp(eq).exp == eq.exp == 2 * a / 3 # powdenest goes the other direction assert powsimp(2**(2 * x)) == 4**x assert powsimp(exp(p / 2)) == exp(p / 2) # issue 6368 eq = Mul(*[sqrt(Dummy(imaginary=True)) for i in range(3)]) assert powsimp(eq) == eq and eq.is_Mul assert all(powsimp(e) == e for e in (sqrt(x**a), sqrt(x**2))) # issue 8836 assert str(powsimp(exp(I * pi / 3) * root(-1, 3))) == '(-1)**(2/3)' # issue 9183 assert powsimp(-0.1**x) == -0.1**x # issue 10095 assert powsimp((1 / (2 * E))**oo) == (exp(-1) / 2)**oo # PR 13131 eq = sin(2 * x)**2 * sin(2.0 * x)**2 assert powsimp(eq) == eq # issue 14615 assert powsimp(x**2 * y**3 * (x * y**2)**(S(3) / 2)) == x * y * (x * y**2)**(S(5) / 2)
def test_separatevars(): x, y, z, n = symbols('x,y,z,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 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 4865 assert separatevars(sqrt(x*y)).is_Pow assert separatevars(sqrt(x*y), force=True) == sqrt(x)*sqrt(y) # issue 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 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 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 s = separatevars(abs(x*y)) assert s == abs(x)*abs(y) and s.is_Mul z = cos(1)**2 + sin(1)**2 - 1 a = abs(x*z) s = separatevars(a) assert not a.is_Mul and s.is_Mul and s == abs(x)*abs(z) s = separatevars(abs(x*y*z)) assert s == abs(x)*abs(y)*abs(z) # abs(x+y)/abs(z) would be better but we test this here to # see that it doesn't raise assert separatevars(abs((x+y)/z)) == abs((x+y)/z)
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), ))
def test_Derivative_as_finite_difference(): # Central 1st derivative at gridpoint x, h = symbols('x h', real=True) dfdx = f(x).diff(x) assert (dfdx.as_finite_difference([x - 2, x - 1, x, x + 1, x + 2]) - (S(1) / 12 * (f(x - 2) - f(x + 2)) + S(2) / 3 * (f(x + 1) - f(x - 1)))).simplify() == 0 # Central 1st derivative "half-way" assert (dfdx.as_finite_difference() - (f(x + S(1) / 2) - f(x - S(1) / 2))).simplify() == 0 assert (dfdx.as_finite_difference(h) - (f(x + h / S(2)) - f(x - h / S(2))) / h).simplify() == 0 assert (dfdx.as_finite_difference([x - 3 * h, x - h, x + h, x + 3 * h]) - (S(9) / (8 * 2 * h) * (f(x + h) - f(x - h)) + S(1) / (24 * 2 * h) * (f(x - 3 * h) - f(x + 3 * h)))).simplify() == 0 # One sided 1st derivative at gridpoint assert (dfdx.as_finite_difference([0, 1, 2], 0) - (-S(3) / 2 * f(0) + 2 * f(1) - f(2) / 2)).simplify() == 0 assert (dfdx.as_finite_difference([x, x + h], x) - (f(x + h) - f(x)) / h).simplify() == 0 assert (dfdx.as_finite_difference([x - h, x, x + h], x - h) - (-S(3) / (2 * h) * f(x - h) + 2 / h * f(x) - S(1) / (2 * h) * f(x + h))).simplify() == 0 # One sided 1st derivative "half-way" assert ( dfdx.as_finite_difference( [x - h, x + h, x + 3 * h, x + 5 * h, x + 7 * h]) - 1 / (2 * h) * (-S(11) / (12) * f(x - h) + S(17) / (24) * f(x + h) + S(3) / 8 * f(x + 3 * h) - S(5) / 24 * f(x + 5 * h) + S(1) / 24 * f(x + 7 * h))).simplify() == 0 d2fdx2 = f(x).diff(x, 2) # Central 2nd derivative at gridpoint assert (d2fdx2.as_finite_difference([x - h, x, x + h]) - h**-2 * (f(x - h) + f(x + h) - 2 * f(x))).simplify() == 0 assert ( d2fdx2.as_finite_difference([x - 2 * h, x - h, x, x + h, x + 2 * h]) - h**-2 * (-S(1) / 12 * (f(x - 2 * h) + f(x + 2 * h)) + S(4) / 3 * (f(x + h) + f(x - h)) - S(5) / 2 * f(x))).simplify() == 0 # Central 2nd derivative "half-way" assert (d2fdx2.as_finite_difference([x - 3 * h, x - h, x + h, x + 3 * h]) - (2 * h)**-2 * (S(1) / 2 * (f(x - 3 * h) + f(x + 3 * h)) - S(1) / 2 * (f(x + h) + f(x - h)))).simplify() == 0 # One sided 2nd derivative at gridpoint assert (d2fdx2.as_finite_difference([x, x + h, x + 2 * h, x + 3 * h]) - h**-2 * (2 * f(x) - 5 * f(x + h) + 4 * f(x + 2 * h) - f(x + 3 * h)) ).simplify() == 0 # One sided 2nd derivative at "half-way" assert ( d2fdx2.as_finite_difference([x - h, x + h, x + 3 * h, x + 5 * h]) - (2 * h)**-2 * (S(3) / 2 * f(x - h) - S(7) / 2 * f(x + h) + S(5) / 2 * f(x + 3 * h) - S(1) / 2 * f(x + 5 * h))).simplify() == 0 d3fdx3 = f(x).diff(x, 3) # Central 3rd derivative at gridpoint assert (d3fdx3.as_finite_difference() - (-f(x - 3 / S(2)) + 3 * f(x - 1 / S(2)) - 3 * f(x + 1 / S(2)) + f(x + 3 / S(2)))).simplify() == 0 assert (d3fdx3.as_finite_difference( [x - 3 * h, x - 2 * h, x - h, x, x + h, x + 2 * h, x + 3 * h]) - h**-3 * (S(1) / 8 * (f(x - 3 * h) - f(x + 3 * h)) - f(x - 2 * h) + f(x + 2 * h) + S(13) / 8 * (f(x - h) - f(x + h)))).simplify() == 0 # Central 3rd derivative at "half-way" assert (d3fdx3.as_finite_difference([x - 3 * h, x - h, x + h, x + 3 * h]) - (2 * h)**-3 * (f(x + 3 * h) - f(x - 3 * h) + 3 * (f(x - h) - f(x + h)))).simplify() == 0 # One sided 3rd derivative at gridpoint assert (d3fdx3.as_finite_difference([x, x + h, x + 2 * h, x + 3 * h]) - h**-3 * (f(x + 3 * h) - f(x) + 3 * (f(x + h) - f(x + 2 * h)))).simplify() == 0 # One sided 3rd derivative at "half-way" assert (d3fdx3.as_finite_difference([x - h, x + h, x + 3 * h, x + 5 * h]) - (2 * h)**-3 * (f(x + 5 * h) - f(x - h) + 3 * (f(x + h) - f(x + 3 * h)))).simplify() == 0 # issue 11007 y = Symbol('y', real=True) d2fdxdy = f(x, y).diff(x, y) ref0 = Derivative(f(x + S(1) / 2, y), y) - Derivative( f(x - S(1) / 2, y), y) assert (d2fdxdy.as_finite_difference(wrt=x) - ref0).simplify() == 0 half = S(1) / 2 xm, xp, ym, yp = x - half, x + half, y - half, y + half ref2 = f(xm, ym) + f(xp, yp) - f(xp, ym) - f(xm, yp) assert (d2fdxdy.as_finite_difference() - ref2).simplify() == 0
def test_issue_4194(): # simplify should call cancel from sympy.abc import x, y f = Function('f') assert simplify((4 * x + 6 * f(y)) / (2 * x + 3 * f(y))) == 2
def test_diff_wrt_value(): assert Expr()._diff_wrt is False assert x._diff_wrt is True assert f(x)._diff_wrt is True assert Derivative(f(x), x)._diff_wrt is True assert Derivative(x**2, x)._diff_wrt is False
("\\frac{a}{b}", a / b), ("\\frac{a + b}{c}", _Mul(a + b, _Pow(c, -1))), ("\\frac{7}{3}", _Mul(7, _Pow(3, -1))), ("(\\csc x)(\\sec y)", csc(x) * sec(y)), ("\\lim_{x \\to 3} a", Limit(a, x, 3)), ("\\lim_{x \\rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Rightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\Longrightarrow 3} a", Limit(a, x, 3)), ("\\lim_{x \\to 3^{+}} a", Limit(a, x, 3, dir='+')), ("\\lim_{x \\to 3^{-}} a", Limit(a, x, 3, dir='-')), ("\\infty", oo), ("\\lim_{x \\to \\infty} \\frac{1}{x}", Limit(_Mul(1, _Pow(x, -1)), x, oo)), ("\\frac{d}{dx} x", Derivative(x, x)), ("\\frac{d}{dt} x", Derivative(x, t)), ("f(x)", f(x)), ("f(x, y)", f(x, y)), ("f(x, y, z)", f(x, y, z)), ("\\frac{d f(x)}{dx}", Derivative(f(x), x)), ("\\frac{d\\theta(x)}{dx}", Derivative(theta(x), x)), ("|x|", _Abs(x)), ("||x||", _Abs(Abs(x))), ("|x||y|", _Abs(x) * _Abs(y)), ("||x||y||", _Abs(_Abs(x) * _Abs(y))), ("\\pi^{|xy|}", Symbol('pi')**_Abs(x * y)), ("\\int x dx", Integral(x, x)), ("\\int x d\\theta", Integral(x, theta)), ("\\int (x^2 - y)dx", Integral(x**2 - y, x)), ("\\int x + a dx", Integral(_Add(x, a), x)), ("\\int da", Integral(1, a)), ("\\int_0^7 dx", Integral(1, (x, 0, 7))), ("\\int_a^b x dx", Integral(x, (x, a, b))), ("\\int^b_a x dx", Integral(x, (x, a, b))),
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))
def test_issue_10555(): f = Function('f') assert solveset(f(x) - pi/2, x, S.Reals) == \ ConditionSet(x, Eq(2*f(x) - pi, 0), S.Reals)
def test_Subs2(): # this reflects a limitation of subs(), probably won't fix assert Subs(f(x), x**2, x).doit() == f(sqrt(x))
def test_collect_Wild(): """Collect with respect to functions with Wild argument""" a, b, x, y = symbols('a b x y') 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_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) 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)
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) 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)
def test_straight_line(): F = f(x) Fd = F.diff(x) L = sqrt(1 + Fd**2) assert diff(L, F) == 0 assert diff(L, Fd) == Fd / sqrt(1 + Fd**2)