kalman_helper.cc

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00032 #include <iostream>
00033 #include "kalman_helper.h"
00034 #include "fastlib/fastlib.h"
00035 
00036 using namespace std;
00037 
00038 void propagate_one_step(const Matrix& a_mat, const Vector& x, 
00039                           const Vector& w, Vector* v) {
00040   Vector temp_1;
00041   la::MulInit(a_mat, x, &temp_1); 
00042   la::AddTo(w, &temp_1); 
00043   (*v).CopyValues(temp_1);
00044 }; 
00045 
00046 void propagate_one_step(const Matrix& a_mat, const Matrix& b_mat, 
00047                         const Vector& x, const Vector &u, const Vector& w,
00048                         Vector* v) {
00049   Vector temp_1, temp_2;
00050   la::MulInit(a_mat, x, &temp_1); 
00051   la::MulInit(b_mat, u, &temp_2);
00052   la::AddTo(temp_2, &temp_1); 
00053   la::AddTo(w, &temp_1); 
00054   (*v).CopyValues(temp_1);
00055 }; 
00056 
00057 void schur(const Matrix& a_mat, const Matrix& b_mat, const Matrix& c_mat, 
00058            const Matrix& d_mat, Matrix* mat) {
00059   Matrix temp_1, temp_2, temp_3; 
00060   temp_1.Copy(c_mat); la::Inverse(&temp_1); // temp1 = inv(c_mat)
00061   la::MulInit(b_mat, temp_1, &temp_2); // temp_2 = b_mat*inv(c_mat)
00062   la::MulInit(temp_2, d_mat, &temp_3); // temp_3 = b_mat*inv(c_mat)*d_mat
00063   Matrix result; 
00064   result.Copy(a_mat);
00065   la::AddExpert(-1, temp_3, &result); 
00066   (*mat).CopyValues(result);
00067 };
00068 
00069 void print_matrix(const Matrix& a_mat, const char* name)
00070 {
00071   std::cout<<std::endl<<"Printing Matrix.."<<name<<std::endl;;
00072   for (int r = 1; r<=(int)a_mat.n_rows();r++)
00073     {
00074       for (int c=1; c<=(int)a_mat.n_cols();c++)
00075         {
00076           std::cout<<" "<<a_mat.get(r-1, c-1)<<" ";
00077         };
00078       std::cout<<std::endl<<std::endl;
00079     };
00080 };
00081 
00082 void matrix_concatenate_col_init(const Matrix& a_mat, const Matrix& b_mat, 
00083                                  Matrix* x_mat) {
00084   int n_rows = a_mat.n_rows(), n_cols = a_mat.n_cols() + b_mat.n_cols();
00085   (*x_mat).Init(n_rows, n_cols);
00086   
00087   for (int r1 = 0; r1<n_rows; r1++) {
00088     for (int c1 = 0; c1<a_mat.n_cols(); c1++)
00089       {
00090         (*x_mat).set(r1, c1, a_mat.get(r1,c1));
00091       };
00092   };
00093   
00094   for (int r2 = 0; r2<n_rows; r2++) {
00095     for (int c2 = a_mat.n_cols(); c2<n_cols; c2++)
00096       {
00097         (*x_mat).set(r2, c2, b_mat.get(r2, c2-a_mat.n_cols() ));
00098       };
00099   };  
00100 };
00101 
00102 
00103 void matrix_concatenate_row_init(const Matrix& a_mat, const Matrix& b_mat, Matrix* x_mat) {
00104   int n_cols = a_mat.n_rows(), n_rows = a_mat.n_rows() + b_mat.n_rows();
00105   (*x_mat).Init(n_rows, n_cols);
00106   
00107   for (int r1 = 0; r1<a_mat.n_rows(); r1++) {
00108     for (int c1 = 0; c1<n_cols; c1++) {
00109       (*x_mat).set(r1, c1, a_mat.get(r1, c1));
00110     };
00111   };
00112   
00113   for (int r2 = a_mat.n_rows(); r2<n_rows; r2++) {
00114     for (int c2 = 0; c2<n_cols; c2++) {
00115       (*x_mat).set(r2, c2, b_mat.get(r2-a_mat.n_rows(), c2));
00116     };
00117   };  
00118 };
00119 
00120 void extract_sub_matrix_init(const Matrix& a_mat, const int& r_in, 
00121                              const int& r_out, const int& c_in, 
00122                              const int& c_out, Matrix* x_mat) {
00123   (*x_mat).Init(r_out-r_in+1, c_out-c_in +1);
00124   
00125   for (int r = 0; r< (*x_mat).n_rows(); r++) {
00126     for (int c = 0; c< (*x_mat).n_cols(); c++) {
00127       (*x_mat).set(r, c, a_mat.get(r_in +r, c_in +c));
00128     };
00129   };
00130 };
00131 
00132 void extract_sub_vector_of_vector_init(const Vector& v, const int& r_in, 
00133                                        const int& r_out, Vector* x) {
00134   (*x).Init(r_out-r_in+1);
00135   double temp [r_out-r_in+1];
00136   for (int r = 0; r< (*x).length(); r++) {
00137     temp[r] = v.get(r_in+r);
00138   };
00139   (*x).CopyValues(temp); 
00140 };
00141 
00142 void extract_sub_vector_of_vector(const Vector& v, const int& r_in, 
00143                                   const int& r_out, Vector* x) {
00144   double temp [r_out-r_in+1];
00145   for (int r = 0; r< (*x).length(); r++) {
00146     temp[r] = v.get(r_in+r);
00147   };
00148   (*x).CopyValues(temp); 
00149 };
00150 
00151 void set_portion_of_matrix(const Matrix& a_mat, const int& r_in,
00152                            const int& r_out, const int& c_in, 
00153                            const int& c_out, Matrix* x_mat) {
00154   for (int r=r_in; r<=r_out; r++) {
00155     for (int c=c_in; c<=c_out; c++) {
00156       (*x_mat).set(r, c, a_mat.get(r-r_in, c-c_in));
00157     };
00158   };  
00159 };
00160 
00161 void set_portion_of_matrix(const Vector& a, const int& r_in,const int& r_out, 
00162                            const int& c, Matrix* x_mat) {
00163   for (int r=r_in; r<=r_out; r++) {
00164     (*x_mat).set(r, c, a.get(r-r_in));
00165   };  
00166 };
00167 
00168 void RandVector(Vector &v) {  
00169   index_t d = v.length();
00170   v.SetZero();
00171   
00172   for(index_t i = 0; i+1 < d; i+=2) {
00173     double a = drand48();
00174     double b = drand48();
00175     double first_term = sqrt(-2 * log(a));
00176     double second_term = 2 * M_PI * b;
00177     v[i] =   first_term * cos(second_term);
00178     v[i+1] = first_term * sin(second_term);
00179   };
00180   
00181   if((d % 2) == 1) {
00182     v[d - 1] = sqrt(-2 * log(drand48())) * cos(2 * M_PI * drand48());
00183   };  
00184 };
00185 
00186 void RandVector(const Matrix& noise_mat, Vector &v) {  
00187   index_t d = v.length();
00188   v.SetZero();
00189   
00190   for(index_t i = 0; i+1 < d; i+=2) {
00191     double a = drand48();
00192     double b = drand48();
00193     double first_term = sqrt(-2 * log(a));
00194     double second_term = 2 * M_PI * b;
00195     v[i] =   first_term * cos(second_term);
00196     v[i+1] = first_term * sin(second_term);
00197   };
00198   
00199   if((d % 2) == 1) {
00200     v[d - 1] = sqrt(-2 * log(drand48())) * cos(2 * M_PI * drand48());
00201   };
00202   
00203   Matrix noise_mat_rt, noise_mat_rt_trans;   
00204   Vector temp;
00205   
00206   la::CholeskyInit(noise_mat, &noise_mat_rt_trans);
00207   la::TransposeInit(noise_mat_rt_trans, &noise_mat_rt);
00208   la::MulInit(noise_mat_rt, v, &temp);
00209   v.CopyValues(temp);
00210 };
00211 
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3