Show
Ignore:
Timestamp:
09/16/09 22:52:57 (15 years ago)
Author:
smidl
Message:

astyle

Files:
1 modified

Legend:

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

    r569 r622  
    2929 
    3030#ifdef MEX 
    31 void mexFunction (int n_output, mxArray *output[], int n_input, const mxArray *input[]) 
    32 { 
     31void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { 
    3332        // 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        if ( n_input != 3 ) mexErrMsgTxt ( "Usage:\n" 
     34                                                   "result=merger(sources, support, merger)\n" 
     35                                                   "  sources= { struct('class','epdf'),... };  % cell of pdfs (epdfs or mpdfs) to be merged,\n" 
     36                                                   "  support= struct(\n" 
     37                                                   "           grid    = {[dim1_start,dim1_end], [dim2_start, dim2_end]...}  %support boundary \n" 
     38                                                   "           nbins   = [bins_in_dim1, bins_in_dim2,...]                    %fixed \n" 
     39                                                   "         === OR ==\n" 
     40                                                   "           pdf     = struct('class','epdf'); % pdf to draw samples from\n" 
     41                                                   "           nsamples= 100;                    % number of samples\n" 
     42                                                   "           );\n" 
     43                                                   "        If all elements are present,  (grid,nbins) is used;\n" 
     44                                                   "  merger = struct('class','merger_*');       % object to be used for merging,\n\n" 
     45                                                   "see documentation of classes epdf, mpdf, merger_base and their offsprings in BDM." ); 
    4746        RV::clear_all(); 
    4847        // LOAD CONFIG 
    4948        UImxArray Cfg; 
    50         Cfg.addList (input[0], "Sources"); 
    51         Cfg.addGroup (input[1], "Support"); 
    52         Cfg.addGroup (input[2], "Merger"); 
     49        Cfg.addList ( input[0], "Sources" ); 
     50        Cfg.addGroup ( input[1], "Support" ); 
     51        Cfg.addGroup ( input[2], "Merger" ); 
    5352 
    5453        //DBG 
    55         Cfg.writeFile ("merger.cfg"); 
     54        Cfg.writeFile ( "merger.cfg" ); 
    5655#else 
    5756int main() { 
    58         UIFile Cfg ("merger.cfg"); 
     57        UIFile Cfg ( "merger.cfg" ); 
    5958#endif 
    6059        // Sources 
    6160        Array<shared_ptr<mpdf> > Sources; 
    6261        //abuse Mer to store sources 
    63         Setting& _Sources = Cfg.lookup ("Sources"); 
     62        Setting& _Sources = Cfg.lookup ( "Sources" ); 
    6463        int Slen = _Sources.getLength(); 
    65         Sources.set_size (Slen); 
    66         for (int i = 0; i < Slen; i++) { 
     64        Sources.set_size ( Slen ); 
     65        for ( int i = 0; i < Slen; i++ ) { 
    6766                try { 
    68                         shared_ptr<mpdf> mtmp = UI::build<mpdf> (_Sources, i); 
    69                         Sources (i) = mtmp; 
    70                 } catch (UIException) { 
     67                        shared_ptr<mpdf> mtmp = UI::build<mpdf> ( _Sources, i ); 
     68                        Sources ( i ) = mtmp; 
     69                } catch ( UIException ) { 
    7170                        // it is not mpdf - see if it is epdf 
    7271                        try { 
    73                                 shared_ptr<epdf> etmp = UI::build<epdf> (_Sources, i); 
    74                                 if (etmp) { 
    75                                         Sources (i) = new mepdf (etmp); // hopefully OK 
     72                                shared_ptr<epdf> etmp = UI::build<epdf> ( _Sources, i ); 
     73                                if ( etmp ) { 
     74                                        Sources ( i ) = new mepdf ( etmp ); // hopefully OK 
    7675                                } 
    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()); 
     76                        } catch ( UIException &e ) { 
     77                                it_error ( "No mpdfs or epdfs found! " + string ( e.what() ) ); 
     78                        } catch ( std::exception e ) { 
     79                                it_error ( "Error in UI at " + _Sources[i].getPath() ); 
    8180                        } 
    82                 } catch (std::exception &e) { 
    83                         it_error ("Error in UI at " + _Sources[i].getPath()); 
     81                } catch ( std::exception &e ) { 
     82                        it_error ( "Error in UI at " + _Sources[i].getPath() ); 
    8483                } 
    8584        } 
    8685 
    87         shared_ptr<merger_base> Merger = UI::build<merger_base> (Cfg, "Merger"); 
     86        shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" ); 
    8887 
    8988        // Support 
    9089        try { 
    91                 shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> (Cfg, "Support"); 
    92                 Merger->set_support(*RecSup); 
    93         } 
    94         catch (UIException &e) { 
    95                 shared_ptr<discrete_support> DisSup = UI::build<discrete_support> (Cfg, "Support"); 
    96                 Merger->set_support (*DisSup); 
     90                shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ); 
     91                Merger->set_support ( *RecSup ); 
     92        } catch ( UIException &e ) { 
     93                shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support" ); 
     94                Merger->set_support ( *DisSup ); 
    9795        } 
    9896// COMPUTE RESULTS 
    99         Merger->set_sources (Sources);  
     97        Merger->set_sources ( Sources ); 
    10098        Merger->merge(); 
    10199 
    102100// save results 
    103         Array<vec> source_vals (Sources.length()); 
    104         for (int i = 0;i < Sources.length();i++) { 
     101        Array<vec> source_vals ( Sources.length() ); 
     102        for ( int i = 0;i < Sources.length();i++ ) { 
    105103                datalink_m2e dl; 
    106                 dl.set_connection (Sources (i)->_rv(), Sources (i)->_rvc(), Merger->_rv()); 
     104                dl.set_connection ( Sources ( i )->_rv(), Sources ( i )->_rvc(), Merger->_rv() ); 
    107105 
    108                 vec ll (Merger->_Smp()._samples().length()); 
    109                 for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { 
    110                         vec dt = dl.pushdown (Merger->_Smp()._samples() (j)); 
    111                         vec dtc = dl.get_cond (Merger->_Smp()._samples() (j)); 
    112                         ll (j) = Sources (i)->evallogcond (dt, dtc); 
     106                vec ll ( Merger->_Smp()._samples().length() ); 
     107                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) { 
     108                        vec dt = dl.pushdown ( Merger->_Smp()._samples() ( j ) ); 
     109                        vec dtc = dl.get_cond ( Merger->_Smp()._samples() ( j ) ); 
     110                        ll ( j ) = Sources ( i )->evallogcond ( dt, dtc ); 
    113111                } 
    114112 
    115                 vec sll = exp (ll); 
     113                vec sll = exp ( ll ); 
    116114 
    117                 source_vals (i) = sll / sum (sll); 
     115                source_vals ( i ) = sll / sum ( sll ); 
    118116        } 
    119117 
    120         merger_mix* MerMix=dynamic_cast<merger_mix*>(Merger.get()); 
     118        merger_mix* MerMix=dynamic_cast<merger_mix*> ( Merger.get() ); 
    121119        vec mix_val; 
    122                          
    123         if (MerMix){ 
    124                 vec ll (Merger->_Smp()._samples().length()); 
    125                 for (int j = 0; j < Merger->_Smp()._samples().length(); j++) { 
    126                         ll (j) = Merger->evallog (Merger->_Smp()._samples() (j)); 
     120 
     121        if ( MerMix ) { 
     122                vec ll ( Merger->_Smp()._samples().length() ); 
     123                for ( int j = 0; j < Merger->_Smp()._samples().length(); j++ ) { 
     124                        ll ( j ) = Merger->evallog ( Merger->_Smp()._samples() ( j ) ); 
    127125                } 
    128          
    129                 vec sll = exp (ll); 
    130          
    131                 mix_val = sll / sum (sll); 
     126 
     127                vec sll = exp ( ll ); 
     128 
     129                mix_val = sll / sum ( sll ); 
    132130        } 
    133131 
     
    135133        mxArray* tmp ; 
    136134        // Save results 
    137         if (n_output > 0) { 
    138                 tmp = mxCreateStructMatrix (1, 1, 0, NULL); 
     135        if ( n_output > 0 ) { 
     136                tmp = mxCreateStructMatrix ( 1, 1, 0, NULL ); 
    139137                //support 
    140138                Array<vec> &samples = Merger->_Smp()._samples(); 
    141                 if (samples.size() > 0) { 
    142                         mxArray* fld = mxCreateDoubleMatrix (samples (0).length(), samples.size(), mxREAL); 
    143                         Arrayvec2mxArray (samples, fld); 
    144                         mxReplaceFieldNM (tmp, "support", fld); 
     139                if ( samples.size() > 0 ) { 
     140                        mxArray* fld = mxCreateDoubleMatrix ( samples ( 0 ).length(), samples.size(), mxREAL ); 
     141                        Arrayvec2mxArray ( samples, fld ); 
     142                        mxReplaceFieldNM ( tmp, "support", fld ); 
    145143                } 
    146144 
    147145                //weights 
    148146                vec &w = Merger->_Smp()._w(); 
    149                 mxArray* fldw = mxCreateDoubleMatrix (1, w.length(), mxREAL); 
    150                 vec2mxArray (w, fldw); 
    151                 mxReplaceFieldNM (tmp, "weights", fldw); 
     147                mxArray* fldw = mxCreateDoubleMatrix ( 1, w.length(), mxREAL ); 
     148                vec2mxArray ( w, fldw ); 
     149                mxReplaceFieldNM ( tmp, "weights", fldw ); 
    152150 
    153151                //mixture values 
    154                 if (mix_val.length()>0){ 
    155                         mxArray* fldm = mxCreateDoubleMatrix (1, w.length(), mxREAL); 
    156                         vec2mxArray (mix_val, fldm); 
    157                         mxReplaceFieldNM (tmp, "mix", fldm); 
     152                if ( mix_val.length() >0 ) { 
     153                        mxArray* fldm = mxCreateDoubleMatrix ( 1, w.length(), mxREAL ); 
     154                        vec2mxArray ( mix_val, fldm ); 
     155                        mxReplaceFieldNM ( tmp, "mix", fldm ); 
    158156                } 
    159157                // sources 
    160158                char srcstr[20]; 
    161                 for (int i = 0;i < Sources.length();i++) { 
    162                         sprintf (srcstr, "source%d", i + 1); 
    163                         mxArray* fldw = mxCreateDoubleMatrix (1, source_vals (i).length(), mxREAL); 
    164                         vec2mxArray (source_vals (i), fldw); 
    165                         mxReplaceFieldNM (tmp, srcstr, fldw); 
     159                for ( int i = 0;i < Sources.length();i++ ) { 
     160                        sprintf ( srcstr, "source%d", i + 1 ); 
     161                        mxArray* fldw = mxCreateDoubleMatrix ( 1, source_vals ( i ).length(), mxREAL ); 
     162                        vec2mxArray ( source_vals ( i ), fldw ); 
     163                        mxReplaceFieldNM ( tmp, srcstr, fldw ); 
    166164                } 
    167165