如何对数组进行mpf?

2024-05-23 22:02:42 发布

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

我有:

import numpy as np
from mpmath import *

mpf(np.array(range(0,600)))

但它不会让我这么做:

TypeError: cannot create mpf from array

那我该怎么办?

本质上,我将使用这个数组,并根据环境(例如1.35626567e10846.2345252e-2732)使用一个难以置信的大或难以置信的小数字对元素进行乘法,因此需要mpf。

更具体地说,我将使用besseli和besselk函数来创建难以置信的大值和小值。

如何让强积金阵列储存这些号码?


Tags: fromimportnumpy环境ascreatenprange
2条回答

签出^{}

import numpy as np
import mpmath as mp

np.array(mp.arange(600))

将一个数组乘以一个mpf数字就可以了:

import numpy as np
import mpmath as mp
small_number = mp.besseli(400, 2)  # This is an mpf number
# Note that creating a list using `range` and then converting it
# to an array is not very efficient. Do this instead:
A = np.arange(600)
result = small_number * A  # Array of dtype object, ie, it contains mpf numbeers

将包含mpf编号的两个数组按元素相乘也可以工作:

result * result

所以你真正的问题是如何计算numpy数组中的mpmath函数。为此,我会使用^{}(前一段时间这是唯一的选择)。

besseli_vec = np.frompyfunc(mp.besseli, 2, 1)
besseli_vec(0, A)

相关问题 更多 >