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