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