Is there a scaled complementary error function in python available?

前端 未结 5 1856
心在旅途
心在旅途 2021-01-18 03:54

In matlab there is a special function which is not available in any of the collections for the Python I know (numpy, scipy, mpmath, ...).

Probably there are other p

5条回答
  •  旧时难觅i
    2021-01-18 04:01

    The gmpy2 library provides access to the MPFR multiple-precision library. For normal precision, it is almost 5x faster than mpmath.

    $ py27 -m timeit -s "import mpmath" -s "def erfcx(x):return mpmath.exp(x**2) * mpmath.erfc(x)" "erfcx(30)"
    10000 loops, best of 3: 47.3 usec per loop
    $ py27 -m timeit -s "import gmpy2" -s "def erfcx(x):return gmpy2.exp(x**2) * gmpy2.erfc(x)" "erfcx(30)"
    100000 loops, best of 3: 10.8 usec per loop
    

    Both libraries return the same result for 30.

    >>> import mpmath
    >>> import gmpy2
    >>> mpmath.exp(30**2) * mpmath.erfc(30)
    mpf('0.018795888861416751')
    >>> gmpy2.exp(30**2) * gmpy2.erfc(30)
    mpfr('0.018795888861416751')
    >>> 
    

    Disclaimer: I maintain gmpy2. I'm actively working towards a new release but there shouldn't be any issues with the current release for this calculation.

    Edit: I was curious about the overhead of making two functions calls instead of just one so I implemented a gmpy2.erfcx() entirely in C but still using MPFR to perform the calculations. The improvement was less than I expected. If you think erfcx() would be useful, I can add it to the next release.

    $ py27 -m timeit -s "import gmpy2" "gmpy2.erfcx(30)"
    100000 loops, best of 3: 9.45 usec per loop
    

提交回复
热议问题