00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef TOON_INCLUDE_WLS_H
00031 #define TOON_INCLUDE_WLS_H
00032
00033 #include <TooN/TooN.h>
00034 #include <TooN/Cholesky.h>
00035 #include <TooN/helpers.h>
00036
00037 #include <cmath>
00038
00039 namespace TooN {
00040
00046 template <int Size=Dynamic, class Precision=double,
00047 template<int Size, class Precision> class Decomposition = Cholesky>
00048 class WLS {
00049 public:
00050
00052 WLS(int size=0) :
00053 my_C_inv(size,size),
00054 my_vector(size),
00055 my_decomposition(size),
00056 my_mu(size)
00057 {
00058 clear();
00059 }
00060
00062 void clear(){
00063 my_C_inv = Zeros;
00064 my_vector = Zeros;
00065 }
00066
00070 void add_prior(Precision val){
00071 for(int i=0; i<my_C_inv.num_rows(); i++){
00072 my_C_inv(i,i)+=val;
00073 }
00074 }
00075
00079 template<class B2>
00080 void add_prior(const Vector<Size,Precision,B2>& v){
00081 SizeMismatch<Size,Size>::test(my_C_inv.num_rows(), v.size());
00082 for(int i=0; i<my_C_inv.num_rows(); i++){
00083 my_C_inv(i,i)+=v[i];
00084 }
00085 }
00086
00090 template<class B2>
00091 void add_prior(const Matrix<Size,Size,Precision,B2>& m){
00092 my_C_inv+=m;
00093 }
00094
00099 template<class B2>
00100 inline void add_mJ(Precision m, const Vector<Size, Precision, B2>& J, Precision weight = 1) {
00101
00102
00103 for(int r=0; r < my_C_inv.num_rows(); r++)
00104 {
00105 double Jw = weight * J[r];
00106 my_vector[r] += m * Jw;
00107 for(int c=r; c < my_C_inv.num_rows(); c++)
00108 my_C_inv[r][c] += Jw * J[c];
00109 }
00110 }
00111
00116 template<int N, class B1, class B2, class B3>
00117 inline void add_mJ(const Vector<N,Precision,B1>& m,
00118 const Matrix<Size,N,Precision,B2>& J,
00119 const Matrix<N,N,Precision,B3>& invcov){
00120 Matrix<Size,N,Precision> temp = J * invcov;
00121 my_C_inv += temp * J.T();
00122 my_vector += temp * m;
00123 }
00124
00125
00128 void compute(){
00129
00130
00131 for(int r=1; r < my_C_inv.num_rows(); r++)
00132 for(int c=0; c < r; c++)
00133 my_C_inv[r][c] = my_C_inv[c][r];
00134
00135 my_decomposition.compute(my_C_inv);
00136 my_mu=my_decomposition.backsub(my_vector);
00137 }
00138
00141 void operator += (const WLS& meas){
00142 my_vector+=meas.my_vector;
00143 my_C_inv += meas.my_C_inv;
00144 }
00145
00147 Matrix<Size,Size,Precision>& get_C_inv() {return my_C_inv;}
00149 const Matrix<Size,Size,Precision>& get_C_inv() const {return my_C_inv;}
00150 Vector<Size,Precision>& get_mu(){return my_mu;}
00151 const Vector<Size,Precision>& get_mu() const {return my_mu;}
00152 Vector<Size,Precision>& get_vector(){return my_vector;}
00153 const Vector<Size,Precision>& get_vector() const {return my_vector;}
00154 Decomposition<Size,Precision>& get_decomposition(){return my_decomposition;}
00155 const Decomposition<Size,Precision>& get_decomposition() const {return my_decomposition;}
00156
00157
00158 private:
00159 Matrix<Size,Size,Precision> my_C_inv;
00160 Vector<Size,Precision> my_vector;
00161 Decomposition<Size,Precision> my_decomposition;
00162 Vector<Size,Precision> my_mu;
00163
00164
00165 WLS( WLS& copyof );
00166 int operator = ( WLS& copyof );
00167 };
00168
00169 }
00170
00171 #endif