1
1
from __future__ import annotations
2
2
3
- from typing import TYPE_CHECKING
3
+ from typing import TYPE_CHECKING , cast
4
4
5
5
import numpy as np
6
6
import numpy .typing as npt
@@ -205,7 +205,7 @@ def eigen(
205
205
206
206
n = matrix .shape [0 ]
207
207
208
- scores = np .ones (n ) / n
208
+ scores = np .ones (n , dtype = np . float64 ) / n
209
209
scores_new = scores .copy ()
210
210
211
211
converged , iterations = False , 0
@@ -221,15 +221,15 @@ def eigen(
221
221
222
222
scores [:] = scores_new
223
223
224
- return scores , iterations
224
+ return cast ( "npt.NDArray[np.float64]" , scores ) , iterations
225
225
226
226
227
227
def pagerank_matrix (
228
- matrix : npt .NDArray [np .float64 ],
228
+ matrix : npt .NDArray [np .floating [ Any ] ],
229
229
damping : float ,
230
- ) -> npt .NDArray [np .float64 ]:
230
+ ) -> npt .NDArray [np .floating [ Any ] ]:
231
231
if not matrix .size :
232
- return np .zeros (0 , dtype = np . float64 )
232
+ return np .zeros (0 , dtype = matrix . dtype )
233
233
234
234
p = 1. / int (matrix .shape [0 ])
235
235
@@ -246,7 +246,7 @@ def pagerank(
246
246
tolerance : float ,
247
247
limit : int ,
248
248
) -> tuple [npt .NDArray [np .float64 ], int ]:
249
- matrix = pagerank_matrix (matrix , damping )
249
+ matrix = cast ( "npt.NDArray[np.float64]" , pagerank_matrix (matrix , damping ) )
250
250
251
251
scores , iterations = eigen (matrix , tolerance = tolerance , limit = limit )
252
252
scores /= np .linalg .norm (scores , ord = 1 )
0 commit comments