SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RationalApproximationCGMJob.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 #include <shogun/lib/SGVector.h>
18 #include <shogun/base/Parameter.h>
19 
20 using namespace Eigen;
21 
22 namespace shogun
23 {
24 
25 CRationalApproximationCGMJob::CRationalApproximationCGMJob()
26  : CIndependentJob()
27 {
28  init();
29 }
30 
33  CCGMShiftedFamilySolver* linear_solver,
34  CLinearOperator<float64_t>* linear_operator,
35  SGVector<float64_t> vector,
37  SGVector<complex128_t> weights,
38  float64_t const_multiplier)
40 {
41  init();
42 
43  m_linear_solver=linear_solver;
44  SG_REF(m_linear_solver);
45 
46  m_operator=linear_operator;
47  SG_REF(m_operator);
48 
49  m_vector=vector;
50 
51  m_shifts=shifts;
52  m_weights=weights;
53  m_const_multiplier=const_multiplier;
54 }
55 
56 void CRationalApproximationCGMJob::init()
57 {
58  m_linear_solver=NULL;
59  m_operator=NULL;
60  m_const_multiplier=0.0;
61 
62  SG_ADD((CSGObject**)&m_linear_solver, "linear_solver",
63  "Linear solver for complex-shifted system", MS_NOT_AVAILABLE);
64 
65  SG_ADD((CSGObject**)&m_operator, "linear_operator",
66  "Linear operator", MS_NOT_AVAILABLE);
67 
68  SG_ADD(&m_vector, "trace_sample",
69  "Sample vector to apply linear operator on", MS_NOT_AVAILABLE);
70 
71  SG_ADD(&m_weights, "complex_shifts",
72  "Shifts in the linear systems to be solved", MS_NOT_AVAILABLE);
73 
74  SG_ADD(&m_weights, "complex_weights",
75  "Weights to be multiplied to the solution vector", MS_NOT_AVAILABLE);
76 
77  SG_ADD(&m_const_multiplier, "constant_multiplier",
78  "Constant multiplier to be multiplied with the final solution", MS_NOT_AVAILABLE);
79 }
80 
82 {
83  SG_UNREF(m_linear_solver);
84  SG_UNREF(m_operator);
85 }
86 
88 {
89  SG_DEBUG("Entering\n");
90 
91  REQUIRE(m_aggregator, "Job result aggregator is not set!\n");
92  REQUIRE(m_operator, "Operator is not set!\n");
93  REQUIRE(m_vector.vector, "Vector is not set!\n");
94  REQUIRE(m_shifts.vector, "Shifts are not set!\n");
95  REQUIRE(m_weights.vector, "Weights are not set!\n");
96  REQUIRE(m_operator->get_dimension()==m_vector.vlen,
97  "Dimension mismatch! %d vs %d\n", m_operator->get_dimension(), m_vector.vlen);
98  REQUIRE(m_shifts.vlen==m_weights.vlen,
99  "Number of shifts and weights are not equal!\n");
100 
101  // solve the linear system with the sample vector
102  SGVector<complex128_t> vec=m_linear_solver->solve_shifted_weighted(
103  m_operator, m_vector, m_shifts, m_weights);
104 
105  // Take negative (see CRationalApproximation for the formula)
106  Map<VectorXcd> v(vec.vector, vec.vlen);
107  v=-v;
108 
109  // take out the imaginary part of the result before
110  // applying linear operator
111  SGVector<float64_t> agg=m_operator->apply(vec.get_imag());
112 
113  // perform dot product
114  Map<VectorXd> map_agg(agg.vector, agg.vlen);
115  Map<VectorXd> map_vector(m_vector.vector, m_vector.vlen);
116  float64_t result=map_vector.dot(map_agg);
117 
118  result*=m_const_multiplier;
119 
120  // form the final result into a scalar result and submit to the aggregator
121  CScalarResult<float64_t>* final_result=new CScalarResult<float64_t>(result);
122  SG_REF(final_result);
123 
124  m_aggregator->submit_result(final_result);
125 
126  SG_UNREF(final_result);
127 
128  SG_DEBUG("Leaving\n");
129 }
130 
131 }
Base class that stores the result of an independent job when the result is a scalar.
Definition: ScalarResult.h:24
const index_t get_dimension() const
SGVector< float64_t > get_imag()
Definition: SGVector.cpp:889
class that uses conjugate gradient method for solving a shifted linear system family where the linear...
Definition: SGMatrix.h:20
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void submit_result(CJobResult *result)=0
#define SG_REF(x)
Definition: SGObject.h:54
index_t vlen
Definition: SGVector.h:494
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
Template class that aggregates scalar job results in each submit_result call, finalize then transform...
double float64_t
Definition: common.h:50
Abstract base class that provides an interface for computing an aggeregation of the job results of in...
virtual SGVector< T > apply(SGVector< T > b) const =0
#define SG_UNREF(x)
Definition: SGObject.h:55
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Abstract base for general computation jobs to be registered in CIndependentComputationEngine. compute method produces a job result and submits it to the internal JobResultAggregator. Each set of jobs that form a result will share the same job result aggregator.
#define SG_ADD(...)
Definition: SGObject.h:84
virtual SGVector< complex128_t > solve_shifted_weighted(CLinearOperator< float64_t > *A, SGVector< float64_t > b, SGVector< complex128_t > shifts, SGVector< complex128_t > weights)
CJobResultAggregator * m_aggregator

SHOGUN Machine Learning Toolbox - Documentation