casacore
Loading...
Searching...
No Matches
FunctionWrapper.h
Go to the documentation of this file.
1// # FunctionWrapper.h: Construct function objects from C++ functions
2// # Copyright (C) 2001,2002,2005
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef SCIMATH_FUNCTIONWRAPPER_H
27#define SCIMATH_FUNCTIONWRAPPER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Functionals/WrapperParam.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <memory>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// # Forward declarations
38template <class T>
39class WrapperBase;
40
41// <summary> Construct nD function objects from C++ functions
42// </summary>
43//
44// <use visibility=export>
45//
46// <reviewed reviewer="" date="1996/02/22" tests="" demos="">
47// </reviewed>
48//
49// <prerequisite>
50// <li> <linkto class="Function">Function</linkto> class
51// <li> <linkto class="FunctionParam">FunctionParam</linkto>
52// </prerequisite>
53//
54// <synopsis>
55// This class is provided so that user can quickly construct a function
56// object from a C++ function pointer without having to write a function
57// class. The constructor constructs a function object from a function
58// pointer, and an optional parameter list.
59// Parameters are necessary if
60// the function has to be used in a functional fitting process (see
61// <linkto class=GenericL2Fit>GenericL2Fit</linkto>).
62//
63// The general function signature is <src>f(x;p)</src>, where <src>x</src>
64// represents the <em>arguments</em>, and <src>p</src> the parameters.
65// The allowed signatures of the function include all combinations of
66// arguments and parameters, and are:
67// <ul>
68// <li> <src>f()</src> no arguments e.g. random number or constant
69// <li> <src>f(x)</src> 1-dimensional, e.g. <src>sin(x)</src>
70// <li> <src>f(Vectorx)</src> n-dimensional, e.g. <src>sin(x+2y)</src>
71// </ul>
72//
73// </synopsis>
74//
75// <example>
76// <srcblock>
77// Float func(const Vector<Float>& x) {return x(0)*x(1);} // x*y
78// // Convert C++ functions to Functionals
79// FunctionWrapper<Float> Func(func,2);
80// </srcblock>
81//
82
83template <class T>
84class FunctionWrapper : public WrapperParam<T> {
85 public:
86 // # Constructors
87 // Default constructor, to enable arrays
89 // A function with no parameters and no arguments.
90 FunctionWrapper(T (*f)());
91 // A function with parameter and no arguments
92 // (Note value of isPar irrelevant)
93 FunctionWrapper(T (*f)(const T &), const Bool isPar);
94 // A function with parameters and no arguments.
95 // (Note value of isPar irrelevant)
96 FunctionWrapper(T (*f)(const Vector<T> &), const Bool isPar);
97 // Construct a 1-dimensional function with no parameters.
98 FunctionWrapper(T (*f)(const T &));
99 // Construct a 1-dimensional function with parameter.
100 FunctionWrapper(T (*f)(const T &, const T &), const T &par);
101 // Construct a 1-dimensional function with parameters.
102 FunctionWrapper(T (*f)(const T &, const Vector<T> &), const Vector<T> &par);
103 // Construct an n-dimensional function with no parameters.
104 FunctionWrapper(T (*f)(const Vector<T> &), const Int dim = 1);
105 // Construct an n-dimensional function with parameter.
106 FunctionWrapper(T (*f)(const Vector<T> &, const T &), const T &par, const uInt dim = 1);
107 // Construct an n-dimensional function with parameters.
108 FunctionWrapper(T (*f)(const Vector<T> &, const Vector<T> &), const Vector<T> &par,
109 const uInt dim = 1);
110 // Copy constructor (reference semantics)
111 // <group>
113 // </group>
114 // Copy assignment (reference semantics)
116
117 // Destructor
118 virtual ~FunctionWrapper() {}
119
120 // # Operators
121 // Evaluate the function at <src>x</src>.
122 // <group>
123 virtual T eval(typename Function<T>::FunctionArg x) const;
124 // </group>
125
126 // # Member functions
127 // Get the dimensionality
128 virtual uInt ndim() const;
129 // Return a copy of this object from the heap. The caller is responsible
130 // for deleting this pointer.
131 // <group>
132 virtual Function<T> *clone() const { return new FunctionWrapper<T>(*this); }
133 // </group>
134
135 protected:
136 // # Data
137 // The function aid object
138 std::shared_ptr<WrapperBase<T>> doit_p;
139
140 // # Make members of parent classes known.
141 protected:
142 using WrapperParam<T>::param_p;
143};
144
145} // namespace casacore
146
147#ifndef CASACORE_NO_AUTO_TEMPLATES
148#include <casacore/scimath/Functionals/FunctionWrapper.tcc>
149#endif // # CASACORE_NO_AUTO_TEMPLATES
150#endif
FunctionWrapper(T(*f)(const T &, const T &), const T &par)
Construct a 1-dimensional function with parameter.
virtual T eval(typename Function< T >::FunctionArg x) const
Evaluate the function at x.
FunctionWrapper(T(*f)(const T &, const Vector< T > &), const Vector< T > &par)
Construct a 1-dimensional function with parameters.
virtual ~FunctionWrapper()
Destructor.
FunctionWrapper(T(*f)(const Vector< T > &), const Int dim=1)
Construct an n-dimensional function with no parameters.
FunctionWrapper< T > & operator=(const FunctionWrapper< T > &other)
Copy assignment (reference semantics).
FunctionWrapper(T(*f)())
A function with no parameters and no arguments.
FunctionWrapper(const FunctionWrapper< T > &other)
Copy constructor (reference semantics).
FunctionWrapper()
Default constructor, to enable arrays.
FunctionWrapper(T(*f)(const Vector< T > &, const Vector< T > &), const Vector< T > &par, const uInt dim=1)
Construct an n-dimensional function with parameters.
virtual uInt ndim() const
Get the dimensionality.
FunctionWrapper(T(*f)(const T &), const Bool isPar)
A function with parameter and no arguments (Note value of isPar irrelevant).
FunctionWrapper(T(*f)(const Vector< T > &), const Bool isPar)
A function with parameters and no arguments.
FunctionWrapper(T(*f)(const T &))
Construct a 1-dimensional function with no parameters.
virtual Function< T > * clone() const
Return a copy of this object from the heap.
FunctionWrapper(T(*f)(const Vector< T > &, const T &), const T &par, const uInt dim=1)
Construct an n-dimensional function with parameter.
std::shared_ptr< WrapperBase< T > > doit_p
The function aid object.
FunctionParam< T > param_p
The parameters and masks.
Definition Function.h:337
const ArgType * FunctionArg
Definition Function.h:204
WrapperParam()
Construct with the given parameters.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40