诱骗numpy / python代表非常大和非常小的数字 [英] Tricking numpy/python into representing very large and very small numbers

查看:955
本文介绍了诱骗numpy / python代表非常大和非常小的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 -150 $ b <$ < p $ p> import numpy as np
from scipy.special import ndtr

def my_func(x):
return np.exp(x ** 2)* 2 * ndtr(x * np.sqrt(2))

问题是,部分功能

  np.exp(x ** 2)
pre>

倾向于无穷大 - 我得到了 inf ,其值 x 小于大约 -26



以及这部分函数

  2 * ndtr(x * np.sqrt(2))

相当于

  from scipy.special import erf 

1 + erf(x)

倾向于0。 b $ b

所以,非常非常大的数字乘以一个非常非常小的数字应该给我一个合理大小的数字 - 但是,而不是那个 python 给我 nan



我能做些什么来规避这个问题? / p>

解决方案

已经有这样的功能: erfcx 。我认为 erfcx(-x)应该给你你想要的被积函数(注意 1 + erf(x)= erfc(-x) code>)。


I need to compute the integral of the following function within ranges that start as low as -150:

import numpy as np
from scipy.special import ndtr

def my_func(x):
    return np.exp(x ** 2) * 2 * ndtr(x * np.sqrt(2))

The problem is that this part of the function

np.exp(x ** 2)

tends toward infinity -- I get inf for values of x less than approximately -26.

And this part of the function

2 * ndtr(x * np.sqrt(2))

which is equivalent to

from scipy.special import erf

1 + erf(x)

tends toward 0.

So, a very, very large number times a very, very small number should give me a reasonably sized number -- but, instead of that, python is giving me nan.

What can I do to circumvent this problem?

解决方案

There already is such a function: erfcx. I think erfcx(-x) should give you the integrand you want (note that 1+erf(x)=erfc(-x)).

这篇关于诱骗numpy / python代表非常大和非常小的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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