| 1 | #include <itpp/itmex.h> | 
|---|
| 2 | #include <estim/merger.h> | 
|---|
| 3 | #include "config2mxstruct.h" | 
|---|
| 4 | #include "mexlog.h" | 
|---|
| 5 | #include "mexparse.h" | 
|---|
| 6 |  | 
|---|
| 7 | using namespace bdm; | 
|---|
| 8 |  | 
|---|
| 9 | void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[]) | 
|---|
| 10 | { | 
|---|
| 11 |     // Check the number of inputs and output arguments | 
|---|
| 12 |         if(n_input!=3) mexErrMsgTxt("Usage:\n"   | 
|---|
| 13 |                 "result=merger(sources, support, merger)\n" | 
|---|
| 14 |                 "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n" | 
|---|
| 15 |                 "  support= struct(\n" | 
|---|
| 16 |                 "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n"    | 
|---|
| 17 |                 "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n"    | 
|---|
| 18 |                 "         === OR ==\n" | 
|---|
| 19 |                 "           pdf     = struct('class','epdf'); % pdf to draw samples from\n" | 
|---|
| 20 |                 "           nsamples= 100;                    % number of samples\n" | 
|---|
| 21 |                 "           );\n" | 
|---|
| 22 |                 "        If all elements are present,  (grid,nbins) is used;\n" | 
|---|
| 23 |                 "  merger = struct('class','merger_*');       % object to be used for merging,\n\n" | 
|---|
| 24 |                 "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM."); | 
|---|
| 25 |  | 
|---|
| 26 |         // LOAD CONFIG | 
|---|
| 27 |         UImxArray Src; | 
|---|
| 28 |         Src.addList(input[0],"Sources"); | 
|---|
| 29 |          | 
|---|
| 30 |         UImxArray Sup (input[1]); | 
|---|
| 31 |         UImxArray Mer (input[2]); | 
|---|
| 32 |         // Sources | 
|---|
| 33 |         Array<mpdf*> Sources; | 
|---|
| 34 |         //abuse Mer to store sources | 
|---|
| 35 |  | 
|---|
| 36 |         Setting& _Sources=Src.lookup("Sources"); | 
|---|
| 37 |         int Slen=_Sources.getLength(); | 
|---|
| 38 |         Sources.set_size(Slen); | 
|---|
| 39 |         for (int i=0; i<Slen; i++){ | 
|---|
| 40 |                 mpdf* mtmp = UI::build<mpdf>(_Sources,i); | 
|---|
| 41 |                 if (mtmp) {  | 
|---|
| 42 |                         Sources(i)=mtmp; | 
|---|
| 43 |                 } else{ // source is not mpdf | 
|---|
| 44 |                         epdf* etmp = UI::build<epdf>(_Sources,i); | 
|---|
| 45 |                         if (etmp){ | 
|---|
| 46 |                                 Sources(i) = new mepdf(etmp, true); | 
|---|
| 47 |                         } | 
|---|
| 48 |                 } | 
|---|
| 49 |  | 
|---|
| 50 |         } | 
|---|
| 51 |  | 
|---|
| 52 |         merger_base* Merger=UI::build<merger_base>(Mer.getRoot()); | 
|---|
| 53 |  | 
|---|
| 54 |         // Support | 
|---|
| 55 |         Setting & _Supp=Sup.getRoot(); | 
|---|
| 56 |          | 
|---|
| 57 |         Array<vec> bounds (0); | 
|---|
| 58 |         UI::get (bounds, _Supp, "grid"); | 
|---|
| 59 |         ivec nbins(0); | 
|---|
| 60 |         UI::get (nbins, _Supp, "nbins"); | 
|---|
| 61 |          | 
|---|
| 62 |         epdf *g0=UI::build<epdf> (_Supp, "pdf"); | 
|---|
| 63 |         int npoints=100; | 
|---|
| 64 |         _Supp.lookupValue("nsamples",npoints); | 
|---|
| 65 |          | 
|---|
| 66 | // COMPUTE RESULTS | 
|---|
| 67 |         Merger->set_sources(Sources,true); // takes care of deletion of sources | 
|---|
| 68 |         if (bounds.length() > 0 && nbins.length()>0) { | 
|---|
| 69 |                 Merger->set_support (bounds,nbins); | 
|---|
| 70 |         } else { | 
|---|
| 71 |                 if (g0) { | 
|---|
| 72 |                         Merger->set_support (*g0,npoints); | 
|---|
| 73 |                         delete g0; | 
|---|
| 74 |                 } | 
|---|
| 75 |         } | 
|---|
| 76 |         Merger->merge(); | 
|---|
| 77 |          | 
|---|
| 78 |         // Save results | 
|---|
| 79 |         if (n_output>0){ | 
|---|
| 80 |                 mxArray* tmp = mxCreateStructMatrix(1,1,0,NULL); | 
|---|
| 81 |                 //support | 
|---|
| 82 |                 Array<vec> &samples=Merger->_Smp()._samples(); | 
|---|
| 83 |                 if (samples.size()>0){ | 
|---|
| 84 |                         mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL); | 
|---|
| 85 |                 Arrayvec2mxArray(samples,fld); | 
|---|
| 86 |                 mxReplaceFieldNM(tmp, "support", fld); | 
|---|
| 87 |                 } | 
|---|
| 88 |  | 
|---|
| 89 |                 //weights | 
|---|
| 90 |                 vec &w = Merger->_Smp()._w(); | 
|---|
| 91 |                 mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL); | 
|---|
| 92 |                 vec2mxArray(w,fldw); | 
|---|
| 93 |                 mxReplaceFieldNM(tmp, "weights", fldw); | 
|---|
| 94 |                  | 
|---|
| 95 |                 output[0] = tmp; | 
|---|
| 96 |         } | 
|---|
| 97 | } | 
|---|