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
:
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 좌표를 가져 와서 함수를 만듭니다. 감사!
시도해 볼 수있는 한 가지는 임의 정밀도 숫자에 대한 간단한 행렬 대수 및 기타 문제를 수행 할 수 있는 라이브러리 mpmath 입니다.
몇 가지주의 사항 : 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))
나는 당신의 진짜 문제가 소프트웨어가 아닌 수학에 있다고 생각하기 때문에 이것이 당신에게 환상적으로 잘 작동 할 것 같지는 않지만 항상 가능합니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다