def test_sympyissue_7687(): f = Function('f')(x) ff = Function('f')(x) match_with_cache = f.match(ff) assert isinstance(f, type(ff)) clear_cache() ff = Function('f')(x) assert isinstance(f, type(ff)) assert match_with_cache == f.match(ff)
def test_implemented_function_evalf(): from diofant.utilities.lambdify import implemented_function f = Function('f') f = implemented_function(f, lambda x: x + 1) assert str(f(x)) == "f(x)" assert str(f(2)) == "f(2)" assert f(2).evalf() == 3 assert f(x).evalf() == f(x) del f._imp_ # XXX: due to caching _imp_ would influence all other tests
def test_euler_high_order(): # an example from hep-th/0309038 m = Symbol('m') k = Symbol('k') x = Function('x') y = Function('y') t = Symbol('t') L = (m * diff(x(t), t)**2 / 2 + m * diff(y(t), t)**2 / 2 - k * diff(x(t), t) * diff(y(t), t, t) + k * diff(y(t), t) * diff(x(t), t, t)) assert euler(L, [x(t), y(t)]) == [ Eq(2 * k * diff(y(t), t, t, t) - m * diff(x(t), t, t)), Eq(-2 * k * diff(x(t), t, t, t) - m * diff(y(t), t, t)) ] w = Symbol('w') L = diff(x(t, w), t, w)**2 / 2 assert euler(L) == [Eq(diff(x(t, w), t, t, w, w))]
def test_dummification(): F = Function('F') G = Function('G') # "\alpha" is not a valid python variable name # lambdify should sub in a dummy for it, and return # without a syntax error alpha = symbols(r'\alpha') some_expr = 2 * F(t)**2 / G(t) lam = lambdify((F(t), G(t)), some_expr) assert lam(3, 9) == 2 lam = lambdify(sin(t), 2 * sin(t)**2) assert lam(F(t)) == 2 * F(t)**2 # Test that \alpha was properly dummified lam = lambdify((alpha, t), 2 * alpha + t) assert lam(2, 1) == 5 pytest.raises(SyntaxError, lambda: lambdify(F(t) * G(t), F(t) * G(t) + 5)) pytest.raises(SyntaxError, lambda: lambdify(2 * F(t), 2 * F(t) + 5)) pytest.raises(SyntaxError, lambda: lambdify(2 * F(t), 4 * F(t) + 5))
def test_user_functions(): assert fcode(sin(x), user_functions={"sin": "zsin"}) == " zsin(x)" assert fcode( gamma(x), user_functions={"gamma": "mygamma"}) == " mygamma(x)" g = Function('g') assert fcode(g(x), user_functions={"g": "great"}) == " great(x)" n = symbols('n', integer=True) assert fcode( factorial(n), user_functions={"factorial": "fct"}) == " fct(n)"
def test_not_fortran(): g = Function('g') assert fcode(gamma( x)) == "C Not supported in Fortran:\nC gamma\n gamma(x)" assert fcode( Integral(sin(x)) ) == "C Not supported in Fortran:\nC Integral\n Integral(sin(x), x)" assert fcode( g(x)) == "C Not supported in Fortran:\nC g\n g(x)"
def test_vector_diff_integrate(): f = Function('f') v = f(a)*C.i + a**2*C.j - C.k assert Derivative(v, a) == Derivative((f(a))*C.i + a**2*C.j + (-1)*C.k, a) assert (diff(v, a) == v.diff(a) == Derivative(v, a).doit() == (Derivative(f(a), a))*C.i + 2*a*C.j) assert (Integral(v, a) == (Integral(f(a), a))*C.i + (Integral(a**2, a))*C.j + (Integral(-1, a))*C.k)
def test_apply_finite_diff(): pytest.raises(ValueError, lambda: apply_finite_diff(1, [1, 2], [3], x)) f = Function('f') assert (apply_finite_diff(1, [x - h, x + h], [f(x - h), f(x + h)], x) - (f(x + h) - f(x - h)) / (2 * h)).simplify() == 0 assert (apply_finite_diff(1, [5, 6, 7], [f(5), f(6), f(7)], 5) - (-3 * f(5) / 2 + 2 * f(6) - f(7) / 2)).simplify() == 0
def test_derivative_bug1(): f = Function("f") a = Wild("a", exclude=[f, x]) b = Wild("b", exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = pattern.xreplace(d1).matches(expr, d1) assert d2 is None
def test_sympyissue_7688(): f = Function('f') # actually an UndefinedFunction clear_cache() class A(UndefinedFunction): pass a = A('f') assert isinstance(a, type(f))
def test_numexpr_userfunctions(): a, b = numpy.random.randn(2, 10) uf = type('uf', (Function, ), {'eval': classmethod(lambda x, y: y**2 + 1)}) func = lambdify(x, 1 - uf(x), modules='numexpr') assert numpy.allclose(func(a), -(a**2)) uf = implemented_function(Function('uf'), lambda x, y: 2 * x * y + 1) func = lambdify((x, y), uf(x, y), modules='numexpr') assert numpy.allclose(func(a, b), 2 * a * b + 1)
def test_derivative_bug1(): f = Function('f') a = Wild('a', exclude=[f, x]) b = Wild('b', exclude=[f]) pattern = a * Derivative(f(x), x, x) + b expr = Derivative(f(x), x) + x**2 d1 = {b: x**2} d2 = expr.match(pattern.xreplace(d1)) assert d2 is None
def test_undefined_function(): from diofant import Function, MellinTransform f = Function('f') assert mellin_transform(f(x), x, s) == MellinTransform(f(x), x, s) assert mellin_transform(f(x) + exp(-x), x, s) == \ (MellinTransform(f(x), x, s) + gamma(s), (0, oo), True) assert laplace_transform(2 * f(x), x, s) == 2 * LaplaceTransform(f(x), x, s)
def test_sympyissue_4757(): x = Symbol('x', extended_real=True) y = Symbol('y', imaginary=True) f = Function('f') assert arg(f(x)).diff(x).subs({ f(x): 1 + I * x**2 }).doit() == 2 * x / (1 + x**4) assert arg(f(y)).diff(y).subs({ f(y): I + y**2 }).doit() == 2 * y / (1 + y**4)
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 pytest.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_derivative_subs(): y = Symbol('y') f = Function('f') assert Derivative(f(x), x).subs(f(x), y) != 0 assert Derivative(f(x), x).subs(f(x), y).subs(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)
def test_derivative_subs(): y = Symbol('y') f = Function('f') assert Derivative(f(x), x).subs({f(x): y}) != 0 assert Derivative(f(x), x).subs({f(x): y}).subs({y: f(x)}) == \ Derivative(f(x), x) # issues sympy/sympy#5085, sympy/sympy#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)
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 pytest.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_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_functional_exponent(): t = standard_transformations + (convert_xor, function_exponentiation) x = Symbol('x') y = Symbol('y') a = Symbol('a') yfcn = Function('y') assert parse_expr('sin^2(x)', transformations=t) == (sin(x))**2 assert parse_expr('sin^y(x)', transformations=t) == (sin(x))**y assert parse_expr('exp^y(x)', transformations=t) == (exp(x))**y assert parse_expr('E^y(x)', transformations=t) == exp(yfcn(x)) assert parse_expr('a^y(x)', transformations=t) == a**(yfcn(x))
def test_other_sums(): f = m**2 + m*exp(m) g = 3*exp(Rational(3, 2))/2 + exp(Rational(1, 2))/2 - exp(-Rational(1, 2))/2 - 3*exp(-Rational(3, 2))/2 + 5 assert summation(f, (m, -Rational(3, 2), Rational(3, 2))).expand() == g assert summation(f, (m, -Rational(3, 2), Rational(3, 2))).evalf().epsilon_eq(g.evalf(), 1e-10) assert summation(n**x, (n, 1, oo)) == Sum(n**x, (n, 1, oo)) f = Function('f') assert summation(f(n)*f(k), (k, m, n)) == Sum(f(n)*f(k), (k, m, n))
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))
def test_other_sums(): f = m**2 + m * exp(m) g = E + 14 + 2 * E**2 + 3 * E**3 assert summation(f, (m, 0, 3)) == g assert summation(f, (m, 0, 3)).evalf().epsilon_eq(g.evalf(), 1e-10) assert summation(n**x, (n, 1, oo)) == Sum(n**x, (n, 1, oo)) f = Function('f') assert summation(f(n) * f(k), (k, m, n)) == Sum(f(n) * f(k), (k, m, n))
def test_sympyissue_7688(): from diofant.core.function import Function, UndefinedFunction f = Function('f') # actually an UndefinedFunction clear_cache() class A(UndefinedFunction): pass a = A('f') assert isinstance(a, type(f))
def test_euler_interface(): x = Function('x') y = Symbol('y') t = Symbol('t') pytest.raises(TypeError, lambda: euler()) pytest.raises(TypeError, lambda: euler(D(x(t), t) * y(t), [x(t), y])) pytest.raises(ValueError, lambda: euler(D(x(t), t) * x(y), [x(t), x(y)])) pytest.raises(TypeError, lambda: euler(D(x(t), t)**2, x(0))) pytest.raises(TypeError, lambda: euler(1, y)) assert euler(D(x(t), t)**2 / 2, {x(t)}) == [Eq(-D(x(t), t, t))] assert euler(D(x(t), t)**2 / 2, x(t), {t}) == [Eq(-D(x(t), t, t))]
def test_derivative1(): p, q = map(Wild, 'pq') f = Function('f', nargs=1) fd = Derivative(f(x), x) assert fd.match(p) == {p: fd} assert (fd + 1).match(p + 1) == {p: fd} assert fd.match(fd) == {} assert (3 * fd).match(p * fd) is not None assert (3 * fd - 1).match(p * fd + q) == {p: 3, q: -1}
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_cosine_transform(): from diofant import Si, Ci t = symbols("t") w = symbols("w") a = symbols("a") f = Function("f") # Test unevaluated form assert cosine_transform(f(t), t, w) == CosineTransform(f(t), t, w) assert inverse_cosine_transform(f(w), w, t) == InverseCosineTransform(f(w), w, t) assert cosine_transform(1 / sqrt(t), t, w) == 1 / sqrt(w) assert inverse_cosine_transform(1 / sqrt(w), w, t) == 1 / sqrt(t) assert cosine_transform(1 / (a**2 + t**2), t, w) == sqrt(2) * sqrt(pi) * exp(-a * w) / (2 * a) assert cosine_transform( t**(-a), t, w) == 2**(-a + Rational(1, 2)) * w**(a - 1) * gamma( (-a + 1) / 2) / gamma(a / 2) assert inverse_cosine_transform( 2**(-a + Rational(1, 2)) * w**(a - 1) * gamma(-a / 2 + Rational(1, 2)) / gamma(a / 2), w, t) == t**(-a) assert cosine_transform(exp(-a * t), t, w) == sqrt(2) * a / (sqrt(pi) * (a**2 + w**2)) assert inverse_cosine_transform( sqrt(2) * a / (sqrt(pi) * (a**2 + w**2)), w, t) == exp(-a * t) assert cosine_transform(exp(-a * sqrt(t)) * cos(a * sqrt(t)), t, w) == a * exp(-a**2 / (2 * w)) / (2 * w**Rational(3, 2)) assert cosine_transform( 1 / (a + t), t, w) == sqrt(2) * ((-2 * Si(a * w) + pi) * sin(a * w) / 2 - cos(a * w) * Ci(a * w)) / sqrt(pi) assert inverse_cosine_transform( sqrt(2) * meijerg(((Rational(1, 2), 0), ()), ((Rational(1, 2), 0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * pi), w, t) == 1 / (a + t) assert cosine_transform(1 / sqrt(a**2 + t**2), t, w) == sqrt(2) * meijerg( ((Rational(1, 2), ), ()), ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi)) assert inverse_cosine_transform( sqrt(2) * meijerg( ((Rational(1, 2), ), ()), ((0, 0), (Rational(1, 2), )), a**2 * w**2 / 4) / (2 * sqrt(pi)), w, t) == 1 / (a * sqrt(1 + t**2 / a**2))
def test_fourier_transform(): from diofant import simplify, expand, expand_complex, factor, expand_trig FT = fourier_transform IFT = inverse_fourier_transform def simp(x): return simplify(expand_trig(expand_complex(expand(x)))) def sinc(x): return sin(pi * x) / (pi * x) k = symbols('k', extended_real=True) f = Function("f") # TODO for this to work with real a, need to expand abs(a*x) to abs(a)*abs(x) a = symbols('a', positive=True) b = symbols('b', positive=True) posk = symbols('posk', positive=True) # Test unevaluated form assert fourier_transform(f(x), x, k) == FourierTransform(f(x), x, k) assert inverse_fourier_transform(f(k), k, x) == InverseFourierTransform(f(k), k, x) # basic examples from wikipedia assert simp(FT(Heaviside(1 - abs(2 * a * x)), x, k)) == sinc(k / a) / a # TODO IFT is a *mess* assert simp(FT(Heaviside(1 - abs(a * x)) * (1 - abs(a * x)), x, k)) == sinc(k / a)**2 / a # TODO IFT assert factor(FT(exp(-a*x)*Heaviside(x), x, k), extension=I) == \ 1/(a + 2*pi*I*k) # NOTE: the ift comes out in pieces assert IFT(1 / (a + 2 * pi * I * x), x, posk, noconds=False) == (exp(-a * posk), True) assert IFT(1 / (a + 2 * pi * I * x), x, -posk, noconds=False) == (0, True) assert IFT(1 / (a + 2 * pi * I * x), x, symbols('k', negative=True), noconds=False) == (0, True) # TODO IFT without factoring comes out as meijer g assert factor(FT(x*exp(-a*x)*Heaviside(x), x, k), extension=I) == \ 1/(a + 2*pi*I*k)**2 assert FT(exp(-a*x)*sin(b*x)*Heaviside(x), x, k) == \ b/(b**2 + (a + 2*I*pi*k)**2) assert FT(exp(-a * x**2), x, k) == sqrt(pi) * exp(-pi**2 * k**2 / a) / sqrt(a) assert IFT(sqrt(pi / a) * exp(-(pi * k)**2 / a), k, x) == exp(-a * x**2) assert FT(exp(-a * abs(x)), x, k) == 2 * a / (a**2 + 4 * pi**2 * k**2)
def test_getn(): # other lines are tested incidentally by the suite assert O(x).getn() == 1 assert O(x / log(x)).getn() == 1 assert O(x**2 / log(x)**2).getn() == 2 assert O(x * log(x)).getn() == 1 pytest.raises(NotImplementedError, (O(x) + O(y)).getn) pytest.raises(NotImplementedError, O(x**y * log(x)**z, (x, 0)).getn) pytest.raises(NotImplementedError, O(x**pi * log(x), (x, 0)).getn) f = Function('f') pytest.raises(NotImplementedError, O(f(x)).getn)
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