6
6
from collections .abc import Iterable
7
7
8
8
from itertools import chain
9
+ from functools import reduce
9
10
import operator
10
11
11
- import math
12
+ from math import sqrt
13
+
12
14
import numpy as np
13
15
14
16
class Vector4DType (Enum ):
@@ -185,6 +187,10 @@ def tr(self): # pylint: disable=invalid-name
185
187
elems = self ._asdict ().values ()
186
188
return type (self )(elems , shape = (cols , rows ))
187
189
190
+ def abs (self ):
191
+ """Returns length of vector."""
192
+ return vect_norm (self .get_field_values ())
193
+
188
194
class Point2D (MixinVector , metaclass = NamedTupleMetaEx ):
189
195
"""Two-dimensional point with x and y ordinate."""
190
196
_shape = (2 ,1 )
@@ -203,6 +209,12 @@ class Barycentric(MixinVector, metaclass=NamedTupleMetaEx):
203
209
one_u_v : float
204
210
u : float
205
211
v : float
212
+
213
+ class Vector2D (MixinVector , metaclass = NamedTupleMetaEx ):
214
+ """Two-dimensional point with x and y ordinate."""
215
+ _shape = (2 ,1 )
216
+ x : float
217
+ y : float
206
218
class Vector3D (MixinVector , metaclass = NamedTupleMetaEx ):
207
219
"""Three-dimensional vector with x, y, z component."""
208
220
_shape = (3 ,1 )
@@ -227,10 +239,6 @@ def expand_4D(self, vtype): # pylint: disable=invalid-name
227
239
228
240
return ValueError
229
241
230
- def abs (self ):
231
- """Returns length of vector."""
232
- return math .sqrt (self .x ** 2 + self .y ** 2 + self .z ** 2 )
233
-
234
242
def normalize (self ):
235
243
"""Normalizes vector to length = 1.0."""
236
244
abl = self .abs ()
@@ -464,6 +472,11 @@ def compfloor(mat_0: list, shape_0: tuple, divisor: float):
464
472
# Return coefficients and shape tuple
465
473
return [int (e // divisor ) for e in mat_0 ], shape_0
466
474
475
+ def vect_norm (all_elems : list ):
476
+ """Return norm of n-dim vector."""
477
+ squared = [elem ** 2 for elem in all_elems ]
478
+ return sqrt (reduce (operator .add , squared ))
479
+
467
480
def matadd (mat_0 : list , shape_0 : tuple , mat_1 : list , shape_1 : tuple ):
468
481
"""Performing componentwise addition."""
469
482
(rows_0 , cols_0 ) = shape_0
0 commit comments