Skip to content

Commit

Permalink
precomputed eigenvalue set added in eigensolver (log-det)
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday committed Sep 5, 2013
1 parent 94c1d75 commit c54bd1d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/shogun/mathematics/logdet/DirectEigenSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ CDirectEigenSolver::~CDirectEigenSolver()

void CDirectEigenSolver::compute()
{
if (m_is_computed_min && m_is_computed_max)
{
SG_DEBUG("Minimum/maximum eigenvalues are already computed, exiting\n");
return;
}

CDenseMatrixOperator<float64_t>* op
=dynamic_cast<CDenseMatrixOperator<float64_t>*>(m_linear_operator);
REQUIRE(op, "Linear operator is not of CDenseMatrixOperator type!\n");
Expand All @@ -51,6 +57,9 @@ void CDirectEigenSolver::compute()
SelfAdjointEigenSolver<MatrixXd> eig_solver(map_m);
m_min_eigenvalue=eig_solver.eigenvalues()[0];
m_max_eigenvalue=eig_solver.eigenvalues()[op->get_dimension()-1];

m_is_computed_min=true;
m_is_computed_max=false;
}

}
Expand Down
31 changes: 31 additions & 0 deletions src/shogun/mathematics/logdet/EigenSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ class CEigenSolver : public CSGObject
*/
virtual void compute() = 0;

/** sets the min eigelvalue of a real valued self-adjoint linear operator */
void set_min_eigenvalue(float64_t min_eigenvalue)
{
m_min_eigenvalue=min_eigenvalue;
m_is_computed_min=true;
}

/** @return min eigenvalue of a real valued self-adjoint linear operator */
const float64_t get_min_eigenvalue() const
{
return m_min_eigenvalue;
}

/** sets the max eigelvalue of a real valued self-adjoint linear operator */
void set_max_eigenvalue(float64_t max_eigenvalue)
{
m_max_eigenvalue=max_eigenvalue;
m_is_computed_max=true;
}

/** @return max eigenvalue of a real valued self-adjoint linear operator */
const float64_t get_max_eigenvalue() const
{
Expand All @@ -83,13 +97,21 @@ class CEigenSolver : public CSGObject
/** the linear solver whose eigenvalues have to be found */
CLinearOperator<float64_t>* m_linear_operator;

/** flag that denotes that the minimum eigenvalue is already computed */
bool m_is_computed_min;

/** flag that denotes that the maximum eigenvalue is already computed */
bool m_is_computed_max;

private:
/** initialize with default values and register params */
void init()
{
m_min_eigenvalue=0.0;
m_max_eigenvalue=0.0;
m_linear_operator=NULL;
m_is_computed_min=false;
m_is_computed_max=false;

SG_ADD(&m_min_eigenvalue, "min_eigenvalue",
"Minimum eigenvalue of a real valued self-adjoint linear operator",
Expand All @@ -102,6 +124,15 @@ class CEigenSolver : public CSGObject
SG_ADD((CSGObject**)&m_linear_operator, "linear_operator",
"Self-adjoint linear operator",
MS_NOT_AVAILABLE);

SG_ADD(&m_is_computed_min, "is_computed_min",
"Flag denoting that the minimum eigenvalue has already been computed",
MS_NOT_AVAILABLE);

SG_ADD(&m_max_eigenvalue, "is_computed_max",
"Flag denoting that the maximum eigenvalue has already been computed",
MS_NOT_AVAILABLE);

}
};

Expand Down
53 changes: 37 additions & 16 deletions src/shogun/mathematics/logdet/LanczosEigenSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ void CLanczosEigenSolver::compute()
{
SG_DEBUG("Entering\n");

if (m_is_computed_min && m_is_computed_max)
{
SG_DEBUG("Minimum/maximum eigenvalues are already computed, exiting\n");
return;
}

REQUIRE(m_linear_operator, "Operator is NULL!\n");

// vector v_0
Expand Down Expand Up @@ -123,25 +129,40 @@ void CLanczosEigenSolver::compute()
std::vector<float64_t> alpha_orig=alpha;
std::vector<float64_t> beta_orig=beta;

// computing min eigenvalue
wrap_dstemr('N', 'I', alpha.size(), &alpha[0], &beta[0],
0.0, 0.0, 1, 1, &M, w.vector, NULL, 1, 1, NULL, 0.0, &info);

m_min_eigenvalue=w[0];

// computing max eigenvalue
wrap_dstemr('N', 'I', alpha_orig.size(), &alpha_orig[0], &beta_orig[0],
0.0, 0.0, w.vlen, w.vlen, &M, w.vector, NULL, 1, 1, NULL, 0.0, &info);

m_max_eigenvalue=w[0];
if (!m_is_computed_min)
{
// computing min eigenvalue
wrap_dstemr('N', 'I', alpha.size(), &alpha[0], &beta[0],
0.0, 0.0, 1, 1, &M, w.vector, NULL, 1, 1, NULL, 0.0, &info);

if (info==0)
{
SG_DEBUG("Iteration took %ld times, residual norm=%.20lf\n",
it.get_iter_info().iteration_count, it.get_iter_info().residual_norm);

m_min_eigenvalue=w[0];
m_is_computed_min=true;
}
else
SG_WARNING("Some error occured while computing eigenvalues!\n");
}

if (info==0)
if (!m_is_computed_max)
{
SG_DEBUG("Iteration took %ld times, residual norm=%.20lf\n",
it.get_iter_info().iteration_count, it.get_iter_info().residual_norm);
// computing max eigenvalue
wrap_dstemr('N', 'I', alpha_orig.size(), &alpha_orig[0], &beta_orig[0],
0.0, 0.0, w.vlen, w.vlen, &M, w.vector, NULL, 1, 1, NULL, 0.0, &info);

if (info==0)
{
SG_DEBUG("Iteration took %ld times, residual norm=%.20lf\n",
it.get_iter_info().iteration_count, it.get_iter_info().residual_norm);
m_max_eigenvalue=w[0];
m_is_computed_max=true;
}
else
SG_WARNING("Some error occured while computing eigenvalues!\n");
}
else
SG_WARNING("Some error occured while computing eigenvalues!\n");

SG_DEBUG("Leaving\n");
}
Expand Down
1 change: 1 addition & 0 deletions src/shogun/mathematics/logdet/LanczosEigenSolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class CLanczosEigenSolver : public CEigenSolver

/** register params and initialize with default values */
void init();

};

}
Expand Down

0 comments on commit c54bd1d

Please sign in to comment.