lsst.afw  14.0-69-ge506bfb5f
FunctionLibrary.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H
26 #define LSST_AFW_MATH_FUNCTIONLIBRARY_H
27 /*
28  * Define a collection of useful Functions.
29  */
30 #include <algorithm>
31 #include <cmath>
32 
33 #include "lsst/afw/geom.h"
34 #include "lsst/afw/math/Function.h"
35 #include "lsst/afw/geom/Angle.h"
36 
37 namespace lsst {
38 namespace afw {
39 namespace math {
40 
41 using boost::serialization::make_nvp;
42 
53 template <typename ReturnT>
54 class IntegerDeltaFunction1 : public Function1<ReturnT> {
55 public:
59  explicit IntegerDeltaFunction1(double xo) : Function1<ReturnT>(0), _xo(xo) {}
60 
65  virtual ~IntegerDeltaFunction1() = default;
66 
69  }
70 
71  virtual ReturnT operator()(double x) const { return static_cast<ReturnT>(x == _xo); }
72 
73  virtual std::string toString(std::string const& prefix = "") const {
75  os << "IntegerDeltaFunction1 [" << _xo << "]: ";
76  os << Function1<ReturnT>::toString(prefix);
77  return os.str();
78  };
79 
80 private:
81  double _xo;
82 
83 protected:
84  /* Default constructor: intended only for serialization */
85  explicit IntegerDeltaFunction1() : Function1<ReturnT>(0), _xo(0.0) {}
86 
87 private:
89  template <class Archive>
90  void serialize(Archive& ar, unsigned int const version) {
91  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
92  ar& make_nvp("xo", this->_xo);
93  }
94 };
95 
106 template <typename ReturnT>
107 class IntegerDeltaFunction2 : public Function2<ReturnT> {
108 public:
112  explicit IntegerDeltaFunction2(double xo, double yo) : Function2<ReturnT>(0), _xo(xo), _yo(yo) {}
113 
114  IntegerDeltaFunction2(IntegerDeltaFunction2 const &) = default;
118  virtual ~IntegerDeltaFunction2() = default;
119 
122  }
123 
124  virtual ReturnT operator()(double x, double y) const {
125  return static_cast<ReturnT>((x == _xo) && (y == _yo));
126  }
127 
128  virtual std::string toString(std::string const& prefix) const {
130  os << "IntegerDeltaFunction2 [" << _xo << ", " << _yo << "]: ";
131  os << Function2<ReturnT>::toString(prefix);
132  return os.str();
133  }
134 
135 private:
136  double _xo;
137  double _yo;
138 
139 protected:
140  /* Default constructor: intended only for serialization */
141  explicit IntegerDeltaFunction2() : Function2<ReturnT>(0), _xo(0.0), _yo(0.0) {}
142 
143 private:
144  friend class boost::serialization::access;
145  template <class Archive>
146  void serialize(Archive& ar, unsigned int const version) {
147  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
148  ar& make_nvp("xo", this->_xo);
149  ar& make_nvp("yo", this->_yo);
150  }
151 };
152 
163 template <typename ReturnT>
164 class GaussianFunction1 : public Function1<ReturnT> {
165 public:
169  explicit GaussianFunction1(double sigma)
170  : Function1<ReturnT>(1),
171  _multFac(1.0 / std::sqrt(lsst::afw::geom::TWOPI)) {
172  this->_params[0] = sigma;
173  }
174  GaussianFunction1(GaussianFunction1 const &) = default;
175  GaussianFunction1(GaussianFunction1 &&) = default;
176  GaussianFunction1 & operator=(GaussianFunction1 const &) = default;
178  virtual ~GaussianFunction1() = default;
179 
182  }
183 
184  virtual ReturnT operator()(double x) const {
185  return static_cast<ReturnT>((_multFac / this->_params[0]) *
186  std::exp(-(x * x) / (2.0 * this->_params[0] * this->_params[0])));
187  }
188 
189  virtual std::string toString(std::string const& prefix) const {
191  os << "GaussianFunction1 [" << _multFac << "]: ";
192  os << Function1<ReturnT>::toString(prefix);
193  return os.str();
194  }
195 
196 private:
197  const double _multFac;
198 
199 protected:
200  /* Default constructor: intended only for serialization */
201  explicit GaussianFunction1() : Function1<ReturnT>(1), _multFac(1.0 / std::sqrt(lsst::afw::geom::TWOPI)) {}
202 
203 private:
204  friend class boost::serialization::access;
205  template <class Archive>
206  void serialize(Archive& ar, unsigned int const version) {
207  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
208  }
209 };
210 
225 template <typename ReturnT>
226 class GaussianFunction2 : public Function2<ReturnT> {
227 public:
231  explicit GaussianFunction2(double sigma1,
232  double sigma2,
233  double angle = 0.0)
234  : Function2<ReturnT>(3),
235  _multFac(1.0 / (lsst::afw::geom::TWOPI)) {
236  this->_params[0] = sigma1;
237  this->_params[1] = sigma2;
238  this->_params[2] = angle;
239  _updateCache();
240  }
241 
242  GaussianFunction2(GaussianFunction2 const &) = default;
243  GaussianFunction2(GaussianFunction2 &&) = default;
244  GaussianFunction2 & operator=(GaussianFunction2 const &) = default;
246  virtual ~GaussianFunction2() = default;
247 
250  new GaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
251  }
252 
253  virtual ReturnT operator()(double x, double y) const {
254  if (_angle != this->_params[2]) {
255  _updateCache();
256  }
257  double pos1 = (_cosAngle * x) + (_sinAngle * y);
258  double pos2 = (-_sinAngle * x) + (_cosAngle * y);
259  return static_cast<ReturnT>((_multFac / (this->_params[0] * this->_params[1])) *
260  std::exp(-((pos1 * pos1) / (2.0 * this->_params[0] * this->_params[0])) -
261  ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
262  }
263 
264  virtual std::string toString(std::string const& prefix) const {
266  os << "GaussianFunction2: ";
267  os << Function2<ReturnT>::toString(prefix);
268  return os.str();
269  }
270 
271  virtual bool isPersistable() const { return true; }
272 
273 protected:
274  virtual std::string getPersistenceName() const;
275 
276  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
277 
278 private:
296  void _updateCache() const {
297  _angle = this->_params[2];
298  _sinAngle = std::sin(_angle);
299  _cosAngle = std::cos(_angle);
300  }
301  const double _multFac;
302  mutable double _angle;
303  mutable double _sinAngle;
304  mutable double _cosAngle;
305 
306 protected:
307  /* Default constructor: intended only for serialization */
308  explicit GaussianFunction2()
309  : Function2<ReturnT>(3),
310  _multFac(1.0 / (lsst::afw::geom::TWOPI)),
311  _angle(0.0),
312  _sinAngle(0.0),
313  _cosAngle(1.0) {}
314 
315 private:
316  friend class boost::serialization::access;
317  template <class Archive>
318  void serialize(Archive& ar, unsigned int const version) {
319  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
320  ar& make_nvp("angle", this->_angle);
321  ar& make_nvp("sinAngle", this->_sinAngle);
322  ar& make_nvp("cosAngle", this->_cosAngle);
323  }
324 };
325 
340 template <typename ReturnT>
341 class DoubleGaussianFunction2 : public Function2<ReturnT> {
342 public:
347  double sigma1,
348  double sigma2 = 0,
349  double ampl2 = 0)
350  : Function2<ReturnT>(3),
351  _multFac(1.0 / (lsst::afw::geom::TWOPI)) {
352  this->_params[0] = sigma1;
353  this->_params[1] = sigma2;
354  this->_params[2] = ampl2;
355  }
356 
361  virtual ~DoubleGaussianFunction2() = default;
362 
365  new DoubleGaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
366  }
367 
368  virtual ReturnT operator()(double x, double y) const {
369  double radSq = (x * x) + (y * y);
370  double sigma1Sq = this->_params[0] * this->_params[0];
371  double sigma2Sq = this->_params[1] * this->_params[1];
372  double b = this->_params[2];
373  return static_cast<ReturnT>(
374  (_multFac / (sigma1Sq + (b * sigma2Sq))) *
375  (std::exp(-radSq / (2.0 * sigma1Sq)) + (b * std::exp(-radSq / (2.0 * sigma2Sq)))));
376  }
377 
378  virtual std::string toString(std::string const& prefix) const {
380  os << "DoubleGaussianFunction2 [" << _multFac << "]: ";
381  os << Function2<ReturnT>::toString(prefix);
382  return os.str();
383  }
384 
385  virtual bool isPersistable() const { return true; }
386 
387 protected:
388  virtual std::string getPersistenceName() const;
389 
390  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
391 
392 private:
393  const double _multFac;
394 
395 protected:
396  /* Default constructor: intended only for serialization */
397  explicit DoubleGaussianFunction2() : Function2<ReturnT>(3), _multFac(1.0 / (lsst::afw::geom::TWOPI)) {}
398 
399 private:
400  friend class boost::serialization::access;
401  template <class Archive>
402  void serialize(Archive& ar, unsigned int const version) {
403  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
404  }
405 };
406 
414 template <typename ReturnT>
415 class PolynomialFunction1 : public Function1<ReturnT> {
416 public:
422  explicit PolynomialFunction1(unsigned int order)
423  : Function1<ReturnT>(order + 1) {}
424 
433  : Function1<ReturnT>(params) {
434  if (params.size() < 1) {
436  "PolynomialFunction1 called with empty vector");
437  }
438  }
439 
440  PolynomialFunction1(PolynomialFunction1 const &) = default;
444  virtual ~PolynomialFunction1() = default;
445 
448  }
449 
450  virtual bool isLinearCombination() const { return true; };
451 
452  virtual ReturnT operator()(double x) const {
453  int const order = static_cast<int>(this->_params.size()) - 1;
454  double retVal = this->_params[order];
455  for (int ii = order - 1; ii >= 0; --ii) {
456  retVal = (retVal * x) + this->_params[ii];
457  }
458  return static_cast<ReturnT>(retVal);
459  }
460 
464  unsigned int getOrder() const { return this->getNParameters() - 1; };
465 
466  virtual std::string toString(std::string const& prefix) const {
468  os << "PolynomialFunction1 []: ";
469  os << Function1<ReturnT>::toString(prefix);
470  return os.str();
471  }
472 
473 protected:
474  /* Default constructor: intended only for serialization */
475  explicit PolynomialFunction1() : Function1<ReturnT>(1) {}
476 
477 private:
478  friend class boost::serialization::access;
479  template <class Archive>
480  void serialize(Archive& ar, unsigned int const version) {
481  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
482  }
483 };
484 
499 template <typename ReturnT>
501 public:
509  explicit PolynomialFunction2(unsigned int order)
510  : BasePolynomialFunction2<ReturnT>(order),
511  _oldY(0),
512  _xCoeffs(this->_order + 1) {}
513 
525  std::vector<double> params)
526  : BasePolynomialFunction2<ReturnT>(params),
528  _oldY(0),
529  _xCoeffs(this->_order + 1) {}
530 
531  PolynomialFunction2(PolynomialFunction2 const &) = default;
535  virtual ~PolynomialFunction2() = default;
536 
539  }
540 
541  virtual ReturnT operator()(double x, double y) const {
542  /* Solve as follows:
543  - f(x,y) = Cx0 + Cx1 x + Cx2 x^2 + Cx3 x^3 + ...
544  where:
545  Cx0 = P0 + P2 y + P5 y^2 + P9 y^3 + ...
546  Cx1 = P1 + P4 y + P8 y2 + ...
547  Cx2 = P3 + P7 y + ...
548  Cx3 = P6 + ...
549  ...
550 
551  Compute Cx0, Cx1...Cxn by solving 1-d polynomials in y in the usual way.
552  These values are cached and only recomputed for new values of Y or if the parameters change.
553 
554  Then compute f(x,y) by solving the 1-d polynomial in x in the usual way.
555  */
556  const int maxXCoeffInd = this->_order;
557 
558  if ((y != _oldY) || !this->_isCacheValid) {
559  // update _xCoeffs cache
560  // note: paramInd is decremented in both of the following loops
561  int paramInd = static_cast<int>(this->_params.size()) - 1;
562 
563  // initialize _xCoeffs to coeffs for pure y^n; e.g. for 3rd order:
564  // _xCoeffs[0] = _params[9], _xCoeffs[1] = _params[8], ... _xCoeffs[3] = _params[6]
565  for (int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
566  _xCoeffs[xCoeffInd] = this->_params[paramInd];
567  }
568 
569  // finish computing _xCoeffs
570  for (int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
571  _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->_params[paramInd];
572  ++xCoeffInd;
573  if (xCoeffInd >= endXCoeffInd) {
574  xCoeffInd = 0;
575  --endXCoeffInd;
576  }
577  }
578 
579  _oldY = y;
580  this->_isCacheValid = true;
581  }
582 
583  // use _xCoeffs to compute result
584  double retVal = _xCoeffs[maxXCoeffInd];
585  for (int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
586  retVal = (retVal * x) + _xCoeffs[xCoeffInd];
587  }
588  return static_cast<ReturnT>(retVal);
589  }
590 
595  virtual std::vector<double> getDFuncDParameters(double x, double y) const;
596 
597  virtual std::string toString(std::string const& prefix) const {
599  os << "PolynomialFunction2 [" << this->_order << "]: ";
600  os << Function2<ReturnT>::toString(prefix);
601  return os.str();
602  }
603 
604  virtual bool isPersistable() const { return true; }
605 
606 protected:
607  virtual std::string getPersistenceName() const;
608 
609  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
610 
611 private:
612  mutable double _oldY;
613  mutable std::vector<double> _xCoeffs;
614 
615 protected:
616  /* Default constructor: intended only for serialization */
617  explicit PolynomialFunction2() : BasePolynomialFunction2<ReturnT>(), _oldY(0), _xCoeffs(0) {}
618 
619 private:
620  friend class boost::serialization::access;
621  template <class Archive>
622  void serialize(Archive& ar, unsigned int const version) {
623  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
624  ar& make_nvp("yCoeffs", this->_xCoeffs); // sets size of _xCoeffs; name is historical
625  }
626 };
627 
644 template <typename ReturnT>
645 class Chebyshev1Function1 : public Function1<ReturnT> {
646 public:
652  explicit Chebyshev1Function1(unsigned int order,
653  double minX = -1,
654  double maxX = 1)
655  : Function1<ReturnT>(order + 1) {
656  _initialize(minX, maxX);
657  }
658 
667  double minX = -1,
668  double maxX = 1)
669  : Function1<ReturnT>(params) {
670  if (params.size() < 1) {
672  "Chebyshev1Function1 called with empty vector");
673  }
674  _initialize(minX, maxX);
675  }
676 
677  Chebyshev1Function1(Chebyshev1Function1 const &) = default;
681  virtual ~Chebyshev1Function1() = default;
682 
684  return std::shared_ptr<Function1<ReturnT>>(new Chebyshev1Function1(this->_params, _minX, _maxX));
685  }
686 
690  double getMinX() const { return _minX; };
691 
695  double getMaxX() const { return _maxX; };
696 
700  unsigned int getOrder() const { return this->getNParameters() - 1; };
701 
702  virtual bool isLinearCombination() const { return true; };
703 
704  virtual ReturnT operator()(double x) const {
705  double xPrime = (x + _offset) * _scale;
706 
707  // Clenshaw function for solving the Chebyshev polynomial
708  // Non-recursive version from Kresimir Cosic
709  int const order = _order;
710  if (order == 0) {
711  return this->_params[0];
712  } else if (order == 1) {
713  return this->_params[0] + (this->_params[1] * xPrime);
714  }
715  double cshPrev = this->_params[order];
716  double csh = (2 * xPrime * this->_params[order]) + this->_params[order - 1];
717  for (int i = order - 2; i > 0; --i) {
718  double cshNext = (2 * xPrime * csh) + this->_params[i] - cshPrev;
719  cshPrev = csh;
720  csh = cshNext;
721  }
722  return (xPrime * csh) + this->_params[0] - cshPrev;
723  }
724 
725  virtual std::string toString(std::string const& prefix) const {
727  os << "Chebyshev1Function1 [" << _minX << ", " << _maxX << "]: ";
728  os << Function1<ReturnT>::toString(prefix);
729  return os.str();
730  }
731 
732 private:
733  double _minX;
734  double _maxX;
735  double _scale;
736  double _offset;
737  unsigned int _order;
738 
742  void _initialize(double minX, double maxX) {
743  _minX = minX;
744  _maxX = maxX;
745  _scale = 2.0 / (_maxX - _minX);
746  _offset = -(_minX + _maxX) * 0.5;
747  _order = this->getNParameters() - 1;
748  }
749 
750 protected:
751  /* Default constructor: intended only for serialization */
753  : Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
754 
755 private:
756  friend class boost::serialization::access;
757  template <class Archive>
758  void serialize(Archive& ar, unsigned int const) {
759  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
760  ar& make_nvp("minX", this->_minX);
761  ar& make_nvp("minX", this->_minX);
762  ar& make_nvp("maxX", this->_maxX);
763  ar& make_nvp("scale", this->_scale);
764  ar& make_nvp("offset", this->_offset);
765  ar& make_nvp("maxInd", this->_order);
766  }
767 };
768 
794 template <typename ReturnT>
796 public:
802  explicit Chebyshev1Function2(unsigned int order,
804  lsst::afw::geom::Point2D(-1.0, -1.0),
805  lsst::afw::geom::Point2D(1.0, 1.0)))
806  : BasePolynomialFunction2<ReturnT>(order),
807  _oldYPrime(0),
808  _yCheby(this->_order + 1),
809  _xCoeffs(this->_order + 1) {
810  _initialize(xyRange);
811  }
812 
823  lsst::afw::geom::Point2D(-1.0, -1.0),
824  lsst::afw::geom::Point2D(1.0, 1.0)))
825  : BasePolynomialFunction2<ReturnT>(params),
826  _oldYPrime(0),
827  _yCheby(this->_order + 1),
828  _xCoeffs(this->_order + 1) {
829  _initialize(xyRange);
830  }
831 
832  Chebyshev1Function2(Chebyshev1Function2 const &) = default;
836  virtual ~Chebyshev1Function2() = default;
837 
840  new Chebyshev1Function2(this->_params, this->getXYRange()));
841  }
842 
848  lsst::afw::geom::Point2D(_maxX, _maxY));
849  };
850 
856  virtual Chebyshev1Function2 truncate(int truncOrder
857  ) const {
858  if (truncOrder > this->_order) {
860  os << "truncated order=" << truncOrder << " must be <= original order=" << this->_order;
862  }
863  int truncNParams = this->nParametersFromOrder(truncOrder);
864  std::vector<double> truncParams(this->_params.begin(), this->_params.begin() + truncNParams);
865  return Chebyshev1Function2(truncParams, this->getXYRange());
866  }
867 
868  virtual ReturnT operator()(double x, double y) const {
869  /* Solve as follows:
870  - f(x,y) = Cy0 T0(y') + Cy1 T1(y') + Cy2 T2(y') + Cy3 T3(y') + ...
871  where:
872  Cy0 = P0 T0(x') + P1 T1(x') + P3 T2(x') + P6 T3(x') + ...
873  Cy1 = P2 T0(x') + P4 T1(x') + P7 T2(x') + ...
874  Cy2 = P5 T0(x') + P8 T1(x') + ...
875  Cy3 = P9 T0(x') + ...
876  ...
877 
878  First compute Tn(x') for each n
879  Then use that to compute Cy0, Cy1, ...Cyn
880  Then solve the y Chebyshev polynomial using the Clenshaw algorithm
881  */
882  double const xPrime = (x + _offsetX) * _scaleX;
883  double const yPrime = (y + _offsetY) * _scaleY;
884 
885  const int nParams = static_cast<int>(this->_params.size());
886  const int order = this->_order;
887 
888  if (order == 0) {
889  return this->_params[0]; // No caching required
890  }
891 
892  if ((yPrime != _oldYPrime) || !this->_isCacheValid) {
893  // update cached _yCheby and _xCoeffs
894  _yCheby[0] = 1.0;
895  _yCheby[1] = yPrime;
896  for (int chebyInd = 2; chebyInd <= order; chebyInd++) {
897  _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
898  }
899 
900  for (int coeffInd = 0; coeffInd <= order; coeffInd++) {
901  _xCoeffs[coeffInd] = 0;
902  }
903  for (int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
904  _xCoeffs[coeffInd] += this->_params[paramInd] * _yCheby[endCoeffInd];
905  --coeffInd;
906  ++endCoeffInd;
907  if (coeffInd < 0) {
908  coeffInd = endCoeffInd;
909  endCoeffInd = 0;
910  }
911  }
912 
913  _oldYPrime = yPrime;
914  this->_isCacheValid = true;
915  }
916 
917  // Clenshaw function for solving the Chebyshev polynomial
918  // Non-recursive version from Kresimir Cosic
919  if (order == 1) {
920  return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
921  }
922  double cshPrev = _xCoeffs[order];
923  double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
924  for (int i = order - 2; i > 0; --i) {
925  double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
926  cshPrev = csh;
927  csh = cshNext;
928  }
929  return (xPrime * csh) + _xCoeffs[0] - cshPrev;
930  }
931 
932  virtual std::string toString(std::string const& prefix) const {
934  os << "Chebyshev1Function2 [";
935  os << this->_order << ", " << this->getXYRange() << "]";
936  os << Function2<ReturnT>::toString(prefix);
937  return os.str();
938  }
939 
940  virtual bool isPersistable() const { return true; }
941 
942 protected:
943  virtual std::string getPersistenceName() const;
944 
945  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
946 
947 private:
948  mutable double _oldYPrime;
949  mutable std::vector<double> _yCheby;
950  mutable std::vector<double> _xCoeffs;
951  double _minX;
952  double _minY;
953  double _maxX;
954  double _maxY;
955  double _scaleX;
956  double _scaleY;
957  double _offsetX;
958  double _offsetY;
959 
963  void _initialize(lsst::afw::geom::Box2D const& xyRange) {
964  _minX = xyRange.getMinX();
965  _minY = xyRange.getMinY();
966  _maxX = xyRange.getMaxX();
967  _maxY = xyRange.getMaxY();
968  _scaleX = 2.0 / (_maxX - _minX);
969  _scaleY = 2.0 / (_maxY - _minY);
970  _offsetX = -(_minX + _maxX) * 0.5;
971  _offsetY = -(_minY + _maxY) * 0.5;
972  }
973 
974 protected:
975  /* Default constructor: intended only for serialization */
977  : BasePolynomialFunction2<ReturnT>(),
978  _oldYPrime(0),
979  _yCheby(0),
980  _xCoeffs(0),
981  _minX(0.0),
982  _minY(0.0),
983  _maxX(0.0),
984  _maxY(0.0),
985  _scaleX(1.0),
986  _scaleY(1.0),
987  _offsetX(0.0),
988  _offsetY(0.0) {}
989 
990 private:
991  friend class boost::serialization::access;
992  template <class Archive>
993  void serialize(Archive& ar, unsigned int const version) {
994  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
995  ar& make_nvp("minX", this->_minX);
996  ar& make_nvp("minY", this->_minY);
997  ar& make_nvp("maxX", this->_maxX);
998  ar& make_nvp("maxY", this->_maxY);
999  ar& make_nvp("scaleX", this->_scaleX);
1000  ar& make_nvp("scaleY", this->_scaleY);
1001  ar& make_nvp("offsetX", this->_offsetX);
1002  ar& make_nvp("offsetY", this->_offsetY);
1003  ar& make_nvp("xCheby", this->_yCheby); // sets size of _yCheby; name is historical
1004  _xCoeffs.resize(_yCheby.size());
1005  }
1006 };
1007 
1020 template <typename ReturnT>
1021 class LanczosFunction1 : public Function1<ReturnT> {
1022 public:
1026  explicit LanczosFunction1(unsigned int n,
1027  double xOffset = 0.0)
1028  : Function1<ReturnT>(1),
1029  _invN(1.0 / static_cast<double>(n)) {
1030  this->_params[0] = xOffset;
1031  }
1032 
1033  LanczosFunction1(LanczosFunction1 const &) = default;
1034  LanczosFunction1(LanczosFunction1 &&) = default;
1035  LanczosFunction1 & operator=(LanczosFunction1 const &) = default;
1037  virtual ~LanczosFunction1() = default;
1038 
1040  return std::shared_ptr<Function1<ReturnT>>(new LanczosFunction1(this->getOrder(), this->_params[0]));
1041  }
1042 
1043  virtual ReturnT operator()(double x) const {
1044  double xArg1 = (x - this->_params[0]) * lsst::afw::geom::PI;
1045  double xArg2 = xArg1 * _invN;
1046  if (std::fabs(xArg1) > 1.0e-5) {
1047  return static_cast<ReturnT>(std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2));
1048  } else {
1049  return static_cast<ReturnT>(1);
1050  }
1051  }
1052 
1056  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1057 
1058  virtual std::string toString(std::string const& prefix) const {
1059  std::ostringstream os;
1060  os << "LanczosFunction1 [" << this->getOrder() << "]: ";
1061  ;
1062  os << Function1<ReturnT>::toString(prefix);
1063  return os.str();
1064  }
1065 
1066 private:
1067  double _invN; // == 1/n
1068 
1069 protected:
1070  /* Default constructor: intended only for serialization */
1071  explicit LanczosFunction1() : Function1<ReturnT>(1), _invN(1.0) {}
1072 
1073 private:
1074  friend class boost::serialization::access;
1075  template <class Archive>
1076  void serialize(Archive& ar, unsigned int const version) {
1077  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
1078  ar& make_nvp("invN", this->_invN);
1079  }
1080 };
1081 
1094 template <typename ReturnT>
1095 class LanczosFunction2 : public Function2<ReturnT> {
1096 public:
1100  explicit LanczosFunction2(unsigned int n,
1101  double xOffset = 0.0,
1102  double yOffset = 0.0)
1103  : Function2<ReturnT>(2),
1104  _invN(1.0 / static_cast<double>(n)) {
1105  this->_params[0] = xOffset;
1106  this->_params[1] = yOffset;
1107  }
1108 
1109  LanczosFunction2(LanczosFunction2 const &) = default;
1110  LanczosFunction2(LanczosFunction2 &&) = default;
1111  LanczosFunction2 & operator=(LanczosFunction2 const &) = default;
1113  virtual ~LanczosFunction2() = default;
1114 
1117  new LanczosFunction2(this->getOrder(), this->_params[0], this->_params[1]));
1118  }
1119 
1120  virtual ReturnT operator()(double x, double y) const {
1121  double xArg1 = (x - this->_params[0]) * lsst::afw::geom::PI;
1122  double xArg2 = xArg1 * _invN;
1123  double xFunc = 1;
1124  if (std::fabs(xArg1) > 1.0e-5) {
1125  xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1126  }
1127  double yArg1 = (y - this->_params[1]) * lsst::afw::geom::PI;
1128  double yArg2 = yArg1 * _invN;
1129  double yFunc = 1;
1130  if (std::fabs(yArg1) > 1.0e-5) {
1131  yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1132  }
1133  return static_cast<ReturnT>(xFunc * yFunc);
1134  }
1135 
1139  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1140 
1141  virtual std::string toString(std::string const& prefix) const {
1142  std::ostringstream os;
1143  os << "LanczosFunction2 [" << this->getOrder() << "]: ";
1144  ;
1145  os << Function2<ReturnT>::toString(prefix);
1146  return os.str();
1147  }
1148 
1149 private:
1150  double _invN;
1151 
1152 protected:
1153  /* Default constructor: intended only for serialization */
1154  explicit LanczosFunction2() : Function2<ReturnT>(2), _invN(1.0) {}
1155 
1156 private:
1157  friend class boost::serialization::access;
1158  template <class Archive>
1159  void serialize(Archive& ar, unsigned int const version) {
1160  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
1161  ar& make_nvp("invN", this->_invN);
1162  }
1163 };
1164 }
1165 }
1166 } // lsst::afw::math
1167 
1168 #endif // #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
virtual ReturnT operator()(double x) const
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
double getMaxX() const
Get maximum allowed x.
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
virtual ReturnT operator()(double x, double y) const
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
virtual ReturnT operator()(double x, double y) const
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual ReturnT operator()(double x, double y) const
An object passed to Persistable::write to allow it to persist itself.
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
T exp(T... args)
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
2-dimensional polynomial function with cross terms
PolynomialFunction1(std::vector< double > params)
Construct a polynomial function with the specified parameters.
1-dimensional weighted sum of Chebyshev polynomials of the first kind.
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual ReturnT operator()(double x) const
STL namespace.
Chebyshev1Function1(unsigned int order, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial of specified order and range.
2-dimensional integer delta function.
unsigned int getOrder() const
Get the polynomial order.
virtual Chebyshev1Function2 truncate(int truncOrder) const
Return a truncated copy of lower (or equal) order.
IntegerDeltaFunction1(double xo)
Construct an integer delta function with specified xo, yo.
unsigned int getOrder() const
Get the polynomial order.
double getMaxX() const
Definition: Box.h:343
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
LanczosFunction2(unsigned int n, double xOffset=0.0, double yOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.
IntegerDeltaFunction1 & operator=(IntegerDeltaFunction1 const &)=default
2-dimensional separable Lanczos function
A Function taking two arguments.
Definition: Function.h:277
friend class boost::serialization::access
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
double getMinX() const
Get minimum allowed x.
T resize(T... args)
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:351
STL class.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
T sin(T... args)
double constexpr PI
The ratio of a circle&#39;s circumference to diameter.
Definition: Angle.h:21
unsigned int getOrder() const
Get the order of Lanczos function.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
std::vector< double > _params
Definition: Function.h:189
virtual ReturnT operator()(double x, double y) const
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
A base class for image defects.
IntegerDeltaFunction2(double xo, double yo)
Construct an integer delta function with specified xo, yo.
1-dimensional integer delta function.
lsst::afw::geom::Box2D getXYRange() const
Get x,y range.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual ReturnT operator()(double x) const
virtual ReturnT operator()(double x) const
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
double getMaxY() const
Definition: Box.h:344
double getMinX() const
Definition: Box.h:339
A Function taking one argument.
Definition: Function.h:213
virtual ReturnT operator()(double x) const
T str(T... args)
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
PolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
T cos(T... args)
T fabs(T... args)
virtual ReturnT operator()(double x, double y) const
virtual ReturnT operator()(double x, double y) const
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
T size(T... args)
#define LSST_EXCEPT(type,...)
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
DoubleGaussianFunction2(double sigma1, double sigma2=0, double ampl2=0)
Construct a Gaussian function with specified x and y sigma.
T begin(T... args)
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
Chebyshev1Function2(unsigned int order, lsst::afw::geom::Box2D const &xyRange=lsst::afw::geom::Box2D(lsst::afw::geom::Point2D(-1.0, -1.0), lsst::afw::geom::Point2D(1.0, 1.0)))
Construct a Chebyshev polynomial of specified order and range.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:116
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
double constexpr TWOPI
Definition: Angle.h:22
unsigned int getOrder() const
Get the order of the Lanczos function.
1-dimensional polynomial function.
double getMinY() const
Definition: Box.h:340
Chebyshev1Function2(std::vector< double > params, lsst::afw::geom::Box2D const &xyRange=lsst::afw::geom::Box2D(lsst::afw::geom::Point2D(-1.0, -1.0), lsst::afw::geom::Point2D(1.0, 1.0)))
Construct a Chebyshev polynomial with specified parameters and range.
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.