scaleKernelMatrix.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @file scaleKernelMatrix.cpp
  3. * @brief scale a kernel matrix
  4. * @author Erik Rodner
  5. * @date 12/21/2009
  6. */
  7. #include "core/basics/Config.h"
  8. #include "core/vector/MatrixT.h"
  9. using namespace std;
  10. using namespace NICE;
  11. /**
  12. scale a kernel matrix
  13. */
  14. int main (int argc, char **argv)
  15. {
  16. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  17. Config conf ( argc, argv );
  18. string kernel_in = conf.gS("main", "kernel_in");
  19. string kernel_out = conf.gS("main", "kernel_out");
  20. Vector labels;
  21. Matrix kernelMatrix;
  22. ifstream ifs ( kernel_in.c_str(), ios::in );
  23. if ( ! ifs.good() )
  24. fthrow(Exception, "Unable to read kernel matrix from " << kernel_in );
  25. ifs >> kernelMatrix;
  26. ifs >> labels;
  27. ifs.close();
  28. if ( kernelMatrix.cols() != kernelMatrix.rows() )
  29. {
  30. fthrow(Exception, "Kernel matrix is not quadratic!");
  31. }
  32. Matrix kernelMatrixScaled ( kernelMatrix.cols(), kernelMatrix.rows() );
  33. for ( uint i = 0 ; i < kernelMatrix.cols(); i++ )
  34. for ( uint j = 0 ; j < kernelMatrix.rows(); j++ )
  35. {
  36. double K_ii = kernelMatrix(i,i);
  37. double K_jj = kernelMatrix(j,j);
  38. kernelMatrixScaled(i,j) = kernelMatrix(i,j) / sqrt(K_ii * K_jj);
  39. }
  40. ofstream ofs ( kernel_out.c_str(), ios::out );
  41. if ( ! ofs.good() )
  42. fthrow(Exception, "Unable to write kernel matrix to " << kernel_out );
  43. ofs << kernelMatrixScaled;
  44. ofs << labels;
  45. ofs.close();
  46. return 0;
  47. }