Author Topic: Inverse Matrix (Numpy) int too large to convert to float  (Read 3859 times)

0 Members and 1 Guest are viewing this topic.

Offline Piman9

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Inverse Matrix (Numpy) int too large to convert to float
« on: April 25, 2015, 05:48:55 pm »

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:
Code: [Select]
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:
Code: [Select]
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!

Offline Piman9

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Re: Inverse Matrix (Numpy) int too large to convert to float
« Reply #1 on: May 05, 2015, 01:11:50 pm »
Maybe distributing a constant across the matrix? Help?

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: Inverse Matrix (Numpy) int too large to convert to float
« Reply #2 on: May 05, 2015, 01:15:09 pm »
Can't you avoid taking the inverse? It's usually not necessary, most of the time you can use basic linear algebra to rewrite it in terms of "taking an RREF of an augmented matrix", things like that. Obviously it depends on what you're doing with it though.
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.

Offline Piman9

  • LV0 Newcomer (Next: 5)
  • Posts: 3
  • Rating: +0/-0
    • View Profile
Re: Inverse Matrix (Numpy) int too large to convert to float
« Reply #3 on: May 05, 2015, 01:22:38 pm »
I'm using the inverse matrix to solve a 365 variable system of equations.  The cells do follow a sort of patter but the pattern is long. I'm not exactly sure what you mean.

Offline harold

  • LV5 Advanced (Next: 300)
  • *****
  • Posts: 226
  • Rating: +41/-3
    • View Profile
Re: Inverse Matrix (Numpy) int too large to convert to float
« Reply #4 on: May 05, 2015, 01:33:14 pm »
Ok see, you don't have to take an inverse to solve a system of equations. Say you have Ax = b, then you can make the matrix [A|b], take the RREF, and you have your answer, that's even how you'd do it manually (right?). With numpy you don't even need that, you can just do numpy.linalg.solve(A, b)
Blog about bitmath: bitmath.blogspot.nl
Check the haroldbot thread for the supported commands and syntax.
You can use haroldbot from this website.