Skip to content

Commit

Permalink
Cleanup eigen.py
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Jul 29, 2020
1 parent 50a0735 commit b156230
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions axelrod/eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

from typing import Tuple

import numpy
import numpy as np


def _normalise(nvec: numpy.ndarray) -> numpy.ndarray:
def _normalise(nvec: np.ndarray) -> np.ndarray:
"""Normalises the given numpy array."""
with numpy.errstate(invalid="ignore"):
result = nvec / numpy.sqrt((nvec @ nvec))
with np.errstate(invalid="ignore"):
result = nvec / np.sqrt((nvec @ nvec))
return result


def _squared_error(vector_1: numpy.ndarray, vector_2: numpy.ndarray) -> float:
def _squared_error(vector_1: np.ndarray, vector_2: np.ndarray) -> float:
"""Computes the squared error between two numpy arrays."""
diff = vector_1 - vector_2
s = diff @ diff
return numpy.sqrt(s)
return np.sqrt(s)


def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:
def _power_iteration(mat: np.array, initial: np.ndarray) -> np.ndarray:
"""
Generator of successive approximations.
Expand All @@ -33,7 +33,7 @@ def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:
mat: numpy.array
The matrix to use for multiplication iteration
initial: numpy.array, None
The initial state. Will be set to numpy.array([1, 1, ...]) if None
The initial state. Will be set to np.array([1, 1, ...]) if None
Yields
------
Expand All @@ -42,13 +42,13 @@ def _power_iteration(mat: numpy.array, initial: numpy.ndarray) -> numpy.ndarray:

vec = initial
while True:
vec = _normalise(numpy.dot(mat, vec))
vec = _normalise(np.dot(mat, vec))
yield vec


def principal_eigenvector(
mat: numpy.array, maximum_iterations=1000, max_error=1e-3
) -> Tuple[numpy.ndarray, float]:
mat: np.array, maximum_iterations=1000, max_error=1e-3
) -> Tuple[np.ndarray, float]:
"""
Computes the (normalised) principal eigenvector of the given matrix.
Expand All @@ -66,12 +66,12 @@ def principal_eigenvector(
ndarray
Eigenvector estimate for the input matrix
float
Eigenvalue corresonding to the returned eigenvector
Eigenvalue corresponding to the returned eigenvector
"""

mat_ = numpy.array(mat)
mat_ = np.array(mat)
size = mat_.shape[0]
initial = numpy.ones(size)
initial = np.ones(size)

# Power iteration
if not maximum_iterations:
Expand Down

0 comments on commit b156230

Please sign in to comment.