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