1 | #define BDMLIB // not an ideal way to prevent double registration of UI factories... |
---|
2 | #include "test_util.h" |
---|
3 | #include "stat/exp_family.h" |
---|
4 | #include <fstream> |
---|
5 | #include <stdexcept> |
---|
6 | #include <string> |
---|
7 | #include <errno.h> |
---|
8 | #include <fcntl.h> |
---|
9 | #include <string.h> |
---|
10 | #include <stdio.h> |
---|
11 | #include <sys/stat.h> |
---|
12 | #include <sys/types.h> |
---|
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> |
---|
21 | #include <unistd.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | namespace bdm { |
---|
25 | |
---|
26 | using itpp::vec; |
---|
27 | using itpp::mat; |
---|
28 | |
---|
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 ); |
---|
35 | } |
---|
36 | |
---|
37 | bool remove_all ( const char *path ) { |
---|
38 | DIR *dir; |
---|
39 | dirent *de; |
---|
40 | |
---|
41 | bool rv = true; |
---|
42 | if ( ( dir = opendir ( path ) ) != 0 ) { |
---|
43 | try { |
---|
44 | std::string top ( path ); |
---|
45 | top += "/"; |
---|
46 | |
---|
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; |
---|
57 | } |
---|
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 | } |
---|
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 | } |
---|