Skip to content

Instantly share code, notes, and snippets.

@agumonkey
Forked from Hydrotoast/fib_matrix.py
Created May 29, 2016 23:26
Show Gist options
  • Save agumonkey/3c4ae579b6785d2be5859de1f58e3d19 to your computer and use it in GitHub Desktop.
Save agumonkey/3c4ae579b6785d2be5859de1f58e3d19 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