13
13
def _normalise (nvec : numpy .ndarray ) -> numpy .ndarray :
14
14
"""Normalises the given numpy array."""
15
15
with numpy .errstate (invalid = "ignore" ):
16
- result = nvec / numpy .sqrt (numpy . dot (nvec , nvec ))
16
+ result = nvec / numpy .sqrt ((nvec @ nvec ))
17
17
return result
18
18
19
19
20
20
def _squared_error (vector_1 : numpy .ndarray , vector_2 : numpy .ndarray ) -> float :
21
21
"""Computes the squared error between two numpy arrays."""
22
22
diff = vector_1 - vector_2
23
- s = numpy . dot ( diff , diff )
23
+ s = diff @ diff
24
24
return numpy .sqrt (s )
25
25
26
26
27
- def _power_iteration (mat : numpy .matrix , initial : numpy .ndarray ) -> numpy .ndarray :
27
+ def _power_iteration (mat : numpy .array , initial : numpy .ndarray ) -> numpy .ndarray :
28
28
"""
29
29
Generator of successive approximations.
30
30
31
31
Params
32
32
------
33
- mat: numpy.matrix
33
+ mat: numpy.array
34
34
The matrix to use for multiplication iteration
35
35
initial: numpy.array, None
36
36
The initial state. Will be set to numpy.array([1, 1, ...]) if None
@@ -47,14 +47,14 @@ def _power_iteration(mat: numpy.matrix, initial: numpy.ndarray) -> numpy.ndarray
47
47
48
48
49
49
def principal_eigenvector (
50
- mat : numpy .matrix , maximum_iterations = 1000 , max_error = 1e-3
50
+ mat : numpy .array , maximum_iterations = 1000 , max_error = 1e-3
51
51
) -> Tuple [numpy .ndarray , float ]:
52
52
"""
53
53
Computes the (normalised) principal eigenvector of the given matrix.
54
54
55
55
Params
56
56
------
57
- mat: numpy.matrix
57
+ mat: numpy.array
58
58
The matrix to use for multiplication iteration
59
59
initial: numpy.array, None
60
60
The initial state. Will be set to numpy.array([1, 1, ...]) if None
@@ -71,7 +71,7 @@ def principal_eigenvector(
71
71
Eigenvalue corresonding to the returned eigenvector
72
72
"""
73
73
74
- mat_ = numpy .matrix (mat )
74
+ mat_ = numpy .array (mat )
75
75
size = mat_ .shape [0 ]
76
76
initial = numpy .ones (size )
77
77
@@ -86,7 +86,7 @@ def principal_eigenvector(
86
86
break
87
87
last = vector
88
88
# Compute the eigenvalue (Rayleigh quotient)
89
- eigenvalue = numpy . dot ( numpy . dot (mat_ , vector ), vector ) / numpy . dot (vector , vector )
89
+ eigenvalue = ( (mat_ @ vector ) @ vector ) / (vector @ vector )
90
90
# Liberate the eigenvalue from numpy
91
91
eigenvalue = float (eigenvalue )
92
92
return vector , eigenvalue
0 commit comments