UnaryConvert.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2011 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/More/MEAL/MEAL/Convert.h
10
11#ifndef __MEAL_UnaryConvert_H
12#define __MEAL_UnaryConvert_H
13
14#include "MEAL/Convert.h"
15
16namespace MEAL {
17
19
22
23 template<typename From, typename To, typename Method>
24 class UnaryConvert : public Convert<From,To>
25 {
26
27 public:
28
29 UnaryConvert (Method m) { method = m; }
30
32 std::string get_name () const
33 { return "UnaryConvert<" + std::string(From::Name)
34 + "," + std::string(To::Name)+ ">"; }
35
36 protected:
37
39 virtual void calculate (typename To::Result& result,
40 std::vector<typename To::Result>* grad)
41 {
42 std::vector<typename From::Result> from_grad;
43 typename From::Result from_result;
44
45 from_result = this->get_model()->evaluate( (grad) ? &from_grad : 0 );
46 result = method (from_result);
47 if (!grad)
48 return;
49
50 for (unsigned i=0; i<this->get_nparam(); i++)
51 (*grad)[i] = method (from_grad[i]);
52 }
53
55 Method method;
56 };
57
58 template<typename T, typename M>
59 UnaryConvert<T,T,M>* convert (T* function, M method)
60 {
61 UnaryConvert<T,T,M>* result = new UnaryConvert<T,T,M> (method);
62 result->set_model (function);
63 return result;
64 }
65
66 template<typename T>
67 T* convert (T* function,
68 typename T::Result (*method) (const typename T::Result&))
69 {
70 typedef typename T::Result (*M) (const typename T::Result&);
71 UnaryConvert<T,T,M>* result = new UnaryConvert<T,T,M> (method);
72 result->set_model (function);
73 return result;
74 }
75
76}
77
78#endif
Convert a function to another type.
Definition Convert.h:20
void set_model(From *_model)
Set the function to be converted.
Definition Convert.h:25
From * get_model()
Get the function to be converted.
Definition Convert.h:32
Convert a function to another type using a unary function.
Definition UnaryConvert.h:25
Method method
The Unary function used to complete the conversion.
Definition UnaryConvert.h:55
std::string get_name() const
Return the name of the class.
Definition UnaryConvert.h:32
virtual void calculate(typename To::Result &result, std::vector< typename To::Result > *grad)
Calculate the Mueller matrix and its gradient.
Definition UnaryConvert.h:39
Namespace in which all modeling and calibration related code is declared.
Definition ExampleComplex2.h:16

Generated using doxygen 1.14.0