SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #ifdef HAVE_EIGEN3
13 #include <shogun/lib/SGVector.h>
19 #include <shogun/base/Parameter.h>
20 
21 using namespace Eigen;
22 
23 namespace shogun
24 {
25 
26 CRationalApproximationCGMJob::CRationalApproximationCGMJob()
27  : CIndependentJob()
28 {
29  init();
30 }
31 
34  CCGMShiftedFamilySolver* linear_solver,
36  SGVector<float64_t> vector,
38  SGVector<complex128_t> weights,
39  float64_t const_multiplier)
41 {
42  init();
43 
44  m_linear_solver=linear_solver;
45  SG_REF(m_linear_solver);
46 
47  m_operator=linear_operator;
48  SG_REF(m_operator);
49 
50  m_vector=vector;
51 
52  m_shifts=shifts;
53  m_weights=weights;
54  m_const_multiplier=const_multiplier;
55 }
56 
57 void CRationalApproximationCGMJob::init()
58 {
59  m_linear_solver=NULL;
60  m_operator=NULL;
61  m_const_multiplier=0.0;
62 
63  SG_ADD((CSGObject**)&m_linear_solver, "linear_solver",
64  "Linear solver for complex-shifted system", MS_NOT_AVAILABLE);
65 
66  SG_ADD((CSGObject**)&m_operator, "linear_operator",
67  "Linear operator", MS_NOT_AVAILABLE);
68 
69  SG_ADD(&m_vector, "trace_sample",
70  "Sample vector to apply linear operator on", MS_NOT_AVAILABLE);
71 
72  SG_ADD(&m_weights, "complex_shifts",
73  "Shifts in the linear systems to be solved", MS_NOT_AVAILABLE);
74 
75  SG_ADD(&m_weights, "complex_weights",
76  "Weights to be multiplied to the solution vector", MS_NOT_AVAILABLE);
77 
78  SG_ADD(&m_const_multiplier, "constant_multiplier",
79  "Constant multiplier to be multiplied with the final solution", MS_NOT_AVAILABLE);
80 }
81 
83 {
84  SG_UNREF(m_linear_solver);
85  SG_UNREF(m_operator);
86 }
87 
89 {
90  SG_DEBUG("Entering\n");
91 
92  REQUIRE(m_aggregator, "Job result aggregator is not set!\n");
93  REQUIRE(m_operator, "Operator is not set!\n");
94  REQUIRE(m_vector.vector, "Vector is not set!\n");
95  REQUIRE(m_shifts.vector, "Shifts are not set!\n");
96  REQUIRE(m_weights.vector, "Weights are not set!\n");
97  REQUIRE(m_operator->get_dimension()==m_vector.vlen,
98  "Dimension mismatch! %d vs %d\n", m_operator->get_dimension(), m_vector.vlen);
99  REQUIRE(m_shifts.vlen==m_weights.vlen,
100  "Number of shifts and weights are not equal!\n");
101 
102  // solve the linear system with the sample vector
103  SGVector<complex128_t> vec=m_linear_solver->solve_shifted_weighted(
104  m_operator, m_vector, m_shifts, m_weights);
105 
106  // Take negative (see CRationalApproximation for the formula)
107  Map<VectorXcd> v(vec.vector, vec.vlen);
108  v=-v;
109 
110  // take out the imaginary part of the result before
111  // applying linear operator
112  SGVector<float64_t> agg=m_operator->apply(vec.get_imag());
113 
114  // perform dot product
115  Map<VectorXd> map_agg(agg.vector, agg.vlen);
116  Map<VectorXd> map_vector(m_vector.vector, m_vector.vlen);
117  float64_t result=map_vector.dot(map_agg);
118 
119  result*=m_const_multiplier;
120 
121  // form the final result into a scalar result and submit to the aggregator
122  CScalarResult<float64_t>* final_result=new CScalarResult<float64_t>(result);
123  SG_REF(final_result);
124 
125  m_aggregator->submit_result(final_result);
126 
127  SG_UNREF(final_result);
128 
129  SG_DEBUG("Leaving\n");
130 }
131 
132 }
133 #endif // HAVE_EIGEN3

SHOGUN Machine Learning Toolbox - Documentation