Rockybilly Rockybilly - 1 month ago 10x
Python Question

Numpy matrix exponentiation gives negative value

I wanted to use

in a Fibonacci question because of its efficiency in matrix multiplication. You know that there is a method for finding Fibonacci numbers with the matrix
[[1, 1], [1, 0]]


I wrote some very simple code but after increasing
, the matrix is starting 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?

also gives negative values.

Note2: I tried numbers from 0 to 100. It starts to give negative values after 47. Is it a large integer issue because NumPy is coded in C ? If so, how could I solve this ?

Edit: Using regular python
matrix with
also gave negative results. Also let me add that not all results are negative after 47, it occurs randomly.

Edit2: I tried using the method @AlbertoGarcia-Raboso suggested. It resolved the negative number problem, however another issues occured. It gives the answer as
where I need
. So I tried using
, it converted it to
, but now it seems the answer is incorrect because of a loss in precision.


The reason you see negative values appearing is because NumPy has defaulted to using the np.int32 dtype for your matrix.

The maximum positive integer this dtype can represent is 231-1 which is 2147483647. Unfortunately, this is less the 47th Fibonacci number, 2971215073. The resulting overflow is causing the negative number to appear:

>>> np.int32(2971215073)

Using a bigger integer type (like np.int64) would fix this, but only temporarily: you'd still run into problems if you kept on asking for larger and larger Fibonacci numbers.

The only sure fix is to use an unlimited-size integer type, such as Python's int type. To do this, modify your matrix to be of np.object type:

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

The np.object type allows a matrix or array to hold any mix of native Python types. Essentially, instead of holding machine types, the matrix is now behaving like a Python list and simply consists of pointers to integer objects in memory. Python integers will be used in the calculation of the Fibonacci numbers now and overflow is not an issue.

This flexibility comes at the cost of decreased performance: NumPy's speed originates from direct storage of machine integer/float types.