小编And*_*oni的帖子

如何正确地将 mpmath 矩阵转换为 numpy ndarray(并将 mpmath.mpf 转换为浮点数)

我正在使用 mpmath python 库来在某些计算过程中获得精度,但我需要将结果转换为 numpy 本机类型。

更准确地说,我需要在 numpy.ndarray(包含浮点类型)中转换一个 mpmath 矩阵(包含 mpf 对象类型)。

我用原始方法解决了这个问题:

# My input Matrix:

matr = mp.matrix(
[[ '115.80200375',  '22.80402473',   '13.69453064',   '54.28049263'],
[  '22.80402473',   '86.14887381',   '53.79999432',   '42.78548627'],
[  '13.69453064',   '53.79999432',  '110.9695448' ,   '37.24270321'],
[  '54.28049263',   '42.78548627',   '37.24270321',   '95.79388469']])

# multiple precision computation
D = MPDBiteration(matr)

# Create a new ndarray  
Z = numpy.ndarray((matr.cols,matr.rows),dtype=numpy.float)

# I fill it pretty "manually"
for i in range(0,matr.rows):
    for j in range(0,matr.cols):
        Z[i,j] = D[i,j] # or float(D[i,j]) seems to work the …
Run Code Online (Sandbox Code Playgroud)

python numpy matrix type-conversion mpmath

6
推荐指数
1
解决办法
3739
查看次数

标签 统计

matrix ×1

mpmath ×1

numpy ×1

python ×1

type-conversion ×1