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