From 352480ac14c8daa5509b269cdf67a6ee2baf3f18 Mon Sep 17 00:00:00 2001 From: David Moody Date: Fri, 24 Oct 2025 10:08:28 -0500 Subject: [PATCH 1/4] add test_dot_product to test_vector.py --- test/test_vector.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_vector.py b/test/test_vector.py index 1f4ae0c..04d7304 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -10,3 +10,11 @@ class TestVector(unittest.TestCase): v3 = v1 + v2 self.assertIsInstance(v3, Vector) self.assertEqual(v3.x, v1.x + v2.x) + + def test_dot_product(self): + v1 = Vector(1, 2, 3) + v2 = Vector(-3, -2, -1) + v3 = v1.dot(v2) + self.assertEqual(v3,v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) + vzero = Vector(0, 0, 0) + self.assertEqual(v1.dot(vzero),0) From 13ac74132c8496cb924f98a568ebb56dfa4ee64a Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Fri, 24 Oct 2025 16:13:25 +0100 Subject: [PATCH 2/4] Add cross-product and tests --- test/test_vector.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test_vector.py b/test/test_vector.py index 1f4ae0c..3606b6d 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -4,6 +4,28 @@ from fancymath.vector import Vector class TestVector(unittest.TestCase): + def test_cross_product(self): + # given + v1 = Vector(1, 2, 3) + v2 = Vector(3, 2, 1) + + # when + v3 = v1 * v2 + + # then + self.assertIsInstance(v3, Vector) + self.assertEqual(v3.x, -4) + self.assertEqual(v3.y, 8) + self.assertEqual(v3.z, -4) + + def test_cross_product_failure(self): + v1 = Vector(1, 2, 3) + v2 = (3, 2, 1) + + with self.assertRaises(TypeError): + v1 * v2 + + def test_add_vector(self): v1 = Vector(1, 2, 3) v2 = Vector(-3, -2, -1) From 4d5e2db71adc7d9e4467d9b07ff72950070b16b1 Mon Sep 17 00:00:00 2001 From: Tim Diller Date: Fri, 24 Oct 2025 10:11:43 -0500 Subject: [PATCH 3/4] add test for proper failure of Vector.dot --- test/test_vector.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_vector.py b/test/test_vector.py index 04d7304..d3b0f65 100644 --- a/test/test_vector.py +++ b/test/test_vector.py @@ -11,6 +11,11 @@ class TestVector(unittest.TestCase): self.assertIsInstance(v3, Vector) self.assertEqual(v3.x, v1.x + v2.x) + def test_dot_failure(self): + with self.assertRaises(ValueError): + v1 = Vector(1, 2, 3) + v1.dot(3) + def test_dot_product(self): v1 = Vector(1, 2, 3) v2 = Vector(-3, -2, -1) From 437be17f51b8bc7fbf9e79cceff3769e98ac04de Mon Sep 17 00:00:00 2001 From: Corran Webster Date: Fri, 24 Oct 2025 16:21:50 +0100 Subject: [PATCH 4/4] Add cross product impleimentation --- src/fancymath/vector.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/fancymath/vector.py b/src/fancymath/vector.py index 6c10466..c027e5b 100644 --- a/src/fancymath/vector.py +++ b/src/fancymath/vector.py @@ -36,6 +36,14 @@ class Vector(object): z = self.z + other.z return self.__class__(x, y, z) + def __mul__(self, other): + if not isinstance(other, Vector): + return NotImplemented + x = self.y * other.z - self.z * other.y + y = self.z * other.x - self.x * other.z + z = self.x * other.y - self.y * other.x + return self.__class__(x, y, z) + if __name__ == "__main__": v1 = Vector(3, 4, 0)