@@ -23,10 +23,147 @@ def vector_addition(vec1: List[int], vec2: List[int]) -> List[int]:
2323 logging .debug (f"Vector addition result: { result } " )
2424 return result
2525
26- # Additional vector operations with similar refactoring...
26+ def vector_substraction (vec1 : List [int ], vec2 : List [int ]) -> List [int ]:
27+ """Substract two vectors element-wise.
28+ Args:
29+ vec1 (List[int]): The first vector.
30+ vec2 (List[int]): The second vector.
31+ Returns:
32+ List[int]: The resultant vector after substraction.
33+ """
34+ if len (vec1 ) != len (vec2 ):
35+ logging .error ("Vectors must be of the same length." )
36+ raise ValueError ("Vectors must be of the same length." )
37+
38+ result = [a - b for a , b in zip (vec1 , vec2 )]
39+ logging .debug (f"Vector substraction result: { result } " )
40+ return result
41+
42+
43+ def vector_division (vec1 : List [int ], vec2 : List [int ]) -> List [int ]:
44+ """Divide two vectors element-wise.
45+ Args:
46+ vec1 (List[int]): The first vector.
47+ vec2 (List[int]): The second vector.
48+ Returns:
49+ List[int]: The resultant vector after division.
50+ """
51+ if len (vec1 ) != len (vec2 ):
52+ logging .error ("Vectors must be of the same length." )
53+ raise ValueError ("Vectors must be of the same length." )
54+
55+ result = [a / b for a , b in zip (vec1 , vec2 )]
56+ logging .debug (f"Vector division result: { result } " )
57+ return result
58+
59+ def dot_product (vec1 : List [int ], vec2 : List [int ]) -> int :
60+ """Calculate the dot product of two vectors.
61+ Args:
62+ vec1 (List[int]): The first vector.
63+ vec2 (List[int]): The second vector.
64+ Returns:
65+ int: The dot product of the two vectors.
66+ """
67+ if len (vec1 ) != len (vec2 ):
68+ logging .error ("Vectors must be of the same length." )
69+ raise ValueError ("Vectors must be of the same length." )
70+
71+ result = sum ([a * b for a , b in zip (vec1 , vec2 )])
72+ logging .debug (f"Dot product result: { result } " )
73+ return result
74+
75+ def cross_product (vec1 : List [int ], vec2 : List [int ]) -> List [int ]:
76+ """Calculate the cross product of two vectors.
77+ Args:
78+ vec1 (List[int]): The first vector.
79+ vec2 (List[int]): The second vector.
80+ Returns:
81+ List[int]: The cross product of the two vectors.
82+ """
83+ if len (vec1 ) != len (vec2 ):
84+ logging .error ("Vectors must be of the same length." )
85+ raise ValueError ("Vectors must be of the same length." )
86+
87+ if len (vec1 ) != 3 :
88+ logging .error ("Cross product is only defined for 3D vectors." )
89+ raise ValueError ("Cross product is only defined for 3D vectors." )
90+
91+ result = [
92+ vec1 [1 ] * vec2 [2 ] - vec1 [2 ] * vec2 [1 ],
93+ vec1 [2 ] * vec2 [0 ] - vec1 [0 ] * vec2 [2 ],
94+ vec1 [0 ] * vec2 [1 ] - vec1 [1 ] * vec2 [0 ]
95+ ]
96+ logging .debug (f"Cross product result: { result } " )
97+ return result
98+
99+ def magnitude (vec : List [int ]) -> float :
100+ """Calculate the magnitude of a vector.
101+ Args:
102+ vec (List[int]): The vector.
103+ Returns:
104+ float: The magnitude of the vector.
105+ """
106+ result = sum ([a ** 2 for a in vec ]) ** 0.5
107+ logging .debug (f"Magnitude result: { result } " )
108+ return result
109+
110+ def normalization (vec : List [int ]) -> List [int ]:
111+ """Normalize a vector.
112+ Args:
113+ vec (List[int]): The vector.
114+ Returns:
115+ List[int]: The normalized vector.
116+ """
117+ mag = magnitude (vec )
118+ result = [a / mag for a in vec ]
119+ logging .debug (f"Normalization result: { result } " )
120+ return result
121+
122+ def vector_angle (vec1 : List [int ], vec2 : List [int ]) -> float :
123+ """Calculate the angle between two vectors.
124+ Args:
125+ vec1 (List[int]): The first vector.
126+ vec2 (List[int]): The second vector.
127+ Returns:
128+ float: The angle between the two vectors.
129+ """
130+ dot = dot_product (vec1 , vec2 )
131+ mag1 = magnitude (vec1 )
132+ mag2 = magnitude (vec2 )
133+ result = dot / (mag1 * mag2 )
134+ logging .debug (f"Vector angle result: { result } " )
135+ return result
136+
137+ def tensor_vector_product (tensor : List [List [int ]], vec : List [int ]) -> List [int ]:
138+ """Calculate the product of a tensor and a vector.
139+ Args:
140+ tensor (List[List[int]]): The tensor.
141+ vec (List[int]): The vector.
142+ Returns:
143+ List[int]: The resultant vector after the product.
144+ """
145+ if len (tensor ) != len (vec ):
146+ logging .error ("Tensor and vector must be of the same length." )
147+ raise ValueError ("Tensor and vector must be of the same length." )
148+
149+ result = [sum ([a * b for a , b in zip (row , vec )]) for row in tensor ]
150+ logging .debug (f"Tensor vector product result: { result } " )
151+ return result
152+
27153
28154# Example usage
29155if __name__ == "__main__" :
30156 v1 = [1 , 2 , 3 ]
31157 v2 = [4 , 5 , 6 ]
32158 print ("Vector Addition:" , vector_addition (v1 , v2 ))
159+
160+ print ("Vector Substraction:" , vector_substraction (v1 , v2 ))
161+ print ("Vector Division:" , vector_division (v1 , v2 ))
162+ print ("Dot Product:" , dot_product (v1 , v2 ))
163+ print ("Cross Product:" , cross_product (v1 , v2 ))
164+ print ("Magnitude:" , magnitude (v1 ))
165+ print ("Normalization:" , normalization (v1 ))
166+ print ("Vector Angle:" , vector_angle (v1 , v2 ))
167+ print ("Tensor Vector Product:" , tensor_vector_product ([[1 , 2 , 3 ], [4 , 5 , 6 ], [7 , 8 , 9 ]], v1 ))
168+
169+
0 commit comments