def test_deprecated_quantity_methods(): step = Quantity("step") with warns_deprecated_sympy(): step.set_dimension(length) step.set_scale_factor(2 * meter) assert convert_to(step, centimeter) == 200 * centimeter assert convert_to(1000 * step / second, kilometer / second) == 2 * kilometer / second
def test_parallel_axis(): # This is for a 2 dof inverted pendulum on a cart. # This tests the parallel axis code in KanesMethod. The inertia of the # pendulum is defined about the hinge, not about the center of mass. # Defining the constants and knowns of the system gravity = symbols("g") k, ls = symbols("k ls") a, mA, mC = symbols("a mA mC") F = dynamicsymbols("F") Ix, Iy, Iz = symbols("Ix Iy Iz") # Declaring the Generalized coordinates and speeds q1, q2 = dynamicsymbols("q1 q2") q1d, q2d = dynamicsymbols("q1 q2", 1) u1, u2 = dynamicsymbols("u1 u2") u1d, u2d = dynamicsymbols("u1 u2", 1) # Creating reference frames N = ReferenceFrame("N") A = ReferenceFrame("A") A.orient(N, "Axis", [-q2, N.z]) A.set_ang_vel(N, -u2 * N.z) # Origin of Newtonian reference frame O = Point("O") # Creating and Locating the positions of the cart, C, and the # center of mass of the pendulum, A C = O.locatenew("C", q1 * N.x) Ao = C.locatenew("Ao", a * A.y) # Defining velocities of the points O.set_vel(N, 0) C.set_vel(N, u1 * N.x) Ao.v2pt_theory(C, N, A) Cart = Particle("Cart", C, mC) Pendulum = RigidBody("Pendulum", Ao, A, mA, (inertia(A, Ix, Iy, Iz), C)) # kinematical differential equations kindiffs = [q1d - u1, q2d - u2] bodyList = [Cart, Pendulum] forceList = [ (Ao, -N.y * gravity * mA), (C, -N.y * gravity * mC), (C, -N.x * k * (q1 - ls)), (C, N.x * F), ] km = KanesMethod(N, [q1, q2], [u1, u2], kindiffs) with warns_deprecated_sympy(): (fr, frstar) = km.kanes_equations(forceList, bodyList) mm = km.mass_matrix_full assert mm[3, 3] == Iz
def test_differentiate_finite(): x, y, h = symbols('x y h') f = Function('f') with warns_deprecated_sympy(): res0 = differentiate_finite(f(x, y) + exp(42), x, y, evaluate=True) xm, xp, ym, yp = [ v + sign * S.Half for v, sign in product([x, y], [-1, 1]) ] ref0 = f(xm, ym) + f(xp, yp) - f(xm, yp) - f(xp, ym) assert (res0 - ref0).simplify() == 0 g = Function('g') with warns_deprecated_sympy(): res1 = differentiate_finite(f(x) * g(x) + 42, x, evaluate=True) ref1 = (-f(x - S.Half) + f(x + S.Half))*g(x) + \ (-g(x - S.Half) + g(x + S.Half))*f(x) assert (res1 - ref1).simplify() == 0 res2 = differentiate_finite(f(x) + x**3 + 42, x, points=[x - 1, x + 1]) ref2 = (f(x + 1) + (x + 1)**3 - f(x - 1) - (x - 1)**3) / 2 assert (res2 - ref2).simplify() == 0 raises(TypeError, lambda: differentiate_finite(f(x) * g(x), x, pints=[x - 1, x + 1])) res3 = differentiate_finite(f(x) * g(x).diff(x), x) ref3 = (-g(x) + g(x + 1)) * f(x + S.Half) - (g(x) - g(x - 1)) * f(x - S.Half) assert res3 == ref3 res4 = differentiate_finite(f(x) * g(x).diff(x).diff(x), x) ref4 = -((g(x - Rational(3, 2)) - 2*g(x - S.Half) + g(x + S.Half))*f(x - S.Half)) \ + (g(x - S.Half) - 2*g(x + S.Half) + g(x + Rational(3, 2)))*f(x + S.Half) assert res4 == ref4 res5_expr = f(x).diff(x) * g(x).diff(x) res5 = differentiate_finite(res5_expr, points=[x - h, x, x + h]) ref5 = (-2*f(x)/h + f(-h + x)/(2*h) + 3*f(h + x)/(2*h))*(-2*g(x)/h + g(-h + x)/(2*h) \ + 3*g(h + x)/(2*h))/(2*h) - (2*f(x)/h - 3*f(-h + x)/(2*h) - \ f(h + x)/(2*h))*(2*g(x)/h - 3*g(-h + x)/(2*h) - g(h + x)/(2*h))/(2*h) assert res5 == ref5 res6 = res5.limit(h, 0).doit() ref6 = diff(res5_expr, x) assert res6 == ref6
def test_source(): # Dummy stdout class StdOut(object): def write(self, x): pass # Test SymPyDeprecationWarning from source() with warns_deprecated_sympy(): # Redirect stdout temporarily so print out is not seen stdout = sys.stdout try: sys.stdout = StdOut() source(point) finally: sys.stdout = stdout
def test_functions(): # Test at least one Function without own _sage_ method assert not "_sage_" in sympy.factorial.__dict__ check_expression("factorial(x)", "x") check_expression("sin(x)", "x") check_expression("cos(x)", "x") check_expression("tan(x)", "x") check_expression("cot(x)", "x") check_expression("asin(x)", "x") check_expression("acos(x)", "x") check_expression("atan(x)", "x") check_expression("atan2(y, x)", "x, y") check_expression("acot(x)", "x") check_expression("sinh(x)", "x") check_expression("cosh(x)", "x") check_expression("tanh(x)", "x") check_expression("coth(x)", "x") check_expression("asinh(x)", "x") check_expression("acosh(x)", "x") check_expression("atanh(x)", "x") check_expression("acoth(x)", "x") check_expression("exp(x)", "x") check_expression("gamma(x)", "x") check_expression("log(x)", "x") check_expression("re(x)", "x") check_expression("im(x)", "x") check_expression("sign(x)", "x") check_expression("abs(x)", "x") check_expression("arg(x)", "x") check_expression("conjugate(x)", "x") # The following tests differently named functions check_expression("besselj(y, x)", "x, y") check_expression("bessely(y, x)", "x, y") check_expression("besseli(y, x)", "x, y") check_expression("besselk(y, x)", "x, y") check_expression("DiracDelta(x)", "x") check_expression("KroneckerDelta(x, y)", "x, y") check_expression("expint(y, x)", "x, y") check_expression("Si(x)", "x") check_expression("Ci(x)", "x") check_expression("Shi(x)", "x") check_expression("Chi(x)", "x") check_expression("loggamma(x)", "x") check_expression("Ynm(n,m,x,y)", "n, m, x, y") with warns_deprecated_sympy(): check_expression("hyper((n,m),(m,n),x)", "n, m, x") check_expression("uppergamma(y, x)", "x, y")
def test_smith_normal(): m = Matrix([[12,6,4,8],[3,9,6,12],[2,16,14,28],[20,10,10,20]]) smf = Matrix([[1, 0, 0, 0], [0, 10, 0, 0], [0, 0, -30, 0], [0, 0, 0, 0]]) assert smith_normal_form(m) == smf x = Symbol('x') with warns_deprecated_sympy(): m = Matrix([[Poly(x-1), Poly(1, x),Poly(-1,x)], [0, Poly(x), Poly(-1,x)], [Poly(0,x),Poly(-1,x),Poly(x)]]) invs = 1, x - 1, x**2 - 1 assert invariant_factors(m, domain=QQ[x]) == invs m = Matrix([[2, 4]]) smf = Matrix([[2, 0]]) assert smith_normal_form(m) == smf
def test_Permutation(): import_stmt = "from sympy.combinatorics import Permutation" sT(Permutation(1, 2)(3, 4), "Permutation([0, 2, 1, 4, 3])", import_stmt, perm_cyclic=False) sT(Permutation(1, 2)(3, 4), "Permutation(1, 2)(3, 4)", import_stmt, perm_cyclic=True) with warns_deprecated_sympy(): old_print_cyclic = Permutation.print_cyclic Permutation.print_cyclic = False sT( Permutation(1, 2)(3, 4), "Permutation([0, 2, 1, 4, 3])", import_stmt) Permutation.print_cyclic = old_print_cyclic
def test_two_dof(): # This is for a 2 d.o.f., 2 particle spring-mass-damper. # The first coordinate is the displacement of the first particle, and the # second is the relative displacement between the first and second # particles. Speeds are defined as the time derivatives of the particles. q1, q2, u1, u2 = dynamicsymbols("q1 q2 u1 u2") q1d, q2d, u1d, u2d = dynamicsymbols("q1 q2 u1 u2", 1) m, c1, c2, k1, k2 = symbols("m c1 c2 k1 k2") N = ReferenceFrame("N") P1 = Point("P1") P2 = Point("P2") P1.set_vel(N, u1 * N.x) P2.set_vel(N, (u1 + u2) * N.x) kd = [q1d - u1, q2d - u2] # Now we create the list of forces, then assign properties to each # particle, then create a list of all particles. FL = [ (P1, (-k1 * q1 - c1 * u1 + k2 * q2 + c2 * u2) * N.x), (P2, (-k2 * q2 - c2 * u2) * N.x), ] pa1 = Particle("pa1", P1, m) pa2 = Particle("pa2", P2, m) BL = [pa1, pa2] # Finally we create the KanesMethod object, specify the inertial frame, # pass relevant information, and form Fr & Fr*. Then we calculate the mass # matrix and forcing terms, and finally solve for the udots. KM = KanesMethod(N, q_ind=[q1, q2], u_ind=[u1, u2], kd_eqs=kd) # The old input format raises a deprecation warning, so catch it here so # it doesn't cause py.test to fail. with warns_deprecated_sympy(): KM.kanes_equations(FL, BL) MM = KM.mass_matrix forcing = KM.forcing rhs = MM.inv() * forcing assert expand(rhs[0]) == expand( (-k1 * q1 - c1 * u1 + k2 * q2 + c2 * u2) / m) assert expand(rhs[1]) == expand( (k1 * q1 + c1 * u1 - 2 * k2 * q2 - 2 * c2 * u2) / m) assert simplify(KM.rhs() - KM.mass_matrix_full.LUsolve(KM.forcing_full)) == zeros( 4, 1)
def test_factor_and_dimension(): assert (3000, Dimension(1)) == SI._collect_factor_and_dimension(3000) assert (1001, length) == SI._collect_factor_and_dimension(meter + km) assert (2, length / time) == SI._collect_factor_and_dimension(meter / second + 36 * km / (10 * hour)) x, y = symbols("x y") assert (x + y / 100, length) == SI._collect_factor_and_dimension(x * m + y * centimeter) cH = Quantity("cH") SI.set_quantity_dimension(cH, amount_of_substance / volume) pH = -log(cH) assert (1, volume / amount_of_substance) == SI._collect_factor_and_dimension(exp(pH)) v_w1 = Quantity("v_w1") v_w2 = Quantity("v_w2") v_w1.set_global_relative_scale_factor(Rational(3, 2), meter / second) v_w2.set_global_relative_scale_factor(2, meter / second) expr = Abs(v_w1 / 2 - v_w2) assert (Rational(5, 4), length / time) == SI._collect_factor_and_dimension(expr) expr = Rational(5, 2) * second / meter * v_w1 - 3000 assert (-(2996 + Rational(1, 4)), Dimension(1)) == SI._collect_factor_and_dimension(expr) expr = v_w1**(v_w2 / v_w1) assert ( (Rational(3, 2))**Rational(4, 3), (length / time)**Rational(4, 3), ) == SI._collect_factor_and_dimension(expr) with warns_deprecated_sympy(): assert (3000, Dimension(1)) == Quantity._collect_factor_and_dimension(3000)
def test_arraycomprehensionmap(): a = ArrayComprehensionMap(lambda i: i + 1, (i, 1, 5)) assert a.doit().tolist() == [2, 3, 4, 5, 6] assert a.shape == (5, ) assert a.is_shape_numeric assert a.tolist() == [2, 3, 4, 5, 6] assert len(a) == 5 assert isinstance(a.doit(), ImmutableDenseNDimArray) expr = ArrayComprehensionMap(lambda i: i + 1, (i, 1, k)) assert expr.doit() == expr assert expr.subs(k, 4) == ArrayComprehensionMap(lambda i: i + 1, (i, 1, 4)) assert expr.subs(k, 4).doit() == ImmutableDenseNDimArray([2, 3, 4, 5]) b = ArrayComprehensionMap(lambda i: i + 1, (i, 1, 2), (i, 1, 3), (i, 1, 4), (i, 1, 5)) assert b.doit().tolist() == [[[[2, 3, 4, 5, 6], [3, 5, 7, 9, 11], [4, 7, 10, 13, 16], [5, 9, 13, 17, 21]], [[3, 5, 7, 9, 11], [5, 9, 13, 17, 21], [7, 13, 19, 25, 31], [9, 17, 25, 33, 41]], [[4, 7, 10, 13, 16], [7, 13, 19, 25, 31], [10, 19, 28, 37, 46], [13, 25, 37, 49, 61]]], [[[3, 5, 7, 9, 11], [5, 9, 13, 17, 21], [7, 13, 19, 25, 31], [9, 17, 25, 33, 41]], [[5, 9, 13, 17, 21], [9, 17, 25, 33, 41], [13, 25, 37, 49, 61], [17, 33, 49, 65, 81]], [[7, 13, 19, 25, 31], [13, 25, 37, 49, 61], [19, 37, 55, 73, 91], [25, 49, 73, 97, 121]]]] # tests about lambda expression assert ArrayComprehensionMap(lambda: 3, (i, 1, 5)).doit().tolist() == [3, 3, 3, 3, 3] assert ArrayComprehensionMap(lambda i: i + 1, (i, 1, 5)).doit().tolist() == [2, 3, 4, 5, 6] raises(ValueError, lambda: ArrayComprehensionMap(i * j, (i, 1, 3), (j, 2, 4))) with warns_deprecated_sympy(): a = ArrayComprehensionMap(lambda i, j: i + j, (i, 1, 5)) raises(ValueError, lambda: a.doit())
def test_linearize_pendulum_kane_minimal(): q1 = dynamicsymbols("q1") # angle of pendulum u1 = dynamicsymbols("u1") # Angular velocity q1d = dynamicsymbols("q1", 1) # Angular velocity L, m, t = symbols("L, m, t") g = 9.8 # Compose world frame N = ReferenceFrame("N") pN = Point("N*") pN.set_vel(N, 0) # A.x is along the pendulum A = N.orientnew("A", "axis", [q1, N.z]) A.set_ang_vel(N, u1 * N.z) # Locate point P relative to the origin N* P = pN.locatenew("P", L * A.x) P.v2pt_theory(pN, N, A) pP = Particle("pP", P, m) # Create Kinematic Differential Equations kde = Matrix([q1d - u1]) # Input the force resultant at P R = m * g * N.x # Solve for eom with kanes method KM = KanesMethod(N, q_ind=[q1], u_ind=[u1], kd_eqs=kde) with warns_deprecated_sympy(): (fr, frstar) = KM.kanes_equations([(P, R)], [pP]) # Linearize A, B, inp_vec = KM.linearize(A_and_B=True, simplify=True) assert A == Matrix([[0, 1], [-9.8 * cos(q1) / L, 0]]) assert B == Matrix([])