在python中将类型从BigFloat转换为Float

时间:2016-10-23 13:02:25

标签: python numpy bigfloat

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

  

找不到匹配指定签名和转换的循环   ufunc inv

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

1 个答案:

答案 0 :(得分: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]]