]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Algorithms/IterativeGaussianModelEstimator.hxx
Generic gaussian model estimator added
[cpPlugins.git] / lib / cpPlugins / Extensions / Algorithms / IterativeGaussianModelEstimator.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__
7
8 #include <cstdarg>
9
10 // -------------------------------------------------------------------------
11 template< class S, unsigned int D >
12 template< class V, class M >
13 void
14 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
15 GetModel( V& m, M& E ) const
16 {
17   TMatrix Es = ( this->m_S1 * this->m_S1.transpose( ) );
18
19   for( unsigned int i = 0; i < D; ++i )
20   {
21     m[ i ] = this->m_S1[ i ][ 0 ];
22     if( this->m_N > 0 )
23       m[ i ] /= double( this->m_N );
24
25     for( unsigned int j = 0; j < D; ++j )
26     {
27       E[ i ][ j ] = double( 0 );
28       if( this->m_N > 0 )
29         E[ i ][ j ] -= double( Es[ i ][ j ] / S( this->m_N ) );
30       if( this->m_N > 1 )
31       {
32         E[ i ][ j ] += double( this->m_S2[ i ][ j ] );
33         E[ i ][ j ] /= double( this->m_N - 1 );
34
35       } // fi
36
37     } // rof
38
39   } // rof
40 }
41
42 // -------------------------------------------------------------------------
43 template< class S, unsigned int D >
44 void
45 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
46 Clear( )
47 {
48   this->m_N = 0;
49   this->m_S1.set_size( D, 1 );
50   this->m_S2.set_size( D, D );
51   this->m_S1.fill( S( 0 ) );
52   this->m_S2.fill( S( 0 ) );
53   this->Modified( );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class S, unsigned int D >
58 template< class V >
59 void
60 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
61 AddSample( const V& sample )
62 {
63   for( unsigned int i = 0; i < D; ++i )
64   {
65     this->m_S1[ i ][ 0 ] += S( sample[ i ] );
66     for( unsigned int j = i; j < D; ++j )
67     {
68       this->m_S2[ i ][ j ] += S( sample[ i ] ) * S( sample[ j ] );
69       this->m_S2[ j ][ i ] = this->m_S2[ i ][ j ];
70
71     } // rof
72
73   } // rof
74   this->m_N++;
75   this->Modified( );
76 }
77
78 // -------------------------------------------------------------------------
79 template< class S, unsigned int D >
80 void
81 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
82 AddSample( const S& s_x, const S& s_y, ... )
83 {
84   static S sample[ D ];
85
86   std::va_list args_lst;
87   va_start( args_lst, s_y );
88   sample[ 0 ] = s_x;
89   sample[ 1 ] = s_y;
90   for( unsigned int d = 2; d < D; ++d )
91     sample[ d ] = S( va_arg( args_lst, double ) );
92   va_end( args_lst );
93   this->AddSample( sample );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class S, unsigned int D >
98 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
99 IterativeGaussianModelEstimator( )
100   : Superclass( )
101 {
102   this->Clear( );
103 }
104
105 // -------------------------------------------------------------------------
106 template< class S, unsigned int D >
107 cpPlugins::Extensions::Algorithms::IterativeGaussianModelEstimator< S, D >::
108 ~IterativeGaussianModelEstimator( )
109 {
110 }
111
112 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__ITERATIVEGAUSSIANMODELESTIMATOR__HXX__
113
114 // eof - $RCSfile$