Changeset 477 for library/tests/test_util.cpp
- Timestamp:
- 08/05/09 14:40:03 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/tests/test_util.cpp
r469 r477 27 27 using itpp::mat; 28 28 29 std::string load_test_file(const char *fname) 30 { 31 char buffer[8192]; 32 memset(buffer, 0, sizeof(buffer)); 33 std::ifstream src(fname, std::ios_base::binary); 34 src.read(buffer, sizeof(buffer) - 1); 35 return std::string(buffer); 29 std::string load_test_file ( const char *fname ) { 30 char buffer[8192]; 31 memset ( buffer, 0, sizeof ( buffer ) ); 32 std::ifstream src ( fname, std::ios_base::binary ); 33 src.read ( buffer, sizeof ( buffer ) - 1 ); 34 return std::string ( buffer ); 36 35 } 37 36 38 bool remove_all (const char *path) {39 40 37 bool remove_all ( const char *path ) { 38 DIR *dir; 39 dirent *de; 41 40 42 43 if ((dir = opendir(path)) != 0) {44 45 std::string top(path);46 41 bool rv = true; 42 if ( ( dir = opendir ( path ) ) != 0 ) { 43 try { 44 std::string top ( path ); 45 top += "/"; 47 46 48 while ((de = readdir(dir)) != 0) { 49 if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) { 50 std::string subpath(top); 51 subpath += de->d_name; 52 remove_all(subpath.c_str()); 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; 53 57 } 54 } 55 } catch (...) { 56 closedir(dir); 57 throw; 58 59 closedir ( dir ); 60 61 if ( rmdir ( path ) ) { 62 std::string msg = "can't remove dir "; 63 msg += path; 64 throw std::runtime_error ( msg ); 65 } 66 } else { 67 if ( errno == ENOTDIR ) { 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 } 58 83 } 59 84 60 closedir(dir); 61 62 if (rmdir(path)) { 63 std::string msg = "can't remove dir "; 64 msg += path; 65 throw std::runtime_error(msg); 66 } 67 } else { 68 if (errno == ENOTDIR) { 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 } 84 } 85 86 return rv; 85 return rv; 87 86 } 88 87 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);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 ); 93 92 94 double xstep = (xb(1) - xb(0)) / xn;95 double ystep = (yb(1) - yb(0)) / yn;93 double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn; 94 double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn; 96 95 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)); 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 104 } 105 }106 105 107 return sumsum(Pdf) * xstep * ystep;106 return sumsum ( Pdf ) * xstep * ystep; 108 107 } 109 108 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);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 ); 114 113 115 double xstep = (xb(1) - xb(0)) / xn;116 double ystep = (yb(1) - yb(0)) / yn;114 double xstep = ( xb ( 1 ) - xb ( 0 ) ) / xn; 115 double ystep = ( yb ( 1 ) - yb ( 0 ) ) / yn; 117 116 118 vec Mu(xn + 1);119 vec Si(yn + 1);117 vec Mu ( xn + 1 ); 118 vec Si ( yn + 1 ); 120 119 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)); 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 130 } 131 }132 131 133 vec fm = sum(Pdf, 2);134 double sfm = sum(fm);135 vec fs = sum(Pdf, 1);136 double sfs = sum(fs);137 138 139 return vec_2(vi0, vi1);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 ); 140 139 } 141 140