SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2013 Soumyajit De 00008 */ 00009 00010 #include <shogun/lib/config.h> 00011 00012 #ifdef HAVE_EIGEN3 00013 #include <shogun/lib/SGMatrix.h> 00014 #include <shogun/mathematics/eigen3.h> 00015 #include <shogun/mathematics/linalg/linop/DenseMatrixOperator.h> 00016 #include <shogun/mathematics/linalg/eigsolver/DirectEigenSolver.h> 00017 00018 using namespace Eigen; 00019 00020 namespace shogun 00021 { 00022 00023 CDirectEigenSolver::CDirectEigenSolver() 00024 : CEigenSolver() 00025 { 00026 SG_GCDEBUG("%s created (%p)\n", this->get_name(), this) 00027 } 00028 00029 CDirectEigenSolver::CDirectEigenSolver( 00030 CDenseMatrixOperator<float64_t>* linear_operator) 00031 : CEigenSolver((CLinearOperator<float64_t>*)linear_operator) 00032 { 00033 SG_GCDEBUG("%s created (%p)\n", this->get_name(), this) 00034 } 00035 00036 CDirectEigenSolver::~CDirectEigenSolver() 00037 { 00038 SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this) 00039 } 00040 00041 void CDirectEigenSolver::compute() 00042 { 00043 if (m_is_computed_min && m_is_computed_max) 00044 { 00045 SG_DEBUG("Minimum/maximum eigenvalues are already computed, exiting\n"); 00046 return; 00047 } 00048 00049 CDenseMatrixOperator<float64_t>* op 00050 =dynamic_cast<CDenseMatrixOperator<float64_t>*>(m_linear_operator); 00051 REQUIRE(op, "Linear operator is not of CDenseMatrixOperator type!\n"); 00052 00053 SGMatrix<float64_t> m=op->get_matrix_operator(); 00054 Map<MatrixXd> map_m(m.matrix, m.num_rows, m.num_cols); 00055 00056 // compute the eigenvalues with Eigen3 00057 SelfAdjointEigenSolver<MatrixXd> eig_solver(map_m); 00058 m_min_eigenvalue=eig_solver.eigenvalues()[0]; 00059 m_max_eigenvalue=eig_solver.eigenvalues()[op->get_dimension()-1]; 00060 00061 m_is_computed_min=true; 00062 m_is_computed_max=false; 00063 } 00064 00065 } 00066 #endif // HAVE_EIGEN3