Changeset 463

Show
Ignore:
Timestamp:
08/01/09 00:12:23 (15 years ago)
Author:
smidl
Message:

compilation fix for shared_ptr

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/mex/merger.cpp

    r429 r463  
    88#endif 
    99 
    10 /*! \file  
     10/*! \file 
    1111\brief Merging static pdfs 
    1212Merger is a program/mex-function for static merging of given pdfs on given support. 
     
    1616Sources = (... any epdf or mpdf  ...); 
    1717Support = {grid=([low1d,high1d],[low2d,high2d],...); 
    18                    nbins=[bins1d,bins2d,... ]};  
     18                   nbins=[bins1d,bins2d,... ]}; 
    1919// OR 
    2020Support = {pdf={class='epdf',...}; 
    21                    nsamples=2};  
     21                   nsamples=2}; 
    2222Merger = {class="merger_base",...} 
    2323\endcode 
     
    2828using namespace bdm; 
    2929 
    30 #ifdef MEX  
    31 void mexFunction(int n_output, mxArray *output[], int n_input, const mxArray *input[]) 
     30#ifdef MEX 
     31void mexFunction (int n_output, mxArray *output[], int n_input, const mxArray *input[]) 
    3232{ 
    33     // Check the number of inputs and output arguments 
    34         if(n_input!=3) mexErrMsgTxt("Usage:\n"   
    35                 "result=merger(sources, support, merger)\n" 
    36                 "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n" 
    37                 "  support= struct(\n" 
    38                 "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n"    
    39                 "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n"    
    40                 "         === OR ==\n" 
    41                 "           pdf     = struct('class','epdf'); % pdf to draw samples from\n" 
    42                 "           nsamples= 100;                    % number of samples\n" 
    43                 "           );\n" 
    44                 "        If all elements are present,  (grid,nbins) is used;\n" 
    45                 "  merger = struct('class','merger_*');       % object to be used for merging,\n\n" 
    46                 "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM."); 
     33        // Check the number of inputs and output arguments 
     34        if (n_input != 3) mexErrMsgTxt ("Usage:\n" 
     35                                            "result=merger(sources, support, merger)\n" 
     36                                            "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n" 
     37                                            "  support= struct(\n" 
     38                                            "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n" 
     39                                            "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n" 
     40                                            "         === OR ==\n" 
     41                                            "           pdf     = struct('class','epdf'); % pdf to draw samples from\n" 
     42                                            "           nsamples= 100;                    % number of samples\n" 
     43                                            "           );\n" 
     44                                            "        If all elements are present,  (grid,nbins) is used;\n" 
     45                                            "  merger = struct('class','merger_*');       % object to be used for merging,\n\n" 
     46                                            "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM."); 
    4747 
    4848        // LOAD CONFIG 
    4949        UImxArray Cfg; 
    50         Cfg.addList(input[0],"Sources"); 
    51         Cfg.addGroup(input[1],"Support"); 
    52         Cfg.addGroup(input[2],"Merger"); 
     50        Cfg.addList (input[0], "Sources"); 
     51        Cfg.addGroup (input[1], "Support"); 
     52        Cfg.addGroup (input[2], "Merger"); 
    5353 
    5454        //DBG 
    55         Cfg.writeFile("merger.cfg"); 
     55        Cfg.writeFile ("merger.cfg"); 
    5656#else 
    57         int main() 
    58         { 
    59                 UIFile Cfg("merger.cfg"); 
    60 #endif   
     57int main() { 
     58        UIFile Cfg ("merger.cfg"); 
     59#endif 
    6160        // Sources 
    6261        Array<mpdf*> Sources; 
    6362        //abuse Mer to store sources 
    64         Setting& _Sources=Cfg.lookup("Sources"); 
    65         int Slen=_Sources.getLength(); 
    66         Sources.set_size(Slen); 
    67         for (int i=0; i<Slen; i++){ 
    68                 try{ 
    69                         mpdf* mtmp = UI::build<mpdf>(_Sources,i); 
    70                         Sources(i)=mtmp; 
    71                 } 
    72                 catch (UIException){ 
     63        Setting& _Sources = Cfg.lookup ("Sources"); 
     64        int Slen = _Sources.getLength(); 
     65        Sources.set_size (Slen); 
     66        for (int i = 0; i < Slen; i++) { 
     67                try { 
     68                        mpdf* mtmp = UI::build<mpdf> (_Sources, i); 
     69                        Sources (i) = mtmp; 
     70                } catch (UIException) { 
    7371                        // it is not mpdf - see if it is epdf 
    7472                        try { 
    75                                 epdf* etmp = UI::build<epdf>(_Sources,i); 
    76                                 if (etmp){ 
    77                                         Sources(i) = new mepdf(etmp, true); 
    78                                 }                                
     73                                shared_ptr<epdf> etmp = UI::build<epdf> (_Sources, i); 
     74                                if (etmp) { 
     75                                        Sources (i) = new mepdf (etmp); 
     76                                } 
     77                        } catch (UIException e) { 
     78                                it_error ("No mpdfs or epdfs found! " + string (e.what())); 
     79                        } catch (std::exception e) { 
     80                                it_error ("Error in UI at " + _Sources[i].getPath()); 
    7981                        } 
    80                         catch (UIException e)  
    81                         { 
    82                                 it_error("No mpdfs or epdfs found! " + string(e.what())); 
    83                         } 
    84                         catch (std::exception e) { 
    85                                 it_error("Error in UI at "+_Sources[i].getPath()); 
    86                         } 
    87                 } 
    88                 catch (std::exception e) { 
    89                         it_error("Error in UI at "+_Sources[i].getPath()); 
     82                } catch (std::exception e) { 
     83                        it_error ("Error in UI at " + _Sources[i].getPath()); 
    9084                } 
    9185        } 
    9286 
    93         merger_base* Merger=UI::build<merger_base>(Cfg,"Merger"); 
     87        merger_base* Merger = UI::build<merger_base> (Cfg, "Merger"); 
    9488 
    9589        // Support 
    96         Setting & _Supp=Cfg.lookup("Support"); 
    97          
    98         if (_Supp.exists("grid") &&  _Supp.exists("nbins")) { 
    99         Array<vec> bounds (0); 
    100         UI::get (bounds, _Supp, "grid"); 
    101         ivec nbins(0); 
    102         UI::get (nbins, _Supp, "nbins"); 
    103         Merger->set_support (bounds,nbins); 
    104          
    105         }else { 
    106                 if (_Supp.exists("pdf") &&  _Supp.exists("nsamples")){ 
    107                         epdf *g0=UI::build<epdf> (_Supp, "pdf"); 
    108                         int npoints=100; 
    109                         _Supp.lookupValue("nsamples",npoints); 
    110                         Merger->set_support (*g0,npoints); 
    111                         delete g0;       
    112                 } 
    113                 else it_error("Use either [grid,nbins] or [pdf,nsamples]."); 
     90        Setting & _Supp = Cfg.lookup ("Support"); 
     91 
     92        if (_Supp.exists ("grid") &&  _Supp.exists ("nbins")) { 
     93                Array<vec> bounds (0); 
     94                UI::get (bounds, _Supp, "grid"); 
     95                ivec nbins (0); 
     96                UI::get (nbins, _Supp, "nbins"); 
     97                Merger->set_support (bounds, nbins); 
     98 
     99        } else { 
     100                if (_Supp.exists ("pdf") &&  _Supp.exists ("nsamples")) { 
     101                        epdf *g0 = UI::build<epdf> (_Supp, "pdf"); 
     102                        int npoints = 100; 
     103                        _Supp.lookupValue ("nsamples", npoints); 
     104                        Merger->set_support (*g0, npoints); 
     105                        delete g0; 
     106                } else it_error ("Use either [grid,nbins] or [pdf,nsamples]."); 
    114107        } 
    115108// COMPUTE RESULTS 
    116         Merger->set_sources(Sources,true); // takes care of deletion of sources 
     109        Merger->set_sources (Sources, true); // takes care of deletion of sources 
    117110        Merger->merge(); 
    118          
     111 
    119112// save results 
    120         Array<vec> source_vals(Sources.length()); 
    121                 for (int i=0;i<Sources.length();i++){ 
    122                         datalink_m2e dl; 
    123                         dl.set_connection(Sources(i)->_rv(), Sources(i)->_rvc(), Merger->_rv()); 
    124                          
    125                         vec ll(Merger->_Smp()._samples().length()); 
    126                         for(int j=0; j<Merger->_Smp()._samples().length(); j++){ 
    127                                 vec dt=dl.pushdown(Merger->_Smp()._samples()(j)); 
    128                                 vec dtc=dl.get_cond(Merger->_Smp()._samples()(j)); 
    129                                 ll(j)=Sources(i)->evallogcond(dt,dtc); 
    130                         } 
     113        Array<vec> source_vals (Sources.length()); 
     114        for (int i = 0;i < Sources.length();i++) { 
     115                datalink_m2e dl; 
     116                dl.set_connection (Sources (i)->_rv(), Sources (i)->_rvc(), Merger->_rv()); 
    131117 
    132                         vec sll = exp(ll); 
    133                          
    134                         source_vals(i)=sll/sum(sll); 
    135                 }                
     118                vec ll (Merger->_Smp()._samples().length()); 
     119                for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { 
     120                        vec dt = dl.pushdown (Merger->_Smp()._samples() (j)); 
     121                        vec dtc = dl.get_cond (Merger->_Smp()._samples() (j)); 
     122                        ll (j) = Sources (i)->evallogcond (dt, dtc); 
     123                } 
    136124 
    137                         vec ll(Merger->_Smp()._samples().length()); 
    138                         for(int j=0; j<Merger->_Smp()._samples().length(); j++){ 
    139                                 ll(j)=Merger->evallog(Merger->_Smp()._samples()(j)); 
    140                         } 
     125                vec sll = exp (ll); 
    141126 
    142                         vec sll = exp(ll); 
    143                          
    144                         vec mix_val=sll/sum(sll); 
    145                  
    146          
     127                source_vals (i) = sll / sum (sll); 
     128        } 
     129 
     130        vec ll (Merger->_Smp()._samples().length()); 
     131        for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { 
     132                ll (j) = Merger->evallog (Merger->_Smp()._samples() (j)); 
     133        } 
     134 
     135        vec sll = exp (ll); 
     136 
     137        vec mix_val = sll / sum (sll); 
     138 
     139 
    147140#ifdef MEX 
    148141        mxArray* tmp ; 
    149142        // Save results 
    150         if (n_output>0){ 
    151                 tmp = mxCreateStructMatrix(1,1,0,NULL); 
     143        if (n_output > 0) { 
     144                tmp = mxCreateStructMatrix (1, 1, 0, NULL); 
    152145                //support 
    153                 Array<vec> &samples=Merger->_Smp()._samples(); 
    154                 if (samples.size()>0){ 
    155                         mxArray* fld=mxCreateDoubleMatrix(samples(0).length(), samples.size(), mxREAL); 
    156                 Arrayvec2mxArray(samples,fld); 
    157                 mxReplaceFieldNM(tmp, "support", fld); 
     146                Array<vec> &samples = Merger->_Smp()._samples(); 
     147                if (samples.size() > 0) { 
     148                        mxArray* fld = mxCreateDoubleMatrix (samples (0).length(), samples.size(), mxREAL); 
     149                        Arrayvec2mxArray (samples, fld); 
     150                        mxReplaceFieldNM (tmp, "support", fld); 
    158151                } 
    159152 
    160153                //weights 
    161154                vec &w = Merger->_Smp()._w(); 
    162                 mxArray* fldw=mxCreateDoubleMatrix(1, w.length(), mxREAL); 
    163                 vec2mxArray(w,fldw); 
    164                 mxReplaceFieldNM(tmp, "weights", fldw); 
    165                  
     155                mxArray* fldw = mxCreateDoubleMatrix (1, w.length(), mxREAL); 
     156                vec2mxArray (w, fldw); 
     157                mxReplaceFieldNM (tmp, "weights", fldw); 
     158 
    166159                //mixture values 
    167                 mxArray* fldm=mxCreateDoubleMatrix(1, w.length(), mxREAL); 
    168                 vec2mxArray(mix_val,fldm); 
    169                 mxReplaceFieldNM(tmp, "mix", fldm); 
     160                mxArray* fldm = mxCreateDoubleMatrix (1, w.length(), mxREAL); 
     161                vec2mxArray (mix_val, fldm); 
     162                mxReplaceFieldNM (tmp, "mix", fldm); 
    170163 
    171164                // sources 
    172165                char srcstr[20]; 
    173                 for (int i=0;i<Sources.length();i++){ 
    174                         sprintf(srcstr,"source%d",i+1); 
    175                         mxArray* fldw=mxCreateDoubleMatrix(1, source_vals(i).length(), mxREAL); 
    176                         vec2mxArray(source_vals(i),fldw); 
    177                         mxReplaceFieldNM(tmp, srcstr, fldw); 
    178                 }                
     166                for (int i = 0;i < Sources.length();i++) { 
     167                        sprintf (srcstr, "source%d", i + 1); 
     168                        mxArray* fldw = mxCreateDoubleMatrix (1, source_vals (i).length(), mxREAL); 
     169                        vec2mxArray (source_vals (i), fldw); 
     170                        mxReplaceFieldNM (tmp, srcstr, fldw); 
     171                } 
    179172 
    180173                output[0] = tmp;