[336] | 1 | #include <itpp/itmex.h> |
---|
| 2 | #include <estim/arx.h> |
---|
[374] | 3 | #include "config2mxstruct.h" |
---|
[336] | 4 | |
---|
| 5 | using namespace bdm; |
---|
| 6 | |
---|
| 7 | class mexEpdf: public epdf{ |
---|
| 8 | protected: |
---|
| 9 | string name; |
---|
[371] | 10 | mxArray *data; |
---|
[336] | 11 | public: |
---|
[371] | 12 | mexEpdf() {}; |
---|
| 13 | void from_setting(const Setting &S) { |
---|
| 14 | name = (const char *) S["name"]; |
---|
[374] | 15 | UImxConfig conf(S); |
---|
| 16 | data = mxDuplicateArray(conf.mxconfig); |
---|
| 17 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
[371] | 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 | } |
---|
[336] | 29 | }; |
---|
[371] | 30 | |
---|
[336] | 31 | class mexBM: public BM{ |
---|
| 32 | protected : |
---|
| 33 | string name; |
---|
| 34 | mexEpdf est; |
---|
[371] | 35 | mxArray *data; |
---|
[336] | 36 | public: |
---|
[371] | 37 | mexBM() {} |
---|
| 38 | |
---|
[374] | 39 | mxArray *get_data() { |
---|
| 40 | //mexCallMATLAB(0, NULL, 1, &data, "dump"); |
---|
| 41 | return mxDuplicateArray(data); |
---|
| 42 | } |
---|
| 43 | |
---|
[371] | 44 | void from_setting(const Setting &S) { |
---|
| 45 | name = (const char *) S["name"]; |
---|
[374] | 46 | UImxConfig conf(S); |
---|
| 47 | data = mxDuplicateArray(conf.mxconfig); |
---|
| 48 | //string fname = name+"_new"; |
---|
| 49 | //mexCallMATLAB(1, &data, 0, 0, (name+"_new").c_str()); |
---|
[371] | 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() { |
---|
[374] | 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"); |
---|
[371] | 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!! |
---|
[336] | 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 |
---|
[371] | 82 | if(n_input!=1) mexErrMsgTxt("Wrong number of input variables - expected parameter 'filename'!"); |
---|
[336] | 83 | |
---|
| 84 | // ------------------ Start of routine --------------------------- |
---|
| 85 | |
---|
[371] | 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; |
---|
[374] | 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(); |
---|
[371] | 97 | |
---|
[336] | 98 | |
---|
| 99 | // ------------------ End of routine ----------------------------- |
---|
| 100 | |
---|
| 101 | // Create output vectors |
---|
[374] | 102 | //output[0] = mxCreateDoubleMatrix(1,1, mxREAL); |
---|
[336] | 103 | |
---|
| 104 | // Convert the IT++ format to Matlab format for output |
---|
[371] | 105 | //vec2mxArray(mB.posterior().mean(), output[0]); |
---|
[336] | 106 | } |
---|