77
88from typing import Tuple
99
10- import numpy
10+ import numpy as np
1111
1212
13- def _normalise (nvec : numpy .ndarray ) -> numpy .ndarray :
13+ def _normalise (nvec : np .ndarray ) -> np .ndarray :
1414 """Normalises the given numpy array."""
15- with numpy .errstate (invalid = "ignore" ):
16- result = nvec / numpy .sqrt ((nvec @ nvec ))
15+ with np .errstate (invalid = "ignore" ):
16+ result = nvec / np .sqrt ((nvec @ nvec ))
1717 return result
1818
1919
20- def _squared_error (vector_1 : numpy .ndarray , vector_2 : numpy .ndarray ) -> float :
20+ def _squared_error (vector_1 : np .ndarray , vector_2 : np .ndarray ) -> float :
2121 """Computes the squared error between two numpy arrays."""
2222 diff = vector_1 - vector_2
2323 s = diff @ diff
24- return numpy .sqrt (s )
24+ return np .sqrt (s )
2525
2626
27- def _power_iteration (mat : numpy .array , initial : numpy .ndarray ) -> numpy .ndarray :
27+ def _power_iteration (mat : np .array , initial : np .ndarray ) -> np .ndarray :
2828 """
2929 Generator of successive approximations.
3030
@@ -33,7 +33,7 @@ def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:
3333 mat: numpy.array
3434 The matrix to use for multiplication iteration
3535 initial: numpy.array, None
36- The initial state. Will be set to numpy .array([1, 1, ...]) if None
36+ The initial state. Will be set to np .array([1, 1, ...]) if None
3737
3838 Yields
3939 ------
@@ -42,13 +42,13 @@ def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:
4242
4343 vec = initial
4444 while True :
45- vec = _normalise (numpy .dot (mat , vec ))
45+ vec = _normalise (np .dot (mat , vec ))
4646 yield vec
4747
4848
4949def principal_eigenvector (
50- mat : numpy .array , maximum_iterations = 1000 , max_error = 1e-3
51- ) -> Tuple [numpy .ndarray , float ]:
50+ mat : np .array , maximum_iterations = 1000 , max_error = 1e-3
51+ ) -> Tuple [np .ndarray , float ]:
5252 """
5353 Computes the (normalised) principal eigenvector of the given matrix.
5454
@@ -66,12 +66,12 @@ def principal_eigenvector(
6666 ndarray
6767 Eigenvector estimate for the input matrix
6868 float
69- Eigenvalue corresonding to the returned eigenvector
69+ Eigenvalue corresponding to the returned eigenvector
7070 """
7171
72- mat_ = numpy .array (mat )
72+ mat_ = np .array (mat )
7373 size = mat_ .shape [0 ]
74- initial = numpy .ones (size )
74+ initial = np .ones (size )
7575
7676 # Power iteration
7777 if not maximum_iterations :
0 commit comments