Skip to content

Commit

Permalink
Merge pull request #814 from juliahou8/type-hints
Browse files Browse the repository at this point in the history
Add type hints to eigen.py
  • Loading branch information
meatballs authored Jan 14, 2017
2 parents ce9c4ed + 517ff93 commit f2b07f6
Showing 1 changed file with 5 additions and 4 deletions.
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

0 comments on commit f2b07f6

Please sign in to comment.