-1

我在 python 中使用 BigFloat 库计算 exp。但随后我必须计算 BigFloats 矩阵的逆矩阵。我使用函数numpy.linalg.inv,但出现以下错误:

没有为 ufunc inv 找到与指定签名和强制转换匹配的循环

所以我想说我需要将 BigFloat 转换为其他类型。我该怎么做?

4

1 回答 1

0

考虑 Bigfloat 值的 2D 列表(使用 exp):

from bigfloat import *
import numpy as np

row = 4
col = 4
mat = []
with precision(1000):
    for i in range(0,row):
        row = []
        for j in range(0,col):
            row.append(exp(1./(i+j+1)))
        mat.append(row)
print "A single element in bigfloat: \n",mat[0][0]
mat_numpivized = np.array([[np.float(col) for col in row] for row in mat])
print "\nNumpived matrix of bigfloat: \n",mat_numpivized
inversed_mat = np.linalg.inv(mat_numpivized)
print "\nInverse of numpivized bigfloat matrix: \n", inversed_mat

结果:

A single element in bigfloat: 
    2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206950

Numpived matrix of bigfloat: 
[[ 2.71828183  1.64872127  1.39561243  1.28402542]
 [ 1.64872127  1.39561243  1.28402542  1.22140276]
 [ 1.39561243  1.28402542  1.22140276  1.18136041]
 [ 1.28402542  1.22140276  1.18136041  1.15356499]]

Inverse of numpivized bigfloat matrix: 
[[    3.82106765   -30.68951855    61.37113204   -34.60880961]
 [  -30.68951855   385.05574805  -890.19682475   538.10687764]
 [   61.37113204  -890.19682475  2211.11911647 -1390.16166038]
 [  -34.60880961   538.10687764 -1390.16166038   893.29628089]]
于 2016-10-23T16:05:20.447 回答