File tree Expand file tree Collapse file tree 1 file changed +6
-1
lines changed
Expand file tree Collapse file tree 1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change 1010
1111def normalise (nvec ):
1212 """Normalises the given numpy array."""
13- return nvec / numpy .sqrt (numpy .dot (nvec , nvec ))
13+ with numpy .errstate (invalid = 'ignore' ):
14+ result = nvec / numpy .sqrt (numpy .dot (nvec , nvec ))
15+ return result
16+
1417
1518def squared_error (vector_1 , vector_2 ):
1619 """Computes the squared error between two numpy arrays."""
1720 diff = vector_1 - vector_2
1821 s = numpy .dot (diff , diff )
1922 return numpy .sqrt (s )
2023
24+
2125def power_iteration (mat , initial ):
2226 """
2327 Generator of successive approximations.
@@ -39,6 +43,7 @@ def power_iteration(mat, initial):
3943 vec = normalise (numpy .dot (mat , vec ))
4044 yield vec
4145
46+
4247def principal_eigenvector (mat , maximum_iterations = None , max_error = 1e-8 ):
4348 """
4449 Computes the (normalised) principal eigenvector of the given matrix.
You can’t perform that action at this time.
0 commit comments