Skip to content

Commit 4587508

Browse files
committed
Fix numpy matrix depreciation warning.
Closes #1213 Also moves from `np.dot` to matrix multiplication operator `@`.
1 parent f32e14b commit 4587508

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

axelrod/eigen.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
def _normalise(nvec: numpy.ndarray) -> numpy.ndarray:
1414
"""Normalises the given numpy array."""
1515
with numpy.errstate(invalid="ignore"):
16-
result = nvec / numpy.sqrt(numpy.dot(nvec, nvec))
16+
result = nvec / numpy.sqrt((nvec @ nvec))
1717
return result
1818

1919

2020
def _squared_error(vector_1: numpy.ndarray, vector_2: numpy.ndarray) -> float:
2121
"""Computes the squared error between two numpy arrays."""
2222
diff = vector_1 - vector_2
23-
s = numpy.dot(diff, diff)
23+
s = diff @ diff
2424
return numpy.sqrt(s)
2525

2626

27-
def _power_iteration(mat: numpy.matrix, initial: numpy.ndarray) -> numpy.ndarray:
27+
def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:
2828
"""
2929
Generator of successive approximations.
3030
3131
Params
3232
------
33-
mat: numpy.matrix
33+
mat: numpy.array
3434
The matrix to use for multiplication iteration
3535
initial: numpy.array, None
3636
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
4747

4848

4949
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
5151
) -> Tuple[numpy.ndarray, float]:
5252
"""
5353
Computes the (normalised) principal eigenvector of the given matrix.
5454
5555
Params
5656
------
57-
mat: numpy.matrix
57+
mat: numpy.array
5858
The matrix to use for multiplication iteration
5959
initial: numpy.array, None
6060
The initial state. Will be set to numpy.array([1, 1, ...]) if None
@@ -71,7 +71,7 @@ def principal_eigenvector(
7171
Eigenvalue corresonding to the returned eigenvector
7272
"""
7373

74-
mat_ = numpy.matrix(mat)
74+
mat_ = numpy.array(mat)
7575
size = mat_.shape[0]
7676
initial = numpy.ones(size)
7777

@@ -86,7 +86,7 @@ def principal_eigenvector(
8686
break
8787
last = vector
8888
# 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)
9090
# Liberate the eigenvalue from numpy
9191
eigenvalue = float(eigenvalue)
9292
return vector, eigenvalue

0 commit comments

Comments
 (0)