Show
Ignore:
Timestamp:
08/12/09 09:27:40 (15 years ago)
Author:
vbarta
Message:

returning shared pointers from epdf::marginal & epdf::condition; testsuite run leaks down from 8402 to 6510 bytes

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/emix.cpp

    r488 r504  
    33namespace bdm { 
    44 
    5 void emix::set_parameters ( const vec &w0, const Array<epdf*> &Coms0, bool copy ) { 
     5void emix::set_parameters ( const vec &w0, const Array<shared_ptr<epdf> > &Coms0 ) { 
    66        w = w0 / sum ( w0 ); 
    77        dim = Coms0 ( 0 )->dimension(); 
     
    1414                it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" ); 
    1515                it_assert_debug ( !isnamed || tmp_rv.equal ( Coms0 ( i )->_rv() ), "Component RVs do not match!" ); 
     16        } 
     17 
     18        Coms = Coms0; 
     19 
     20        if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv 
     21} 
     22 
     23vec emix::sample() const { 
     24        //Sample which component 
     25        vec cumDist = cumsum ( w ); 
     26        double u0; 
     27#pragma omp critical 
     28        u0 = UniRNG.sample(); 
     29 
     30        int i = 0; 
     31        while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) { 
     32                i++; 
     33        } 
     34 
     35        return Coms ( i )->sample(); 
     36} 
     37 
     38shared_ptr<epdf> emix::marginal ( const RV &rv ) const { 
     39        emix *tmp = new emix(); 
     40        shared_ptr<epdf> narrow(tmp); 
     41        marginal ( rv, *tmp ); 
     42        return narrow; 
     43} 
     44 
     45void emix::marginal ( const RV &rv, emix &target ) const { 
     46        it_assert_debug ( isnamed(), "rvs are not assigned" ); 
     47 
     48        Array<shared_ptr<epdf> > Cn ( Coms.length() ); 
     49        for ( int i = 0; i < Coms.length(); i++ ) { 
     50                Cn ( i ) = Coms ( i )->marginal ( rv ); 
     51        } 
     52 
     53        target.set_parameters ( w, Cn ); 
     54} 
     55 
     56shared_ptr<mpdf> emix::condition ( const RV &rv ) const { 
     57        it_assert_debug ( isnamed(), "rvs are not assigned" ); 
     58        mratio *tmp = new mratio ( this, rv ); 
     59        return shared_ptr<mpdf>(tmp); 
     60} 
     61 
     62void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) { 
     63        w = w0 / sum ( w0 ); 
     64        dim = Coms0 ( 0 )->dimension(); 
     65        int i; 
     66        for ( i = 0; i < w.length(); i++ ) { 
     67                it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" ); 
    1668        } 
    1769        if ( copy ) { 
     
    2678                destroyComs = false; 
    2779        } 
    28         if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv 
    29 } 
    30  
    31 vec emix::sample() const { 
    32         //Sample which component 
    33         vec cumDist = cumsum ( w ); 
    34         double u0; 
    35 #pragma omp critical 
    36         u0 = UniRNG.sample(); 
    37  
    38         int i = 0; 
    39         while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) { 
    40                 i++; 
    41         } 
    42  
    43         return Coms ( i )->sample(); 
    44 } 
    45  
    46 emix* emix::marginal ( const RV &rv ) const { 
    47         it_assert_debug ( isnamed(), "rvs are not assigned" ); 
    48  
    49         Array<epdf*> Cn ( Coms.length() ); 
    50         for ( int i = 0; i < Coms.length(); i++ ) { 
    51                 Cn ( i ) = Coms ( i )->marginal ( rv ); 
    52         } 
    53         emix* tmp = new emix(); 
    54         tmp->set_parameters ( w, Cn, false ); 
    55         tmp->ownComs(); 
    56         return tmp; 
    57 } 
    58  
    59 mratio* emix::condition ( const RV &rv ) const { 
    60         it_assert_debug ( isnamed(), "rvs are not assigned" ); 
    61         return new mratio ( this, rv ); 
    62 }; 
    63  
    64 void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) { 
    65         w = w0 / sum ( w0 ); 
    66         dim = Coms0 ( 0 )->dimension(); 
    67         int i; 
    68         for ( i = 0; i < w.length(); i++ ) { 
    69                 it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" ); 
    70         } 
    71         if ( copy ) { 
    72                 Coms.set_length ( Coms0.length() ); 
    73                 for ( i = 0; i < w.length(); i++ ) { 
    74                         it_error ( "Not imp..." ); 
    75                         *Coms ( i ) = *Coms0 ( i ); 
    76                 } 
    77                 destroyComs = true; 
    78         } else { 
    79                 Coms = Coms0; 
    80                 destroyComs = false; 
    81         } 
    8280} 
    8381 
     
    118116} 
    119117 
    120 emix* egiwmix::marginal ( const RV &rv ) const { 
     118shared_ptr<epdf> egiwmix::marginal ( const RV &rv ) const { 
     119        emix *tmp = new emix(); 
     120        shared_ptr<epdf> narrow(tmp); 
     121        marginal ( rv, *tmp ); 
     122        return narrow; 
     123} 
     124 
     125void egiwmix::marginal ( const RV &rv, emix &target ) const { 
    121126        it_assert_debug ( isnamed(), "rvs are not assigned" ); 
    122127 
    123         Array<epdf*> Cn ( Coms.length() ); 
     128        Array<shared_ptr<epdf> > Cn ( Coms.length() ); 
    124129        for ( int i = 0; i < Coms.length(); i++ ) { 
    125130                Cn ( i ) = Coms ( i )->marginal ( rv ); 
    126131        } 
    127         emix* tmp = new emix(); 
    128         tmp->set_parameters ( w, Cn, false ); 
    129         tmp->ownComs(); 
    130         return tmp; 
     132 
     133        target.set_parameters ( w, Cn ); 
    131134} 
    132135