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

后端 未结 8 2039
日久生厌
日久生厌 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: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
    

提交回复
热议问题