Coverage for src\attipy\_vectorops.py: 64%

11 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-15 11:24 +0100

1import numpy as np 

2from numba import njit 

3from numpy.typing import NDArray 

4 

5 

6@njit # type: ignore[misc] 

7def _normalize(q: NDArray[np.float64]) -> NDArray[np.float64]: 

8 """ 

9 L2-normalize a vector. 

10 

11 Parameters 

12 ---------- 

13 q : numpy.ndarray 

14 Vector to be normalized 

15 

16 Returns 

17 ------- 

18 numpy.ndarray 

19 Normalized copy of `q`. 

20 """ 

21 return q / np.sqrt((q * q).sum()) # type: ignore[no-any-return] # numpy funcs declare Any as return when given scalar-like 

22 

23 

24@njit # type: ignore[misc] 

25def _quaternion_product( 

26 qa: NDArray[np.float64], qb: NDArray[np.float64] 

27) -> NDArray[np.float64]: 

28 """ 

29 Unit quaternion (Hamilton) product: q_a ⊗ q_b. 

30 

31 Parameters 

32 ---------- 

33 qa, qb : numpy.ndarray, shape (4,) 

34 Unit quaternions. 

35 

36 Returns 

37 ------- 

38 numpy.ndarray, shape (4,) 

39 Unit quaternions result of the product. 

40 """ 

41 qa_w, qa_xyz = np.split(qa, [1]) 

42 qb_w, qb_xyz = np.split(qb, [1]) 

43 return np.concatenate( 

44 ( 

45 qa_w * qb_w - qa_xyz.T @ qb_xyz, 

46 qa_w * qb_xyz + qb_w * qa_xyz + np.cross(qa_xyz, qb_xyz), 

47 ), 

48 axis=0, 

49 )