Compare commits

..

4 Commits

Author SHA1 Message Date
491c531ed8 Merge pull request 'Add cross-product and tests' (#9) from enh/cross-product into unit_tests
Reviewed-on: #9
2025-10-24 15:25:26 +00:00
Corran Webster
437be17f51 Add cross product impleimentation 2025-10-24 16:21:50 +01:00
64f06ce7c1 Merge branch 'unit_tests' into enh/cross-product 2025-10-24 15:19:46 +00:00
Corran Webster
13ac74132c Add cross-product and tests 2025-10-24 16:13:25 +01:00
2 changed files with 30 additions and 0 deletions

View File

@@ -36,6 +36,14 @@ class Vector(object):
z = self.z + other.z z = self.z + other.z
return self.__class__(x, y, 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__": if __name__ == "__main__":
v1 = Vector(3, 4, 0) v1 = Vector(3, 4, 0)

View File

@@ -4,6 +4,28 @@ from fancymath.vector import Vector
class TestVector(unittest.TestCase): 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): def test_add_vector(self):
v1 = Vector(1, 2, 3) v1 = Vector(1, 2, 3)
v2 = Vector(-3, -2, -1) v2 = Vector(-3, -2, -1)