小编Mis*_*sha的帖子

python中是否有可扩展的互补错误函数?

在matlab中有一个特殊的函数,在我所知道的Python(numpy,scipy,mpmath,...)的任何集合中都没有.

可能还有其他地方可以找到像这样的功能吗?

UPD对于所有认为这个问题很简单的人,请首先尝试为参数~30计算此函数.

UPD2任意精度是一个很好的解决方法,但如果可能的话我宁愿避免它.我需要一个"标准"的机器精度(不多也不少)和最高速度.

UPD3事实证明,mpmath结果令人惊讶地不准确.即使标准python math工作,mpmath结果也会更糟.这使它绝对毫无价值.

UPD4用于比较计算erfcx的不同方法的代码.

import numpy as np 

def int_erfcx(x):
    "Integral which gives erfcx" 
    from scipy import integrate
    def f(xi):
        return np.exp(-x*xi)*np.exp(-0.5*xi*xi)
    return 0.79788456080286535595*integrate.quad(f,
                           0.0,min(2.0,50.0/(1.0+x))+100.0,limit=500)[0] 

def my_erfcx(x):
    """M. M. Shepherd and J. G. Laframboise, 
       MATHEMATICS OF COMPUTATION 36, 249 (1981)
       Note that it is reasonable to compute it in long double 
       (or whatever python has)
    """
    ch_coef=[np.float128(0.1177578934567401754080e+01),
             np.float128(  -0.4590054580646477331e-02),
             np.float128( -0.84249133366517915584e-01),
             np.float128(  0.59209939998191890498e-01),
             np.float128( -0.26658668435305752277e-01),
             np.float128( …
Run Code Online (Sandbox Code Playgroud)

python math mpmath

7
推荐指数
1
解决办法
1521
查看次数

Windows上的os.rename会引发WindowsError而不是OSError

Python文档说明了这一点

os.rename(src,dst)

... 在Windows上,如果dst已经存在,即使它是一个文件,也会引发OSError ...

但是,对我来说它引发了WindowsError.文档中有错误吗?

问题的第二部分(更一般,但受上述问题的启发):

UPD对不起,问题的第二部分是不正确的.WindowsError确实except OSError应该得到应有的效果.

python exception file-rename

4
推荐指数
1
解决办法
2949
查看次数

mayavi中的屏幕截图具有透明度

我正在尝试使用mayavi场景的透明度保存屏幕截图。基于此讨论,我编写了一个脚本来保存图形,但结果是输出混乱。在最小的工作示例下面:在mayavi中,我创建一个具有两个球体的场景,然后先将其保存为RGB,然后再保存为RGBA格式。当RGBA文件混乱时,保存为RGB可以工作。我相信,问题出在mayavi方面,因为如果我从mayavi拍摄RGB图像,添加透明通道并使用PIL保存文件,结果将是我所期望的。

这是bug还是mayavi rgba格式应以某种方式转换为PIL接受的格式?

from mayavi import mlab
from PIL import Image

fig=mlab.figure(1, bgcolor=(1, 1, 1), size=(700, 700))
# Set camera position and properties
fig.scene.parallel_projection = True
fig.scene.show_axes = True

# Draw atoms
x, y, z, t = [0.0,1.0] , [0.0,1.0], [0.0,0.0], [1,2]
dat = mlab.pipeline.scalar_scatter(x, y, z, t, figure=fig)
fig = mlab.pipeline.glyph(dat,scale_mode='none', scale_factor=0.5, figure=fig)

imgmap_RGB = mlab.screenshot(figure=fig, mode='rgb', antialiased=True)
img_RGB = Image.fromarray(imgmap_RGB, 'RGB')
img_RGB.save('foo_RGB.png')

imgmap_RGBA = mlab.screenshot(figure=fig, mode='rgba', antialiased=True)
img_RGBA = Image.fromarray(imgmap_RGBA, 'RGBA')
img_RGBA.save('foo_RGBA.png')

mlab.show()
Run Code Online (Sandbox Code Playgroud)

python mayavi

4
推荐指数
1
解决办法
540
查看次数

标签 统计

python ×3

exception ×1

file-rename ×1

math ×1

mayavi ×1

mpmath ×1