def test_cross(): assert cross(A.x, A.x) == 0 assert cross(A.x, A.y) == A.z assert cross(A.x, A.z) == -A.y assert cross(A.y, A.x) == -A.z assert cross(A.y, A.y) == 0 assert cross(A.y, A.z) == A.x assert cross(A.z, A.x) == A.y assert cross(A.z, A.y) == -A.x assert cross(A.z, A.z) == 0
def _orient_frames(self): self.child.frame.orient_axis(self.parent.frame, self.parent_axis, 0) angle, axis = self._alignment_rotation(self.parent_axis, self.child_axis) odd_multiple_pi = False if angle % pi == 0: if (angle / pi).is_odd: odd_multiple_pi = True with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) if axis != Vector(0) or odd_multiple_pi: if odd_multiple_pi: axis = cross(self.parent_axis, self._generate_vector()) int_frame = ReferenceFrame('int_frame') int_frame.orient_axis(self.child.frame, self.child_axis, 0) int_frame.orient_axis(self.parent.frame, axis, angle) self.child.frame.orient_axis(int_frame, self.parent_axis, self.coordinates[0]) else: self.child.frame.orient_axis(self.parent.frame, self.parent_axis, self.coordinates[0])
def _generate_vector(self): parent_frame = self.parent.frame components = self.parent_axis.to_matrix(parent_frame) x, y, z = components[0], components[1], components[2] if x != 0: if y != 0: if z != 0: return cross(self.parent_axis, parent_frame.x) if z != 0: return parent_frame.y return parent_frame.z if x == 0: if y != 0: if z != 0: return parent_frame.x return parent_frame.x return parent_frame.y
def _orient_frames(self): self.child.frame.orient_axis(self.parent.frame, self.parent_axis, 0) angle, axis = self._alignment_rotation(self.parent_axis, self.child_axis) with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=UserWarning) if axis != Vector(0) or angle == pi: if angle == pi: axis = cross(self.parent_axis, self._generate_vector()) int_frame = ReferenceFrame('int_frame') int_frame.orient_axis(self.child.frame, self.child_axis, 0) int_frame.orient_axis(self.parent.frame, axis, angle) self.child.frame.orient_axis(int_frame, self.parent_axis, self.coordinates[0]) else: self.child.frame.orient_axis(self.parent.frame, self.parent_axis, self.coordinates[0])
def _alignment_rotation(self, parent, child): # Returns the axis and angle between two axis(vectors). angle = parent.angle_between(child) axis = cross(child, parent).normalize() return angle, axis
def test_operator_match(): """Test that the output of dot, cross, outer functions match operator behavior. """ A = ReferenceFrame('A') v = A.x + A.y d = v | v zerov = Vector(0) zerod = Dyadic(0) # dot products assert d & d == dot(d, d) assert d & zerod == dot(d, zerod) assert zerod & d == dot(zerod, d) assert d & v == dot(d, v) assert v & d == dot(v, d) assert d & zerov == dot(d, zerov) assert zerov & d == dot(zerov, d) raises(TypeError, lambda: dot(d, S(0))) raises(TypeError, lambda: dot(S(0), d)) raises(TypeError, lambda: dot(d, 0)) raises(TypeError, lambda: dot(0, d)) assert v & v == dot(v, v) assert v & zerov == dot(v, zerov) assert zerov & v == dot(zerov, v) raises(TypeError, lambda: dot(v, S(0))) raises(TypeError, lambda: dot(S(0), v)) raises(TypeError, lambda: dot(v, 0)) raises(TypeError, lambda: dot(0, v)) # cross products raises(TypeError, lambda: cross(d, d)) raises(TypeError, lambda: cross(d, zerod)) raises(TypeError, lambda: cross(zerod, d)) assert d ^ v == cross(d, v) assert v ^ d == cross(v, d) assert d ^ zerov == cross(d, zerov) assert zerov ^ d == cross(zerov, d) assert zerov ^ d == cross(zerov, d) raises(TypeError, lambda: cross(d, S(0))) raises(TypeError, lambda: cross(S(0), d)) raises(TypeError, lambda: cross(d, 0)) raises(TypeError, lambda: cross(0, d)) assert v ^ v == cross(v, v) assert v ^ zerov == cross(v, zerov) assert zerov ^ v == cross(zerov, v) raises(TypeError, lambda: cross(v, S(0))) raises(TypeError, lambda: cross(S(0), v)) raises(TypeError, lambda: cross(v, 0)) raises(TypeError, lambda: cross(0, v)) # outer products raises(TypeError, lambda: outer(d, d)) raises(TypeError, lambda: outer(d, zerod)) raises(TypeError, lambda: outer(zerod, d)) raises(TypeError, lambda: outer(d, v)) raises(TypeError, lambda: outer(v, d)) raises(TypeError, lambda: outer(d, zerov)) raises(TypeError, lambda: outer(zerov, d)) raises(TypeError, lambda: outer(zerov, d)) raises(TypeError, lambda: outer(d, S(0))) raises(TypeError, lambda: outer(S(0), d)) raises(TypeError, lambda: outer(d, 0)) raises(TypeError, lambda: outer(0, d)) assert v | v == outer(v, v) assert v | zerov == outer(v, zerov) assert zerov | v == outer(zerov, v) raises(TypeError, lambda: outer(v, S(0))) raises(TypeError, lambda: outer(S(0), v)) raises(TypeError, lambda: outer(v, 0)) raises(TypeError, lambda: outer(0, v))
def test_cross_different_frames(): assert cross(N.x, A.x) == sin(q1) * A.z assert cross(N.x, A.y) == cos(q1) * A.z assert cross(N.x, A.z) == -sin(q1) * A.x - cos(q1) * A.y assert cross(N.y, A.x) == -cos(q1) * A.z assert cross(N.y, A.y) == sin(q1) * A.z assert cross(N.y, A.z) == cos(q1) * A.x - sin(q1) * A.y assert cross(N.z, A.x) == A.y assert cross(N.z, A.y) == -A.x assert cross(N.z, A.z) == 0 assert cross(N.x, A.x) == sin(q1) * A.z assert cross(N.x, A.y) == cos(q1) * A.z assert cross(N.x, A.x + A.y) == sin(q1) * A.z + cos(q1) * A.z assert cross(A.x + A.y, N.x) == -sin(q1) * A.z - cos(q1) * A.z assert cross(A.x, C.x) == sin(q3) * C.y assert cross(A.x, C.y) == -sin(q3) * C.x + cos(q3) * C.z assert cross(A.x, C.z) == -cos(q3) * C.y assert cross(C.x, A.x) == -sin(q3) * C.y assert cross(C.y, A.x) == sin(q3) * C.x - cos(q3) * C.z assert cross(C.z, A.x) == cos(q3) * C.y
def test_cross_different_frames(): assert cross(N.x, A.x) == sin(q1)*A.z assert cross(N.x, A.y) == cos(q1)*A.z assert cross(N.x, A.z) == -sin(q1)*A.x - cos(q1)*A.y assert cross(N.y, A.x) == -cos(q1)*A.z assert cross(N.y, A.y) == sin(q1)*A.z assert cross(N.y, A.z) == cos(q1)*A.x - sin(q1)*A.y assert cross(N.z, A.x) == A.y assert cross(N.z, A.y) == -A.x assert cross(N.z, A.z) == 0 assert cross(N.x, A.x) == sin(q1)*A.z assert cross(N.x, A.y) == cos(q1)*A.z assert cross(N.x, A.x + A.y) == sin(q1)*A.z + cos(q1)*A.z assert cross(A.x + A.y, N.x) == -sin(q1)*A.z - cos(q1)*A.z assert cross(A.x, C.x) == sin(q3)*C.y assert cross(A.x, C.y) == -sin(q3)*C.x + cos(q3)*C.z assert cross(A.x, C.z) == -cos(q3)*C.y assert cross(C.x, A.x) == -sin(q3)*C.y assert cross(C.y, A.x) == sin(q3)*C.x - cos(q3)*C.z assert cross(C.z, A.x) == cos(q3)*C.y