11from __future__ import annotations
22
3- import array
43import numbers
54from functools import total_ordering
65from math import sqrt
7- from typing import Any
6+ from typing import SupportsFloat
87from typing import Union
98
10- from .dtypes import Dtype
11- from .dtypes import Number
12- from .dtypes import float64
13-
149
1510@total_ordering
16- class VectorND :
17- def __init__ (self , * args : Any , dtype : Dtype = float64 ) -> None :
18- """Create a vector with the given values.
11+ class Vector2D :
12+ def __init__ (self , x : SupportsFloat = 0.0 , y : SupportsFloat = 0.0 ) -> None :
13+ """Create a vector with the given x and y values.
1914
2015 Args:
21- args (Any): The vector values .
22- dtype (Dtype): The data type of the array.array .
16+ x: x-Value .
17+ y: y-Value .
2318
2419 Raises:
2520 TypeError: If x or y are not a number.
2621 """
27- if len (args ) == 1 and isinstance (args [0 ], list ):
28- self .values = array .array (dtype , args [0 ])
29- elif len (args ) > 1 :
30- inputs = [val for val in args ]
31- self .values = array .array (dtype , inputs )
22+ if isinstance (x , numbers .Real ) and isinstance (y , numbers .Real ):
23+ self .x = x
24+ self .y = y
3225 else :
3326 raise TypeError ('You must pass in int/float value for x and y!' )
3427
@@ -38,23 +31,23 @@ def __repr__(self) -> str:
3831 Returns:
3932 The representation of the vector.
4033 """
41- return f'vector.VectorND ({ self .values } )'
34+ return f'vector.Vector2D ({ self .x } , { self . y } )'
4235
4336 def __str__ (self ) -> str :
4437 """The vector as a string.
4538
4639 Returns:
4740 The vector as a string.
4841 """
49- return f'({ self .values } )'
42+ return f'({ self .x } , { self . y } )'
5043
5144 def __abs__ (self ) -> float :
5245 """Return the length (magnitude) of the vector.
5346
5447 Returns:
5548 Length of the vector.
5649 """
57- return sqrt (sum ( pow (val , 2 ) for val in self .values ))
50+ return sqrt (pow (self . x , 2 ) + pow ( self .y , 2 ))
5851
5952 def __eq__ (self , other_vector : object ) -> bool :
6053 """Check if the vectors have the same values.
@@ -66,11 +59,11 @@ def __eq__(self, other_vector: object) -> bool:
6659 True, if the both vectors have the same values.
6760 False, else.
6861 """
69- if not isinstance (other_vector , VectorND ):
62+ if not isinstance (other_vector , Vector2D ):
7063 return False
71- return self .values == other_vector .values
64+ return self .x == other_vector .x and self . y == other_vector . y
7265
73- def __lt__ (self , other_vector : VectorND ) -> bool :
66+ def __lt__ (self , other_vector : Vector2D ) -> bool :
7467 """Check if the self is less than the other vector.
7568
7669 Args:
@@ -80,25 +73,26 @@ def __lt__(self, other_vector: VectorND) -> bool:
8073 True, if the self is less than the other vector.
8174 False, else.
8275 """
83- if not isinstance (other_vector , VectorND ):
84- raise TypeError ('You must pass in a VectorND instance!' )
76+ if not isinstance (other_vector , Vector2D ):
77+ raise TypeError ('You must pass in a Vector2D instance!' )
8578 return abs (self ) < abs (other_vector )
8679
87- def __add__ (self , other_vector : VectorND ) -> VectorND :
88- """Returns the additon vector of the self and the other vector.
80+ def __add__ (self , other_vector : Vector2D ) -> Vector2D :
81+ """Returns the addition vector of the self and the other vector.
8982
9083 Args:
9184 other_vector: Other vector (rhs).
9285
9386 Returns:
94- The additon vector of the self and the other vector.
87+ The addition vector of the self and the other vector.
9588 """
96- if not isinstance (other_vector , VectorND ):
97- raise TypeError ('You must pass in a VectorND instance!' )
98- result = [v1 + v2 for v1 , v2 in zip (self .values , other_vector .values )]
99- return VectorND (result )
89+ if not isinstance (other_vector , Vector2D ):
90+ raise TypeError ('You must pass in a Vector2D instance!' )
91+ x = self .x + other_vector .x
92+ y = self .y + other_vector .y
93+ return Vector2D (x , y )
10094
101- def __sub__ (self , other_vector : VectorND ) -> VectorND :
95+ def __sub__ (self , other_vector : Vector2D ) -> Vector2D :
10296 """Return the subtraction vector of the self and the other vector.
10397
10498 Args:
@@ -107,14 +101,15 @@ def __sub__(self, other_vector: VectorND) -> VectorND:
107101 Returns:
108102 The subtraction vector of the self and the other vector.
109103 """
110- if not isinstance (other_vector , VectorND ):
111- raise TypeError ('You must pass in a VectorND instance!' )
112- result = [v1 - v2 for v1 , v2 in zip (self .values , other_vector .values )]
113- return VectorND (result )
104+ if not isinstance (other_vector , Vector2D ):
105+ raise TypeError ('You must pass in a Vector2D instance!' )
106+ x = self .x - other_vector .x
107+ y = self .y - other_vector .y
108+ return Vector2D (x , y )
114109
115110 def __mul__ (
116- self , other : Union [VectorND , Number ]
117- ) -> Union [VectorND , Number ]:
111+ self , other : Union [Vector2D , SupportsFloat ]
112+ ) -> Union [Vector2D , SupportsFloat ]:
118113 """Return the multiplication of self and the other vector/number.
119114
120115 Args:
@@ -126,13 +121,14 @@ def __mul__(
126121 Returns:
127122 The multiplication of self and the other vector/number.
128123 """
129- if isinstance (other , VectorND ):
130- return sum ([v1 * v2 for v1 , v2 in zip (self .values , other .values )])
131- if not isinstance (other , int ) and not isinstance (other , float ):
124+ if isinstance (other , Vector2D ):
125+ result : SupportsFloat = self .x * other .x + self .y * other .y
126+ return result
127+ if not isinstance (other , numbers .Real ):
132128 raise TypeError ('You must pass in an int/float!' )
133- return VectorND ([ v * other for v in self .values ] )
129+ return Vector2D ( self . x * other , self .y * other )
134130
135- def __truediv__ (self , other : Number ) -> VectorND :
131+ def __truediv__ (self , other : SupportsFloat ) -> Vector2D :
136132 """Return the multiplication of self and the other vector/number.
137133
138134 Args:
@@ -145,35 +141,6 @@ def __truediv__(self, other: Number) -> VectorND:
145141 Returns:
146142 The multiplication of self and the other vector/number.
147143 """
148- if not isinstance (other , int ) and not isinstance ( other , float ):
144+ if not isinstance (other , numbers . Real ):
149145 raise TypeError ('You must pass in an int/float!' )
150- return VectorND ([v / other for v in self .values ])
151-
152- def __len__ (self ) -> int :
153- """Returns the length of the vector.
154-
155- Returns:
156- int: The length.
157- """
158- return len (self .values )
159-
160- def __getitem__ (self , idx : int ) -> Number :
161- """Returns the i-th component of the vector.
162-
163- Args:
164- idx (int): i-th component index
165-
166- Returns:
167- Number: The value at the i-th component
168- """
169- result : Number = self .values [idx ]
170- return result
171-
172- def __setitem__ (self , idx : int , val : Number ) -> None :
173- """Updates the i-th component of the vector.
174-
175- Args:
176- idx (int): i-th component index
177- val (Number): The updated valued
178- """
179- self .values [idx ] = val
146+ return Vector2D (self .x / other , self .y / other )
0 commit comments