The expression of the degree of complexity of the matrix gives a negative value

I wanted to use NumPyFibonacci in the question because of its effectiveness in matrix multiplication. You know that there is a method for finding Fibonacci numbers with a matrix [[1, 1], [1, 0]].

Fibo

I wrote a very simple code, but after increasing n, the matrix begins to give negative numbers.

import numpy
def fib(n):
    return (numpy.matrix("1 1; 1 0")**n).item(1)

print fib(90)
# Gives -1581614984

What could be the reason for this?

Note. linalg.matrix_poweralso gives negative values.

Note2: I tried numbers from 0 to 100. It starts to give negative values โ€‹โ€‹after 47. Is this a big integer problem because NumPy is encoded in C? If so, how can I solve this?

: python list linalg.matrix_power . , 47, .

Edit2: @AlbertoGarcia-Raboso. , . -5.168070885485832e+19, -51680708854858323072L. int(), L, , - .

+4
1

, NumPy np.int32 dtype .

, dtype , 2 31 -1, 2147483647. , 47- , 2971215073. :

>>> np.int32(2971215073)
-1323752223

(, np.int64) , : , .

- , Python int. np.object:

def fib_2(n):
    return (np.matrix("1 1; 1 0", dtype=np.object)**n).item(1)

np.object Python. , , , Python . Python , .

>>> fib_2(300)
222232244629420445529739893461909967206666939096499764990979600

: NumPy integer/float, .

+7

Source: https://habr.com/ru/post/1655237/


All Articles