1
1
from __future__ import annotations
2
2
3
- import array
4
3
import numbers
5
4
from functools import total_ordering
6
5
from math import sqrt
7
- from typing import Any
6
+ from typing import SupportsFloat
8
7
from typing import Union
9
8
10
- from .dtypes import Dtype
11
- from .dtypes import Number
12
- from .dtypes import float64
13
-
14
9
15
10
@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.
19
14
20
15
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 .
23
18
24
19
Raises:
25
20
TypeError: If x or y are not a number.
26
21
"""
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
32
25
else :
33
26
raise TypeError ('You must pass in int/float value for x and y!' )
34
27
@@ -38,23 +31,23 @@ def __repr__(self) -> str:
38
31
Returns:
39
32
The representation of the vector.
40
33
"""
41
- return f'vector.VectorND ({ self .values } )'
34
+ return f'vector.Vector2D ({ self .x } , { self . y } )'
42
35
43
36
def __str__ (self ) -> str :
44
37
"""The vector as a string.
45
38
46
39
Returns:
47
40
The vector as a string.
48
41
"""
49
- return f'({ self .values } )'
42
+ return f'({ self .x } , { self . y } )'
50
43
51
44
def __abs__ (self ) -> float :
52
45
"""Return the length (magnitude) of the vector.
53
46
54
47
Returns:
55
48
Length of the vector.
56
49
"""
57
- return sqrt (sum ( pow (val , 2 ) for val in self .values ))
50
+ return sqrt (pow (self . x , 2 ) + pow ( self .y , 2 ))
58
51
59
52
def __eq__ (self , other_vector : object ) -> bool :
60
53
"""Check if the vectors have the same values.
@@ -66,11 +59,11 @@ def __eq__(self, other_vector: object) -> bool:
66
59
True, if the both vectors have the same values.
67
60
False, else.
68
61
"""
69
- if not isinstance (other_vector , VectorND ):
62
+ if not isinstance (other_vector , Vector2D ):
70
63
return False
71
- return self .values == other_vector .values
64
+ return self .x == other_vector .x and self . y == other_vector . y
72
65
73
- def __lt__ (self , other_vector : VectorND ) -> bool :
66
+ def __lt__ (self , other_vector : Vector2D ) -> bool :
74
67
"""Check if the self is less than the other vector.
75
68
76
69
Args:
@@ -80,25 +73,26 @@ def __lt__(self, other_vector: VectorND) -> bool:
80
73
True, if the self is less than the other vector.
81
74
False, else.
82
75
"""
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!' )
85
78
return abs (self ) < abs (other_vector )
86
79
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.
89
82
90
83
Args:
91
84
other_vector: Other vector (rhs).
92
85
93
86
Returns:
94
- The additon vector of the self and the other vector.
87
+ The addition vector of the self and the other vector.
95
88
"""
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 )
100
94
101
- def __sub__ (self , other_vector : VectorND ) -> VectorND :
95
+ def __sub__ (self , other_vector : Vector2D ) -> Vector2D :
102
96
"""Return the subtraction vector of the self and the other vector.
103
97
104
98
Args:
@@ -107,14 +101,15 @@ def __sub__(self, other_vector: VectorND) -> VectorND:
107
101
Returns:
108
102
The subtraction vector of the self and the other vector.
109
103
"""
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 )
114
109
115
110
def __mul__ (
116
- self , other : Union [VectorND , Number ]
117
- ) -> Union [VectorND , Number ]:
111
+ self , other : Union [Vector2D , SupportsFloat ]
112
+ ) -> Union [Vector2D , SupportsFloat ]:
118
113
"""Return the multiplication of self and the other vector/number.
119
114
120
115
Args:
@@ -126,13 +121,14 @@ def __mul__(
126
121
Returns:
127
122
The multiplication of self and the other vector/number.
128
123
"""
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 ):
132
128
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 )
134
130
135
- def __truediv__ (self , other : Number ) -> VectorND :
131
+ def __truediv__ (self , other : SupportsFloat ) -> Vector2D :
136
132
"""Return the multiplication of self and the other vector/number.
137
133
138
134
Args:
@@ -145,35 +141,6 @@ def __truediv__(self, other: Number) -> VectorND:
145
141
Returns:
146
142
The multiplication of self and the other vector/number.
147
143
"""
148
- if not isinstance (other , int ) and not isinstance ( other , float ):
144
+ if not isinstance (other , numbers . Real ):
149
145
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