If you are going to play with Fibonacci numbers, itβs probably worth sacrificing some speed and using arbitrarily large Python numbers. You can do this by setting your matrix dtype to object .
You also do not need to use the np.matrix object, it is almost always better to stick with regular arrays. And you can extract the corresponding element without converting the array to a list:
def fibonacci(n): qmatrix = numpy.array([[1, 1], [1, 0]], dtype=object) return numpy.linalg.matrix_power(qmatrix, n)[0, 1]
Jaime source share