SHOGUN  4.2.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 #include <shogun/base/Parameter.h>
13 #include <shogun/lib/SGVector.h>
14 #include <shogun/lib/SGMatrix.h>
22 #include <typeinfo>
23 
24 namespace shogun
25 {
26 
28  : CRationalApproximation(NULL, NULL, NULL, 0, OF_LOG)
29 {
30  init();
31 
32  SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
33 }
34 
36  CMatrixOperator<float64_t>* linear_operator,
37  CIndependentComputationEngine* computation_engine,
38  CEigenSolver* eigen_solver,
40  float64_t desired_accuracy)
41  : CRationalApproximation(linear_operator, computation_engine,
42  eigen_solver, desired_accuracy, OF_LOG)
43 {
44  init();
45 
46  m_linear_solver=linear_solver;
47  SG_REF(m_linear_solver);
48 
49  SG_GCDEBUG("%s created (%p)\n", this->get_name(), this)
50 }
51 
52 void CLogRationalApproximationIndividual::init()
53 {
54  m_linear_solver=NULL;
55 
56  SG_ADD((CSGObject**)&m_linear_solver, "linear_solver",
57  "Linear solver for complex systems", MS_NOT_AVAILABLE);
58 }
59 
61 {
62  SG_UNREF(m_linear_solver);
63 
64  SG_GCDEBUG("%s destroyed (%p)\n", this->get_name(), this)
65 }
66 
68  SGVector<float64_t> sample)
69 {
70  SG_DEBUG("OperatorFunction::submit_jobs(): Entering..\n");
71  REQUIRE(sample.vector, "Sample is not initialized!\n");
72  REQUIRE(m_linear_operator, "Operator is not initialized!\n");
73  REQUIRE(m_computation_engine, "Computation engine is NULL\n");
74 
75  // create the aggregator with sample, and the multiplier
78  // we don't want the aggregator to be destroyed when the job is unref-ed
79  SG_REF(agg);
80 
81  // this enum will save from repeated typechecking for all jobs
82  enum typeID {DENSE=1, SPARSE, UNKNOWN} operator_type=UNKNOWN;
83 
84  // create a complex copy of the matrix linear operator
85  CMatrixOperator<complex128_t>* complex_op=NULL;
87  {
88  operator_type=DENSE;
89 
92 
93  REQUIRE(op->get_matrix_operator().matrix, "Matrix is not initialized!\n");
94 
95  // create complex dense matrix operator
96  complex_op=static_cast<CDenseMatrixOperator<complex128_t>*>(*op);
97  }
98  else if (typeid(*m_linear_operator)==typeid(CSparseMatrixOperator<float64_t>))
99  {
100  operator_type=SPARSE;
101 
104 
105  REQUIRE(op->get_matrix_operator().sparse_matrix, "Matrix is not initialized!\n");
106 
107  // create complex sparse matrix operator
108  complex_op=static_cast<CSparseMatrixOperator<complex128_t>*>(*op);
109  }
110  else
111  {
112  // something weird happened
113  SG_ERROR("OperatorFunction::submit_jobs(): Unknown MatrixOperator given!\n");
114  }
115 
116  // create num_shifts number of jobs for current sample vector
117  for (index_t i=0; i<m_num_shifts; ++i)
118  {
119  // create a deep copy of the operator
120  CMatrixOperator<complex128_t>* shifted_op=NULL;
121 
122  switch(operator_type)
123  {
124  case DENSE:
126  (*dynamic_cast<CDenseMatrixOperator<complex128_t>*>(complex_op));
127  break;
128  case SPARSE:
130  (*dynamic_cast<CSparseMatrixOperator<complex128_t>*>(complex_op));
131  break;
132  default:
133  break;
134  }
135 
136  REQUIRE(shifted_op, "OperatorFunction::submit_jobs():"
137  "MatrixOperator typeinfo was not detected!\n");
138 
139  // move the shift inside the operator
140  // (see CRationalApproximation)
141  SGVector<complex128_t> diag=shifted_op->get_diagonal();
142  for (index_t j=0; j<diag.vlen; ++j)
143  diag[j]-=m_shifts[i];
144  shifted_op->set_diagonal(diag);
145 
146  // create a job and submit to the engine
148  =new CRationalApproximationIndividualJob(agg, m_linear_solver,
149  shifted_op, sample, m_weights[i]);
150  SG_REF(job);
151 
153 
154  // we can safely unref the job here, computation engine takes it from here
155  SG_UNREF(job);
156  }
157 
158  SG_UNREF(complex_op);
159 
160  SG_DEBUG("OperatorFunction::submit_jobs(): Leaving..\n");
161  return agg;
162 }
163 
164 }
SGMatrix< T > get_matrix_operator() const
CIndependentComputationEngine * m_computation_engine
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:54
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:492
#define SG_GCDEBUG(...)
Definition: SGIO.h:102
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
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:55
#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...
CLinearOperator< float64_t > * m_linear_operator
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:84
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