| 1 |
|
| |
| 2 |
|
| #ifdef __cplusplus |
| 3 |
|
| extern "C" { |
| 4 |
|
| #endif |
| 5 |
|
| #include "EXTERN.h" |
| 6 |
|
| #include "perl.h" |
| 7 |
|
| #include "XSUB.h" |
| 8 |
|
| #ifdef __cplusplus |
| 9 |
|
| } |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| #include "math.h" |
| 13 |
|
| |
| 14 |
|
| #define SQRT2PI 2.506628274631 |
| 15 |
|
| |
| 16 |
|
| inline NV _pdf(NV x, NV m, NV s) { |
| 17 |
|
| NV z = (x - m)/s; |
| 18 |
|
| return exp(-0.5 * z * z) / (SQRT2PI * s); |
| 19 |
|
| } |
| 20 |
|
| |
| 21 |
|
| MODULE = Math::Gauss::XS PACKAGE = Math::Gauss::XS |
| 22 |
|
| |
| 23 |
|
| PROTOTYPES: DISABLE |
| 24 |
|
| |
| 25 |
|
| NV |
| 26 |
|
| pdf(...) |
| 27 |
|
| CODE: |
| 28 |
0 |
▶ | NV x = items > 0 ? SvNV(ST(0)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV x = items > 0 ? SvNV(ST(0)) : 0; |
0 |
0 |
0 |
|
| 29 |
0 |
▶ | NV m = items > 1 ? SvNV(ST(1)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV m = items > 1 ? SvNV(ST(1)) : 0; |
0 |
0 |
0 |
|
| 30 |
0 |
▶ | NV s = items > 2 ? SvNV(ST(2)) : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| NV s = items > 2 ? SvNV(ST(2)) : 1; |
0 |
0 |
0 |
|
| 31 |
|
| |
| 32 |
0 |
▶ | if (s <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s <= 0) { |
0 |
0 |
0 |
|
| 33 |
0 |
| Perl_croak(aTHX_ "Can't evaluate Math::Gauss:pdf for $s=%f not strictly positive", s); |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
0 |
| RETVAL = _pdf(x, m, s); |
| 37 |
|
| OUTPUT: |
| 38 |
|
| RETVAL |
| 39 |
|
| |
| 40 |
|
| |
| 41 |
|
| NV |
| 42 |
|
| cdf(...) |
| 43 |
|
| CODE: |
| 44 |
0 |
▶ | NV x = items > 0 ? SvNV(ST(0)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV x = items > 0 ? SvNV(ST(0)) : 0; |
0 |
0 |
0 |
|
| 45 |
0 |
▶ | NV m = items > 1 ? SvNV(ST(1)) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| NV m = items > 1 ? SvNV(ST(1)) : 0; |
0 |
0 |
0 |
|
| 46 |
0 |
▶ | NV s = items > 2 ? SvNV(ST(2)) : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| NV s = items > 2 ? SvNV(ST(2)) : 1; |
0 |
0 |
0 |
|
| 47 |
|
| |
| 48 |
0 |
▶ | if (s <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s <= 0) { |
0 |
0 |
0 |
|
| 49 |
0 |
| Perl_croak(aTHX_ "Can't evaluate Math::Gauss:cdf for $s=%f not strictly positive", s); |
| 50 |
|
| } |
| 51 |
|
| |
| 52 |
0 |
| NV z = (x - m)/s; |
| 53 |
0 |
| NV t = 1.0/(1.0 + 0.2316419*fabs(z)); |
| 54 |
0 |
| NV y = t*(0.319381530 |
| 55 |
0 |
| + t*(-0.356563782 |
| 56 |
0 |
| + t*(1.781477937 |
| 57 |
0 |
| + t*(-1.821255978 |
| 58 |
0 |
| + t*1.330274429 )))); |
| 59 |
|
| // fprintf(stderr, "x = %f, t = %f, y = %f\n", x, t, y); |
| 60 |
0 |
▶ | RETVAL = (z > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (z > 0) |
0 |
0 |
0 |
|
| 61 |
0 |
| ? (1.0 - _pdf( z, 0, 1 ) * y) |
| 62 |
0 |
▶ | : _pdf( z, 0, 1 ) * y; |
Branch: 1 branch
| Branch | True | False | Total |
| : _pdf( z, 0, 1 ) * y; |
0 |
0 |
0 |
|
| 63 |
|
| OUTPUT: |
| 64 |
|
| RETVAL |
| 65 |
|
| |
| 66 |
|
| |
| 67 |
|
| NV |
| 68 |
|
| inv_cdf(NV x) |
| 69 |
|
| CODE: |
| 70 |
0 |
▶ | if ( x<=0.0 || x>=1.0 ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( x<=0.0 || x>=1.0 ) { |
0 |
0 |
0 |
| if ( x<=0.0 || x>=1.0 ) { |
0 |
0 |
0 |
|
| 71 |
0 |
| Perl_croak(aTHX_ "Can't evaluate Math::Gauss::inv_cdf for $x=%f outside ]0,1[", x); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
0 |
▶ | NV t = (x < 0.5) ? sqrt(-2.0 * log(x)) : sqrt( -2.0*log(1.0 - x) ); |
Branch: 1 branch
| Branch | True | False | Total |
| NV t = (x < 0.5) ? sqrt(-2.0 * log(x)) : sqrt( -2.0*log(1.0 - x) ); |
0 |
0 |
0 |
|
| 75 |
0 |
| NV y = (2.515517 + t*(0.802853 + t*0.010328)); |
| 76 |
0 |
| y /= 1.0 + t*(1.432788 + t*(0.189269 + t*0.001308)); |
| 77 |
0 |
▶ | RETVAL = (x < 0.5) ? y - t : t -y; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (x < 0.5) ? y - t : t -y; |
0 |
0 |
0 |
| RETVAL = (x < 0.5) ? y - t : t -y; |
0 |
0 |
0 |
|
| 78 |
|
| OUTPUT: |
| 79 |
|
| RETVAL |