kernel_matrix_generator.h

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 "fastlib/fastlib.h"
00033 
00034 int main(int argc, char *argv[]) {
00035 
00036   // initialize FastExec (parameter handling stuff)
00037   fx_init(argc, argv, &kde_main_doc);
00038   
00039   Matrix references;
00040   const char *references_file_name = fx_param_str_req(fx_root, "data");
00041   double bandwidth = fx_param_double_req(fx_root, "bandwidth");
00042   data::Load(references_file_name, &references);
00043   
00044   // Kernel matrix to be outputted.
00045   Matrix kernel_matrix;
00046 
00047   // Initialize the kernel.
00048   GaussianKernel kernel;
00049   kernel.Init(bandwidth);
00050 
00051   kernel_matrix.Init(references.n_cols(), references.n_cols());
00052   for(index_t r = 0; r < references.n_cols(); r++) {
00053     const double *r_col = references.GetColumnPtr(r);
00054 
00055     for(index_t q = 0; q < references.n_cols(); q++) {
00056       
00057       double dsqd = la::DistanceSqEuclidean(references.n_rows(), q_col,
00058                                             r_col);
00059       const double *q_col = references.GetColumnPtr(q);
00060       kernel_matrix.set(q, r, kernel.EvalUnnormOnSq(dsqd));
00061     }
00062   }
00063 
00064   // Output the matrix.
00065   const char *file_name = "kernel_matrix.txt";
00066   FILE *output_file = fopen(file_name, "w+");
00067   for(index_t r = 0; r < references.n_cols(); r++) {
00068     for(index_t c = 0; c < references.n_cols(); c++) {
00069       fprintf(output_file, "%g ", kernel_matrix.get(c, r));
00070     }
00071     fprintf(output_file, "\n");
00072   }
00073 
00074   fx_done(fx_root);
00075   return 0;
00076 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3