[436] | 1 | #define BDMLIB // not an ideal way to prevent double registration of UI factories... |
---|
[425] | 2 | #include "test_util.h" |
---|
[436] | 3 | #include "stat/exp_family.h" |
---|
[425] | 4 | #include <fstream> |
---|
| 5 | #include <stdexcept> |
---|
| 6 | #include <string> |
---|
| 7 | #include <errno.h> |
---|
| 8 | #include <fcntl.h> |
---|
| 9 | #include <string.h> |
---|
[469] | 10 | #include <stdio.h> |
---|
[425] | 11 | #include <sys/stat.h> |
---|
| 12 | #include <sys/types.h> |
---|
[469] | 13 | |
---|
| 14 | #ifdef WIN32 |
---|
| 15 | #include "dirent.h" |
---|
| 16 | #include <direct.h> |
---|
| 17 | #define rmdir _rmdir |
---|
| 18 | #define unlink _unlink |
---|
| 19 | #else |
---|
| 20 | #include <dirent.h> |
---|
[425] | 21 | #include <unistd.h> |
---|
[469] | 22 | #endif |
---|
[425] | 23 | |
---|
| 24 | namespace bdm { |
---|
| 25 | |
---|
[436] | 26 | using itpp::vec; |
---|
| 27 | using itpp::mat; |
---|
| 28 | |
---|
[477] | 29 | std::string load_test_file ( const char *fname ) { |
---|
| 30 | char buffer[8192]; |
---|
| 31 | memset ( buffer, 0, sizeof ( buffer ) ); |
---|
[486] | 32 | std::ifstream src ( fname ); |
---|
[477] | 33 | src.read ( buffer, sizeof ( buffer ) - 1 ); |
---|
| 34 | return std::string ( buffer ); |
---|
[425] | 35 | } |
---|
| 36 | |
---|
[477] | 37 | bool remove_all ( const char *path ) { |
---|
| 38 | DIR *dir; |
---|
| 39 | dirent *de; |
---|
[425] | 40 | |
---|
[477] | 41 | bool rv = true; |
---|
| 42 | if ( ( dir = opendir ( path ) ) != 0 ) { |
---|
| 43 | try { |
---|
| 44 | std::string top ( path ); |
---|
| 45 | top += "/"; |
---|
[425] | 46 | |
---|
[477] | 47 | while ( ( de = readdir ( dir ) ) != 0 ) { |
---|
| 48 | if ( strcmp ( de->d_name, "." ) && strcmp ( de->d_name, ".." ) ) { |
---|
| 49 | std::string subpath ( top ); |
---|
| 50 | subpath += de->d_name; |
---|
| 51 | remove_all ( subpath.c_str() ); |
---|
| 52 | } |
---|
| 53 | } |
---|
| 54 | } catch ( ... ) { |
---|
| 55 | closedir ( dir ); |
---|
| 56 | throw; |
---|
[425] | 57 | } |
---|
| 58 | |
---|
[477] | 59 | closedir ( dir ); |
---|
[425] | 60 | |
---|
[477] | 61 | if ( rmdir ( path ) ) { |
---|
| 62 | std::string msg = "can't remove dir "; |
---|
| 63 | msg += path; |
---|
| 64 | throw std::runtime_error ( msg ); |
---|
| 65 | } |
---|
[425] | 66 | } else { |
---|
[486] | 67 | if ( ( errno == ENOTDIR ) || // Linux |
---|
| 68 | ( errno == EINVAL ) ) { // Windows |
---|
[477] | 69 | if ( unlink ( path ) ) { |
---|
| 70 | std::string msg = "can't remove file "; |
---|
| 71 | msg += path; |
---|
| 72 | throw std::runtime_error ( msg ); |
---|
| 73 | } |
---|
| 74 | } else { |
---|
| 75 | if ( errno != ENOENT ) { |
---|
| 76 | std::string msg = "can't remove "; |
---|
| 77 | msg += path; |
---|
| 78 | throw std::runtime_error ( msg ); |
---|
| 79 | } else { |
---|
| 80 | // it wasn't there in the first place |
---|
| 81 | rv = false; |
---|
| 82 | } |
---|
| 83 | } |
---|
[425] | 84 | } |
---|
| 85 | |
---|
[477] | 86 | return rv; |
---|
[425] | 87 | } |
---|
| 88 | |
---|
[477] | 89 | double normcoef ( const epdf *ep, const vec &xb, const vec &yb, |
---|
| 90 | int xn, int yn ) { |
---|
| 91 | mat Pdf ( xn + 1, yn + 1 ); |
---|
| 92 | vec rgr ( 2 ); |
---|
[436] | 93 | |
---|
[477] | 94 | double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn; |
---|
| 95 | double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn; |
---|
[436] | 96 | |
---|
[477] | 97 | double x = xb ( 0 ); |
---|
| 98 | for ( int i = 0; i <= xn; x += xstep, i++ ) { |
---|
| 99 | rgr ( 0 ) = x; |
---|
| 100 | double y = yb ( 0 ); |
---|
| 101 | for ( int j = 0; j <= yn; y += ystep, j++ ) { |
---|
| 102 | rgr ( 1 ) = y; |
---|
| 103 | Pdf ( i, j ) = exp ( ep->evallog ( rgr ) ); |
---|
| 104 | } |
---|
[436] | 105 | } |
---|
| 106 | |
---|
[477] | 107 | return sumsum ( Pdf ) * xstep * ystep; |
---|
[425] | 108 | } |
---|
[436] | 109 | |
---|
[477] | 110 | vec num_mean2 ( const epdf *ep, const vec &xb, const vec &yb, |
---|
| 111 | int xn, int yn ) { |
---|
| 112 | mat Pdf ( xn + 1, yn + 1 ); |
---|
| 113 | vec rgr ( 2 ); |
---|
[436] | 114 | |
---|
[477] | 115 | double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn; |
---|
| 116 | double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn; |
---|
[436] | 117 | |
---|
[477] | 118 | vec Mu ( xn + 1 ); |
---|
| 119 | vec Si ( yn + 1 ); |
---|
[436] | 120 | |
---|
[477] | 121 | double x = xb ( 0 ); |
---|
| 122 | for ( int i = 0; i <= xn; x += xstep, i++ ) { |
---|
| 123 | Mu ( i ) = x; |
---|
| 124 | rgr ( 0 ) = x; |
---|
| 125 | double y = yb ( 0 ); |
---|
| 126 | for ( int j = 0; j <= yn; y += ystep, j++ ) { |
---|
| 127 | Si ( j ) = y; |
---|
| 128 | rgr ( 1 ) = y; |
---|
| 129 | Pdf ( i, j ) = exp ( ep->evallog ( rgr ) ); |
---|
| 130 | } |
---|
[436] | 131 | } |
---|
| 132 | |
---|
[477] | 133 | vec fm = sum ( Pdf, 2 ); |
---|
| 134 | double sfm = sum ( fm ); |
---|
| 135 | vec fs = sum ( Pdf, 1 ); |
---|
| 136 | double sfs = sum ( fs ); |
---|
| 137 | double vi0 = Mu * fm / sfm; |
---|
| 138 | double vi1 = Si * fs / sfs; |
---|
| 139 | return vec_2 ( vi0, vi1 ); |
---|
[436] | 140 | } |
---|
| 141 | |
---|
| 142 | } |
---|