SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
DenseMatrixExactLog.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/common.h>
00011 
00012 #ifdef HAVE_EIGEN3
00013 #include <shogun/mathematics/eigen3.h>
00014 
00015 #if EIGEN_VERSION_AT_LEAST(3,1,0)
00016 #include <unsupported/Eigen/MatrixFunctions>
00017 #endif // EIGEN_VERSION_AT_LEAST(3,1,0)
00018 
00019 #include <shogun/lib/SGVector.h>
00020 #include <shogun/lib/SGMatrix.h>
00021 #include <shogun/lib/computation/aggregator/StoreScalarAggregator.h>
00022 #include <shogun/lib/computation/engine/IndependentComputationEngine.h>
00023 #include <shogun/mathematics/linalg/linop/DenseMatrixOperator.h>
00024 #include <shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h>
00025 #include <shogun/mathematics/linalg/ratapprox/logdet/computation/job/DenseExactLogJob.h>
00026 
00027 using namespace Eigen;
00028 
00029 namespace shogun
00030 {
00031 
00032 CDenseMatrixExactLog::CDenseMatrixExactLog()
00033     : COperatorFunction<float64_t>(NULL, NULL, OF_LOG)
00034 {
00035     SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
00036 }
00037 
00038 CDenseMatrixExactLog::CDenseMatrixExactLog(
00039     CDenseMatrixOperator<float64_t>* op,
00040     CIndependentComputationEngine* engine)
00041     : COperatorFunction<float64_t>(
00042         (CLinearOperator<float64_t>*)op, engine, OF_LOG)
00043 {
00044     SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
00045 }
00046 
00047 CDenseMatrixExactLog::~CDenseMatrixExactLog()
00048 {
00049     SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
00050 }
00051 
00052 #if EIGEN_VERSION_AT_LEAST(3,1,0)
00053 void CDenseMatrixExactLog::precompute()
00054 {
00055     SG_DEBUG("Entering...\n");
00056 
00057     // check for proper downcast
00058     CDenseMatrixOperator<float64_t>* op
00059         =dynamic_cast<CDenseMatrixOperator<float64_t>*>(m_linear_operator);
00060     REQUIRE(op, "Operator not an instance of DenseMatrixOperator!\n");
00061     SGMatrix<float64_t> m=op->get_matrix_operator();
00062 
00063     // compute log(C) using Eigen3
00064     Map<MatrixXd> mat(m.matrix, m.num_rows, m.num_cols);
00065     SGMatrix<float64_t> log_m(m.num_rows, m.num_cols);
00066     Map<MatrixXd> log_mat(log_m.matrix, log_m.num_rows, log_m.num_cols);
00067     log_mat=mat.log();
00068 
00069     // the log(C) is also a linear operator here
00070     // reset the operator of this function with log(C)
00071     SG_UNREF(m_linear_operator);
00072     m_linear_operator=new CDenseMatrixOperator<float64_t>(log_m);
00073     SG_REF(m_linear_operator);
00074 
00075     SG_DEBUG("Leaving...\n");
00076 }
00077 #else
00078 void CDenseMatrixExactLog::precompute()
00079 {
00080     SG_WARNING("Eigen3.1.0 or later required!\n")
00081 }
00082 #endif // EIGEN_VERSION_AT_LEAST(3,1,0)
00083 
00084 CJobResultAggregator* CDenseMatrixExactLog::submit_jobs(SGVector<float64_t>
00085     sample)
00086 {
00087     SG_DEBUG("Entering...\n");
00088 
00089     CStoreScalarAggregator<float64_t>* agg=new CStoreScalarAggregator<float64_t>;
00090     // we don't want the aggregator to be destroyed when the job is unref-ed
00091     SG_REF(agg);
00092     CDenseExactLogJob* job=new CDenseExactLogJob(agg,
00093         dynamic_cast<CDenseMatrixOperator<float64_t>*>(m_linear_operator), sample);
00094     SG_REF(job);
00095     // sanity check
00096     REQUIRE(m_computation_engine, "Computation engine is NULL\n");
00097     m_computation_engine->submit_job(job);
00098     // we can safely unref the job here, computation engine takes it from here
00099     SG_UNREF(job);
00100 
00101     SG_DEBUG("Leaving...\n");
00102     return agg;
00103 }
00104 
00105 }
00106 #endif // HAVE_EIGEN3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation