def _visit_Call(self, stmt): args = [] if stmt.args: args += self._visit(stmt.args) if stmt.keywords: args += self._visit(stmt.keywords) if len(args) == 0: args = () func = self._visit(stmt.func) if isinstance(func, Symbol): f_name = func.name if str(f_name) == "print": func = PythonPrint(PythonTuple(*args)) else: func = Function(f_name)(*args) elif isinstance(func, DottedVariable): f_name = func.rhs.name func_attr = Function(f_name)(*args) func = DottedVariable(func.lhs, func_attr) else: raise NotImplementedError(' Unknown function type {}'.format( str(type(func)))) return func
def test_Function(): sT(Function("f")(x), "Function('f')(Symbol('x'))") # test unapplied Function sT(Function('f'), "Function('f')") sT(sin(x), "sin(Symbol('x'))") sT(sin, "sin")
def test_latex_printer(): r = Function('r')('t') assert VectorLatexPrinter().doprint(r**2) == "r^{2}" r2 = Function('r^2')('t') assert VectorLatexPrinter().doprint(r2.diff()) == r'\dot{r^{2}}' ra = Function('r__a')('t') assert VectorLatexPrinter().doprint(ra.diff().diff()) == r'\ddot{r^{a}}'
def test_undef_fcn_float_issue_6938(): f = Function('ceil') assert not f(0.3).is_number f = Function('sin') assert not f(0.3).is_number assert not f(pi).evalf().is_number x = Symbol('x') assert not f(x).evalf(subs={x:1.2}).is_number
def test_arg(): assert arg(0) is nan assert arg(1) == 0 assert arg(-1) == pi assert arg(I) == pi / 2 assert arg(-I) == -pi / 2 assert arg(1 + I) == pi / 4 assert arg(-1 + I) == pi * Rational(3, 4) assert arg(1 - I) == -pi / 4 assert arg(exp_polar(4 * pi * I)) == 4 * pi assert arg(exp_polar(-7 * pi * I)) == -7 * pi assert arg(exp_polar(5 - 3 * pi * I / 4)) == pi * Rational(-3, 4) f = Function('f') assert not arg(f(0) + I * f(1)).atoms(re) # check nesting x = Symbol('x') assert arg(arg(arg(x))) is not S.NaN assert arg(arg(arg(arg(x)))) is S.NaN r = Symbol('r', extended_real=True) assert arg(arg(r)) is not S.NaN assert arg(arg(arg(r))) is S.NaN p = Function('p', extended_positive=True) assert arg(p(x)) == 0 assert arg((3 + I) * p(x)) == arg(3 + I) p = Symbol('p', positive=True) assert arg(p) == 0 assert arg(p * I) == pi / 2 n = Symbol('n', negative=True) assert arg(n) == pi assert arg(n * I) == -pi / 2 x = Symbol('x') assert conjugate(arg(x)) == arg(x) e = p + I * p**2 assert arg(e) == arg(1 + p * I) # make sure sign doesn't swap e = -2 * p + 4 * I * p**2 assert arg(e) == arg(-1 + 2 * p * I) # make sure sign isn't lost x = symbols('x', real=True) # could be zero e = x + I * x assert arg(e) == arg(x * (1 + I)) assert arg(e / p) == arg(x * (1 + I)) e = p * cos(p) + I * log(p) * exp(p) assert arg(e).args[0] == e # keep it simple -- let the user do more advanced cancellation e = (p + 1) + I * (p**2 - 1) assert arg(e).args[0] == e f = Function('f') e = 2 * x * (f(0) - 1) - 2 * x * f(0) assert arg(e) == arg(-2 * x) assert arg(f(0)).func == arg and arg(f(0)).args == (f(0), )
def cse(expr): """ symplify a complicated sympy expression into a list of expression using the cse sympy function """ ls = list(expr.atoms(Sum)) if not ls: return [expr] ls += [expr] (ls, m) = sympy_cse(ls) (vars_old, stmts) = map(list, zip(*ls)) vars_new = [] free_gl = expr.free_symbols free_gl.update(expr.atoms(IndexedBase)) free_gl.update(vars_old) stmts.append(expr) for i in range(len(stmts) - 1): free = stmts[i].free_symbols free = free.difference(free_gl) free = list(free) var = create_variable(stmts[i]) if len(free) > 0: var = IndexedBase(var)[free] vars_new.append(var) for i in range(len(stmts) - 1): stmts[i + 1] = stmts[i + 1].replace(vars_old[i], vars_new[i]) stmts[-1] = stmts[-1].replace(stmts[i], vars_new[i]) allocate = [] for i in range(len(stmts) - 1): stmts[i] = Assign(vars_new[i], stmts[i]) stmts[i] = pyccel_sum(stmts[i]) if isinstance(vars_new[i], Indexed): ind = vars_new[i].indices tp = list(stmts[i + 1].atoms(Tuple)) size = None size = [None] * len(ind) for (j, k) in enumerate(ind): for t in tp: if k == t[0]: size[j] = t[2] - t[1] + 1 break if not all(size): raise ValueError('Unable to find range of index') name = str(vars_new[i].base) var = Symbol(name) stmt = Assign(var, Function('empty')(size[0])) allocate.append(stmt) stmts[i] = For(ind[0], Function('range')(size[0]), [stmts[i]], strict=False) lhs = create_variable(expr) stmts[-1] = Assign(lhs, stmts[-1]) imports = [Import('empty', 'numpy')] return imports + allocate + stmts
def test_rolling_disc(): # Rolling Disc Example # Here the rolling disc is formed from the contact point up, removing the # need to introduce generalized speeds. Only 3 configuration and 3 # speed variables are need to describe this system, along with the # disc's mass and radius, and the local gravity. q1, q2, q3 = dynamicsymbols('q1 q2 q3') q1d, q2d, q3d = dynamicsymbols('q1 q2 q3', 1) r, m, g = symbols('r m g') # The kinematics are formed by a series of simple rotations. Each simple # rotation creates a new frame, and the next rotation is defined by the new # frame's basis vectors. This example uses a 3-1-2 series of rotations, or # Z, X, Y series of rotations. Angular velocity for this is defined using # the second frame's basis (the lean frame). N = ReferenceFrame('N') Y = N.orientnew('Y', 'Axis', [q1, N.z]) L = Y.orientnew('L', 'Axis', [q2, Y.x]) R = L.orientnew('R', 'Axis', [q3, L.y]) # This is the translational kinematics. We create a point with no velocity # in N; this is the contact point between the disc and ground. Next we form # the position vector from the contact point to the disc's center of mass. # Finally we form the velocity and acceleration of the disc. C = Point('C') C.set_vel(N, 0) Dmc = C.locatenew('Dmc', r * L.z) Dmc.v2pt_theory(C, N, R) # Forming the inertia dyadic. I = inertia(L, m / 4 * r**2, m / 2 * r**2, m / 4 * r**2) BodyD = RigidBody('BodyD', Dmc, R, m, (I, Dmc)) # Finally we form the equations of motion, using the same steps we did # before. Supply the Lagrangian, the generalized speeds. BodyD.potential_energy = -m * g * r * cos(q2) Lag = Lagrangian(N, BodyD) q = [q1, q2, q3] q1 = Function('q1') q2 = Function('q2') q3 = Function('q3') l = LagrangesMethod(Lag, q) l.form_lagranges_equations() RHS = l.rhs() RHS.simplify() t = symbols('t') assert (l.mass_matrix[3:6] == [0, 5 * m * r**2 / 4, 0]) assert RHS[4].simplify() == ( (-8 * g * sin(q2(t)) + r * (5 * sin(2 * q2(t)) * Derivative(q1(t), t) + 12 * cos(q2(t)) * Derivative(q3(t), t)) * Derivative(q1(t), t)) / (10 * r)) assert RHS[5] == (-5 * cos(q2(t)) * Derivative(q1(t), t) + 6 * tan(q2(t)) * Derivative(q3(t), t) + 4 * Derivative(q1(t), t) / cos(q2(t))) * Derivative( q2(t), t)
def test_noncommutative_issue_15131(): x = Symbol('x', commutative=False) t = Symbol('t', commutative=False) fx = Function('Fx', commutative=False)(x) ft = Function('Ft', commutative=False)(t) A = Symbol('A', commutative=False) eq = fx * A * ft eqdt = eq.diff(t) assert eqdt.args[-1] == ft.diff(t)
def test_process_return_type(): from sympy.core.function import Function Int = Function("Int") ExpandToSum = Function("ExpandToSum") s = ('\n q = Expon(Pq, x)\n Pqq = Coeff(Pq, x, q)', 'With(List(Set(Pqq, Coeff(Pq, x, q))), Pqq*c**(n - q + S(-1))*(c*x)**(m - n + q + S(1))*(a*x**j + b*x**n)**(p + S(1))/(b*(m + n*p + q + S(1))) + Int((c*x)**m*(a*x**j + b*x**n)**p*ExpandToSum(Pq - Pqq*a*x**(-n + q)*(m - n + q + S(1))/(b*(m + n*p + q + S(1))) - Pqq*x**q, x), x))') result = process_return_type(s, []) expected = ('\n Pqq = Coeff(Pq, x, q)',\ Pqq*c**(n - q - 1)*(c*x)**(m - n + q + 1)*(a*x**j + b*x**n)**(p + 1)/(b*(m + n*p + q + 1)) + Int((c*x)**m*(a*x**j + b*x**n)**p*ExpandToSum(Pq - Pqq*a*x**(-n + q)*(m - n + q + 1)/(b*(m + n*p + q + 1)) - Pqq*x**q, x), x),\ True) assert result == expected
def test_euler_henonheiles(): x = Function('x') y = Function('y') t = Symbol('t') L = sum(D(z(t), t)**2 / 2 - z(t)**2 / 2 for z in [x, y]) L += -x(t)**2 * y(t) + y(t)**3 / 3 assert euler(L, [x(t), y(t)], t) == [ Eq(-2 * x(t) * y(t) - x(t) - D(x(t), t, t), 0), Eq(-x(t)**2 + y(t)**2 - y(t) - D(y(t), t, t), 0) ]
def test_implemented_function_evalf(): from sympy.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) f = implemented_function(Function('sin'), lambda x: x + 1) assert f(2).evalf() != sin(2) del f._imp_ # XXX: due to caching _imp_ would influence all other tests
def test_issue_7687(): from sympy.core.function import Function from sympy.abc import x f = Function('f')(x) ff = Function('f')(x) match_with_cache = ff.matches(f) assert isinstance(f, type(ff)) clear_cache() ff = Function('f')(x) assert isinstance(f, type(ff)) assert match_with_cache == ff.matches(f)
def test_subs_in_derivative(): expr = sin(x*exp(y)) u = Function('u') v = Function('v') assert Derivative(expr, y).subs(expr, y) == Derivative(y, y) assert Derivative(expr, y).subs(y, x).doit() == \ Derivative(expr, y).doit().subs(y, x) assert Derivative(f(x, y), y).subs(y, x) == Subs(Derivative(f(x, y), y), y, x) assert Derivative(f(x, y), y).subs(x, y) == Subs(Derivative(f(x, y), y), x, y) assert Derivative(f(x, y), y).subs(y, g(x, y)) == Subs(Derivative(f(x, y), y), y, g(x, y)).doit() assert Derivative(f(x, y), y).subs(x, g(x, y)) == Subs(Derivative(f(x, y), y), x, g(x, y)) assert Derivative(f(x, y), g(y)).subs(x, g(x, y)) == Derivative(f(g(x, y), y), g(y)) assert Derivative(f(u(x), h(y)), h(y)).subs(h(y), g(x, y)) == \ Subs(Derivative(f(u(x), h(y)), h(y)), h(y), g(x, y)).doit() assert Derivative(f(x, y), y).subs(y, z) == Derivative(f(x, z), z) assert Derivative(f(x, y), y).subs(y, g(y)) == Derivative(f(x, g(y)), g(y)) assert Derivative(f(g(x), h(y)), h(y)).subs(h(y), u(y)) == \ Derivative(f(g(x), u(y)), u(y)) assert Derivative(f(x, f(x, x)), f(x, x)).subs( f, Lambda((x, y), x + y)) == Subs( Derivative(z + x, z), z, 2*x) assert Subs(Derivative(f(f(x)), x), f, cos).doit() == sin(x)*sin(cos(x)) assert Subs(Derivative(f(f(x)), f(x)), f, cos).doit() == -sin(cos(x)) # Issue 13791. No comparison (it's a long formula) but this used to raise an exception. assert isinstance(v(x, y, u(x, y)).diff(y).diff(x).diff(y), Expr) # This is also related to issues 13791 and 13795; issue 15190 F = Lambda((x, y), exp(2*x + 3*y)) abstract = f(x, f(x, x)).diff(x, 2) concrete = F(x, F(x, x)).diff(x, 2) assert (abstract.subs(f, F).doit() - concrete).simplify() == 0 # don't introduce a new symbol if not necessary assert x in f(x).diff(x).subs(x, 0).atoms() # case (4) assert Derivative(f(x,f(x,y)), x, y).subs(x, g(y) ) == Subs(Derivative(f(x, f(x, y)), x, y), x, g(y)) assert Derivative(f(x, x), x).subs(x, 0 ) == Subs(Derivative(f(x, x), x), x, 0) # issue 15194 assert Derivative(f(y, g(x)), (x, z)).subs(z, x ) == Derivative(f(y, g(x)), (x, x)) df = f(x).diff(x) assert df.subs(df, 1) is S.One assert df.diff(df) is S.One dxy = Derivative(f(x, y), x, y) dyx = Derivative(f(x, y), y, x) assert dxy.subs(Derivative(f(x, y), y, x), 1) is S.One assert dxy.diff(dyx) is S.One assert Derivative(f(x, y), x, 2, y, 3).subs( dyx, g(x, y)) == Derivative(g(x, y), x, 1, y, 2) assert Derivative(f(x, x - y), y).subs(x, x + y) == Subs( Derivative(f(x, x - y), y), x, x + y)
def test_as_finite_diff(): x = symbols('x') f = Function('f') dx = Function('dx') _as_finite_diff(f(x).diff(x), [x - 2, x - 1, x, x + 1, x + 2]) # Use of undefined functions in ``points`` df_true = -f(x+dx(x)/2-dx(x+dx(x)/2)/2) / dx(x+dx(x)/2) \ + f(x+dx(x)/2+dx(x+dx(x)/2)/2) / dx(x+dx(x)/2) df_test = diff(f(x), x).as_finite_difference(points=dx(x), x0=x + dx(x) / 2) assert (df_test - df_true).simplify() == 0
def test_match_deriv_bug1(): n = Function('n') l = Function('l') x = Symbol('x') p = Wild('p') e = diff(l(x), x)/x - diff(diff(n(x), x), x)/2 - \ diff(n(x), x)**2/4 + diff(n(x), x)*diff(l(x), x)/4 e = e.subs(n(x), -l(x)).doit() t = x*exp(-l(x)) t2 = t.diff(x, x)/t assert e.match( (p*t2).expand() ) == {p: Rational(-1, 2)}
def test_undefined_function_eq(): f = Function('f') f2 = Function('f') g = Function('g') f_real = Function('f', is_real=True) # This test may only be meaningful if the cache is turned off assert f == f2 assert hash(f) == hash(f2) assert f == f assert f != g assert f != f_real
def test_Subs_subs(): assert Subs(x * y, x, x).subs(x, y) == Subs(x * y, x, y) assert Subs(x*y, x, x + 1).subs(x, y) == \ Subs(x*y, x, y + 1) assert Subs(x*y, y, x + 1).subs(x, y) == \ Subs(y**2, y, y + 1) a = Subs(x * y * z, (y, x, z), (x + 1, x + z, x)) b = Subs(x * y * z, (y, x, z), (x + 1, y + z, y)) assert a.subs(x, y) == b and \ a.doit().subs(x, y) == a.subs(x, y).doit() f = Function('f') g = Function('g') assert Subs(2 * f(x, y) + g(x), f(x, y), 1).subs(y, 2) == Subs(2 * f(x, y) + g(x), (f(x, y), y), (1, 2))
def test_derivative_subs(): f = Function('f') g = Function('g') assert Derivative(f(x), x).subs(f(x), y) != 0 # need xreplace to put the function back, see #13803 assert Derivative(f(x), x).subs(f(x), y).xreplace({y: f(x)}) == \ Derivative(f(x), x) # issues 5085, 5037 assert cse(Derivative(f(x), x) + f(x))[1][0].has(Derivative) assert cse(Derivative(f(x, y), x) + Derivative(f(x, y), y))[1][0].has(Derivative) eq = Derivative(g(x), g(x)) assert eq.subs(g, f) == Derivative(f(x), f(x)) assert eq.subs(g(x), f(x)) == Derivative(f(x), f(x)) assert eq.subs(g, cos) == Subs(Derivative(y, y), y, cos(x))
def test_vlatex(): # vlatex is broken #12078 from sympy.physics.vector import vlatex x = symbols('x') J = symbols('J') f = Function('f') g = Function('g') h = Function('h') expected = r'J \left(\frac{d}{d x} g{\left(x \right)} - \frac{d}{d x} h{\left(x \right)}\right)' expr = J * f(x).diff(x).subs(f(x), g(x) - h(x)) assert vlatex(expr) == expected
def test_find_simple_recurrence(): a = Function('a') n = Symbol('n') assert find_simple_recurrence([fibonacci(k) for k in range(12) ]) == (-a(n) - a(n + 1) + a(n + 2)) f = Function('a') i = Symbol('n') a = [1, 1, 1] for k in range(15): a.append(5 * a[-1] - 3 * a[-2] + 8 * a[-3]) assert find_simple_recurrence(a, A=f, N=i) == (-8 * f(i) + 3 * f(i + 1) - 5 * f(i + 2) + f(i + 3)) assert find_simple_recurrence([0, 2, 15, 74, 12, 3, 0, 1, 2, 85, 4, 5, 63]) == 0
def test_literal_evalf_is_number_is_zero_is_comparable(): from sympy.integrals.integrals import Integral from sympy.core.symbol import symbols from sympy.core.function import Function from sympy.functions.elementary.trigonometric import cos, sin x = symbols('x') f = Function('f') # issue 5033 assert f.is_number is False # issue 6646 assert f(1).is_number is False i = Integral(0, (x, x, x)) # expressions that are symbolically 0 can be difficult to prove # so in case there is some easy way to know if something is 0 # it should appear in the is_zero property for that object; # if is_zero is true evalf should always be able to compute that # zero assert i.n() == 0 assert i.is_zero assert i.is_number is False assert i.evalf(2, strict=False) == 0 # issue 10268 n = sin(1)**2 + cos(1)**2 - 1 assert n.is_comparable is False assert n.n(2).is_comparable is False assert n.n(2).n(2).is_comparable
def test_collect_D_0(): D = Derivative f = Function('f') x, a, b = symbols('x,a,b') fxx = D(f(x), x, x) assert collect(a * fxx + b * fxx, fxx) == (a + b) * fxx
def test_literal_evalf_is_number_is_zero_is_comparable(): from sympy.integrals.integrals import Integral from sympy.core.symbol import symbols from sympy.core.function import Function from sympy.functions.elementary.trigonometric import cos, sin x = symbols('x') f = Function('f') # the following should not be changed without a lot of dicussion # `foo.is_number` should be equivalent to `not foo.free_symbols` # it should not attempt anything fancy; see is_zero, is_constant # and equals for more rigorous tests. assert f(1).is_number is True i = Integral(0, (x, x, x)) # expressions that are symbolically 0 can be difficult to prove # so in case there is some easy way to know if something is 0 # it should appear in the is_zero property for that object; # if is_zero is true evalf should always be able to compute that # zero assert i.n() == 0 assert i.is_zero assert i.is_number is False assert i.evalf(2, strict=False) == 0 # issue 10268 n = sin(1)**2 + cos(1)**2 - 1 assert n.is_comparable is False assert n.n(2).is_comparable is False assert n.n(2).n(2).is_comparable
def test_complicated_derivative_with_Indexed(): x, y = symbols("x,y", cls=IndexedBase) sigma = symbols("sigma") i, j, k = symbols("i,j,k") m0, m1, m2, m3, m4, m5 = symbols("m0:6") f = Function("f") expr = f((x[i] - y[i])**2 / sigma) _xi_1 = symbols("xi_1", cls=Dummy) assert expr.diff(x[m0]).dummy_eq( (x[i] - y[i])*KroneckerDelta(i, m0)*\ 2*Subs( Derivative(f(_xi_1), _xi_1), (_xi_1,), ((x[i] - y[i])**2/sigma,) )/sigma ) assert expr.diff(x[m0]).diff(x[m1]).dummy_eq( 2*KroneckerDelta(i, m0)*\ KroneckerDelta(i, m1)*Subs( Derivative(f(_xi_1), _xi_1), (_xi_1,), ((x[i] - y[i])**2/sigma,) )/sigma + \ 4*(x[i] - y[i])**2*KroneckerDelta(i, m0)*KroneckerDelta(i, m1)*\ Subs( Derivative(f(_xi_1), _xi_1, _xi_1), (_xi_1,), ((x[i] - y[i])**2/sigma,) )/sigma**2 )
def test__sympify(): x = Symbol('x') f = Function('f') # positive _sympify assert _sympify(x) is x assert _sympify(1) == Integer(1) assert _sympify(0.5) == Float("0.5") assert _sympify(1 + 1j) == 1.0 + I * 1.0 # Function f is not Basic and can't sympify to Basic. We allow it to pass # with sympify but not with _sympify. # https://github.com/sympy/sympy/issues/20124 assert sympify(f) is f raises(SympifyError, lambda: _sympify(f)) class A: def _sympy_(self): return Integer(5) a = A() assert _sympify(a) == Integer(5) # negative _sympify raises(SympifyError, lambda: _sympify('1')) raises(SympifyError, lambda: _sympify([1, 2, 3]))
def test_iterargs(): f = Function('f') x = symbols('x') assert list(iterfreeargs(Integral(f(x), (f(x), 1)))) == [ Integral(f(x), (f(x), 1)), 1] assert list(iterargs(Integral(f(x), (f(x), 1)))) == [ Integral(f(x), (f(x), 1)), f(x), (f(x), 1), x, f(x), 1, x]
def test_issue_4855(): assert 1 / O(1) != O(1) assert 1 / O(x) != O(1 / x) assert 1 / O(x, (x, oo)) != O(1 / x, (x, oo)) f = Function('f') assert 1 / O(f(x)) != O(1 / x)
def test_Function(): f = Function('f') fx = f(x) w = WildFunction('w') assert str(f) == "f" assert str(fx) == "f(x)" assert str(w) == "w_"
def test_Derivative_bug1(): f = Function("f") x = abc.x a = Wild("a", exclude=[f(x)]) b = Wild("b", exclude=[f(x)]) eq = f(x).diff(x) assert eq.match(a*Derivative(f(x), x) + b) == {a: 1, b: 0}
def test_ideal_soliton(): raises(ValueError, lambda: IdealSoliton('sol', -12)) raises(ValueError, lambda: IdealSoliton('sol', 13.2)) raises(ValueError, lambda: IdealSoliton('sol', 0)) f = Function('f') raises(ValueError, lambda: density(IdealSoliton('sol', 10)).pmf(f)) k = Symbol('k', integer=True, positive=True) x = Symbol('x', integer=True, positive=True) t = Symbol('t') sol = IdealSoliton('sol', k) assert density(sol).low == S.One assert density(sol).high == k assert density(sol).dict == Density(density(sol)) assert density(sol).pmf(x) == Piecewise( (1 / k, Eq(x, 1)), (1 / (x * (x - 1)), k >= x), (0, True)) k_vals = [5, 20, 50, 100, 1000] for i in k_vals: assert E(sol.subs(k, i)) == harmonic(i) == moment(sol.subs(k, i), 1) assert variance(sol.subs( k, i)) == (i - 1) + harmonic(i) - harmonic(i)**2 == cmoment( sol.subs(k, i), 2) assert skewness(sol.subs(k, i)) == smoment(sol.subs(k, i), 3) assert kurtosis(sol.subs(k, i)) == smoment(sol.subs(k, i), 4) assert exp(I * t) / 10 + Sum(exp(I * t * x) / (x * x - x), (x, 2, k)).subs( k, 10).doit() == characteristic_function(sol.subs(k, 10))(t) assert exp(t) / 10 + Sum(exp(t * x) / (x * x - x), (x, 2, k)).subs( k, 10).doit() == moment_generating_function(sol.subs(k, 10))(t)