如何强积金数组? [英] How to mpf an array?
问题描述
我有:
import numpy as np
from mpmath import *
mpf(np.array(range(0,600)))
但它不会让我做到这一点:
But it won't let me do it:
TypeError: cannot create mpf from array
所以,我应该怎么做?
So what should I be doing?
基本上我将不得不使用这个数组和乘元素明智的根据环境令人难以置信的大或小的令人难以置信的数字(如 1.35626567e1084
或 6.2345252e-2732
),因此需要强积金。
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.
我如何得到一个强积金数组来保存这些数字?
How do I get an mpf array to hold these numbers?
推荐答案
由强积金数乘以一个数组只是作品:
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
乘载强积金号元素的明智两个数组也可以工作:
Multiplying element-wise two arrays containing mpf numbers also works:
result * result
所以,你真正的问题是如何在numpy的阵列评估一个mpmath功能。要做到这一点,我会使用<一个href=\"http://docs.scipy.org/doc/numpy/reference/generated/numpy.frompyfunc.html\"><$c$c>np.frompyfunc$c$c> (前段时间,这是唯一的选择)。
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)
这篇关于如何强积金数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!