SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
RationalApproximationIndividualJob.cpp
Go to the documentation of this file.
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/SGVector.h>
00014 #include <shogun/lib/SGMatrix.h>
00015 #include <shogun/lib/computation/jobresult/VectorResult.h>
00016 #include <shogun/mathematics/eigen3.h>
00017 #include <shogun/mathematics/linalg/linop/LinearOperator.h>
00018 #include <shogun/mathematics/linalg/linsolver/LinearSolver.h>
00019 #include <shogun/mathematics/linalg/ratapprox/logdet/computation/job/RationalApproximationIndividualJob.h>
00020 #include <shogun/base/Parameter.h>
00021 
00022 using namespace Eigen;
00023 
00024 namespace shogun
00025 {
00026 
00027 CRationalApproximationIndividualJob::CRationalApproximationIndividualJob()
00028     : CIndependentJob()
00029 {
00030     init();
00031 
00032     SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
00033 }
00034 
00035 CRationalApproximationIndividualJob::CRationalApproximationIndividualJob(
00036     CJobResultAggregator* aggregator,
00037     CLinearSolver<complex128_t, float64_t>* linear_solver,
00038     CLinearOperator<complex128_t>* linear_operator,
00039     SGVector<float64_t> vector,
00040     complex128_t weight)
00041     : CIndependentJob(aggregator)
00042 {
00043     init();
00044 
00045     m_linear_solver=linear_solver;
00046     SG_REF(m_linear_solver);
00047 
00048     m_operator=linear_operator;
00049     SG_REF(m_operator);
00050 
00051     m_vector=vector;
00052 
00053     m_weight=weight;
00054 
00055     SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
00056 }
00057 
00058 void CRationalApproximationIndividualJob::init()
00059 {
00060     m_linear_solver=NULL;
00061     m_operator=NULL;
00062     m_weight=complex128_t(0.0);
00063 
00064     SG_ADD((CSGObject**)&m_linear_solver, "linear_solver",
00065         "Linear solver for complex system", MS_NOT_AVAILABLE);
00066 
00067     SG_ADD((CSGObject**)&m_operator, "shifted_operator",
00068         "Shifted linear operator", MS_NOT_AVAILABLE);
00069 
00070     SG_ADD(&m_vector, "trace_sample",
00071         "Sample vector to apply linear operator on", MS_NOT_AVAILABLE);
00072 
00073     SG_ADD(&m_weight, "complex_weight",
00074         "Weight to be multiplied to the solution vector", MS_NOT_AVAILABLE);
00075 }
00076 
00077 CRationalApproximationIndividualJob::~CRationalApproximationIndividualJob()
00078 {
00079     SG_UNREF(m_linear_solver);
00080     SG_UNREF(m_operator);
00081 
00082     SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
00083 }
00084 
00085 void CRationalApproximationIndividualJob::compute()
00086 {
00087     REQUIRE(m_aggregator, "Job result aggregator is not set!\n");
00088     REQUIRE(m_operator, "Operator is not set!\n");
00089     REQUIRE(m_vector.vector, "Vector is not set!\n");
00090 
00091     // solve the linear system with the sample vector
00092     SGVector<complex128_t> vec=m_linear_solver->solve(m_operator, m_vector);
00093 
00094     // multiply with the weight using Eigen3 and take negative
00095     // (see CRationalApproximation for the formula)
00096     Map<VectorXcd> v(vec.vector, vec.vlen);
00097     v*=m_weight;
00098     v=-v;
00099 
00100     // set as a vector result and submit to the aggregator
00101     CVectorResult<complex128_t>* result=new CVectorResult<complex128_t>(vec);
00102     SG_REF(result);
00103 
00104     m_aggregator->submit_result(result);
00105 
00106     SG_UNREF(result);
00107 }
00108 
00109 }
00110 #endif // HAVE_EIGEN3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation