casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //# $Id$
26 
27 #ifndef SCIMATH_INTERPOLATE1D_H
28 #define SCIMATH_INTERPOLATE1D_H
29 
30 #include <casacore/casa/aips.h>
33 
34 namespace casacore { //# NAMESPACE CASACORE - BEGIN
35 
36 template<class Range> class SampledFunctional;
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 
140 template <class Domain, class Range> class Interpolate1D :
141 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.
157  Interpolate1D();
158 
159  // Construct an object with the specified data
161  const SampledFunctional<Range> &y,
162  const Bool sorted=False,
163  const Bool uniq=False);
164 
165  // Define a new data set for the class to operate on. Equivalent in many
166  // aspects to creating a new object.
167  void setData(const SampledFunctional<Domain> &x,
168  const SampledFunctional<Range> &y,
169  const Bool sorted=False,
170  const Bool uniq=False);
171 
172  // The standard copy constructor, assignment operator and
173  // destructor. Internal data is copied in both cases (copy semantics)
174  // <group>
178  ~Interpolate1D();
179  // </group>
180 
181  // Name of function
182  virtual const String &name() const { static String x("interpolate1d");
183  return x; }
184 
185  // Interpolation is done using the () operator (see example above). Actual
186  // use is through the virtual <src>eval()</src> function.
187  virtual Range eval(typename Function1D<Domain, Range>::FunctionArg x)
188  const;
189 
190  // inquire/set the current interpolation method. uInts are used as
191  // arguments instead of the Interpolate1D::Method enumerator due to
192  // compiler limitations. See the example above (or the demo code) for the
193  // recommended way to call these functions.
194  // <group>
195  uInt getMethod() const {return curMethod;}
196  void setMethod(uInt method);
197  // </group>
198 
199  // Access the data set that interpolation is done over. This will usually be
200  // sorted.
201  // <group>
202  Vector<Domain> getX() const;
203  Vector<Range> getY() const;
204  // </group>
205 
206  // A function to copy the Interpolate1D object
207  // <group>
208  virtual Function<Domain, Range> *clone() const;
209  // </group>
210 
211 private:
212  // A private function for doing polynomial interpolation
213  Range polynomialInterpolation(const Domain x, uInt n, uInt offset) const;
214 
215  uInt curMethod; // interpolation method to use
216  uInt nElements; // how many elements in the data set
217  Block<Domain> xValues; // the abscissa of the data set (sorted)
218  Block<Range> yValues; // The corresponding ordinate of the data set
219  Block<Range> y2Values; // The numerical second derivates (only for splines)
220 
221 };
222 
223 
224 } //# NAMESPACE CASACORE - END
225 
226 #ifndef CASACORE_NO_AUTO_TEMPLATES
227 #include <casacore/scimath/Functionals/Interpolate1D.tcc>
228 #endif //# CASACORE_NO_AUTO_TEMPLATES
229 #endif
Interpolate1D()
The default constructor generates a useless object until the setData function has been called...
virtual const String & name() const
Name of function.
Interpolate in one dimension.
Range polynomialInterpolation(const Domain x, uInt n, uInt offset) const
A private function for doing polynomial interpolation.
void setMethod(uInt method)
A base class for indexing into arbitrary data types.
uInt getMethod() const
inquire/set the current interpolation method.
Vector< Range > getY() const
Crude but sometimes useful.
Numerical functional interface class.
virtual Function< Domain, Range > * clone() const
A function to copy the Interpolate1D object.
virtual Range eval(typename Function1D< Domain, Range >::FunctionArg x) const
Interpolation is done using the () operator (see example above).
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
The most common method and the Default.
const Bool False
Definition: aipstype.h:44
Block< Range > yValues
Interpolate1D< Domain, Range > & operator=(const Interpolate1D< Domain, Range > &other)
Vector< Domain > getX() const
Access the data set that interpolation is done over.
Numerical functional interface class for 1 dimension.
Definition: Function1D.h:75
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Natural Cubic Splines.
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.
Block< Range > y2Values
unsigned int uInt
Definition: aipstype.h:51
Fits a third order polynomial to 4 pts.
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42
Block< Domain > xValues