lsst.afw  16.0-33-ge7f9a7920+1
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/geom.h"
34 #include "lsst/afw/math/Function.h"
35 #include "lsst/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  ~IntegerDeltaFunction1() noexcept override = default;
66 
69  }
70 
71  ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
72  return static_cast<ReturnT>(x == _xo);
73  }
74 
75  std::string toString(std::string const& prefix = "") const override {
77  os << "IntegerDeltaFunction1 [" << _xo << "]: ";
78  os << Function1<ReturnT>::toString(prefix);
79  return os.str();
80  };
81 
82 private:
83  double _xo;
84 
85 protected:
86  /* Default constructor: intended only for serialization */
87  explicit IntegerDeltaFunction1() : Function1<ReturnT>(0), _xo(0.0) {}
88 
89 private:
91  template <class Archive>
92  void serialize(Archive& ar, unsigned int const version) {
93  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
94  ar& make_nvp("xo", this->_xo);
95  }
96 };
97 
108 template <typename ReturnT>
109 class IntegerDeltaFunction2 : public Function2<ReturnT> {
110 public:
114  explicit IntegerDeltaFunction2(double xo, double yo) : Function2<ReturnT>(0), _xo(xo), _yo(yo) {}
115 
120  ~IntegerDeltaFunction2() noexcept override = default;
121 
124  }
125 
126  ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
127  return static_cast<ReturnT>((x == _xo) && (y == _yo));
128  }
129 
130  std::string toString(std::string const& prefix) const override {
132  os << "IntegerDeltaFunction2 [" << _xo << ", " << _yo << "]: ";
133  os << Function2<ReturnT>::toString(prefix);
134  return os.str();
135  }
136 
137 private:
138  double _xo;
139  double _yo;
140 
141 protected:
142  /* Default constructor: intended only for serialization */
143  explicit IntegerDeltaFunction2() : Function2<ReturnT>(0), _xo(0.0), _yo(0.0) {}
144 
145 private:
146  friend class boost::serialization::access;
147  template <class Archive>
148  void serialize(Archive& ar, unsigned int const version) {
149  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
150  ar& make_nvp("xo", this->_xo);
151  ar& make_nvp("yo", this->_yo);
152  }
153 };
154 
165 template <typename ReturnT>
166 class GaussianFunction1 : public Function1<ReturnT> {
167 public:
171  explicit GaussianFunction1(double sigma)
172  : Function1<ReturnT>(1), _multFac(1.0 / std::sqrt(lsst::geom::TWOPI)) {
173  this->_params[0] = sigma;
174  }
175  GaussianFunction1(GaussianFunction1 const&) = default;
177  GaussianFunction1& operator=(GaussianFunction1 const&) = default;
179  ~GaussianFunction1() noexcept override = default;
180 
183  }
184 
185  ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
186  return static_cast<ReturnT>((_multFac / this->_params[0]) *
187  std::exp(-(x * x) / (2.0 * this->_params[0] * this->_params[0])));
188  }
189 
190  std::string toString(std::string const& prefix) const override {
192  os << "GaussianFunction1 [" << _multFac << "]: ";
193  os << Function1<ReturnT>::toString(prefix);
194  return os.str();
195  }
196 
197 private:
198  const double _multFac;
199 
200 protected:
201  /* Default constructor: intended only for serialization */
202  explicit GaussianFunction1() : Function1<ReturnT>(1), _multFac(1.0 / std::sqrt(lsst::geom::TWOPI)) {}
203 
204 private:
205  friend class boost::serialization::access;
206  template <class Archive>
207  void serialize(Archive& ar, unsigned int const version) {
208  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
209  }
210 };
211 
226 template <typename ReturnT>
227 class GaussianFunction2 : public Function2<ReturnT> {
228 public:
232  explicit GaussianFunction2(double sigma1,
233  double sigma2,
234  double angle = 0.0)
235  : Function2<ReturnT>(3), _multFac(1.0 / (lsst::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;
244  GaussianFunction2& operator=(GaussianFunction2 const&) = default;
246  ~GaussianFunction2() noexcept override = default;
247 
250  new GaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
251  }
252 
253  ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
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  std::string toString(std::string const& prefix) const override {
266  os << "GaussianFunction2: ";
267  os << Function2<ReturnT>::toString(prefix);
268  return os.str();
269  }
270 
271  bool isPersistable() const noexcept override { return true; }
272 
273 protected:
274  std::string getPersistenceName() const override;
275 
276  void write(afw::table::io::OutputArchiveHandle& handle) const override;
277 
278 private:
296  void _updateCache() const noexcept {
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::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), _multFac(1.0 / (lsst::geom::TWOPI)) {
351  this->_params[0] = sigma1;
352  this->_params[1] = sigma2;
353  this->_params[2] = ampl2;
354  }
355 
360  ~DoubleGaussianFunction2() noexcept override = default;
361 
364  new DoubleGaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
365  }
366 
367  ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
368  double radSq = (x * x) + (y * y);
369  double sigma1Sq = this->_params[0] * this->_params[0];
370  double sigma2Sq = this->_params[1] * this->_params[1];
371  double b = this->_params[2];
372  return static_cast<ReturnT>(
373  (_multFac / (sigma1Sq + (b * sigma2Sq))) *
374  (std::exp(-radSq / (2.0 * sigma1Sq)) + (b * std::exp(-radSq / (2.0 * sigma2Sq)))));
375  }
376 
377  std::string toString(std::string const& prefix) const override {
379  os << "DoubleGaussianFunction2 [" << _multFac << "]: ";
380  os << Function2<ReturnT>::toString(prefix);
381  return os.str();
382  }
383 
384  bool isPersistable() const noexcept override { return true; }
385 
386 protected:
387  std::string getPersistenceName() const override;
388 
389  void write(afw::table::io::OutputArchiveHandle& handle) const override;
390 
391 private:
392  const double _multFac;
393 
394 protected:
395  /* Default constructor: intended only for serialization */
396  explicit DoubleGaussianFunction2() : Function2<ReturnT>(3), _multFac(1.0 / (lsst::geom::TWOPI)) {}
397 
398 private:
399  friend class boost::serialization::access;
400  template <class Archive>
401  void serialize(Archive& ar, unsigned int const version) {
402  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
403  }
404 };
405 
413 template <typename ReturnT>
414 class PolynomialFunction1 : public Function1<ReturnT> {
415 public:
421  explicit PolynomialFunction1(unsigned int order)
422  : Function1<ReturnT>(order + 1) {}
423 
432  : Function1<ReturnT>(params) {
433  if (params.size() < 1) {
435  "PolynomialFunction1 called with empty vector");
436  }
437  }
438 
439  PolynomialFunction1(PolynomialFunction1 const&) = default;
443  ~PolynomialFunction1() noexcept override = default;
444 
447  }
448 
449  bool isLinearCombination() const noexcept override { return true; };
450 
451  ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
452  int const order = static_cast<int>(this->_params.size()) - 1;
453  double retVal = this->_params[order];
454  for (int ii = order - 1; ii >= 0; --ii) {
455  retVal = (retVal * x) + this->_params[ii];
456  }
457  return static_cast<ReturnT>(retVal);
458  }
459 
463  unsigned int getOrder() const noexcept { return this->getNParameters() - 1; };
464 
465  std::string toString(std::string const& prefix) const override {
467  os << "PolynomialFunction1 []: ";
468  os << Function1<ReturnT>::toString(prefix);
469  return os.str();
470  }
471 
472 protected:
473  /* Default constructor: intended only for serialization */
474  explicit PolynomialFunction1() : Function1<ReturnT>(1) {}
475 
476 private:
477  friend class boost::serialization::access;
478  template <class Archive>
479  void serialize(Archive& ar, unsigned int const version) {
480  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
481  }
482 };
483 
498 template <typename ReturnT>
500 public:
508  explicit PolynomialFunction2(unsigned int order)
509  : BasePolynomialFunction2<ReturnT>(order), _oldY(0), _xCoeffs(this->_order + 1) {}
510 
522  std::vector<double> params)
523  : BasePolynomialFunction2<ReturnT>(params), _oldY(0), _xCoeffs(this->_order + 1) {}
525 
526  PolynomialFunction2(PolynomialFunction2 const&) = default;
530  ~PolynomialFunction2() noexcept override = default;
531 
534  }
535 
536  ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
537  /* Solve as follows:
538  - f(x,y) = Cx0 + Cx1 x + Cx2 x^2 + Cx3 x^3 + ...
539  where:
540  Cx0 = P0 + P2 y + P5 y^2 + P9 y^3 + ...
541  Cx1 = P1 + P4 y + P8 y2 + ...
542  Cx2 = P3 + P7 y + ...
543  Cx3 = P6 + ...
544  ...
545 
546  Compute Cx0, Cx1...Cxn by solving 1-d polynomials in y in the usual way.
547  These values are cached and only recomputed for new values of Y or if the parameters change.
548 
549  Then compute f(x,y) by solving the 1-d polynomial in x in the usual way.
550  */
551  const int maxXCoeffInd = this->_order;
552 
553  if ((y != _oldY) || !this->_isCacheValid) {
554  // update _xCoeffs cache
555  // note: paramInd is decremented in both of the following loops
556  int paramInd = static_cast<int>(this->_params.size()) - 1;
557 
558  // initialize _xCoeffs to coeffs for pure y^n; e.g. for 3rd order:
559  // _xCoeffs[0] = _params[9], _xCoeffs[1] = _params[8], ... _xCoeffs[3] = _params[6]
560  for (int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
561  _xCoeffs[xCoeffInd] = this->_params[paramInd];
562  }
563 
564  // finish computing _xCoeffs
565  for (int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
566  _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->_params[paramInd];
567  ++xCoeffInd;
568  if (xCoeffInd >= endXCoeffInd) {
569  xCoeffInd = 0;
570  --endXCoeffInd;
571  }
572  }
573 
574  _oldY = y;
575  this->_isCacheValid = true;
576  }
577 
578  // use _xCoeffs to compute result
579  double retVal = _xCoeffs[maxXCoeffInd];
580  for (int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
581  retVal = (retVal * x) + _xCoeffs[xCoeffInd];
582  }
583  return static_cast<ReturnT>(retVal);
584  }
585 
590  std::vector<double> getDFuncDParameters(double x, double y) const override;
591 
592  std::string toString(std::string const& prefix) const override {
594  os << "PolynomialFunction2 [" << this->_order << "]: ";
595  os << Function2<ReturnT>::toString(prefix);
596  return os.str();
597  }
598 
599  bool isPersistable() const noexcept override { return true; }
600 
601 protected:
602  std::string getPersistenceName() const override;
603 
604  void write(afw::table::io::OutputArchiveHandle& handle) const override;
605 
606 private:
607  mutable double _oldY;
608  mutable std::vector<double> _xCoeffs;
609 
610 protected:
611  /* Default constructor: intended only for serialization */
612  explicit PolynomialFunction2() : BasePolynomialFunction2<ReturnT>(), _oldY(0), _xCoeffs(0) {}
613 
614 private:
615  friend class boost::serialization::access;
616  template <class Archive>
617  void serialize(Archive& ar, unsigned int const version) {
618  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
619  ar& make_nvp("yCoeffs", this->_xCoeffs); // sets size of _xCoeffs; name is historical
620  }
621 };
622 
639 template <typename ReturnT>
640 class Chebyshev1Function1 : public Function1<ReturnT> {
641 public:
647  explicit Chebyshev1Function1(unsigned int order,
648  double minX = -1,
649  double maxX = 1)
650  : Function1<ReturnT>(order + 1) {
651  _initialize(minX, maxX);
652  }
653 
662  double minX = -1,
663  double maxX = 1)
664  : Function1<ReturnT>(params) {
665  if (params.size() < 1) {
667  "Chebyshev1Function1 called with empty vector");
668  }
669  _initialize(minX, maxX);
670  }
671 
672  Chebyshev1Function1(Chebyshev1Function1 const&) = default;
676  ~Chebyshev1Function1() noexcept override = default;
677 
679  return std::shared_ptr<Function1<ReturnT>>(new Chebyshev1Function1(this->_params, _minX, _maxX));
680  }
681 
685  double getMinX() const noexcept { return _minX; };
686 
690  double getMaxX() const noexcept { return _maxX; };
691 
695  unsigned int getOrder() const noexcept { return this->getNParameters() - 1; };
696 
697  bool isLinearCombination() const noexcept override { return true; };
698 
699  ReturnT operator()(double x) const override {
700  double xPrime = (x + _offset) * _scale;
701 
702  // Clenshaw function for solving the Chebyshev polynomial
703  // Non-recursive version from Kresimir Cosic
704  int const order = _order;
705  if (order == 0) {
706  return this->_params[0];
707  } else if (order == 1) {
708  return this->_params[0] + (this->_params[1] * xPrime);
709  }
710  double cshPrev = this->_params[order];
711  double csh = (2 * xPrime * this->_params[order]) + this->_params[order - 1];
712  for (int i = order - 2; i > 0; --i) {
713  double cshNext = (2 * xPrime * csh) + this->_params[i] - cshPrev;
714  cshPrev = csh;
715  csh = cshNext;
716  }
717  return (xPrime * csh) + this->_params[0] - cshPrev;
718  }
719 
720  std::string toString(std::string const& prefix) const override {
722  os << "Chebyshev1Function1 [" << _minX << ", " << _maxX << "]: ";
723  os << Function1<ReturnT>::toString(prefix);
724  return os.str();
725  }
726 
727 private:
728  double _minX;
729  double _maxX;
730  double _scale;
731  double _offset;
732  unsigned int _order;
733 
737  void _initialize(double minX, double maxX) {
738  _minX = minX;
739  _maxX = maxX;
740  _scale = 2.0 / (_maxX - _minX);
741  _offset = -(_minX + _maxX) * 0.5;
742  _order = this->getNParameters() - 1;
743  }
744 
745 protected:
746  /* Default constructor: intended only for serialization */
748  : Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
749 
750 private:
751  friend class boost::serialization::access;
752  template <class Archive>
753  void serialize(Archive& ar, unsigned int const) {
754  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
755  ar& make_nvp("minX", this->_minX);
756  ar& make_nvp("minX", this->_minX);
757  ar& make_nvp("maxX", this->_maxX);
758  ar& make_nvp("scale", this->_scale);
759  ar& make_nvp("offset", this->_offset);
760  ar& make_nvp("maxInd", this->_order);
761  }
762 };
763 
789 template <typename ReturnT>
791 public:
797  explicit Chebyshev1Function2(unsigned int order,
798  lsst::geom::Box2D const& xyRange = lsst::geom::Box2D(
799  lsst::geom::Point2D(-1.0, -1.0),
800  lsst::geom::Point2D(1.0, 1.0)))
801  : BasePolynomialFunction2<ReturnT>(order),
802  _oldYPrime(0),
803  _yCheby(this->_order + 1),
804  _xCoeffs(this->_order + 1) {
805  _initialize(xyRange);
806  }
807 
816  lsst::geom::Box2D const& xyRange = lsst::geom::Box2D(
818  lsst::geom::Point2D(-1.0, -1.0),
819  lsst::geom::Point2D(1.0, 1.0)))
820  : BasePolynomialFunction2<ReturnT>(params),
821  _oldYPrime(0),
822  _yCheby(this->_order + 1),
823  _xCoeffs(this->_order + 1) {
824  _initialize(xyRange);
825  }
826 
827  Chebyshev1Function2(Chebyshev1Function2 const&) = default;
831  ~Chebyshev1Function2() noexcept override = default;
832 
835  new Chebyshev1Function2(this->_params, this->getXYRange()));
836  }
837 
841  lsst::geom::Box2D getXYRange() const noexcept {
842  return lsst::geom::Box2D(lsst::geom::Point2D(_minX, _minY), lsst::geom::Point2D(_maxX, _maxY));
843  };
844 
850  virtual Chebyshev1Function2 truncate(int truncOrder
851  ) const {
852  if (truncOrder > this->_order) {
854  os << "truncated order=" << truncOrder << " must be <= original order=" << this->_order;
856  }
857  int truncNParams = this->nParametersFromOrder(truncOrder);
858  std::vector<double> truncParams(this->_params.begin(), this->_params.begin() + truncNParams);
859  return Chebyshev1Function2(truncParams, this->getXYRange());
860  }
861 
862  ReturnT operator()(double x, double y) const override {
863  /* Solve as follows:
864  - f(x,y) = Cy0 T0(y') + Cy1 T1(y') + Cy2 T2(y') + Cy3 T3(y') + ...
865  where:
866  Cy0 = P0 T0(x') + P1 T1(x') + P3 T2(x') + P6 T3(x') + ...
867  Cy1 = P2 T0(x') + P4 T1(x') + P7 T2(x') + ...
868  Cy2 = P5 T0(x') + P8 T1(x') + ...
869  Cy3 = P9 T0(x') + ...
870  ...
871 
872  First compute Tn(x') for each n
873  Then use that to compute Cy0, Cy1, ...Cyn
874  Then solve the y Chebyshev polynomial using the Clenshaw algorithm
875  */
876  double const xPrime = (x + _offsetX) * _scaleX;
877  double const yPrime = (y + _offsetY) * _scaleY;
878 
879  const int nParams = static_cast<int>(this->_params.size());
880  const int order = this->_order;
881 
882  if (order == 0) {
883  return this->_params[0]; // No caching required
884  }
885 
886  if ((yPrime != _oldYPrime) || !this->_isCacheValid) {
887  // update cached _yCheby and _xCoeffs
888  _yCheby[0] = 1.0;
889  _yCheby[1] = yPrime;
890  for (int chebyInd = 2; chebyInd <= order; chebyInd++) {
891  _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
892  }
893 
894  for (int coeffInd = 0; coeffInd <= order; coeffInd++) {
895  _xCoeffs[coeffInd] = 0;
896  }
897  for (int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
898  _xCoeffs[coeffInd] += this->_params[paramInd] * _yCheby[endCoeffInd];
899  --coeffInd;
900  ++endCoeffInd;
901  if (coeffInd < 0) {
902  coeffInd = endCoeffInd;
903  endCoeffInd = 0;
904  }
905  }
906 
907  _oldYPrime = yPrime;
908  this->_isCacheValid = true;
909  }
910 
911  // Clenshaw function for solving the Chebyshev polynomial
912  // Non-recursive version from Kresimir Cosic
913  if (order == 1) {
914  return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
915  }
916  double cshPrev = _xCoeffs[order];
917  double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
918  for (int i = order - 2; i > 0; --i) {
919  double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
920  cshPrev = csh;
921  csh = cshNext;
922  }
923  return (xPrime * csh) + _xCoeffs[0] - cshPrev;
924  }
925 
926  std::string toString(std::string const& prefix) const override {
928  os << "Chebyshev1Function2 [";
929  os << this->_order << ", " << this->getXYRange() << "]";
930  os << Function2<ReturnT>::toString(prefix);
931  return os.str();
932  }
933 
934  bool isPersistable() const noexcept override { return true; }
935 
936 protected:
937  std::string getPersistenceName() const override;
938 
939  void write(afw::table::io::OutputArchiveHandle& handle) const override;
940 
941 private:
942  mutable double _oldYPrime;
943  mutable std::vector<double> _yCheby;
944  mutable std::vector<double> _xCoeffs;
945  double _minX;
946  double _minY;
947  double _maxX;
948  double _maxY;
949  double _scaleX;
950  double _scaleY;
951  double _offsetX;
952  double _offsetY;
953 
957  void _initialize(lsst::geom::Box2D const& xyRange) {
958  _minX = xyRange.getMinX();
959  _minY = xyRange.getMinY();
960  _maxX = xyRange.getMaxX();
961  _maxY = xyRange.getMaxY();
962  _scaleX = 2.0 / (_maxX - _minX);
963  _scaleY = 2.0 / (_maxY - _minY);
964  _offsetX = -(_minX + _maxX) * 0.5;
965  _offsetY = -(_minY + _maxY) * 0.5;
966  }
967 
968 protected:
969  /* Default constructor: intended only for serialization */
971  : BasePolynomialFunction2<ReturnT>(),
972  _oldYPrime(0),
973  _yCheby(0),
974  _xCoeffs(0),
975  _minX(0.0),
976  _minY(0.0),
977  _maxX(0.0),
978  _maxY(0.0),
979  _scaleX(1.0),
980  _scaleY(1.0),
981  _offsetX(0.0),
982  _offsetY(0.0) {}
983 
984 private:
985  friend class boost::serialization::access;
986  template <class Archive>
987  void serialize(Archive& ar, unsigned int const version) {
988  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
989  ar& make_nvp("minX", this->_minX);
990  ar& make_nvp("minY", this->_minY);
991  ar& make_nvp("maxX", this->_maxX);
992  ar& make_nvp("maxY", this->_maxY);
993  ar& make_nvp("scaleX", this->_scaleX);
994  ar& make_nvp("scaleY", this->_scaleY);
995  ar& make_nvp("offsetX", this->_offsetX);
996  ar& make_nvp("offsetY", this->_offsetY);
997  ar& make_nvp("xCheby", this->_yCheby); // sets size of _yCheby; name is historical
998  _xCoeffs.resize(_yCheby.size());
999  }
1000 };
1001 
1014 template <typename ReturnT>
1015 class LanczosFunction1 : public Function1<ReturnT> {
1016 public:
1020  explicit LanczosFunction1(unsigned int n,
1021  double xOffset = 0.0)
1022  : Function1<ReturnT>(1), _invN(1.0 / static_cast<double>(n)) {
1023  this->_params[0] = xOffset;
1024  }
1025 
1026  LanczosFunction1(LanczosFunction1 const&) = default;
1027  LanczosFunction1(LanczosFunction1&&) = default;
1028  LanczosFunction1& operator=(LanczosFunction1 const&) = default;
1030  ~LanczosFunction1() noexcept override = default;
1031 
1033  return std::shared_ptr<Function1<ReturnT>>(new LanczosFunction1(this->getOrder(), this->_params[0]));
1034  }
1035 
1036  ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
1037  double xArg1 = (x - this->_params[0]) * lsst::geom::PI;
1038  double xArg2 = xArg1 * _invN;
1039  if (std::fabs(xArg1) > 1.0e-5) {
1040  return static_cast<ReturnT>(std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2));
1041  } else {
1042  return static_cast<ReturnT>(1);
1043  }
1044  }
1045 
1049  unsigned int getOrder() const noexcept { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1050 
1051  std::string toString(std::string const& prefix) const override {
1053  os << "LanczosFunction1 [" << this->getOrder() << "]: ";
1054  ;
1055  os << Function1<ReturnT>::toString(prefix);
1056  return os.str();
1057  }
1058 
1059 private:
1060  double _invN; // == 1/n
1061 
1062 protected:
1063  /* Default constructor: intended only for serialization */
1064  explicit LanczosFunction1() : Function1<ReturnT>(1), _invN(1.0) {}
1065 
1066 private:
1067  friend class boost::serialization::access;
1068  template <class Archive>
1069  void serialize(Archive& ar, unsigned int const version) {
1070  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
1071  ar& make_nvp("invN", this->_invN);
1072  }
1073 };
1074 
1087 template <typename ReturnT>
1088 class LanczosFunction2 : public Function2<ReturnT> {
1089 public:
1093  explicit LanczosFunction2(unsigned int n,
1094  double xOffset = 0.0,
1095  double yOffset = 0.0)
1096  : Function2<ReturnT>(2), _invN(1.0 / static_cast<double>(n)) {
1097  this->_params[0] = xOffset;
1098  this->_params[1] = yOffset;
1099  }
1100 
1101  LanczosFunction2(LanczosFunction2 const&) = default;
1102  LanczosFunction2(LanczosFunction2&&) = default;
1103  LanczosFunction2& operator=(LanczosFunction2 const&) = default;
1105  ~LanczosFunction2() noexcept override = default;
1106 
1109  new LanczosFunction2(this->getOrder(), this->_params[0], this->_params[1]));
1110  }
1111 
1112  ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
1113  double xArg1 = (x - this->_params[0]) * lsst::geom::PI;
1114  double xArg2 = xArg1 * _invN;
1115  double xFunc = 1;
1116  if (std::fabs(xArg1) > 1.0e-5) {
1117  xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1118  }
1119  double yArg1 = (y - this->_params[1]) * lsst::geom::PI;
1120  double yArg2 = yArg1 * _invN;
1121  double yFunc = 1;
1122  if (std::fabs(yArg1) > 1.0e-5) {
1123  yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1124  }
1125  return static_cast<ReturnT>(xFunc * yFunc);
1126  }
1127 
1131  unsigned int getOrder() const noexcept { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1132 
1133  std::string toString(std::string const& prefix) const override {
1135  os << "LanczosFunction2 [" << this->getOrder() << "]: ";
1136  ;
1137  os << Function2<ReturnT>::toString(prefix);
1138  return os.str();
1139  }
1140 
1141 private:
1142  double _invN;
1143 
1144 protected:
1145  /* Default constructor: intended only for serialization */
1146  explicit LanczosFunction2() : Function2<ReturnT>(2), _invN(1.0) {}
1147 
1148 private:
1149  friend class boost::serialization::access;
1150  template <class Archive>
1151  void serialize(Archive& ar, unsigned int const version) {
1152  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
1153  ar& make_nvp("invN", this->_invN);
1154  }
1155 };
1156 } // namespace math
1157 } // namespace afw
1158 } // namespace lsst
1159 
1160 #endif // #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H
double getMinY() const noexcept
ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
ReturnT operator()(double x, double y) const override
ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
unsigned int getNParameters() const noexcept
Return the number of function parameters.
Definition: Function.h:120
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
std::shared_ptr< Function1< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
double getMaxX() const noexcept
Get maximum allowed x.
unsigned int getOrder() const noexcept
Get the order of the Lanczos function.
An object passed to Persistable::write to allow it to persist itself.
double constexpr TWOPI
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
double getMinX() const noexcept
Get minimum allowed x.
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
double getMinX() const noexcept
T exp(T... args)
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
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.
double getMaxX() const noexcept
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.
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.
Chebyshev1Function2(std::vector< double > params, lsst::geom::Box2D const &xyRange=lsst::geom::Box2D(lsst::geom::Point2D(-1.0, -1.0), lsst::geom::Point2D(1.0, 1.0)))
Construct a Chebyshev polynomial with specified parameters and range.
ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
bool isLinearCombination() const noexcept override
Is the function a linear combination of its parameters?
std::shared_ptr< Function1< ReturnT > > clone() const override
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
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
2-dimensional separable Lanczos function
A Function taking two arguments.
Definition: Function.h:281
friend class boost::serialization::access
bool isLinearCombination() const noexcept override
Is the function a linear combination of its parameters?
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
ReturnT operator()(double x) const override
T resize(T... args)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: Persistable.cc:38
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:355
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:45
STL class.
T sin(T... args)
double getMaxY() const noexcept
std::vector< double > _params
Definition: Function.h:193
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.
unsigned int getOrder() const noexcept
Get the order of Lanczos function.
table::Key< int > b
A Function taking one argument.
Definition: Function.h:217
ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
T str(T... args)
PolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
T cos(T... args)
std::shared_ptr< Function1< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
double x
ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
T fabs(T... args)
ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
T size(T... args)
#define LSST_EXCEPT(type,...)
std::string toString(std::string const &prefix) const override
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.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
T begin(T... args)
lsst::geom::Box2D getXYRange() const noexcept
Get x,y range.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
unsigned int getOrder() const noexcept
Get the polynomial order.
table::Key< double > angle
~IntegerDeltaFunction1() noexcept override=default
ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >) override
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
Chebyshev1Function2(unsigned int order, lsst::geom::Box2D const &xyRange=lsst::geom::Box2D(lsst::geom::Point2D(-1.0, -1.0), lsst::geom::Point2D(1.0, 1.0)))
Construct a Chebyshev polynomial of specified order and range.
std::string prefix
Definition: SchemaMapper.cc:79
std::shared_ptr< Function1< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
std::shared_ptr< Function1< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
table::Key< double > ampl2
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
std::string toString(std::string const &prefix="") const override
Return a string representation of the function.
std::ostream * os
Definition: Schema.cc:737
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
table::Key< double > sigma2
int y
Definition: SpanSet.cc:44
double constexpr PI
unsigned int getOrder() const noexcept
Get the polynomial order.
std::string toString(std::string const &prefix) const override
Return a string representation of the function.
1-dimensional polynomial function.
table::Key< double > sigma1
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.