Is there a scaled complementary error function in python available?

前端 未结 5 1855
心在旅途
心在旅途 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:05

    I don't know that any of the standard sources include that function, but you can implement it in straightforward fashion, at least if you use mpmath and aren't too worried about performance:

    import math
    import mpmath
    
    def erfcx(x):
        return math.exp(x**2) * math.erfc(x)
    
    def erfcx_mp(x):
        return mpmath.exp(x**2) * mpmath.erfc(x)
    
    where = mpmath.linspace(1, 50, 10) + mpmath.linspace(100, 1000, 5)
    for x in where:
        try:
            std = erfcx(x)
        except OverflowError:
            std = None
        new = erfcx_mp(x)
        approx = (1/(x*mpmath.pi**0.5))
        print x, std, new, (new-approx)/approx 
    

    produces

    1.0 0.427583576156 0.427583576155807 -0.242127843858688
    6.44444444444444 0.0865286153111 0.0865286153111425 -0.0116285899486798
    11.8888888888889 0.0472890800456 0.0472890800455829 -0.00350053472385845
    17.3333333333333 0.032495498521 0.0324954985209682 -0.00165596082986796
    22.7777777777778 0.024745497 0.0247454970000106 -0.000960939188986022
    28.2222222222222 None 0.0199784436993529 -0.000626572735073611
    33.6666666666667 None 0.0167507236463156 -0.000440550710337029
    39.1111111111111 None 0.0144205913280408 -0.000326545959369654
    44.5555555555556 None 0.0126594222570918 -0.00025167403795913
    50.0 None 0.0112815362653238 -0.000199880119832415
    100.0 None 0.00564161378298943 -4.99925018743586e-5
    325.0 None 0.00173595973189465 -4.73366058776083e-6
    550.0 None 0.00102579754728657 -1.6528843659911e-6
    775.0 None 0.000727985953393782 -8.32464102161289e-7
    1000.0 None 0.000564189301453388 -4.9999925011689e-7
    

    And it behaves as it should even when the math.* routines overflow. mpmath's interval support isn't quite up to the task (without some hackery I'm too lazy to do), but for this I'm pretty sure mpfs will suffice, as erfcx is simply the product of two things mpmath can compute nicely.

提交回复
热议问题