Python 逆矩阵(Numpy)int太大,无法转换为浮点

Python 逆矩阵(Numpy)int太大,无法转换为浮点,python,numpy,matrix,overflow,matrix-inverse,Python,Numpy,Matrix,Overflow,Matrix Inverse,我试图取365x365矩阵的逆矩阵。有些值会变大到365**365,因此会转换为长数字。我不知道linalg.matrix\u power()函数是否可以处理长数字。我知道问题就出在这里(因为有错误信息,而且我的程序对于较小的矩阵也能正常工作),但我不确定是否有办法解决这个问题。代码需要用于NxN矩阵 这是我的密码: item=0 for i in xlist: xtotal.append(arrayit.arrayit(xlist[item],len(xlist))) item

我试图取365x365矩阵的逆矩阵。有些值会变大到365**365,因此会转换为长数字。我不知道
linalg.matrix\u power()
函数是否可以处理长数字。我知道问题就出在这里(因为有错误信息,而且我的程序对于较小的矩阵也能正常工作),但我不确定是否有办法解决这个问题。代码需要用于NxN矩阵

这是我的密码:

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;
该程序从一个列表(x的列表和y的列表)中获取x,y坐标,并生成一个函数。
谢谢

你听说过拉格朗日插值或牛顿插值吗?这将避免整个VanderMonde矩阵的构造。但不是系数中潜在的大数字


一般来说,您不需要逆矩阵。你不需要计算它。你想要的是解一个线性方程组

x = numpy.linalg.solve(A, b)
求解系统
A*x=b


您(真的)可能想要查找龙格效应。等间距采样点插值是一项条件越来越差的任务。对于一位数的度数可以得到有用的结果,较大的度数往往会给出剧烈振荡的多项式



您经常可以使用多项式回归,即用某种低阶的最佳多项式来近似数据集。

您可以尝试的一件事是库,它可以对任意精度的数字进行简单的矩阵代数和其他此类问题

有几个注意事项:它几乎肯定会比使用numpy慢,而且,正如Lutzl在中指出的,这个问题很可能在数学上没有很好的定义。此外,您还需要在开始之前确定所需的精度

一些简单的示例代码

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))

我怀疑你真正的问题是数学,而不是软件,所以我怀疑这对你来说会非常好,但这总是可能的

您可以使用
十进制(365**365)
您能描述一下您的用例吗?这些都是一些巨大的数字。我相当自信,<代码> NoPy.Lalalg。MatRXXPOWER (但考虑使用<代码> NUMPI.LIALG.IV.<代码> >只适用于适合于<代码>双< /代码>或 long double < /代码>的数字。我不认为它专门研究一般对象,比如
decimal.decimal
。如果你可以用一个常数来缩放整个矩阵,那么你应该会有更多的运气(在你的情况下,看起来你不可能…。@Padraiccningham是的,我试过了,但是
numpy.linalg.matrix\u power
函数将所有的东西都转换为浮点,而@DavidW我想我可能需要算出一个常数@user2357112该程序将多项式函数拟合到n个点集。谢谢非常感谢。事实上,我有完整的计算代码,只是numpy库不支持。已经过去一年半了,所以现在可能会有所不同。很抱歉,我当时正在睡觉D