def test_vector_form_append_2(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) s = sp.Symbol('s', commutative=False) C = sp.Symbol('C', commutative=False) # vector 1-form Q1 = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q1_ = st.col_stack(Q1, sp.zeros(2, 6)) w1 = pc.VectorDifferentialForm(1, XX, coeff=Q1_) # 1-forms Q2 = sp.Matrix([[x1, x2, x3], [x3, x1, x2]]) Q2_ = st.col_stack(Q2, sp.zeros(2, 6)) w2 = pc.VectorDifferentialForm(1, XX, coeff=Q2_) w1.append(w2) # vector form to compare with: B = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3], [x1, x2, x3], [x3, x1, x2]]) B_ = st.col_stack(B, sp.zeros(4, 6)) self.assertEqual(w1.coeff, B_)
def test_left_mul_by_2(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) C = sp.Symbol('C', commutative=False) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) # matrix independent of s M2 = sp.Matrix([[1, 0], [-C, 1]]) # 1-forms w1 = pc.DifferentialForm(1, XX, coeff=Q_[0, :]) w2 = pc.DifferentialForm(1, XX, coeff=Q_[1, :]) # vector 1-form w = pc.VectorDifferentialForm(1, XX, coeff=Q_) t = w.left_mul_by(M2, additional_symbols=[C]) # object to compare with: t2 = -C * w1 + w2 self.assertEqual(t2.coeff, t.coeff.row(1).T)
def test_left_mul_by_1(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) s = sp.Symbol('s', commutative=False) C = sp.Symbol('C', commutative=False) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) # s-dependent matrix M1 = sp.Matrix([[1, 0], [-C * s, 1]]) # 1-forms w1 = pc.DifferentialForm(1, XX, coeff=Q_[0, :]) w2 = pc.DifferentialForm(1, XX, coeff=Q_[1, :]) # vector 1-form w = pc.VectorDifferentialForm(1, XX, coeff=Q_) t = w.left_mul_by(M1, s, [C]) t2 = -C * w1.dot() + w2 self.assertEqual(t2.coeff, t.coeff.row(1).T)
def test_mul(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) s = sp.Symbol('s', commutative=False) C = sp.Symbol('C', commutative=False) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) W = pc.VectorDifferentialForm(1, xx, coeff=Q) W1 = s * W W2 = W * C self.assertEqual(W1.coeff, nct.nc_mul(s, W.coeff)) self.assertNotEqual(W1.coeff, nct.nc_mul(W.coeff, s)) self.assertEqual(W2.coeff, nct.nc_mul(W.coeff, C)) self.assertNotEqual(W2.coeff, nct.nc_mul(C, W.coeff)) alpha = pc.DifferentialForm(1, xx) with self.assertRaises(TypeError) as cm: alpha * W1 with self.assertRaises(TypeError) as cm: W1 * alpha M = sp.eye(2) with self.assertRaises(sp.SympifyError) as cm: M * W1 with self.assertRaises(TypeError) as cm: W1 * M
def test_simplify(self): x1, x2, x3 = xx = st.symb_vector('x1:4', ) Q = sp.Matrix([[sin(x1)**2 + cos(x1)**2 - 1, 0, 0], [0, x3 * (1 - sin(x2)**2) - cos(x2)**2 * x3, 0]]) Omega = pc.VectorDifferentialForm(1, xx, coeff=Q) Omega2 = pc.simplify(Omega) self.assertEqual(Omega2.coeff, 0 * Omega.coeff)
def test_vector_form_dot(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [1, 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) # vector 1-forms omega = pc.VectorDifferentialForm(1, XX, coeff=Q_) omega_dot = omega.dot() omega_1, omega_2 = omega.unpack() omega_1dot = omega_1.dot() omega_2dot = omega_2.dot() Qdot_ = st.row_stack(omega_1dot.coeff.T, omega_2dot.coeff.T) # vector form to compare with omegadot_comp = pc.VectorDifferentialForm(1, XX, coeff=Qdot_) self.assertEqual(omega_dot.coeff, omegadot_comp.coeff)
def test_difference_vector_forms(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) A = sp.Matrix([[x3 / sin(x1), 1, 0], [1, 0, x3]]) A_ = st.col_stack(A, sp.zeros(2, 6)) B = sp.Matrix([[x3 / cos(x1), 0, 1], [-tan(x1), 0, x3]]) B_ = st.col_stack(B, sp.zeros(2, 6)) # vector 1-forms omega_a = pc.VectorDifferentialForm(1, XX, coeff=A_) omega_b = pc.VectorDifferentialForm(1, XX, coeff=B_) omega_c = omega_a - omega_b # vector form to compare with Q_ = A_ - B_ omega_comp = pc.VectorDifferentialForm(1, XX, coeff=Q_) self.assertEqual(omega_c.coeff, omega_comp.coeff)
def test_vector_form_dot(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [1, 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) # vector 1-forms omega = pc.VectorDifferentialForm(1, XX, coeff=Q_) with self.assertRaises(ValueError) as cm: # coordinates are not part of basis omega.dot().dot().dot()
def test_vector_form_get_coeff_from_idcs(self): x1, x2, x3 = xx = st.symb_vector('x1:4') xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) w = pc.VectorDifferentialForm(1, XX, coeff=Q_) w_0 = w.get_coeff_from_idcs(0) Q_0 = Q_.col(0) self.assertEqual(w_0, Q_0) w_1 = w.get_coeff_from_idcs(1) Q_1 = Q_.col(1) self.assertEqual(w_1, Q_1)
def test_stack_to_vector_form(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) # 1-forms w1 = pc.DifferentialForm(1, XX, coeff=Q_[0, :]) w2 = pc.DifferentialForm(1, XX, coeff=Q_[1, :]) w_stacked = pc.stack_to_vector_form(w1, w2) # vector 1-form w = pc.VectorDifferentialForm(1, XX, coeff=Q_) self.assertEqual(w.coeff, w_stacked.coeff)
def test_vector_k_form(self): x1, x2, x3 = xx = st.symb_vector('x1:4') xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) w1 = pc.DifferentialForm(1, XX, coeff=Q_[0, :]) w2 = pc.DifferentialForm(1, XX, coeff=Q_[1, :]) w = pc.VectorDifferentialForm(1, XX, coeff=Q_) w1_tilde = w.get_differential_form(0) self.assertEqual(w1.coeff, w1_tilde.coeff) w2_tilde = w.get_differential_form(1) self.assertEqual(w2.coeff, w2_tilde.coeff)
def test_vector_form_subs(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) C = sp.Symbol('C', commutative=False) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [C, 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) B = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) B_ = st.col_stack(B, sp.zeros(2, 6)) # vector 1-forms omega = pc.VectorDifferentialForm(1, XX, coeff=Q_).subs(C, -tan(x1)) self.assertEqual(B_, omega.coeff)
def test_left_mul_by_3(self): x1, x2, x3 = xx = st.symb_vector('x1:4', commutative=False) xdot1, xdot2, xdot3 = xxdot = st.time_deriv(xx, xx) xddot1, xddot2, xddot3 = xxddot = st.time_deriv(xxdot, xxdot) XX = st.row_stack(xx, xxdot, xxddot) s = sp.Symbol('s', commutative=False) C = sp.Symbol('C', commutative=False) Q = sp.Matrix([[x3 / sin(x1), 1, 0], [-tan(x1), 0, x3]]) Q_ = st.col_stack(Q, sp.zeros(2, 6)) M3 = sp.Matrix([[1, 0], [-C * s**2, 1]]) # vector 1-forms w = pc.VectorDifferentialForm(1, XX, coeff=Q_) with self.assertRaises(Exception) as cm: # raises NotImplemented but this might change t = w.left_mul_by(M3, s, additional_symbols=[C])