SHOGUN
v3.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
mathematics
linalg
ratapprox
logdet
opfunc
LogRationalApproximationIndividual.cpp
Go to the documentation of this file.
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/config.h
>
11
12
#ifdef HAVE_EIGEN3
13
#include <
shogun/base/Parameter.h
>
14
#include <
shogun/lib/SGVector.h
>
15
#include <
shogun/lib/SGMatrix.h
>
16
#include <
shogun/mathematics/linalg/linsolver/LinearSolver.h
>
17
#include <
shogun/mathematics/linalg/linop/DenseMatrixOperator.h
>
18
#include <
shogun/mathematics/linalg/linop/SparseMatrixOperator.h
>
19
#include <
shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.h
>
20
#include <
shogun/mathematics/linalg/ratapprox/logdet/computation/job/RationalApproximationIndividualJob.h
>
21
#include <
shogun/mathematics/linalg/ratapprox/logdet/computation/aggregator/IndividualJobResultAggregator.h
>
22
#include <
shogun/lib/computation/engine/IndependentComputationEngine.h
>
23
#include <typeinfo>
24
25
namespace
shogun
26
{
27
28
CLogRationalApproximationIndividual::CLogRationalApproximationIndividual
()
29
:
CRationalApproximation
(NULL, NULL, NULL, 0,
OF_LOG
)
30
{
31
init();
32
33
SG_GCDEBUG
(
"%s created (%p)\n"
, this->
get_name
(),
this
)
34
}
35
36
CLogRationalApproximationIndividual::CLogRationalApproximationIndividual
(
37
CMatrixOperator<float64_t>
* linear_operator,
38
CIndependentComputationEngine
* computation_engine,
39
CEigenSolver
* eigen_solver,
40
CLinearSolver<complex128_t, float64_t>
* linear_solver,
41
float64_t
desired_accuracy)
42
:
CRationalApproximation
(linear_operator, computation_engine,
43
eigen_solver, desired_accuracy,
OF_LOG
)
44
{
45
init();
46
47
m_linear_solver=linear_solver;
48
SG_REF
(m_linear_solver);
49
50
SG_GCDEBUG
(
"%s created (%p)\n"
, this->
get_name
(),
this
)
51
}
52
53
void
CLogRationalApproximationIndividual::init()
54
{
55
m_linear_solver=NULL;
56
57
SG_ADD
((
CSGObject
**)&m_linear_solver,
"linear_solver"
,
58
"Linear solver for complex systems"
,
MS_NOT_AVAILABLE
);
59
}
60
61
CLogRationalApproximationIndividual::~CLogRationalApproximationIndividual
()
62
{
63
SG_UNREF
(m_linear_solver);
64
65
SG_GCDEBUG
(
"%s destroyed (%p)\n"
, this->
get_name
(),
this
)
66
}
67
68
CJobResultAggregator
*
CLogRationalApproximationIndividual::submit_jobs
(
69
SGVector<float64_t>
sample)
70
{
71
SG_DEBUG
(
"OperatorFunction::submit_jobs(): Entering..\n"
);
72
REQUIRE
(sample.
vector
,
"Sample is not initialized!\n"
);
73
REQUIRE
(
m_linear_operator
,
"Operator is not initialized!\n"
);
74
REQUIRE
(
m_computation_engine
,
"Computation engine is NULL\n"
);
75
76
// create the aggregator with sample, and the multiplier
77
CIndividualJobResultAggregator
* agg=
new
CIndividualJobResultAggregator
(
78
m_linear_operator
, sample,
m_constant_multiplier
);
79
// we don't want the aggregator to be destroyed when the job is unref-ed
80
SG_REF
(agg);
81
82
// this enum will save from repeated typechecking for all jobs
83
enum
typeID {DENSE=1, SPARSE,
UNKNOWN
} operator_type=
UNKNOWN
;
84
85
// create a complex copy of the matrix linear operator
86
CMatrixOperator<complex128_t>
* complex_op=NULL;
87
if
(
typeid
(*
m_linear_operator
)==
typeid
(
CDenseMatrixOperator<float64_t>
))
88
{
89
operator_type=DENSE;
90
91
CDenseMatrixOperator<float64_t>
* op
92
=
dynamic_cast<
CDenseMatrixOperator<float64_t>
*
>
(
m_linear_operator
);
93
94
REQUIRE
(op->
get_matrix_operator
().
matrix
,
"Matrix is not initialized!\n"
);
95
96
// create complex dense matrix operator
97
complex_op=
static_cast<
CDenseMatrixOperator<complex128_t>
*
>
(*op);
98
}
99
else
if
(
typeid
(*
m_linear_operator
)==
typeid
(
CSparseMatrixOperator<float64_t>
))
100
{
101
operator_type=SPARSE;
102
103
CSparseMatrixOperator<float64_t>
* op
104
=
dynamic_cast<
CSparseMatrixOperator<float64_t>
*
>
(
m_linear_operator
);
105
106
REQUIRE
(op->
get_matrix_operator
().sparse_matrix,
"Matrix is not initialized!\n"
);
107
108
// create complex sparse matrix operator
109
complex_op=
static_cast<
CSparseMatrixOperator<complex128_t>
*
>
(*op);
110
}
111
else
112
{
113
// something weird happened
114
SG_ERROR
(
"OperatorFunction::submit_jobs(): Unknown MatrixOperator given!\n"
);
115
}
116
117
// create num_shifts number of jobs for current sample vector
118
for
(
index_t
i=0; i<
m_num_shifts
; ++i)
119
{
120
// create a deep copy of the operator
121
CMatrixOperator<complex128_t>
* shifted_op=NULL;
122
123
switch
(operator_type)
124
{
125
case
DENSE:
126
shifted_op=
new
CDenseMatrixOperator<complex128_t>
127
(*
dynamic_cast<
CDenseMatrixOperator<complex128_t>
*
>
(complex_op));
128
break
;
129
case
SPARSE:
130
shifted_op=
new
CSparseMatrixOperator<complex128_t>
131
(*
dynamic_cast<
CSparseMatrixOperator<complex128_t>
*
>
(complex_op));
132
break
;
133
default
:
134
break
;
135
}
136
137
REQUIRE
(shifted_op,
"OperatorFunction::submit_jobs():"
138
"MatrixOperator typeinfo was not detected!\n"
);
139
140
// move the shift inside the operator
141
// (see CRationalApproximation)
142
SGVector<complex128_t>
diag=shifted_op->
get_diagonal
();
143
for
(
index_t
j=0; j<diag.
vlen
; ++j)
144
diag[j]-=
m_shifts
[i];
145
shifted_op->
set_diagonal
(diag);
146
147
// create a job and submit to the engine
148
CRationalApproximationIndividualJob
* job
149
=
new
CRationalApproximationIndividualJob
(agg, m_linear_solver,
150
shifted_op, sample,
m_weights
[i]);
151
SG_REF
(job);
152
153
m_computation_engine
->
submit_job
(job);
154
155
// we can safely unref the job here, computation engine takes it from here
156
SG_UNREF
(job);
157
}
158
159
SG_UNREF
(complex_op);
160
161
SG_DEBUG
(
"OperatorFunction::submit_jobs(): Leaving..\n"
);
162
return
agg;
163
}
164
165
}
166
#endif // HAVE_EIGEN3
SHOGUN
Machine Learning Toolbox - Documentation