SHOGUN
3.2.1
首页
相关页面
模块
类
文件
文件列表
文件成员
全部
类
命名空间
文件
函数
变量
类型定义
枚举
枚举值
友元
宏定义
组
页
src
shogun
mathematics
linalg
ratapprox
logdet
opfunc
DenseMatrixExactLog.cpp
浏览该文件的文档.
1
/*
2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 3 of the License, or
5
* (at your option) any later version.
6
*
7
* Written (W) 2013 Soumyajit De
8
*/
9
10
#include <
shogun/lib/common.h
>
11
12
#ifdef HAVE_EIGEN3
13
#include <
shogun/mathematics/eigen3.h
>
14
15
#if EIGEN_VERSION_AT_LEAST(3,1,0)
16
#include <unsupported/Eigen/MatrixFunctions>
17
#endif // EIGEN_VERSION_AT_LEAST(3,1,0)
18
19
#include <
shogun/lib/SGVector.h
>
20
#include <
shogun/lib/SGMatrix.h
>
21
#include <
shogun/lib/computation/aggregator/StoreScalarAggregator.h
>
22
#include <
shogun/lib/computation/engine/IndependentComputationEngine.h
>
23
#include <
shogun/mathematics/linalg/linop/DenseMatrixOperator.h
>
24
#include <
shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h
>
25
#include <
shogun/mathematics/linalg/ratapprox/logdet/computation/job/DenseExactLogJob.h
>
26
27
using namespace
Eigen;
28
29
namespace
shogun
30
{
31
32
CDenseMatrixExactLog::CDenseMatrixExactLog()
33
:
COperatorFunction
<
float64_t
>(NULL, NULL,
OF_LOG
)
34
{
35
SG_GCDEBUG
(
"%s created (%p)\n"
, this->
get_name
(),
this
)
36
}
37
38
CDenseMatrixExactLog::CDenseMatrixExactLog
(
39
CDenseMatrixOperator<float64_t>
* op,
40
CIndependentComputationEngine
* engine)
41
:
COperatorFunction
<
float64_t
>(
42
(
CLinearOperator
<
float64_t
>*)op, engine,
OF_LOG
)
43
{
44
SG_GCDEBUG
(
"%s created (%p)\n"
, this->
get_name
(),
this
)
45
}
46
47
CDenseMatrixExactLog::~CDenseMatrixExactLog
()
48
{
49
SG_GCDEBUG
(
"%s destroyed (%p)\n"
, this->
get_name
(),
this
)
50
}
51
52
#if EIGEN_VERSION_AT_LEAST(3,1,0)
53
void
CDenseMatrixExactLog::precompute
()
54
{
55
SG_DEBUG
(
"Entering...\n"
);
56
57
// check for proper downcast
58
CDenseMatrixOperator<float64_t>
* op
59
=
dynamic_cast<
CDenseMatrixOperator<float64_t>
*
>
(
m_linear_operator
);
60
REQUIRE
(op,
"Operator not an instance of DenseMatrixOperator!\n"
);
61
SGMatrix<float64_t>
m=op->
get_matrix_operator
();
62
63
// compute log(C) using Eigen3
64
Map<MatrixXd> mat(m.
matrix
, m.
num_rows
, m.
num_cols
);
65
SGMatrix<float64_t>
log_m(m.
num_rows
, m.
num_cols
);
66
Map<MatrixXd> log_mat(log_m.matrix, log_m.num_rows, log_m.num_cols);
67
log_mat=mat.log();
68
69
// the log(C) is also a linear operator here
70
// reset the operator of this function with log(C)
71
SG_UNREF
(
m_linear_operator
);
72
m_linear_operator
=
new
CDenseMatrixOperator<float64_t>
(log_m);
73
SG_REF
(
m_linear_operator
);
74
75
SG_DEBUG
(
"Leaving...\n"
);
76
}
77
#else
78
void
CDenseMatrixExactLog::precompute
()
79
{
80
SG_WARNING
(
"Eigen3.1.0 or later required!\n"
)
81
}
82
#endif // EIGEN_VERSION_AT_LEAST(3,1,0)
83
84
CJobResultAggregator
*
CDenseMatrixExactLog::submit_jobs
(
SGVector<float64_t>
85
sample)
86
{
87
SG_DEBUG
(
"Entering...\n"
);
88
89
CStoreScalarAggregator<float64_t>
* agg=
new
CStoreScalarAggregator<float64_t>
;
90
// we don't want the aggregator to be destroyed when the job is unref-ed
91
SG_REF
(agg);
92
CDenseExactLogJob
* job=
new
CDenseExactLogJob
(agg,
93
dynamic_cast<
CDenseMatrixOperator<float64_t>
*
>
(
m_linear_operator
), sample);
94
SG_REF
(job);
95
// sanity check
96
REQUIRE
(
m_computation_engine
,
"Computation engine is NULL\n"
);
97
m_computation_engine
->
submit_job
(job);
98
// we can safely unref the job here, computation engine takes it from here
99
SG_UNREF
(job);
100
101
SG_DEBUG
(
"Leaving...\n"
);
102
return
agg;
103
}
104
105
}
106
#endif // HAVE_EIGEN3
SHOGUN
机器学习工具包 - 项目文档