如何 mpf 数组? [英] How to mpf an array?

查看:16
本文介绍了如何 mpf 数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

import numpy as np
from mpmath import *

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

但它不会让我这样做:

TypeError: cannot create mpf from array

那我该怎么办?

本质上,我将使用这个数组并根据情况将元素乘以一个非常大或非常小的数字(例如 1.35626567e10846.2345252e-2732代码>) 因此需要 mpf.

Essentially I'm going to have use this array and multiply element-wise with an incredibly large or incredible small number depending on circumstance (eg 1.35626567e1084 or 6.2345252e-2732) hence the need for mpf.

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

More specifically I'll be using the besseli and besselk function which create the incredible large and incredible small values.

如何获得一个 mpf 数组来保存这些数字?

How do I get an mpf array to hold these numbers?

推荐答案

将一个数组乘以一个 mpf 数字很有效:

Multiplying an array by a mpf number just works:

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 数字的两个数组按元素相乘也有效:

Multiplying element-wise two arrays containing mpf numbers also works:

result * result

所以你真正的问题是如何评估一个 numpy 数组中的 mpmath 函数.为此,我会使用 np.frompyfunc(前一段时间这是唯一的选择).

So your real problem is how to evaluate an mpmath function in a numpy array. To do that, I'd use np.frompyfunc (some time ago this was the only option).

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

这篇关于如何 mpf 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆