Is there an easily available implementation of erf() for Python?

后端 未结 8 1951
日久生厌
日久生厌 2020-12-24 04:54

I can implement the error function, erf, myself, but I\'d prefer not to. Is there a python package with no external dependencies that contains an implementation of this func

相关标签:
8条回答
  • 2020-12-24 05:39

    SciPy has an implementation of the erf function, see scipy.special.erf.

    0 讨论(0)
  • 2020-12-24 05:40

    One note for those aiming for higher performance: vectorize, if possible.

    import numpy as np
    from scipy.special import erf
    
    def vectorized(n):
        x = np.random.randn(n)
        return erf(x)
    
    def loopstyle(n):
        x = np.random.randn(n)
        return [erf(v) for v in x]
    
    %timeit vectorized(10e5)
    %timeit loopstyle(10e5)
    

    gives results

    # vectorized
    10 loops, best of 3: 108 ms per loop
    
    # loops
    1 loops, best of 3: 2.34 s per loop
    
    0 讨论(0)
提交回复
热议问题