lsst.afw  16.0-10-gd1b3a1c94
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  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 
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), _multFac(1.0 / std::sqrt(lsst::geom::TWOPI)) {
171  this->_params[0] = sigma;
172  }
173  GaussianFunction1(GaussianFunction1 const&) = default;
175  GaussianFunction1& operator=(GaussianFunction1 const&) = default;
177  virtual ~GaussianFunction1() = default;
178 
181  }
182 
183  virtual ReturnT operator()(double x) const {
184  return static_cast<ReturnT>((_multFac / this->_params[0]) *
185  std::exp(-(x * x) / (2.0 * this->_params[0] * this->_params[0])));
186  }
187 
188  virtual std::string toString(std::string const& prefix) const {
190  os << "GaussianFunction1 [" << _multFac << "]: ";
191  os << Function1<ReturnT>::toString(prefix);
192  return os.str();
193  }
194 
195 private:
196  const double _multFac;
197 
198 protected:
199  /* Default constructor: intended only for serialization */
200  explicit GaussianFunction1() : Function1<ReturnT>(1), _multFac(1.0 / std::sqrt(lsst::geom::TWOPI)) {}
201 
202 private:
203  friend class boost::serialization::access;
204  template <class Archive>
205  void serialize(Archive& ar, unsigned int const version) {
206  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
207  }
208 };
209 
224 template <typename ReturnT>
225 class GaussianFunction2 : public Function2<ReturnT> {
226 public:
230  explicit GaussianFunction2(double sigma1,
231  double sigma2,
232  double angle = 0.0)
233  : Function2<ReturnT>(3), _multFac(1.0 / (lsst::geom::TWOPI)) {
234  this->_params[0] = sigma1;
235  this->_params[1] = sigma2;
236  this->_params[2] = angle;
237  _updateCache();
238  }
239 
240  GaussianFunction2(GaussianFunction2 const&) = default;
242  GaussianFunction2& operator=(GaussianFunction2 const&) = default;
244  virtual ~GaussianFunction2() = default;
245 
248  new GaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
249  }
250 
251  virtual ReturnT operator()(double x, double y) const {
252  if (_angle != this->_params[2]) {
253  _updateCache();
254  }
255  double pos1 = (_cosAngle * x) + (_sinAngle * y);
256  double pos2 = (-_sinAngle * x) + (_cosAngle * y);
257  return static_cast<ReturnT>((_multFac / (this->_params[0] * this->_params[1])) *
258  std::exp(-((pos1 * pos1) / (2.0 * this->_params[0] * this->_params[0])) -
259  ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
260  }
261 
262  virtual std::string toString(std::string const& prefix) const {
264  os << "GaussianFunction2: ";
265  os << Function2<ReturnT>::toString(prefix);
266  return os.str();
267  }
268 
269  virtual bool isPersistable() const { return true; }
270 
271 protected:
272  virtual std::string getPersistenceName() const;
273 
274  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
275 
276 private:
294  void _updateCache() const {
295  _angle = this->_params[2];
296  _sinAngle = std::sin(_angle);
297  _cosAngle = std::cos(_angle);
298  }
299  const double _multFac;
300  mutable double _angle;
301  mutable double _sinAngle;
302  mutable double _cosAngle;
303 
304 protected:
305  /* Default constructor: intended only for serialization */
306  explicit GaussianFunction2()
307  : Function2<ReturnT>(3),
308  _multFac(1.0 / (lsst::geom::TWOPI)),
309  _angle(0.0),
310  _sinAngle(0.0),
311  _cosAngle(1.0) {}
312 
313 private:
314  friend class boost::serialization::access;
315  template <class Archive>
316  void serialize(Archive& ar, unsigned int const version) {
317  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
318  ar& make_nvp("angle", this->_angle);
319  ar& make_nvp("sinAngle", this->_sinAngle);
320  ar& make_nvp("cosAngle", this->_cosAngle);
321  }
322 };
323 
338 template <typename ReturnT>
339 class DoubleGaussianFunction2 : public Function2<ReturnT> {
340 public:
345  double sigma1,
346  double sigma2 = 0,
347  double ampl2 = 0)
348  : Function2<ReturnT>(3), _multFac(1.0 / (lsst::geom::TWOPI)) {
349  this->_params[0] = sigma1;
350  this->_params[1] = sigma2;
351  this->_params[2] = ampl2;
352  }
353 
358  virtual ~DoubleGaussianFunction2() = default;
359 
362  new DoubleGaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
363  }
364 
365  virtual ReturnT operator()(double x, double y) const {
366  double radSq = (x * x) + (y * y);
367  double sigma1Sq = this->_params[0] * this->_params[0];
368  double sigma2Sq = this->_params[1] * this->_params[1];
369  double b = this->_params[2];
370  return static_cast<ReturnT>(
371  (_multFac / (sigma1Sq + (b * sigma2Sq))) *
372  (std::exp(-radSq / (2.0 * sigma1Sq)) + (b * std::exp(-radSq / (2.0 * sigma2Sq)))));
373  }
374 
375  virtual std::string toString(std::string const& prefix) const {
377  os << "DoubleGaussianFunction2 [" << _multFac << "]: ";
378  os << Function2<ReturnT>::toString(prefix);
379  return os.str();
380  }
381 
382  virtual bool isPersistable() const { return true; }
383 
384 protected:
385  virtual std::string getPersistenceName() const;
386 
387  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
388 
389 private:
390  const double _multFac;
391 
392 protected:
393  /* Default constructor: intended only for serialization */
394  explicit DoubleGaussianFunction2() : Function2<ReturnT>(3), _multFac(1.0 / (lsst::geom::TWOPI)) {}
395 
396 private:
397  friend class boost::serialization::access;
398  template <class Archive>
399  void serialize(Archive& ar, unsigned int const version) {
400  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
401  }
402 };
403 
411 template <typename ReturnT>
412 class PolynomialFunction1 : public Function1<ReturnT> {
413 public:
419  explicit PolynomialFunction1(unsigned int order)
420  : Function1<ReturnT>(order + 1) {}
421 
430  : Function1<ReturnT>(params) {
431  if (params.size() < 1) {
433  "PolynomialFunction1 called with empty vector");
434  }
435  }
436 
437  PolynomialFunction1(PolynomialFunction1 const&) = default;
441  virtual ~PolynomialFunction1() = default;
442 
445  }
446 
447  virtual bool isLinearCombination() const { return true; };
448 
449  virtual ReturnT operator()(double x) const {
450  int const order = static_cast<int>(this->_params.size()) - 1;
451  double retVal = this->_params[order];
452  for (int ii = order - 1; ii >= 0; --ii) {
453  retVal = (retVal * x) + this->_params[ii];
454  }
455  return static_cast<ReturnT>(retVal);
456  }
457 
461  unsigned int getOrder() const { return this->getNParameters() - 1; };
462 
463  virtual std::string toString(std::string const& prefix) const {
465  os << "PolynomialFunction1 []: ";
466  os << Function1<ReturnT>::toString(prefix);
467  return os.str();
468  }
469 
470 protected:
471  /* Default constructor: intended only for serialization */
472  explicit PolynomialFunction1() : Function1<ReturnT>(1) {}
473 
474 private:
475  friend class boost::serialization::access;
476  template <class Archive>
477  void serialize(Archive& ar, unsigned int const version) {
478  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
479  }
480 };
481 
496 template <typename ReturnT>
498 public:
506  explicit PolynomialFunction2(unsigned int order)
507  : BasePolynomialFunction2<ReturnT>(order), _oldY(0), _xCoeffs(this->_order + 1) {}
508 
520  std::vector<double> params)
521  : BasePolynomialFunction2<ReturnT>(params), _oldY(0), _xCoeffs(this->_order + 1) {}
523 
524  PolynomialFunction2(PolynomialFunction2 const&) = default;
528  virtual ~PolynomialFunction2() = default;
529 
532  }
533 
534  virtual ReturnT operator()(double x, double y) const {
535  /* Solve as follows:
536  - f(x,y) = Cx0 + Cx1 x + Cx2 x^2 + Cx3 x^3 + ...
537  where:
538  Cx0 = P0 + P2 y + P5 y^2 + P9 y^3 + ...
539  Cx1 = P1 + P4 y + P8 y2 + ...
540  Cx2 = P3 + P7 y + ...
541  Cx3 = P6 + ...
542  ...
543 
544  Compute Cx0, Cx1...Cxn by solving 1-d polynomials in y in the usual way.
545  These values are cached and only recomputed for new values of Y or if the parameters change.
546 
547  Then compute f(x,y) by solving the 1-d polynomial in x in the usual way.
548  */
549  const int maxXCoeffInd = this->_order;
550 
551  if ((y != _oldY) || !this->_isCacheValid) {
552  // update _xCoeffs cache
553  // note: paramInd is decremented in both of the following loops
554  int paramInd = static_cast<int>(this->_params.size()) - 1;
555 
556  // initialize _xCoeffs to coeffs for pure y^n; e.g. for 3rd order:
557  // _xCoeffs[0] = _params[9], _xCoeffs[1] = _params[8], ... _xCoeffs[3] = _params[6]
558  for (int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
559  _xCoeffs[xCoeffInd] = this->_params[paramInd];
560  }
561 
562  // finish computing _xCoeffs
563  for (int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
564  _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->_params[paramInd];
565  ++xCoeffInd;
566  if (xCoeffInd >= endXCoeffInd) {
567  xCoeffInd = 0;
568  --endXCoeffInd;
569  }
570  }
571 
572  _oldY = y;
573  this->_isCacheValid = true;
574  }
575 
576  // use _xCoeffs to compute result
577  double retVal = _xCoeffs[maxXCoeffInd];
578  for (int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
579  retVal = (retVal * x) + _xCoeffs[xCoeffInd];
580  }
581  return static_cast<ReturnT>(retVal);
582  }
583 
588  virtual std::vector<double> getDFuncDParameters(double x, double y) const;
589 
590  virtual std::string toString(std::string const& prefix) const {
592  os << "PolynomialFunction2 [" << this->_order << "]: ";
593  os << Function2<ReturnT>::toString(prefix);
594  return os.str();
595  }
596 
597  virtual bool isPersistable() const { return true; }
598 
599 protected:
600  virtual std::string getPersistenceName() const;
601 
602  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
603 
604 private:
605  mutable double _oldY;
606  mutable std::vector<double> _xCoeffs;
607 
608 protected:
609  /* Default constructor: intended only for serialization */
610  explicit PolynomialFunction2() : BasePolynomialFunction2<ReturnT>(), _oldY(0), _xCoeffs(0) {}
611 
612 private:
613  friend class boost::serialization::access;
614  template <class Archive>
615  void serialize(Archive& ar, unsigned int const version) {
616  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
617  ar& make_nvp("yCoeffs", this->_xCoeffs); // sets size of _xCoeffs; name is historical
618  }
619 };
620 
637 template <typename ReturnT>
638 class Chebyshev1Function1 : public Function1<ReturnT> {
639 public:
645  explicit Chebyshev1Function1(unsigned int order,
646  double minX = -1,
647  double maxX = 1)
648  : Function1<ReturnT>(order + 1) {
649  _initialize(minX, maxX);
650  }
651 
660  double minX = -1,
661  double maxX = 1)
662  : Function1<ReturnT>(params) {
663  if (params.size() < 1) {
665  "Chebyshev1Function1 called with empty vector");
666  }
667  _initialize(minX, maxX);
668  }
669 
670  Chebyshev1Function1(Chebyshev1Function1 const&) = default;
674  virtual ~Chebyshev1Function1() = default;
675 
677  return std::shared_ptr<Function1<ReturnT>>(new Chebyshev1Function1(this->_params, _minX, _maxX));
678  }
679 
683  double getMinX() const { return _minX; };
684 
688  double getMaxX() const { return _maxX; };
689 
693  unsigned int getOrder() const { return this->getNParameters() - 1; };
694 
695  virtual bool isLinearCombination() const { return true; };
696 
697  virtual ReturnT operator()(double x) const {
698  double xPrime = (x + _offset) * _scale;
699 
700  // Clenshaw function for solving the Chebyshev polynomial
701  // Non-recursive version from Kresimir Cosic
702  int const order = _order;
703  if (order == 0) {
704  return this->_params[0];
705  } else if (order == 1) {
706  return this->_params[0] + (this->_params[1] * xPrime);
707  }
708  double cshPrev = this->_params[order];
709  double csh = (2 * xPrime * this->_params[order]) + this->_params[order - 1];
710  for (int i = order - 2; i > 0; --i) {
711  double cshNext = (2 * xPrime * csh) + this->_params[i] - cshPrev;
712  cshPrev = csh;
713  csh = cshNext;
714  }
715  return (xPrime * csh) + this->_params[0] - cshPrev;
716  }
717 
718  virtual std::string toString(std::string const& prefix) const {
720  os << "Chebyshev1Function1 [" << _minX << ", " << _maxX << "]: ";
721  os << Function1<ReturnT>::toString(prefix);
722  return os.str();
723  }
724 
725 private:
726  double _minX;
727  double _maxX;
728  double _scale;
729  double _offset;
730  unsigned int _order;
731 
735  void _initialize(double minX, double maxX) {
736  _minX = minX;
737  _maxX = maxX;
738  _scale = 2.0 / (_maxX - _minX);
739  _offset = -(_minX + _maxX) * 0.5;
740  _order = this->getNParameters() - 1;
741  }
742 
743 protected:
744  /* Default constructor: intended only for serialization */
746  : Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
747 
748 private:
749  friend class boost::serialization::access;
750  template <class Archive>
751  void serialize(Archive& ar, unsigned int const) {
752  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
753  ar& make_nvp("minX", this->_minX);
754  ar& make_nvp("minX", this->_minX);
755  ar& make_nvp("maxX", this->_maxX);
756  ar& make_nvp("scale", this->_scale);
757  ar& make_nvp("offset", this->_offset);
758  ar& make_nvp("maxInd", this->_order);
759  }
760 };
761 
787 template <typename ReturnT>
789 public:
795  explicit Chebyshev1Function2(unsigned int order,
796  lsst::geom::Box2D const& xyRange = lsst::geom::Box2D(
797  lsst::geom::Point2D(-1.0, -1.0),
798  lsst::geom::Point2D(1.0, 1.0)))
799  : BasePolynomialFunction2<ReturnT>(order),
800  _oldYPrime(0),
801  _yCheby(this->_order + 1),
802  _xCoeffs(this->_order + 1) {
803  _initialize(xyRange);
804  }
805 
814  lsst::geom::Box2D const& xyRange = lsst::geom::Box2D(
816  lsst::geom::Point2D(-1.0, -1.0),
817  lsst::geom::Point2D(1.0, 1.0)))
818  : BasePolynomialFunction2<ReturnT>(params),
819  _oldYPrime(0),
820  _yCheby(this->_order + 1),
821  _xCoeffs(this->_order + 1) {
822  _initialize(xyRange);
823  }
824 
825  Chebyshev1Function2(Chebyshev1Function2 const&) = default;
829  virtual ~Chebyshev1Function2() = default;
830 
833  new Chebyshev1Function2(this->_params, this->getXYRange()));
834  }
835 
840  return lsst::geom::Box2D(lsst::geom::Point2D(_minX, _minY), lsst::geom::Point2D(_maxX, _maxY));
841  };
842 
848  virtual Chebyshev1Function2 truncate(int truncOrder
849  ) const {
850  if (truncOrder > this->_order) {
852  os << "truncated order=" << truncOrder << " must be <= original order=" << this->_order;
854  }
855  int truncNParams = this->nParametersFromOrder(truncOrder);
856  std::vector<double> truncParams(this->_params.begin(), this->_params.begin() + truncNParams);
857  return Chebyshev1Function2(truncParams, this->getXYRange());
858  }
859 
860  virtual ReturnT operator()(double x, double y) const {
861  /* Solve as follows:
862  - f(x,y) = Cy0 T0(y') + Cy1 T1(y') + Cy2 T2(y') + Cy3 T3(y') + ...
863  where:
864  Cy0 = P0 T0(x') + P1 T1(x') + P3 T2(x') + P6 T3(x') + ...
865  Cy1 = P2 T0(x') + P4 T1(x') + P7 T2(x') + ...
866  Cy2 = P5 T0(x') + P8 T1(x') + ...
867  Cy3 = P9 T0(x') + ...
868  ...
869 
870  First compute Tn(x') for each n
871  Then use that to compute Cy0, Cy1, ...Cyn
872  Then solve the y Chebyshev polynomial using the Clenshaw algorithm
873  */
874  double const xPrime = (x + _offsetX) * _scaleX;
875  double const yPrime = (y + _offsetY) * _scaleY;
876 
877  const int nParams = static_cast<int>(this->_params.size());
878  const int order = this->_order;
879 
880  if (order == 0) {
881  return this->_params[0]; // No caching required
882  }
883 
884  if ((yPrime != _oldYPrime) || !this->_isCacheValid) {
885  // update cached _yCheby and _xCoeffs
886  _yCheby[0] = 1.0;
887  _yCheby[1] = yPrime;
888  for (int chebyInd = 2; chebyInd <= order; chebyInd++) {
889  _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
890  }
891 
892  for (int coeffInd = 0; coeffInd <= order; coeffInd++) {
893  _xCoeffs[coeffInd] = 0;
894  }
895  for (int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
896  _xCoeffs[coeffInd] += this->_params[paramInd] * _yCheby[endCoeffInd];
897  --coeffInd;
898  ++endCoeffInd;
899  if (coeffInd < 0) {
900  coeffInd = endCoeffInd;
901  endCoeffInd = 0;
902  }
903  }
904 
905  _oldYPrime = yPrime;
906  this->_isCacheValid = true;
907  }
908 
909  // Clenshaw function for solving the Chebyshev polynomial
910  // Non-recursive version from Kresimir Cosic
911  if (order == 1) {
912  return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
913  }
914  double cshPrev = _xCoeffs[order];
915  double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
916  for (int i = order - 2; i > 0; --i) {
917  double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
918  cshPrev = csh;
919  csh = cshNext;
920  }
921  return (xPrime * csh) + _xCoeffs[0] - cshPrev;
922  }
923 
924  virtual std::string toString(std::string const& prefix) const {
926  os << "Chebyshev1Function2 [";
927  os << this->_order << ", " << this->getXYRange() << "]";
928  os << Function2<ReturnT>::toString(prefix);
929  return os.str();
930  }
931 
932  virtual bool isPersistable() const { return true; }
933 
934 protected:
935  virtual std::string getPersistenceName() const;
936 
937  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
938 
939 private:
940  mutable double _oldYPrime;
941  mutable std::vector<double> _yCheby;
942  mutable std::vector<double> _xCoeffs;
943  double _minX;
944  double _minY;
945  double _maxX;
946  double _maxY;
947  double _scaleX;
948  double _scaleY;
949  double _offsetX;
950  double _offsetY;
951 
955  void _initialize(lsst::geom::Box2D const& xyRange) {
956  _minX = xyRange.getMinX();
957  _minY = xyRange.getMinY();
958  _maxX = xyRange.getMaxX();
959  _maxY = xyRange.getMaxY();
960  _scaleX = 2.0 / (_maxX - _minX);
961  _scaleY = 2.0 / (_maxY - _minY);
962  _offsetX = -(_minX + _maxX) * 0.5;
963  _offsetY = -(_minY + _maxY) * 0.5;
964  }
965 
966 protected:
967  /* Default constructor: intended only for serialization */
969  : BasePolynomialFunction2<ReturnT>(),
970  _oldYPrime(0),
971  _yCheby(0),
972  _xCoeffs(0),
973  _minX(0.0),
974  _minY(0.0),
975  _maxX(0.0),
976  _maxY(0.0),
977  _scaleX(1.0),
978  _scaleY(1.0),
979  _offsetX(0.0),
980  _offsetY(0.0) {}
981 
982 private:
983  friend class boost::serialization::access;
984  template <class Archive>
985  void serialize(Archive& ar, unsigned int const version) {
986  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
987  ar& make_nvp("minX", this->_minX);
988  ar& make_nvp("minY", this->_minY);
989  ar& make_nvp("maxX", this->_maxX);
990  ar& make_nvp("maxY", this->_maxY);
991  ar& make_nvp("scaleX", this->_scaleX);
992  ar& make_nvp("scaleY", this->_scaleY);
993  ar& make_nvp("offsetX", this->_offsetX);
994  ar& make_nvp("offsetY", this->_offsetY);
995  ar& make_nvp("xCheby", this->_yCheby); // sets size of _yCheby; name is historical
996  _xCoeffs.resize(_yCheby.size());
997  }
998 };
999 
1012 template <typename ReturnT>
1013 class LanczosFunction1 : public Function1<ReturnT> {
1014 public:
1018  explicit LanczosFunction1(unsigned int n,
1019  double xOffset = 0.0)
1020  : Function1<ReturnT>(1), _invN(1.0 / static_cast<double>(n)) {
1021  this->_params[0] = xOffset;
1022  }
1023 
1024  LanczosFunction1(LanczosFunction1 const&) = default;
1025  LanczosFunction1(LanczosFunction1&&) = default;
1026  LanczosFunction1& operator=(LanczosFunction1 const&) = default;
1028  virtual ~LanczosFunction1() = default;
1029 
1031  return std::shared_ptr<Function1<ReturnT>>(new LanczosFunction1(this->getOrder(), this->_params[0]));
1032  }
1033 
1034  virtual ReturnT operator()(double x) const {
1035  double xArg1 = (x - this->_params[0]) * lsst::geom::PI;
1036  double xArg2 = xArg1 * _invN;
1037  if (std::fabs(xArg1) > 1.0e-5) {
1038  return static_cast<ReturnT>(std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2));
1039  } else {
1040  return static_cast<ReturnT>(1);
1041  }
1042  }
1043 
1047  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1048 
1049  virtual std::string toString(std::string const& prefix) const {
1051  os << "LanczosFunction1 [" << this->getOrder() << "]: ";
1052  ;
1053  os << Function1<ReturnT>::toString(prefix);
1054  return os.str();
1055  }
1056 
1057 private:
1058  double _invN; // == 1/n
1059 
1060 protected:
1061  /* Default constructor: intended only for serialization */
1062  explicit LanczosFunction1() : Function1<ReturnT>(1), _invN(1.0) {}
1063 
1064 private:
1065  friend class boost::serialization::access;
1066  template <class Archive>
1067  void serialize(Archive& ar, unsigned int const version) {
1068  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
1069  ar& make_nvp("invN", this->_invN);
1070  }
1071 };
1072 
1085 template <typename ReturnT>
1086 class LanczosFunction2 : public Function2<ReturnT> {
1087 public:
1091  explicit LanczosFunction2(unsigned int n,
1092  double xOffset = 0.0,
1093  double yOffset = 0.0)
1094  : Function2<ReturnT>(2), _invN(1.0 / static_cast<double>(n)) {
1095  this->_params[0] = xOffset;
1096  this->_params[1] = yOffset;
1097  }
1098 
1099  LanczosFunction2(LanczosFunction2 const&) = default;
1100  LanczosFunction2(LanczosFunction2&&) = default;
1101  LanczosFunction2& operator=(LanczosFunction2 const&) = default;
1103  virtual ~LanczosFunction2() = default;
1104 
1107  new LanczosFunction2(this->getOrder(), this->_params[0], this->_params[1]));
1108  }
1109 
1110  virtual ReturnT operator()(double x, double y) const {
1111  double xArg1 = (x - this->_params[0]) * lsst::geom::PI;
1112  double xArg2 = xArg1 * _invN;
1113  double xFunc = 1;
1114  if (std::fabs(xArg1) > 1.0e-5) {
1115  xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1116  }
1117  double yArg1 = (y - this->_params[1]) * lsst::geom::PI;
1118  double yArg2 = yArg1 * _invN;
1119  double yFunc = 1;
1120  if (std::fabs(yArg1) > 1.0e-5) {
1121  yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1122  }
1123  return static_cast<ReturnT>(xFunc * yFunc);
1124  }
1125 
1129  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1130 
1131  virtual std::string toString(std::string const& prefix) const {
1133  os << "LanczosFunction2 [" << this->getOrder() << "]: ";
1134  ;
1135  os << Function2<ReturnT>::toString(prefix);
1136  return os.str();
1137  }
1138 
1139 private:
1140  double _invN;
1141 
1142 protected:
1143  /* Default constructor: intended only for serialization */
1144  explicit LanczosFunction2() : Function2<ReturnT>(2), _invN(1.0) {}
1145 
1146 private:
1147  friend class boost::serialization::access;
1148  template <class Archive>
1149  void serialize(Archive& ar, unsigned int const version) {
1150  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
1151  ar& make_nvp("invN", this->_invN);
1152  }
1153 };
1154 } // namespace math
1155 } // namespace afw
1156 } // namespace lsst
1157 
1158 #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.
double constexpr TWOPI
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.
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.
unsigned int getOrder() const
Get the polynomial order.
double getMinX() const
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:273
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)
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:347
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:45
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)
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:185
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.
double getMaxX() const
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
table::Key< int > b
A Function taking one argument.
Definition: Function.h:209
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)
double x
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)
table::Key< double > angle
lsst::geom::Box2D getXYRange() const
Get x,y range.
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.
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.
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.
std::string prefix
Definition: SchemaMapper.cc:79
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.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: Persistable.cc:34
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
table::Key< double > ampl2
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.
std::ostream * os
Definition: Schema.cc:737
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:112
table::Key< double > sigma2
int y
Definition: SpanSet.cc:44
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 PI
double getMaxY() const
double getMinY() const
unsigned int getOrder() const
Get the order of the Lanczos 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.