修复函数的奇点
假设您有一个像
F = lambda x: sin(x)/x
评估 F(0.0)
这样的函数会导致除以零警告,并且不会给出 1.0
的预期结果。是否可以编写另一个函数 fix_singularity
,该函数在应用于上述函数时会给出所需的结果,以便
fix_singularity(F)(0.0) == 1.0
或正式 fix_singularity
应通过以下测试:
import numpy as np
def test_fix_singularity():
F = lambda x: np.sin(x)/x
x = np.array((0.0, pi))
np.testing.assert_array_almost_equal( F(x), [nan, 0] )
np.testing.assert_array_almost_equal( fix_singularity(F)(x), [1, 0] )
一种可能的实现是
def fix_singularity(F):
""" Fix the singularity of function F(x) """
def L(x):
f = F(x)
i = np.isnan(f)
f[i] = F(x[i] + 1e-16)
return f
return L
有更好的方法吗?
编辑: 另外我怎样才能抑制警告:
Warning: invalid value encountered in divide
Assume you have a function like
F = lambda x: sin(x)/x
Evaluating F(0.0)
would result in a divide by zero warning, and would not give the expected result of 1.0
. Is it possible to write another function fix_singularity
that would give the desired result when applied to the above function, so that
fix_singularity(F)(0.0) == 1.0
Or formally fix_singularity
should pass the following tests:
import numpy as np
def test_fix_singularity():
F = lambda x: np.sin(x)/x
x = np.array((0.0, pi))
np.testing.assert_array_almost_equal( F(x), [nan, 0] )
np.testing.assert_array_almost_equal( fix_singularity(F)(x), [1, 0] )
One possible implementation is
def fix_singularity(F):
""" Fix the singularity of function F(x) """
def L(x):
f = F(x)
i = np.isnan(f)
f[i] = F(x[i] + 1e-16)
return f
return L
Are there better ways of doing this?
EDIT:
Also how can I suppress the warning:
Warning: invalid value encountered in divide
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
numpy
有一个sinc()
函数,它是函数的规范化形式,即它正确处理
x == 0.0
的情况,为了“非正常化”,
numpy
has asinc()
function, which is the normalised form of your function, i.e.It handles the case for
x == 0.0
correctly,To "unnormalize" do,
如果您已经在使用 numpy,则:
将计算而不会出错,并在
b[0]
中留下NaN
值。如果您想这样处理的话,您可以将其替换为以下内容:更新 要抑制警告,请尝试:
If you are already using numpy then:
Will calculate without error leaving a
NaN
value inb[0]
. You could then just replace it by the following if that's how you want to handle it:Update To suppress the warning, try:
一般来说,您不能像您想象的那样编写一个简单的 fix 装饰器。例如,一般函数不需要像这个特定示例那样在奇点处具有有限极限值。
通常的做法是根据具体情况实施特殊处理。
In general you can't write a simple fix decorator as you might imagine. For example, a general function need not have a finite limiting value at a singularity as this particular example does.
Normal practice is to implement special handling on a case by case basis.
我会尝试这个
I'll try this
我不知道这是否适合您的确切目的,但是有一个名为 sage 的 python 库 可以处理相当多的微积分类型的情况。
I don't know if this would work for your exact purposes, but there's a python library called sage that can handle quite a bit of Calculus-type situations.
我相信 sympy (符号 python)可以做限制,这就是你真正要问的(该解决方案仅作为限制)。无论如何,你应该检查一下。
I believe sympy (symbolic python) can do limits, which is what you are really asking (that solution is only true as a limit). Regardless, you should check it out.