SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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>
23 #include <typeinfo>
24 
25 namespace shogun
26 {
27 
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 
37  CMatrixOperator<float64_t>* linear_operator,
38  CIndependentComputationEngine* computation_engine,
39  CEigenSolver* eigen_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 
62 {
63  SG_UNREF(m_linear_solver);
64 
65  SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
66 }
67 
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
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;
88  {
89  operator_type=DENSE;
90 
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 
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:
127  (*dynamic_cast<CDenseMatrixOperator<complex128_t>*>(complex_op));
128  break;
129  case SPARSE:
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
149  =new CRationalApproximationIndividualJob(agg, m_linear_solver,
150  shifted_op, sample, m_weights[i]);
151  SG_REF(job);
152 
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
SGMatrix< T > get_matrix_operator() const
CIndependentComputationEngine * m_computation_engine
CLinearOperator< SGVector< float64_t >, SGVector< float64_t > > * m_linear_operator
SGSparseMatrix< T > get_matrix_operator() const
int32_t index_t
Definition: common.h:62
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual SGVector< T > get_diagonal() const =0
SGVector< complex128_t > m_shifts
#define SG_REF(x)
Definition: SGObject.h:51
Class that aggregates vector job results in each submit_result call of jobs generated from rational a...
Implementation of independent job that solves one of the family of shifted systems in rational approx...
index_t vlen
Definition: SGVector.h:494
#define SG_GCDEBUG(...)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
Abstract base class of the rational approximation of a function of a linear operator (A) times vector...
virtual void set_diagonal(SGVector< T > diag)=0
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
Abstract base class that provides an interface for computing an aggeregation of the job results of in...
SGVector< complex128_t > m_weights
unknown alphabet
Definition: Alphabet.h:71
virtual CJobResultAggregator * submit_jobs(SGVector< float64_t > sample)
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
Abstract base class that provides an abstract compute method for computing eigenvalues of a real valu...
Definition: EigenSolver.h:24
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Class that represents a sparse-matrix linear operator. It computes matrix-vector product in its appl...
Abstract base class for solving multiple independent instances of CIndependentJob. It has one method, submit_job, which may add the job to an internal queue and might block if there is yet not space in the queue. After jobs are submitted, it might not yet be ready. wait_for_all waits until all jobs are completed, which must be called to guarantee that all jobs are finished.
#define SG_ADD(...)
Definition: SGObject.h:81
virtual void submit_job(CIndependentJob *job)=0
Abstract base class that represents a matrix linear operator. It provides an interface to computes ma...

SHOGUN Machine Learning Toolbox - Documentation