def test_eq_between_classes(self): class Vec7(CoordinateVector): @classmethod def dimension(cls): return 7 @classmethod def zero(cls): coords = tuple(0 for _ in range(0, cls.dimension())) return Vec7(*coords) self.assertFalse(Vec5(1, 2, 3, 4, 5) == Vec7(1, 2, 3, 4, 5, 6, 7)) self.assertFalse(Vec7(1, 2, 3, 4, 5, 6, 7) == Vec5(1, 2, 3, 4, 5))
def test_scalar_rmult_args(self): with self.assertRaises( TypeError, msg='should raise TypeError when passing numeric scalar'): u = Vec5(*self.random_coordinates(5)) s = 'a' s * u
def test_sub_incompatible_types(self): with self.assertRaises( TypeError, msg='should raise TypeError when not compatible class'): u = Vec5(*self.random_coordinates(5)) v = 'v' u - v
def test_call_happy_path(self): linear_map = LinearMap_3d_to_5d( ((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (13, 14, 15))) v3d = Vec3(1, 1, 1) expected_result = Vec5(6, 15, 24, 33, 42) self.assertEqual(linear_map(v3d), expected_result)
def test_add_incompatible_subclasses(self): def random_vec2(): return Vec2(self.random_scalar(), self.random_scalar()) with self.assertRaises( TypeError, msg='should raise TypeError when not compatible subclasses'): u = Vec5(*self.random_coordinates(5)) v = random_vec2() u + v
def test_mult_incompatible_types(self): class A(): def sayHello(self): return 'hello' with self.assertRaises( TypeError, msg='should raise TypeError when not compatible class'): u = Vec5(*self.random_coordinates(5)) v = A() u + v
def test_multiply_matrix_by_vector(self): m = Matrix_5x3(( (1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12), (13, 14, 15) )) v = Vec3(1, 1, 1) result = m * v self.assertEqual(result, Vec5(6, 15, 24, 33, 42))
def test_add_between_classes(self): class Vec7(CoordinateVector): @classmethod def dimension(cls): return 7 @classmethod def zero(cls): coords = tuple(0 for _ in range(0, cls.dimension())) return Vec7(*coords) with self.assertRaises( TypeError, msg='should raise TypeError when not compatible class'): Vec5(1, 2, 3, 4, 5) + Vec7(1, 2, 3, 4, 5, 6, 7)
def __mul__(self, other): if isinstance(other, (int, float)): return super().__mul__(other) if not Vec3 in other.__class__.mro(): raise TypeError( 'matrix multiplication expects a compatible vector with {} elements' .format(self.columns)) if len(other.coordinates) != self.columns: raise TypeError( 'matrix multiplication expects a compatible vector with {} elements' .format(self.columns)) resulting_coordinates = multiply_matrix_vector(self.items, other.coordinates) return Vec5(*resulting_coordinates)
def test_negate_vector(self): self.assertEqual(-Vec5(-1, 2, -3, 4, -5), Vec5(1, -2, 3, -4, 5))
def test_zero_vector(self): self.assertEqual(Vec5.zero(), Vec5(0, 0, 0, 0, 0))
def test_vector6d_is_vector_space(self): for _ in range(0, 100): a, b = self.random_scalar(), self.random_scalar() u, v, w = Vec5(*self.random_coordinates(5)), Vec5( *self.random_coordinates(5)), Vec5(*self.random_coordinates(5)) self.check_vector_space_rules(self.approx_equal, a, b, u, v, w)
def test_scalar_division(self): self.approx_equal( Vec5(1, -2, 3, -4, pi) / 3.25, Vec5(1 / 3.25, -2 / 3.25, 3 / 3.25, -4 / 3.25, pi / 3.25))
def __call__(self, v): result = multiply_matrix_vector(self.items, v.coordinates) return Vec5(*result)