Search code examples
pythonnumpymatrixoverflowmatrix-inverse

Inverse Matrix (Numpy) int too large to convert to float


I am trying to take the inverse of a 365x365 matrix. Some of the values get as large as 365**365 and so they are converted to long numbers. I don't know if the linalg.matrix_power() function can handle long numbers. I know the problem comes from this (because of the error message and because my program works just fine for smaller matrices) but I am not sure if there is a way around this. The code needs to work for a NxN matrix.

Here's my code:

item=0
for i in xlist:
    xtotal.append(arrayit.arrayit(xlist[item],len(xlist)))
    item=item+1
print xtotal
xinverted=numpy.linalg.matrix_power(xtotal,-1)
coeff=numpy.dot(xinverted,ylist)

arrayit.arrayit:

def arrayit(number, length):
    newarray=[]
    import decimal
    i=0
    while i!=(length):
        newarray.insert(0,decimal.Decimal(number**i))
        i=i+1
    return newarray;

The program is taking x,y coordinates from a list (list of x's and list of y's) and makes a function. Thanks!


Solution

  • One thing you might try is the library mpmath, which can do simple matrix algebra and other such problems on arbitrary precision numbers.

    A couple of caveats: It will almost certainly be slower than using numpy, and, as Lutzl points out in his answer to this question, the problem may well not be mathematically well defined. Also, you need to decide on the precision you want before you start.

    Some brief example code,

    from mpmath import mp, matrix
    
    # set the precision - see http://mpmath.org/doc/current/basics.html#setting-the-precision
    mp.prec = 5000 # set it to something big at the cost of speed.
       # Ideally you'd precalculate what you need.
       # a quick trial with 100*100 showed that 5000 works and 500 fails
    
    # see the documentation at http://mpmath.org/doc/current/matrices.html
    # where xtotal is the output from arrayit
    my_matrix = matrix(xtotal) # I think this should work. If not you'll have to create it and copy
    
    # do the inverse
    xinverted = my_matrix**-1
    coeff = xinverted*matrix(ylist)
    # note that as lutlz pointed out you really want to use solve instead of calculating the inverse. 
    # I think this is something like
    from mpmath import lu_solve
    coeff = lu_solve(my_matrix,matrix(ylist))
    

    I suspect your real problem is with the maths rather than the software, so I doubt this will work fantastically well for you, but it's always possible!