diff options
author | Jarrod Millman <jarrod.millman@gmail.com> | 2021-10-24 14:22:38 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-24 17:22:38 -0400 |
commit | 7349e9c697a13f98bb42126eac3bfc0118cc3d47 (patch) | |
tree | 640d4f253979e4a1cfab8961ae450f81c12cda92 | |
parent | 0c503e31ba0062dc9a4ed47c380481aa8d6978c8 (diff) | |
download | networkx-7349e9c697a13f98bb42126eac3bfc0118cc3d47.tar.gz |
Use math.hypot (#5145)
-rw-r--r-- | networkx/algorithms/centrality/eigenvector.py | 5 | ||||
-rw-r--r-- | networkx/algorithms/centrality/katz.py | 4 |
2 files changed, 4 insertions, 5 deletions
diff --git a/networkx/algorithms/centrality/eigenvector.py b/networkx/algorithms/centrality/eigenvector.py index de66dcfd..2867a2b7 100644 --- a/networkx/algorithms/centrality/eigenvector.py +++ b/networkx/algorithms/centrality/eigenvector.py @@ -1,6 +1,5 @@ """Functions for computing eigenvector centrality.""" - -from math import sqrt +import math import networkx as nx from networkx.utils import not_implemented_for @@ -130,7 +129,7 @@ def eigenvector_centrality(G, max_iter=100, tol=1.0e-6, nstart=None, weight=None # should never be zero by the Perron--Frobenius # theorem. However, in case it is due to numerical error, we # assume the norm to be one instead. - norm = sqrt(sum(z ** 2 for z in x.values())) or 1 + norm = math.hypot(*x.values()) or 1 x = {k: v / norm for k, v in x.items()} # Check for convergence (in the L_1 norm). if sum(abs(x[n] - xlast[n]) for n in x) < nnodes * tol: diff --git a/networkx/algorithms/centrality/katz.py b/networkx/algorithms/centrality/katz.py index 33fcf096..523dce04 100644 --- a/networkx/algorithms/centrality/katz.py +++ b/networkx/algorithms/centrality/katz.py @@ -1,5 +1,5 @@ """Katz centrality.""" -from math import sqrt +import math import networkx as nx from networkx.utils import not_implemented_for @@ -181,7 +181,7 @@ def katz_centrality( if normalized: # normalize vector try: - s = 1.0 / sqrt(sum(v ** 2 for v in x.values())) + s = 1.0 / math.hypot(*x.values()) # this should never be zero? except ZeroDivisionError: s = 1.0 |