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