InverseRule.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/InverseRule.h
10
11#ifndef __MEAL_InverseRule_H
12#define __MEAL_InverseRule_H
13
14#include "MEAL/UnaryRule.h"
15
16namespace MEAL {
17
19 template<class T>
20 class InverseRule : public UnaryRule<T>
21 {
22
23 public:
24
25 typedef typename T::Result Result;
26
27 // ///////////////////////////////////////////////////////////////////
28 //
29 // Function implementation
30 //
31 // ///////////////////////////////////////////////////////////////////
32
34 std::string get_name () const;
35
36 protected:
37
38 // ///////////////////////////////////////////////////////////////////
39 //
40 // Optimized implementation
41 //
42 // ///////////////////////////////////////////////////////////////////
43
45 void calculate (Result& result, std::vector<Result>* gradient);
46
47 };
48
49}
50
51template<class T>
53{
54 return "InverseRule<" + std::string(T::Name)+ ">";
55}
56
57
58template<class T>
60 std::vector<Result>* grad)
61{
62 if (!this->model)
63 throw Error (InvalidState, "MEAL::InverseRule::calculate",
64 "no model to evaluate");
65
66 if (T::verbose)
67 std::cerr << "MEAL::InverseRule::calculate" << std::endl;
68
69 result = inv( this->model->evaluate (grad) );
70
71 if (T::verbose)
72 std::cerr << "MEAL::InverseRule::calculate result\n"
73 " " << result << std::endl;
74
75 if (!grad)
76 return;
77
78 for (unsigned igrad=0; igrad<grad->size(); igrad++)
79 (*grad)[igrad] = - result * (*grad)[igrad] * result;
80
81 if (T::verbose) {
82 std::cerr << "MEAL::InverseRule::calculate gradient\n";
83 for (unsigned i=0; i<grad->size(); i++)
84 std::cerr << " "
85 << i << ":" << this->get_infit(i) << "=" << (*grad)[i] << std::endl;
86 }
87
88}
89
90
91#endif
Computes the inverse of a model and its partial derivatives.
Definition InverseRule.h:21
std::string get_name() const
Return the name of the class.
Definition InverseRule.h:52
void calculate(Result &result, std::vector< Result > *gradient)
Return the Result and its gradient.
Definition InverseRule.h:59
UnaryRule()
Default constructor.
Definition UnaryRule.h:27
Project< T > model
The Function on which the operation will be performed.
Definition UnaryRule.h:44
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0