def test_multiply_matrix_vector_non_valid_matrix_3(self): with self.assertRaises( TypeError, msg= 'should raise TypeError when passing not well-formed matrix for multiplication' ): m = ((1, 2, 3), (4, 5)) v = (1, 2, 3) multiply_matrix_vector(m, v)
def test_multiply_matrix_vector_non_valid_matrix_1(self): with self.assertRaises( TypeError, msg= 'should raise TypeError when passing invalid sizes for multiplication' ): m = ((1, 2, 'a'), (4, 5, 6)) v = (1, 2, 3, 4) multiply_matrix_vector(m, v)
def test_multiply_matrix_vector_non_valid_vector_2(self): with self.assertRaises( TypeError, msg= 'should raise TypeError when passing not valid vector for multiplication' ): m = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) v = 1 multiply_matrix_vector(m, v)
def new_function(v): a, b = translation_vector x, y = v matrix = ((1, 0, a), (0, 1, b), (0, 0, 1)) upgraded_vector = (x, y, 1) x_out, y_out, _ = multiply_matrix_vector(matrix, upgraded_vector) return (x_out, y_out)
def new_function(target): a, b, c = translation_vector x, y, z = target matrix = ((1, 0, 0, a), (0, 1, 0, b), (0, 0, 1, c), (0, 0, 0, 1)) vector = (x, y, z, 1) x_out, y_out, z_out, _ = \ multiply_matrix_vector(matrix, vector) return (x_out, y_out, z_out)
def test_multiply_matrix_vector_happy_path_3d(self): m = ((1, 2, 3), (4, 5, 6), (7, 8, 9)) v = (1, 2, 3) actual_multiplication_result = multiply_matrix_vector(m, v) expected_multiplication_result = (14, 32, 50) self.assertEqual(actual_multiplication_result, expected_multiplication_result, msg='multiplication results should match')
def test_multiply_matrix_vector_happy_path_2d(self): m = ((1, 2), (3, 4)) v = (1, 2) actual_multiplication_result = multiply_matrix_vector(m, v) expected_multiplication_result = (5, 11) self.assertEqual(actual_multiplication_result, expected_multiplication_result, msg='multiplication results should match')
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 new_function(v): return multiply_matrix_vector(matrix, v)
def __call__(self, v): result = multiply_matrix_vector(self.items, v.coordinates) return Vec5(*result)