Skip to content

Instantly share code, notes, and snippets.

@Hydrotoast
Created July 6, 2015 00:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Hydrotoast/da8c410c3f60722da86c to your computer and use it in GitHub Desktop.
Save Hydrotoast/da8c410c3f60722da86c to your computer and use it in GitHub Desktop.
import numpy as np
# The Fibonacii matrix
M = np.array([[0, 1], [1, 1]], dtype=np.float32)
# Compute the eigendecomposition of the matrix
w, v = np.linalg.eig(M)
inv_v = np.linalg.inv(v)
base_case = np.array([0, 1]).T # base case as a column vector
# Print the first ten values of the Fibonaci sequence
for i in range(1, 10):
result = v * (np.diag(w) ** i) * inv_v * base_case # matrix form of the solution
int_result = round(result[1,1]) # solution is stored in the bottom right cell
print(int_result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment