NegationRule.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/NegationRule.h
10 
11 #ifndef __MEAL_NegationRule_H
12 #define __MEAL_NegationRule_H
13 
14 #include "MEAL/UnaryRule.h"
15 
16 namespace MEAL {
17 
19  template<class T>
20  class NegationRule : 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 
50  template<class T>
51  NegationRule<T>* negation (T* model)
52  {
53  NegationRule<T>* result = new NegationRule<T>;
54  result->set_model (model);
55  return result;
56  }
57 }
58 
59 template<class T>
60 std::string MEAL::NegationRule<T>::get_name () const
61 {
62  return "NegationRule<" + std::string(T::Name)+ ">";
63 }
64 
65 
66 template<class T>
67 void MEAL::NegationRule<T>::calculate (Result& result,
68  std::vector<Result>* grad)
69 {
70  if (!this->model)
71  throw Error (InvalidState, "MEAL::NegationRule::calculate",
72  "no model to evaluate");
73 
74  if (T::verbose)
75  std::cerr << "MEAL::NegationRule::calculate" << std::endl;
76 
77  result = - this->model->evaluate (grad);
78 
79  if (T::verbose)
80  std::cerr << "MEAL::NegationRule::calculate result\n"
81  " " << result << std::endl;
82 
83  if (!grad)
84  return;
85 
86  for (unsigned igrad=0; igrad<grad->size(); igrad++)
87  (*grad)[igrad] *= -1.0;
88 
89  if (T::verbose) {
90  std::cerr << "MEAL::NegationRule::calculate gradient\n";
91  for (unsigned i=0; i<grad->size(); i++)
92  std::cerr << " "
93  << i << ":" << this->get_infit(i) << "=" << (*grad)[i] << std::endl;
94  }
95 
96 }
97 
98 
99 #endif
NegationRule< T > * negation(T *model)
Returns a new negation of the model.
Definition: NegationRule.h:56
Computes the inverse of a model and its partial derivatives.
Definition: NegationRule.h:25
Namespace in which all modeling and calibration related code is declared.
Definition: ExampleComplex2.h:16
std::string get_name() const
Return the name of the class.
Definition: NegationRule.h:60
void calculate(Result &result, std::vector< Result > *gradient)
Return the Result and its gradient.
Definition: NegationRule.h:67

Generated using doxygen 1.8.17