casacore
Loading...
Searching...
No Matches
ChebyshevParam.h
Go to the documentation of this file.
1// # ChebyshevParam.h: Parameter handling for Chebyshev polynomial
2// # Copyright (C) 2000,2001,2002,2003,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// #! ========================================================================
27
28#ifndef SCIMATH_CHEBYSHEVPARAM_H
29#define SCIMATH_CHEBYSHEVPARAM_H
30
31#include <casacore/casa/aips.h>
32#include <casacore/casa/Arrays/ArrayFwd.h>
33#include <casacore/casa/BasicSL/String.h>
34#include <casacore/scimath/Functionals/Function1D.h>
35
36namespace casacore { // # NAMESPACE CASACORE - BEGIN
37
38// # Forward Declarations
39class RecordInterface;
40
41// <summary>
42// Define enums for Chebyshev classes
43// </summary>
45 public:
46 // Modes that identify how this function behaves outside its Chebyshev
47 // interval (see setInterval()).
49
50 // return a constant, default value. The value returned is
51 // set with setDefault().
53
54 // return a constant value equal to the zero-th order coefficient
56
57 // evaluate the polynomial based on its coefficients just as it
58 // would be inside the interval. Thus, the function's range is not
59 // guaranteed to remain within the characteristic bounds of the
60 // Chebyshev interval.
62
63 // evaluate the function as if the range is cyclic, repeating the
64 // range values from its canonical domain. The period of the cycle
65 // will be equal to getIntervalMax()-getIntervalMin(). When the
66 // function is evaluated outside this interval, the input value will
67 // shifted an integer number of periods until it falls within the
68 // Chebyshev interval; the value returned is the polynomial evaluated
69 // at the shifted (x-axis) value. Obviously, this mode is most
70 // expensive computationally when evaluating outside the range.
72
73 // evaluate the function at nearest interval edge
75
76 // number of enumerators
78 };
79};
80
81// <summary> Parameter handling for Chebyshev polynomial parameters
82// </summary>
83
84// <use visibility=local>
85
86// <reviewed reviewer="wbrouw" date="2001/11/12" tests="tChebyshev" demos="">
87// </reviewed>
88
89// <prerequisite>
90// <li> <linkto class="FunctionParam">FunctionParam</linkto> class
91// <li> <linkto class="Function1D">Function1D</linkto>
92// <li> <linkto class="Chebyshev">Chebyshev</linkto>
93// </prerequisite>
94//
95// <etymology>
96// This class is named after Chebyshev Type I polynomials; it handles the
97// "fixed" parameters for the function.
98// </etymology>
99//
100// <synopsis>
101// This class assists in forming and evaluating a function as a
102// Chebyshev series, a linear combination of so-called Chebyshev
103// polynomials. Users do not instantiate this abstract class directly;
104// instead they instantiate the child class
105// <linkto class="Chebyshev">Chebyshev</linkto>. This class holds the part
106// of the implementation used by the
107// <linkto class="Chebyshev">Chebyshev</linkto> class that manages the "fixed"
108// parameters of the function (e.g. the polynomial coefficients, interval of
109// interest, etc.)
110//
111// For a full description, see the
112// <linkto class="Chebyshev">Chebyshev</linkto> class.
113//
114// </synopsis>
115//
116// <example>
117// In this example, a 2nd order Chebyshev polynomial series is
118// created.
119// <srcblock>
120// // set coeffs to desired values
121// Vector<Double> coeffs(3, 1);
122//
123// // configure the function
124// Chebyshev<Double> cheb;
125// cheb.setInterval(-0.8, 7.2);
126// cheb.setDefault(1.0);
127// cheb.setCoefficients(coeffs);
128//
129// // evaluate the function as necessary
130// Double z = cheb(-0.5); // -0.5 is within range, z = 0.78625
131// z = cheb(4.2); // 4.2 is within range, z = 0.375
132// z = cheb(-3); // -3 is out of the interval, z = 1
133// </srcblock>
134// </example>
135//
136// <motivation>
137// This class was created to support systematic errors in the simulator tool.
138// It can be used by Jones matrix classes to vary gains in a predictable way,
139// mimicing natural processes of the atmosphere or instrumental effects.
140//
141// The Chebyshev implementation is split between this class,
142// <src>ChebyshevParam</src> and its child
143// <linkto class="Chebyshev">Chebyshev</linkto> to better support the
144// <linkto class="AutoDiff">AutoDiff framework</linkto> for evaluating
145// derivatives.
146// </motivation>
147//
148// <templating arg=T>
149// <li> T should have standard numerical operators. Current
150// implementation only tested for real types (and their AutoDiffs).
151// </templating>
152//
153// <thrown>
154// <li> Assertion if indices out-of-range
155// </thrown>
156//
157// <todo asof="2001/08/22">
158// <li> It would be helpful to be able to convert to and from the
159// Polynomial<T> type; this would be supported via a function,
160// Polynomial<T> polynomial(), and constructor,
161// Chebyshev(Polynomial<T>)
162// </todo>
163
164template <class T>
165class ChebyshevParam : public Function1D<T> {
166 public:
167 // # Constructors
168 // create a zero-th order Chebyshev polynomial with the first coefficient
169 // equal to zero. The bounded domain is [T(-1), T(1)]. The
170 // OutOfDomainMode is CONSTANT, and the default value is T(0).
172
173 // create an n-th order Chebyshev polynomial with the coefficients
174 // equal to zero. The bounded domain is [T(-1), T(1)]. The
175 // OutOfDomainMode is CONSTANT, and the default value is T(0).
176 explicit ChebyshevParam(const uInt n);
177
178 // create a zero-th order Chebyshev polynomical with the first coefficient
179 // equal to one.
180 // min is the minimum value of its Chebyshev interval, and
181 // max is the maximum value.
182 // mode sets the behavior of the function outside the Chebyshev interval
183 // (see setOutOfIntervalMode() and OutOfIntervalMode enumeration
184 // definition for details).
185 // defval is the value returned when the function is evaluated outside
186 // the Chebyshev interval and mode=CONSTANT.
187 ChebyshevParam(const T &min, const T &max,
189 const T &defval = T(0));
190
191 // create a fully specified Chebyshev polynomial.
192 // coeffs holds the coefficients of the Chebyshev polynomial (see
193 // setCoefficients() for details).
194 // min is the minimum value of its canonical range, and
195 // max is the maximum value.
196 // mode sets the behavior of the function outside the Chebyshev interval
197 // (see setOutOfIntervalMode() and OutOfIntervalMode enumeration
198 // definition for details).
199 // defval is the value returned when the function is evaluated outside
200 // the canonical range and mode=CONSTANT.
201 ChebyshevParam(const Vector<T> &coeffs, const T &min, const T &max,
203 const T &defval = T(0));
204
205 // create a fully specified Chebyshev polynomial.
206 // config is a record that contains the non-coefficient data
207 // that configures this class.
208 // The fields recognized by this class are those documented for the
209 // setMode() function below.
210 // <group>
212 ChebyshevParam(const Vector<T> &coeffs, const RecordInterface &mode);
213 // </group>
214
215 // create a deep copy of another Chebyshev polynomial
216 // <group>
218 template <class W>
220 : Function1D<T>(other),
221 def_p(other.getDefault()),
222 minx_p(other.getIntervalMin()),
223 maxx_p(other.getIntervalMax()),
224 mode_p(other.getOutOfIntervalMode()) {}
225 // </group>
226
227 // make a (deep) copy of another Chebyshev polynomial
229
230 // Destructor
232
233 // set the Chebyshev coefficients.
234 // coeffs holds the coefficients in order, beginning with the zero-th
235 // order term. The order of the polynomial, then, would be the size
236 // of the Vector minus one.
237 void setCoefficients(const Vector<T> &coeffs);
238
239 // set a particular Chebyshev coefficient.
240 // which is the coefficient order (i.e. 0 refers to the constant offset).
241 // value is the coefficient value.
242 // If which is larger than current order of the function, the order will
243 // be increased to the value of which, and that coefficient is set to
244 // value; missing coefficients less than this value will be set to zero.
245 // Thus, the order can be increased with this function; however, it cannot
246 // be decreased (even if the highest order coefficient is set to zero).
247 // To lower the order, use setCoefficients() with a Vector having the
248 // desired number of coefficients.
249 void setCoefficient(const uInt which, const T &value);
250
251 // return the current set of coefficients into a given Vector.
253
254 // return a particular coefficient.
255 // which is the coefficient order (i.e. 0 refers to the constant offset).
256 // If which is out of range, zero is returned.
257 T getCoefficient(const uInt which) const {
258 return ((which < nparameters()) ? param_p[which] : T(0));
259 }
260
261 // return the number of coeefficients currently loaded. This does not
262 // guarantee that the coefficients are non-zero
263 uInt nCoefficients() const { return nparameters(); }
264
265 // set the Chebyshev interval for this function. The function will
266 // be scaled and shifted to such that the central bounded range of the
267 // Chebyshev polynomials ([-1, 1] in untransformed space) spans the
268 // given range.
269 // min is the minimum value for the interval, and
270 // max is the maximum value. See setOutOfIntervalMode() for the behavior
271 // of this function outside the set range.
272 void setInterval(T xmin, T xmax) {
273 if (xmin < xmax) {
274 minx_p = xmin;
275 maxx_p = xmax;
276 } else {
277 minx_p = xmax;
278 maxx_p = xmin;
279 }
280 }
281
282 // return the minimum value for the currently Chebyshev interval.
283 // See setInterval() for additional details.
284 T getIntervalMin() const { return minx_p; }
285
286 // return the maximum value for the currently Chebyshev interval.
287 // See setInterval() for additional details.
288 T getIntervalMax() const { return maxx_p; }
289
290 // set the behavior of this function when it is evaluated outside its
291 // Chebyshev interval
293
294 // return the behavior of this function when it is evaluated outside of
295 // its Chebyshev interval.
297
298 // set the default value of this function. This value is used when
299 // the getOutOfIntervalMode() returns Chebyshev::CONSTANT; it is returned
300 // when the a value outside of the Chebyshev interval is passed to
301 // the () operator.
302 void setDefault(const T &val) { def_p = val; }
303
304 // return the currently set default value. See setDefault() for details
305 // on the use of this value.
306 const T &getDefault() const { return def_p; }
307
308 // return the order of this polynomial. This returns the value of
309 // nCoefficients()-1;
310 uInt order() const { return param_p.nelements() - 1; }
311
312 // transform a set of Chebyshev polynomial coefficients into a set
313 // representing the series' derivative. coeffs should be assuming
314 // an interval of [-1, 1]. xmin and xmax can be provided to transform
315 // the series to another interval.
316 static void derivativeCoeffs(Vector<T> &coeffs, const T &xmin = T(-1), const T &xmax = T(1));
317
318 // convert a set of Chebyshev polynomial coefficients to power series
319 // coefficients. The values passed in coeffs are taken to
320 // be chebyshev coefficients; these values will be replaced with the
321 // power series coefficients. They should be ordered beginning
322 // with the zero-th order coefficient.
323 static void chebyshevToPower(Vector<T> &coeffs);
324
325 // convert a set of power series coefficients to Chebyshev
326 // polynomial coefficients. The values passed in coeffs are taken to
327 // be power series coefficients; these values will be replaced with the
328 // Chebyshev polynomial coefficients. They should be ordered beginning
329 // with the zero-th order coefficient.
330 static void powerToChebyshev(Vector<T> &coeffs);
331
332 // Give name of function
333 virtual const String &name() const {
334 static String x("chebyshev");
335 return x;
336 }
337
338 protected:
339 // Default value if outside interval
341 // Lowest interval bound
343 // Highest inetrval bound
345 // Out-of-interval handling type
347
349
350 // # Make members of parent classes known.
351 protected:
352 using Function1D<T>::param_p;
353
354 public:
356 using Function1D<T>::setMode;
357};
358
359// <summary> A ChebyshevParam with the get/setMode implementation </summary>
360//
361// <synopsis>
362// The get/setMode() implementation is separated from ChebyshevParam
363// to enable simple specialization for AutoDiff. See
364// <linkto class="ChebyshevParam">ChebyshevParam</linkto> for documentation
365// </synopsis>
366template <class T>
368 public:
370
371 explicit ChebyshevParamModeImpl(const uInt n) : ChebyshevParam<T>(n) {}
372
373 ChebyshevParamModeImpl(const T &min, const T &max,
375 const T &defval = T(0))
376 : ChebyshevParam<T>(min, max, mode, defval) {}
377
378 ChebyshevParamModeImpl(const Vector<T> &coeffs, const T &min, const T &max,
380 const T &defval = T(0))
381 : ChebyshevParam<T>(coeffs, min, max, mode, defval) {}
382
384 setMode(mode);
385 }
387 : ChebyshevParam<T>(coeffs, mode) {
388 setMode(mode);
389 }
390
392
393 // get/set the function mode. This is an alternate way to get/set the
394 // non-coefficient data for this function. The supported record fields
395 // are as follows:
396 // <pre>
397 // Field Name Type Role
398 // -------------------------------------------------------------------
399 // min template type the minimum value of the Chebyshev
400 // interval of interest
401 // max template type the maximum value of the Chebyshev
402 // interval of interest
403 // intervalMode TpString the out-of-interval mode; recognized
404 // values are "constant", "zeroth",
405 // "extrapolate", "cyclic", and "edge".
406 // setMode() recognizes a
407 // case-insensitive, minimum match.
408 // default template type the out-of-range value that is returned
409 // when the out-of-interval mode is
410 // "constant".
411 // </pre>
412 // An exception is thrown if interval mode is unrecognized.
413 // <group>
414 virtual void setMode(const RecordInterface &mode);
415 virtual void getMode(RecordInterface &mode) const;
416 // </group>
417
418 // return True if the implementing function supports a mode. This
419 // implementation always returns True.
420 virtual Bool hasMode() const;
421
422 // # Make members of parent classes known.
423 protected:
425
426 public:
432};
433
434#define ChebyshevParamModeImpl_PS ChebyshevParamModeImpl
435
436// <summary> Partial specialization of ChebyshevParamModeImpl for
437// <src>AutoDiff</src>
438// </summary>
439// <synopsis>
440// <note role=warning> The name <src>ChebyshevParamModeImpl_PS</src> is only
441// for cxx2html limitations.
442// </note>
443// </synopsis>
444template <class T>
445class ChebyshevParamModeImpl_PS<AutoDiff<T>> : public ChebyshevParam<AutoDiff<T>> {
446 public:
448
450
456
458 const Vector<AutoDiff<T>> &coeffs, const AutoDiff<T> &min, const AutoDiff<T> &max,
460 const AutoDiff<T> &defval = AutoDiff<T>(0))
461 : ChebyshevParam<AutoDiff<T>>(coeffs, min, max, mode, defval) {}
462
466 : ChebyshevParam<AutoDiff<T>>(coeffs, mode) {}
467
470
471 virtual void setMode(const RecordInterface &mode);
472 virtual void getMode(RecordInterface &mode) const;
473
474 // # Make members of parent classes known.
475 protected:
477
478 public:
484};
485
486#define ChebyshevParamModeImpl_PSA ChebyshevParamModeImpl
487
488// <summary> Partial specialization of ChebyshevParamModeImpl for
489// <src>AutoDiff</src>
490// </summary>
491// <synopsis>
492// <note role=warning> The name <src>ChebyshevParamModeImpl_PS</src> is only
493// for cxx2html limitations.
494// </note>
495// </synopsis>
496template <class T>
497class ChebyshevParamModeImpl_PSA<AutoDiffA<T>> : public ChebyshevParam<AutoDiffA<T>> {
498 public:
500
502
508
510 const Vector<AutoDiffA<T>> &coeffs, const AutoDiffA<T> &min, const AutoDiffA<T> &max,
512 const AutoDiffA<T> &defval = AutoDiffA<T>(0))
513 : ChebyshevParam<AutoDiffA<T>>(coeffs, min, max, mode, defval) {}
514
518 : ChebyshevParam<AutoDiffA<T>>(coeffs, mode) {}
519
522
523 virtual void setMode(const RecordInterface &mode);
524 virtual void getMode(RecordInterface &mode) const;
525
526 // # Make members of parent classes known.
527 protected:
529
530 public:
536};
537
538} // namespace casacore
539
540#ifndef CASACORE_NO_AUTO_TEMPLATES
541#include <casacore/scimath/Functionals/ChebyshevParam.tcc>
542#endif // # CASACORE_NO_AUTO_TEMPLATES
543#endif
OutOfIntervalMode
Modes that identify how this function behaves outside its Chebyshev interval (see setInterval()).
@ NOutOfIntervalModes
number of enumerators
@ CYCLIC
evaluate the function as if the range is cyclic, repeating the range values from its canonical domain...
@ CONSTANT
return a constant, default value.
@ EDGE
evaluate the function at nearest interval edge
@ ZEROTH
return a constant value equal to the zero-th order coefficient
@ EXTRAPOLATE
evaluate the polynomial based on its coefficients just as it would be inside the interval.
virtual void setMode(const RecordInterface &mode)
get/set the function mode.
ChebyshevParamModeImpl_PSA(const Vector< AutoDiffA< T > > &coeffs, const AutoDiffA< T > &min, const AutoDiffA< T > &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const AutoDiffA< T > &defval=AutoDiffA< T >(0))
ChebyshevParamModeImpl_PSA(uInt order, const RecordInterface &mode)
ChebyshevParamModeImpl_PSA(const AutoDiffA< T > &min, const AutoDiffA< T > &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const AutoDiffA< T > &defval=AutoDiffA< T >(0))
ChebyshevParamModeImpl_PSA(const ChebyshevParamModeImpl_PSA &other)
virtual void getMode(RecordInterface &mode) const
ChebyshevParamModeImpl_PSA(const Vector< AutoDiffA< T > > &coeffs, const RecordInterface &mode)
virtual void setMode(const RecordInterface &mode)
get/set the function mode.
ChebyshevParamModeImpl_PS(const AutoDiff< T > &min, const AutoDiff< T > &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const AutoDiff< T > &defval=AutoDiff< T >(0))
virtual void getMode(RecordInterface &mode) const
ChebyshevParamModeImpl_PS(const Vector< AutoDiff< T > > &coeffs, const AutoDiff< T > &min, const AutoDiff< T > &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const AutoDiff< T > &defval=AutoDiff< T >(0))
ChebyshevParamModeImpl_PS(uInt order, const RecordInterface &mode)
ChebyshevParamModeImpl_PS(const Vector< AutoDiff< T > > &coeffs, const RecordInterface &mode)
ChebyshevParamModeImpl_PS(const ChebyshevParamModeImpl_PS &other)
virtual Bool hasMode() const
return True if the implementing function supports a mode.
virtual void getMode(RecordInterface &mode) const
ChebyshevParamModeImpl(const ChebyshevParamModeImpl &other)
ChebyshevParamModeImpl(const Vector< T > &coeffs, const T &min, const T &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const T &defval=T(0))
ChebyshevParamModeImpl(const T &min, const T &max, typename ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const T &defval=T(0))
ChebyshevParamModeImpl(uInt order, const RecordInterface &mode)
virtual void setMode(const RecordInterface &mode)
get/set the function mode.
ChebyshevParamModeImpl(const Vector< T > &coeffs, const RecordInterface &mode)
ChebyshevParam(const uInt n)
create an n-th order Chebyshev polynomial with the coefficients equal to zero.
void setCoefficients(const Vector< T > &coeffs)
set the Chebyshev coefficients.
T getIntervalMin() const
return the minimum value for the currently Chebyshev interval.
void setCoefficient(const uInt which, const T &value)
set a particular Chebyshev coefficient.
T def_p
Default value if outside interval.
ChebyshevParam()
create a zero-th order Chebyshev polynomial with the first coefficient equal to zero.
void setDefault(const T &val)
set the default value of this function.
uInt order() const
return the order of this polynomial.
ChebyshevParam(uInt order, const RecordInterface &mode)
create a fully specified Chebyshev polynomial.
ChebyshevParam(const T &min, const T &max, ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const T &defval=T(0))
create a zero-th order Chebyshev polynomical with the first coefficient equal to one.
static void derivativeCoeffs(Vector< T > &coeffs, const T &xmin=T(-1), const T &xmax=T(1))
transform a set of Chebyshev polynomial coefficients into a set representing the series' derivative.
T getIntervalMax() const
return the maximum value for the currently Chebyshev interval.
ChebyshevEnums::OutOfIntervalMode getOutOfIntervalMode() const
return the behavior of this function when it is evaluated outside of its Chebyshev interval.
ChebyshevParam(const ChebyshevParam &other)
create a deep copy of another Chebyshev polynomial
static void powerToChebyshev(Vector< T > &coeffs)
convert a set of power series coefficients to Chebyshev polynomial coefficients.
ChebyshevParam(const ChebyshevParam< W > &other)
void setOutOfIntervalMode(ChebyshevEnums::OutOfIntervalMode mode)
set the behavior of this function when it is evaluated outside its Chebyshev interval
T maxx_p
Highest inetrval bound.
static Vector< String > modes_s
T getCoefficient(const uInt which) const
return a particular coefficient.
virtual const String & name() const
Give name of function.
const T & getDefault() const
return the currently set default value.
ChebyshevParam< T > & operator=(const ChebyshevParam< T > &other)
make a (deep) copy of another Chebyshev polynomial
virtual ~ChebyshevParam()
Destructor.
ChebyshevParam(const Vector< T > &coeffs, const T &min, const T &max, ChebyshevEnums::OutOfIntervalMode mode=ChebyshevEnums::CONSTANT, const T &defval=T(0))
create a fully specified Chebyshev polynomial.
ChebyshevParam(const Vector< T > &coeffs, const RecordInterface &mode)
uInt nCoefficients() const
return the number of coeefficients currently loaded.
ChebyshevEnums::OutOfIntervalMode mode_p
Out-of-interval handling type.
T minx_p
Lowest interval bound.
void setInterval(T xmin, T xmax)
set the Chebyshev interval for this function.
const Vector< T > & getCoefficients() const
return the current set of coefficients into a given Vector.
static void chebyshevToPower(Vector< T > &coeffs)
convert a set of Chebyshev polynomial coefficients to power series coefficients.
Function1D()
Constructors.
Definition Function1D.h:81
FunctionParam< T > param_p
Definition Function.h:337
virtual void setMode(const RecordInterface &mode)
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
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
RecordInterface()
The default constructor creates an empty record with a variable structure.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
NewDelAllocator< T > NewDelAllocator< T >::value
Definition Allocator.h:360