From ae9f0048b2bdbde9357473431242af20ae83f74f Mon Sep 17 00:00:00 2001 From: "J.V.G. Ransika" Date: Sat, 6 Jan 2024 13:22:47 +0530 Subject: [PATCH] ad dot() --- pymathzone/utils/vector/__init__.py | 3 +- pymathzone/utils/vector/multiplication.py | 18 +++++++ pymathzone/vector.py | 15 ++++++ tests/test_vector.py | 58 +++++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 pymathzone/utils/vector/multiplication.py diff --git a/pymathzone/utils/vector/__init__.py b/pymathzone/utils/vector/__init__.py index 1b2fee8..d38bd0a 100644 --- a/pymathzone/utils/vector/__init__.py +++ b/pymathzone/utils/vector/__init__.py @@ -1 +1,2 @@ -from .addition import addition \ No newline at end of file +from .addition import addition +from .multiplication import * \ No newline at end of file diff --git a/pymathzone/utils/vector/multiplication.py b/pymathzone/utils/vector/multiplication.py new file mode 100644 index 0000000..b071ed7 --- /dev/null +++ b/pymathzone/utils/vector/multiplication.py @@ -0,0 +1,18 @@ +def multiply_with_scalar(v, s): + """ + :param v: vector + :param s: scalar + :return: tuple with components of result vector + """ + return tuple(x * s for x in v.components) + +def dot_multiplication_with_vector(v1, v2): + """ + :param v1: + :param v2: + :return: a number + """ + if v1.get_dimension() != v2.get_dimension(): + raise Exception("both vector must be in same dimension for dot product") + + return sum(x*y for x,y in zip(v1.components, v2.components)) \ No newline at end of file diff --git a/pymathzone/vector.py b/pymathzone/vector.py index b5bb283..c24e609 100644 --- a/pymathzone/vector.py +++ b/pymathzone/vector.py @@ -51,5 +51,20 @@ def __eq__(self, other): return self.components == other.components + def __mul__(self, other): + if isinstance(other, int) or isinstance(other, float): + return Vector(multiply_with_scalar(self, other)) + else: + raise Exception("Invalid multiplication") + + def dot(self, other): + if isinstance(other, int) or isinstance(other, float): + return self.__mul__(other) + elif isinstance(other, Vector): + return dot_multiplication_with_vector(self,other) + else: + raise Exception("Invalid dot product") + + def get_dimension(self): return len(self.components) diff --git a/tests/test_vector.py b/tests/test_vector.py index a9621ac..fbd3b5f 100644 --- a/tests/test_vector.py +++ b/tests/test_vector.py @@ -34,6 +34,64 @@ def test_add_negative_vector(self): result = v1 + v2 self.assertEqual(result.components, (0, 0, 0)) + def test_multiplication_with_scalar(self): + vector = Vector((2, 3)) + result = vector * 2 + expected = Vector((4, 6)) + self.assertEqual(result, expected) + + def test_multiplication_with_negative_scalar(self): + vector = Vector((5, 1)) + result = vector * -3 + expected = Vector((-15, -3)) + self.assertEqual(result, expected) + + def test_multiplication_with_float_scalar(self): + vector = Vector((1.5, 2.4)) + result = vector * 0.5 + expected = Vector((0.75, 1.2)) + self.assertEqual(result, expected) + + def test_multiplication_with_invalid_type(self): + vector = Vector((1, 2)) + with self.assertRaises(Exception) as context: + vector * "hello" + self.assertEqual(str(context.exception), "Invalid multiplication") + + def test_multiplication_with_vector(self): + vector1 = Vector((1, 2)) + vector2 = Vector((3, 4)) + with self.assertRaises(Exception) as context: + vector1 * vector2 + self.assertEqual(str(context.exception), "Invalid multiplication") + + def test_dot_with_scalar(self): + vector = Vector((2, 3)) + result = vector.dot(5) + expected = Vector((10, 15)) # Assuming __mul__ handles scalar multiplication + self.assertEqual(result, expected) + + def test_dot_with_vector(self): + vector1 = Vector((1, 2)) + vector2 = Vector((3, 4)) + result = vector1.dot(vector2) + expected = 11 # Assuming dot_multiplication_with_vector calculates correctly + self.assertEqual(result, expected) + + def test_dot_with_different_dimension_vectors(self): + vector1 = Vector((1, 2)) + vector2 = Vector((3, 4, 5)) + with self.assertRaises(Exception) as context: + vector1.dot(vector2) + self.assertEqual(str(context.exception), "both vector must be in same dimension for dot product") + + def test_dot_with_invalid_type(self): + vector = Vector((1, 2)) + with self.assertRaises(Exception) as context: + vector.dot("hello") + self.assertEqual(str(context.exception), "Invalid dot product") + + if __name__ == '__main__': unittest.main()