1 | #include <itpp/itmex.h> |
---|
2 | #include <estim/arx.h> |
---|
3 | #include "config2mxstruct.h" |
---|
4 | |
---|
5 | using namespace bdm; |
---|
6 | |
---|
7 | class mexEpdf: public epdf{ |
---|
8 | protected: |
---|
9 | string name; |
---|
10 | mxArray *data; |
---|
11 | public: |
---|
12 | mexEpdf() {}; |
---|
13 | void from_setting(const Setting &S) { |
---|
14 | name = (const char *) S["name"]; |
---|
15 | UImxConfig conf(S); |
---|
16 | data = mxDuplicateArray(conf.mxconfig); |
---|
17 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
18 | //TODO (future...): |
---|
19 | //mxArray * init_data = setting2mxarray S["init_data"]; |
---|
20 | //mexCallMATLAB(1, &data, 1, &init_data, name+"_from_setting"); |
---|
21 | //delete init_data; |
---|
22 | } |
---|
23 | vec mean() const { |
---|
24 | mxArray *tmp; |
---|
25 | string fname = name+"_mean"; |
---|
26 | mexCallMATLAB(1, &tmp, 1, (mxArray **) &data, fname.c_str()); |
---|
27 | return mxArray2vec(tmp); |
---|
28 | } |
---|
29 | }; |
---|
30 | |
---|
31 | class mexBM: public BM{ |
---|
32 | protected : |
---|
33 | string name; |
---|
34 | mexEpdf est; |
---|
35 | mxArray *data; |
---|
36 | public: |
---|
37 | mexBM() {} |
---|
38 | |
---|
39 | mxArray *get_data() { |
---|
40 | //mexCallMATLAB(0, NULL, 1, &data, "dump"); |
---|
41 | return mxDuplicateArray(data); |
---|
42 | } |
---|
43 | |
---|
44 | void from_setting(const Setting &S) { |
---|
45 | name = (const char *) S["name"]; |
---|
46 | UImxConfig conf(S); |
---|
47 | data = mxDuplicateArray(conf.mxconfig); |
---|
48 | //string fname = name+"_new"; |
---|
49 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
50 | //the following works as long as the posterior is the |
---|
51 | //only member object there could be a structure of |
---|
52 | //member objects in the setting and a for cycle over |
---|
53 | //all of them right here in the code |
---|
54 | Setting &posterior = S["posterior"]; |
---|
55 | est.from_setting(posterior); |
---|
56 | } |
---|
57 | void bayes(const vec &dt) { |
---|
58 | //void bayes() { |
---|
59 | mxArray *tmp, *old; |
---|
60 | mxArray *in[2]; |
---|
61 | in[0] = data; |
---|
62 | in[1] = mxCreateDoubleMatrix(dt.size(), 1, mxREAL); |
---|
63 | vec2mxArray(dt, in[1]); |
---|
64 | mexCallMATLAB(1, &tmp, 2, in, (name+"_bayes").c_str()); |
---|
65 | old = data; |
---|
66 | data = mxDuplicateArray(tmp); |
---|
67 | if (old) mxDestroyArray(old); |
---|
68 | if (tmp) mxDestroyArray(tmp); |
---|
69 | //mexCallMATLAB(0, NULL, 1, &data, "dump"); |
---|
70 | } |
---|
71 | const mexEpdf& posterior() const { |
---|
72 | return est; |
---|
73 | } //tohle by melo zustat!! |
---|
74 | const mexEpdf* _e() const { |
---|
75 | return &est; |
---|
76 | } //tohle by melo zustat!! |
---|
77 | }; |
---|
78 | |
---|
79 | void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[]) |
---|
80 | { |
---|
81 | // Check the number of inputs and output arguments |
---|
82 | if(n_input!=1) mexErrMsgTxt("Wrong number of input variables - expected parameter 'filename'!"); |
---|
83 | |
---|
84 | // ------------------ Start of routine --------------------------- |
---|
85 | |
---|
86 | //mexBM mB(mxArray2string(input[0])); // naplni |
---|
87 | //mB.bayes(vec_1(1.3)); |
---|
88 | string filename = mxArray2string(input[0]); |
---|
89 | Config config; |
---|
90 | config.readFile(filename.c_str()); |
---|
91 | mexBM mb; |
---|
92 | Setting &root = config.getRoot(); |
---|
93 | mb.from_setting(root); |
---|
94 | vec dt = "18.0"; |
---|
95 | mb.bayes(dt); |
---|
96 | if(n_output>0) output[0] = mb.get_data(); |
---|
97 | |
---|
98 | |
---|
99 | // ------------------ End of routine ----------------------------- |
---|
100 | |
---|
101 | // Create output vectors |
---|
102 | //output[0] = mxCreateDoubleMatrix(1,1, mxREAL); |
---|
103 | |
---|
104 | // Convert the IT++ format to Matlab format for output |
---|
105 | //vec2mxArray(mB.posterior().mean(), output[0]); |
---|
106 | } |
---|