逆矩阵(Numpy)整数太大,无法控制

2024-10-05 13:09:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着取一个365x365矩阵的逆矩阵。有些值会变大到365**365,因此它们被转换为长数字。我不知道linalg.matrix_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

^{pr2}$

这个程序从一个列表(x的列表和y的列表)中获取x,y坐标并生成一个函数。 谢谢!在


Tags: 函数代码程序numpy列表矩阵数字item
2条回答

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


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

x = numpy.linalg.solve(A, b)

解决系统A*x=b。在


你(真的)可能想查一下伦格效应。等间距采样点插值是一个日益病态的任务。对于更大的单数阶多项式,可以得到非常有用的结果。在


你可以经常使用多项式回归,也就是说,用某个低阶的最佳多项式来近似你的数据集。在

您可以尝试使用库mpmath,它可以对任意精度的数字进行简单的矩阵代数和其他类似的问题。在

几个注意事项:它几乎肯定比使用numpy慢,而且,正如Lutzl在his answer to this question中指出的那样,这个问题很可能在数学上没有很好的定义。另外,你需要在开始之前决定你想要的精度。在

一些简单的示例代码

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

我怀疑你真正的问题是数学而不是软件,所以我怀疑这对你是否有效,但它总是有可能的!在

相关问题 更多 >

    热门问题