Skip to content

Commit

Permalink
Merge pull request #18 from JvgRansika/dot-multiplication
Browse files Browse the repository at this point in the history
added dot()
  • Loading branch information
jv-ransika authored Jan 6, 2024
2 parents 943a0b4 + ae9f004 commit fa07bfe
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pymathzone/utils/vector/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .addition import addition
from .addition import addition
from .multiplication import *
18 changes: 18 additions & 0 deletions pymathzone/utils/vector/multiplication.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions pymathzone/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
58 changes: 58 additions & 0 deletions tests/test_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit fa07bfe

Please sign in to comment.