Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to eigen.py #814

Merged
merged 2 commits into from
Jan 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions axelrod/eigen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
"""

import numpy
from typing import Tuple


def normalise(nvec):
def normalise(nvec: numpy.ndarray) -> numpy.ndarray:
"""Normalises the given numpy array."""
with numpy.errstate(invalid='ignore'):
result = nvec / numpy.sqrt(numpy.dot(nvec, nvec))
return result


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


def power_iteration(mat, initial):
def power_iteration(mat: numpy.matrix, initial: numpy.ndarray) -> numpy.ndarray:
"""
Generator of successive approximations.

Expand All @@ -44,7 +45,7 @@ def power_iteration(mat, initial):
yield vec


def principal_eigenvector(mat, maximum_iterations=1000, max_error=1e-3):
def principal_eigenvector(mat: numpy.matrix, maximum_iterations=1000, max_error=1e-3) -> Tuple[numpy.ndarray, float]:
"""
Computes the (normalised) principal eigenvector of the given matrix.

Expand Down