casacore
Loading...
Searching...
No Matches
Interpolate1D.h
Go to the documentation of this file.
1// # Interpolate1D.h: Interpolate in one dimension
2// # Copyright (C) 1996,1997,1999,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_INTERPOLATE1D_H
27#define SCIMATH_INTERPOLATE1D_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/scimath/Functionals/Function1D.h>
31#include <casacore/casa/Containers/Block.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35template <class Range>
37
38// <summary> Interpolate in one dimension </summary>
39
40// <use visibility=export>
41
42// <reviewed reviewer="wyoung" date="1996/10/18" tests="tInterpolate1D" demos="dInterpolate1D">
43// </reviewed>
44
45// <prerequisite>
46// <li> <linkto class=SampledFunctional>SampledFunctional</linkto>
47// <li> <linkto class=Function1D>Function1D</linkto>
48// </prerequisite>
49
50// <etymology>
51// The Interpolate1D class does interpolation in one dimension only.
52// </etymology>
53
54// <synopsis>
55// This class will, given the abscissa and ordinates of a set of one
56// dimensional data, interpolate on this data set giving the value at any
57// specified ordinate. It will extrapolate if necessary, but this is will
58// usually give a poor result. There is no requirement for the ordinates to
59// be regularly spaced, or even sorted in any numerical order. However each
60// abscissa should have a unique value.
61//
62// Interpolation can be done using the following methods:
63// <ul>
64// <li> Nearest Neighbour (default if there is one data point)
65// <li> Linear (default unless there is only one data point)
66// <li> Cubic Polynomial
67// <li> Natural Cubic Spline
68// </ul>
69//
70// The restriction that each abcissus has a unique value can be lifted
71// by setting the <src>uniq=True </src> option in the appropriate
72// functions. This imposes the following additional restrictions on
73// interpolation.
74// <ul>
75// <li> You cannot use cubic spline interpolation.
76// <li> You cannot cannot interpolate within two data points of a repeated
77// x-value when using cubic interpolation.
78// <li> You cannot interpolate within one data point of a repeated
79// x-value when using linear or nearest neighbour interpolation.
80// </ul>
81//
82// The abscissa must be a SampledFunctional that returns a scalar value that
83// can be ordered. ie. an uInt, Int, Float or Double (not Complex). The
84// ordinate can be any data type that has addition, and subtraction defined
85// as well as multiplication by a scalar. So the ordinate can be complex
86// numbers, where the interpolation is done separately on the real and
87// imaginary components, or an array, where the interpolation is done
88// separately on an element by element basis.
89//
90// This class will curently make an internal copy of the data supplied to
91// it, and sort the data if it is not told it is already sorted, by using
92// the <src> sorted=True </src> flag.
93// </synopsis>
94
95// <example>
96// This code fragment sets the interpolation method to cubic before
97// interpolating on the supplied (x,y) vectors.
98// <srcblock>
99// Vector<Float> x(4); indgen(x);
100// Vector<Double> y(4); indgen(y); y = y*y*y;
101// ScalarSampledFunctional<Float> fx(x)
102// ScalarSampledFunctional<Double> fy(y);
103// Interpolate1D<Float, Double> gain(fx, fy);
104// gain.setMethod(Interpolate1D<Float,Double>::cubic);
105// for (Float xs = -1; xs < 5; xs += 0.1)
106// cout << "gain(" << xs << "):" << gain(xs) << endl;
107// </srcblock>
108// </example>
109
110// <motivation>
111// This class is motivated by the need to interpolate over the gain
112// solutions obtained from calibrator observations, in order to get the gain
113// at arbitrary times.
114// </motivation>
115
116// <templating arg=Domain>
117// <li> The Domain class must be a type that can be ordered in a mathematical
118// sense. This includes uInt, Int, Float, Double, but not Complex.
119// </templating>
120
121// <templating arg=Range>
122// <li> The Range class must have addition and subtraction of Range objects with
123// each other as well as multiplication by a scalar defined. Besides the
124// scalar types listed above this includes Complex, DComplex, and Arrays of
125// any of these types.
126// </templating>
127
128// <thrown>
129// <li> AipsError
130// </thrown>
131
132// <todo asof="1996/10/22">
133// <li> avoid an internal copy of the data and have an index array as the
134// only private data (plus the interpolation method and pointers to
135// the actual data).
136// <li> Review the use of copy semantics in the copy constructor &
137// assignment operator after making the above change.
138// </todo>
139
140template <class Domain, class Range>
141class Interpolate1D : public Function1D<Domain, Range> {
142 public:
143 // The different interpolation methods are enumerated here
144 enum Method {
145 // Crude but sometimes useful
147 // The most common method and the Default
149 // Fits a third order polynomial to 4 pts
151 // Natural Cubic Splines
153 };
154
155 // The default constructor generates a useless object until the setData
156 // function has been called.
158
159 // Construct an object with the specified data
161 const Bool sorted = False, const Bool uniq = False);
162
163 // Define a new data set for the class to operate on. Equivalent in many
164 // aspects to creating a new object.
166 const Bool sorted = False, const Bool uniq = False);
167
168 // The standard copy constructor, assignment operator and
169 // destructor. Internal data is copied in both cases (copy semantics)
170 // <group>
174 // </group>
175
176 // Name of function
177 virtual const String &name() const {
178 static String x("interpolate1d");
179 return x;
180 }
181
182 // Interpolation is done using the () operator (see example above). Actual
183 // use is through the virtual <src>eval()</src> function.
184 virtual Range eval(typename Function1D<Domain, Range>::FunctionArg x) const;
185
186 // inquire/set the current interpolation method. uInts are used as
187 // arguments instead of the Interpolate1D::Method enumerator due to
188 // compiler limitations. See the example above (or the demo code) for the
189 // recommended way to call these functions.
190 // <group>
191 uInt getMethod() const { return curMethod; }
192 void setMethod(uInt method);
193 // </group>
194
195 // Access the data set that interpolation is done over. This will usually be
196 // sorted.
197 // <group>
200 // </group>
201
202 // A function to copy the Interpolate1D object
203 // <group>
205 // </group>
206
207 private:
208 // A private function for doing polynomial interpolation
209 Range polynomialInterpolation(const Domain x, uInt n, uInt offset) const;
210
211 uInt curMethod; // interpolation method to use
212 uInt nElements; // how many elements in the data set
213 Block<Domain> xValues; // the abscissa of the data set (sorted)
214 Block<Range> yValues; // The corresponding ordinate of the data set
215 Block<Range> y2Values; // The numerical second derivates (only for splines)
216};
217
218} // namespace casacore
219
220#ifndef CASACORE_NO_AUTO_TEMPLATES
221#include <casacore/scimath/Functionals/Interpolate1D.tcc>
222#endif // # CASACORE_NO_AUTO_TEMPLATES
223#endif
const T * FunctionArg
Definition Function1D.h:76
Range polynomialInterpolation(const Domain x, uInt n, uInt offset) const
A private function for doing polynomial interpolation.
void setData(const SampledFunctional< Domain > &x, const SampledFunctional< Range > &y, const Bool sorted=False, const Bool uniq=False)
Define a new data set for the class to operate on.
Interpolate1D(const SampledFunctional< Domain > &x, const SampledFunctional< Range > &y, const Bool sorted=False, const Bool uniq=False)
Construct an object with the specified data.
Method
The different interpolation methods are enumerated here.
@ nearestNeighbour
Crude but sometimes useful.
@ linear
The most common method and the Default.
@ cubic
Fits a third order polynomial to 4 pts.
@ spline
Natural Cubic Splines.
Vector< Range > getY() const
virtual Function< Domain, Range > * clone() const
A function to copy the Interpolate1D object.
Interpolate1D(const Interpolate1D< Domain, Range > &other)
The standard copy constructor, assignment operator and destructor.
Block< Domain > xValues
uInt getMethod() const
inquire/set the current interpolation method.
Interpolate1D()
The default constructor generates a useless object until the setData function has been called.
void setMethod(uInt method)
virtual const String & name() const
Name of function.
Interpolate1D< Domain, Range > & operator=(const Interpolate1D< Domain, Range > &other)
virtual Range eval(typename Function1D< Domain, Range >::FunctionArg x) const
Interpolation is done using the () operator (see example above).
Vector< Domain > getX() const
Access the data set that interpolation is done over.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
int offset(int, int) const
compute a linear offset from array indicies
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40