Need code for Inverse Error Function

前端 未结 7 1872
清歌不尽
清歌不尽 2021-02-10 00:54

Does anyone know where I could find code for the \"Inverse Error Function?\" Freepascal/Delphi would be preferable but C/C++ would be fine too.

The TMath/DMath library d

7条回答
  •  再見小時候
    2021-02-10 01:33

    I've used this, which I believe is reasonably accurate and quick (usually 2 iterations of the loop), but of course caveat emptor. NormalX assumes that 0<=Q<=1, and would likely give silly answers if that assumption doesn't hold.

    /* return P(N>X) for normal N */
    double  NormalQ( double x)
    {   return 0.5*erfc( x/sqrt(2.0));
    }
    
    #define NORX_C0   2.8650422353e+00
    #define NORX_C1   3.3271545598e+00
    #define NORX_C2   2.7147548996e-01
    #define NORX_D1   2.8716448975e+00
    #define NORX_D2   1.1690926940e+00
    #define NORX_D3   4.7994444496e-02
    /* return X such that P(N>X) = Q for normal N */
    double  NormalX( double Q)  
    {
    double  eps = 1e-12;
    int signum = Q < 0.5;
    double  QF = signum ? Q : (1.0-Q);
    double  T = sqrt( -2.0*log(QF));
    double  X = T - ((NORX_C2*T + NORX_C1)*T + NORX_C0)
                        /(((NORX_D3*T + NORX_D2)*T + NORX_D1)*T + 1.0);
    double  SPI2 = sqrt( 2.0 * M_PI);
    int i;
        /* newton's method */
        for( i=0; i<10; ++i)
        {
        double  dX  = (NormalQ(X) - QF)*exp(0.5*X*X)*SPI2;
                X += dX;
                if ( fabs( dX) < eps)   
                {   break;
                }
        }
        return signum ? X : -X;
    }
    

提交回复
热议问题