scaleKernelMatrix.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifndef __clang__
  17. #ifndef __llvm__
  18. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  19. #endif
  20. #endif
  21. Config conf ( argc, argv );
  22. string kernel_in = conf.gS("main", "kernel_in");
  23. string kernel_out = conf.gS("main", "kernel_out");
  24. Vector labels;
  25. Matrix kernelMatrix;
  26. ifstream ifs ( kernel_in.c_str(), ios::in );
  27. if ( ! ifs.good() )
  28. fthrow(Exception, "Unable to read kernel matrix from " << kernel_in );
  29. ifs >> kernelMatrix;
  30. ifs >> labels;
  31. ifs.close();
  32. if ( kernelMatrix.cols() != kernelMatrix.rows() )
  33. {
  34. fthrow(Exception, "Kernel matrix is not quadratic!");
  35. }
  36. Matrix kernelMatrixScaled ( kernelMatrix.cols(), kernelMatrix.rows() );
  37. for ( uint i = 0 ; i < kernelMatrix.cols(); i++ )
  38. for ( uint j = 0 ; j < kernelMatrix.rows(); j++ )
  39. {
  40. double K_ii = kernelMatrix(i,i);
  41. double K_jj = kernelMatrix(j,j);
  42. kernelMatrixScaled(i,j) = kernelMatrix(i,j) / sqrt(K_ii * K_jj);
  43. }
  44. ofstream ofs ( kernel_out.c_str(), ios::out );
  45. if ( ! ofs.good() )
  46. fthrow(Exception, "Unable to write kernel matrix to " << kernel_out );
  47. ofs << kernelMatrixScaled;
  48. ofs << labels;
  49. ofs.close();
  50. return 0;
  51. }