lsst.afw  14.0-25-g0e8fc950e+6
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 
62 
65  }
66 
67  virtual ReturnT operator()(double x) const { return static_cast<ReturnT>(x == _xo); }
68 
69  virtual std::string toString(std::string const& prefix = "") const {
71  os << "IntegerDeltaFunction1 [" << _xo << "]: ";
72  os << Function1<ReturnT>::toString(prefix);
73  return os.str();
74  };
75 
76 private:
77  double _xo;
78 
79 protected:
80  /* Default constructor: intended only for serialization */
81  explicit IntegerDeltaFunction1() : Function1<ReturnT>(0), _xo(0.0) {}
82 
83 private:
85  template <class Archive>
86  void serialize(Archive& ar, unsigned int const version) {
87  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
88  ar& make_nvp("xo", this->_xo);
89  }
90 };
91 
102 template <typename ReturnT>
103 class IntegerDeltaFunction2 : public Function2<ReturnT> {
104 public:
108  explicit IntegerDeltaFunction2(double xo, double yo) : Function2<ReturnT>(0), _xo(xo), _yo(yo) {}
109 
111 
114  }
115 
116  virtual ReturnT operator()(double x, double y) const {
117  return static_cast<ReturnT>((x == _xo) && (y == _yo));
118  }
119 
120  virtual std::string toString(std::string const& prefix) const {
122  os << "IntegerDeltaFunction2 [" << _xo << ", " << _yo << "]: ";
123  os << Function2<ReturnT>::toString(prefix);
124  return os.str();
125  }
126 
127 private:
128  double _xo;
129  double _yo;
130 
131 protected:
132  /* Default constructor: intended only for serialization */
133  explicit IntegerDeltaFunction2() : Function2<ReturnT>(0), _xo(0.0), _yo(0.0) {}
134 
135 private:
136  friend class boost::serialization::access;
137  template <class Archive>
138  void serialize(Archive& ar, unsigned int const version) {
139  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
140  ar& make_nvp("xo", this->_xo);
141  ar& make_nvp("yo", this->_yo);
142  }
143 };
144 
155 template <typename ReturnT>
156 class GaussianFunction1 : public Function1<ReturnT> {
157 public:
161  explicit GaussianFunction1(double sigma)
162  : Function1<ReturnT>(1),
163  _multFac(1.0 / std::sqrt(lsst::afw::geom::TWOPI)) {
164  this->_params[0] = sigma;
165  }
166  virtual ~GaussianFunction1() {}
167 
170  }
171 
172  virtual ReturnT operator()(double x) const {
173  return static_cast<ReturnT>((_multFac / this->_params[0]) *
174  std::exp(-(x * x) / (2.0 * this->_params[0] * this->_params[0])));
175  }
176 
177  virtual std::string toString(std::string const& prefix) const {
179  os << "GaussianFunction1 [" << _multFac << "]: ";
180  os << Function1<ReturnT>::toString(prefix);
181  return os.str();
182  }
183 
184 private:
185  const double _multFac;
186 
187 protected:
188  /* Default constructor: intended only for serialization */
189  explicit GaussianFunction1() : Function1<ReturnT>(1), _multFac(1.0 / std::sqrt(lsst::afw::geom::TWOPI)) {}
190 
191 private:
192  friend class boost::serialization::access;
193  template <class Archive>
194  void serialize(Archive& ar, unsigned int const version) {
195  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
196  }
197 };
198 
213 template <typename ReturnT>
214 class GaussianFunction2 : public Function2<ReturnT> {
215 public:
219  explicit GaussianFunction2(double sigma1,
220  double sigma2,
221  double angle = 0.0)
222  : Function2<ReturnT>(3),
223  _multFac(1.0 / (lsst::afw::geom::TWOPI)) {
224  this->_params[0] = sigma1;
225  this->_params[1] = sigma2;
226  this->_params[2] = angle;
227  _updateCache();
228  }
229 
230  virtual ~GaussianFunction2() {}
231 
234  new GaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
235  }
236 
237  virtual ReturnT operator()(double x, double y) const {
238  if (_angle != this->_params[2]) {
239  _updateCache();
240  }
241  double pos1 = (_cosAngle * x) + (_sinAngle * y);
242  double pos2 = (-_sinAngle * x) + (_cosAngle * y);
243  return static_cast<ReturnT>((_multFac / (this->_params[0] * this->_params[1])) *
244  std::exp(-((pos1 * pos1) / (2.0 * this->_params[0] * this->_params[0])) -
245  ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
246  }
247 
248  virtual std::string toString(std::string const& prefix) const {
250  os << "GaussianFunction2: ";
251  os << Function2<ReturnT>::toString(prefix);
252  return os.str();
253  }
254 
255  virtual bool isPersistable() const { return true; }
256 
257 protected:
258  virtual std::string getPersistenceName() const;
259 
260  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
261 
262 private:
280  void _updateCache() const {
281  _angle = this->_params[2];
282  _sinAngle = std::sin(_angle);
283  _cosAngle = std::cos(_angle);
284  }
285  const double _multFac;
286  mutable double _angle;
287  mutable double _sinAngle;
288  mutable double _cosAngle;
289 
290 protected:
291  /* Default constructor: intended only for serialization */
292  explicit GaussianFunction2()
293  : Function2<ReturnT>(3),
294  _multFac(1.0 / (lsst::afw::geom::TWOPI)),
295  _angle(0.0),
296  _sinAngle(0.0),
297  _cosAngle(1.0) {}
298 
299 private:
300  friend class boost::serialization::access;
301  template <class Archive>
302  void serialize(Archive& ar, unsigned int const version) {
303  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
304  ar& make_nvp("angle", this->_angle);
305  ar& make_nvp("sinAngle", this->_sinAngle);
306  ar& make_nvp("cosAngle", this->_cosAngle);
307  }
308 };
309 
324 template <typename ReturnT>
325 class DoubleGaussianFunction2 : public Function2<ReturnT> {
326 public:
331  double sigma1,
332  double sigma2 = 0,
333  double ampl2 = 0)
334  : Function2<ReturnT>(3),
335  _multFac(1.0 / (lsst::afw::geom::TWOPI)) {
336  this->_params[0] = sigma1;
337  this->_params[1] = sigma2;
338  this->_params[2] = ampl2;
339  }
340 
342 
345  new DoubleGaussianFunction2(this->_params[0], this->_params[1], this->_params[2]));
346  }
347 
348  virtual ReturnT operator()(double x, double y) const {
349  double radSq = (x * x) + (y * y);
350  double sigma1Sq = this->_params[0] * this->_params[0];
351  double sigma2Sq = this->_params[1] * this->_params[1];
352  double b = this->_params[2];
353  return static_cast<ReturnT>(
354  (_multFac / (sigma1Sq + (b * sigma2Sq))) *
355  (std::exp(-radSq / (2.0 * sigma1Sq)) + (b * std::exp(-radSq / (2.0 * sigma2Sq)))));
356  }
357 
358  virtual std::string toString(std::string const& prefix) const {
360  os << "DoubleGaussianFunction2 [" << _multFac << "]: ";
361  os << Function2<ReturnT>::toString(prefix);
362  return os.str();
363  }
364 
365  virtual bool isPersistable() const { return true; }
366 
367 protected:
368  virtual std::string getPersistenceName() const;
369 
370  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
371 
372 private:
373  const double _multFac;
374 
375 protected:
376  /* Default constructor: intended only for serialization */
377  explicit DoubleGaussianFunction2() : Function2<ReturnT>(3), _multFac(1.0 / (lsst::afw::geom::TWOPI)) {}
378 
379 private:
380  friend class boost::serialization::access;
381  template <class Archive>
382  void serialize(Archive& ar, unsigned int const version) {
383  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
384  }
385 };
386 
394 template <typename ReturnT>
395 class PolynomialFunction1 : public Function1<ReturnT> {
396 public:
402  explicit PolynomialFunction1(unsigned int order)
403  : Function1<ReturnT>(order + 1) {}
404 
413  : Function1<ReturnT>(params) {
414  if (params.size() < 1) {
416  "PolynomialFunction1 called with empty vector");
417  }
418  }
419 
420  virtual ~PolynomialFunction1() {}
421 
424  }
425 
426  virtual bool isLinearCombination() const { return true; };
427 
428  virtual ReturnT operator()(double x) const {
429  int const order = static_cast<int>(this->_params.size()) - 1;
430  double retVal = this->_params[order];
431  for (int ii = order - 1; ii >= 0; --ii) {
432  retVal = (retVal * x) + this->_params[ii];
433  }
434  return static_cast<ReturnT>(retVal);
435  }
436 
440  unsigned int getOrder() const { return this->getNParameters() - 1; };
441 
442  virtual std::string toString(std::string const& prefix) const {
444  os << "PolynomialFunction1 []: ";
445  os << Function1<ReturnT>::toString(prefix);
446  return os.str();
447  }
448 
449 protected:
450  /* Default constructor: intended only for serialization */
451  explicit PolynomialFunction1() : Function1<ReturnT>(1) {}
452 
453 private:
454  friend class boost::serialization::access;
455  template <class Archive>
456  void serialize(Archive& ar, unsigned int const version) {
457  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
458  }
459 };
460 
475 template <typename ReturnT>
477 public:
485  explicit PolynomialFunction2(unsigned int order)
486  : BasePolynomialFunction2<ReturnT>(order),
487  _oldY(0),
488  _xCoeffs(this->_order + 1) {}
489 
501  std::vector<double> params)
502  : BasePolynomialFunction2<ReturnT>(params),
504  _oldY(0),
505  _xCoeffs(this->_order + 1) {}
506 
507  virtual ~PolynomialFunction2() {}
508 
511  }
512 
513  virtual ReturnT operator()(double x, double y) const {
514  /* Solve as follows:
515  - f(x,y) = Cx0 + Cx1 x + Cx2 x^2 + Cx3 x^3 + ...
516  where:
517  Cx0 = P0 + P2 y + P5 y^2 + P9 y^3 + ...
518  Cx1 = P1 + P4 y + P8 y2 + ...
519  Cx2 = P3 + P7 y + ...
520  Cx3 = P6 + ...
521  ...
522 
523  Compute Cx0, Cx1...Cxn by solving 1-d polynomials in y in the usual way.
524  These values are cached and only recomputed for new values of Y or if the parameters change.
525 
526  Then compute f(x,y) by solving the 1-d polynomial in x in the usual way.
527  */
528  const int maxXCoeffInd = this->_order;
529 
530  if ((y != _oldY) || !this->_isCacheValid) {
531  // update _xCoeffs cache
532  // note: paramInd is decremented in both of the following loops
533  int paramInd = static_cast<int>(this->_params.size()) - 1;
534 
535  // initialize _xCoeffs to coeffs for pure y^n; e.g. for 3rd order:
536  // _xCoeffs[0] = _params[9], _xCoeffs[1] = _params[8], ... _xCoeffs[3] = _params[6]
537  for (int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
538  _xCoeffs[xCoeffInd] = this->_params[paramInd];
539  }
540 
541  // finish computing _xCoeffs
542  for (int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
543  _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->_params[paramInd];
544  ++xCoeffInd;
545  if (xCoeffInd >= endXCoeffInd) {
546  xCoeffInd = 0;
547  --endXCoeffInd;
548  }
549  }
550 
551  _oldY = y;
552  this->_isCacheValid = true;
553  }
554 
555  // use _xCoeffs to compute result
556  double retVal = _xCoeffs[maxXCoeffInd];
557  for (int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
558  retVal = (retVal * x) + _xCoeffs[xCoeffInd];
559  }
560  return static_cast<ReturnT>(retVal);
561  }
562 
567  virtual std::vector<double> getDFuncDParameters(double x, double y) const;
568 
569  virtual std::string toString(std::string const& prefix) const {
571  os << "PolynomialFunction2 [" << this->_order << "]: ";
572  os << Function2<ReturnT>::toString(prefix);
573  return os.str();
574  }
575 
576  virtual bool isPersistable() const { return true; }
577 
578 protected:
579  virtual std::string getPersistenceName() const;
580 
581  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
582 
583 private:
584  mutable double _oldY;
585  mutable std::vector<double> _xCoeffs;
586 
587 protected:
588  /* Default constructor: intended only for serialization */
589  explicit PolynomialFunction2() : BasePolynomialFunction2<ReturnT>(), _oldY(0), _xCoeffs(0) {}
590 
591 private:
592  friend class boost::serialization::access;
593  template <class Archive>
594  void serialize(Archive& ar, unsigned int const version) {
595  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
596  ar& make_nvp("yCoeffs", this->_xCoeffs); // sets size of _xCoeffs; name is historical
597  }
598 };
599 
616 template <typename ReturnT>
617 class Chebyshev1Function1 : public Function1<ReturnT> {
618 public:
624  explicit Chebyshev1Function1(unsigned int order,
625  double minX = -1,
626  double maxX = 1)
627  : Function1<ReturnT>(order + 1) {
628  _initialize(minX, maxX);
629  }
630 
639  double minX = -1,
640  double maxX = 1)
641  : Function1<ReturnT>(params) {
642  if (params.size() < 1) {
644  "Chebyshev1Function1 called with empty vector");
645  }
646  _initialize(minX, maxX);
647  }
648 
649  virtual ~Chebyshev1Function1() {}
650 
652  return std::shared_ptr<Function1<ReturnT>>(new Chebyshev1Function1(this->_params, _minX, _maxX));
653  }
654 
658  double getMinX() const { return _minX; };
659 
663  double getMaxX() const { return _maxX; };
664 
668  unsigned int getOrder() const { return this->getNParameters() - 1; };
669 
670  virtual bool isLinearCombination() const { return true; };
671 
672  virtual ReturnT operator()(double x) const {
673  double xPrime = (x + _offset) * _scale;
674 
675  // Clenshaw function for solving the Chebyshev polynomial
676  // Non-recursive version from Kresimir Cosic
677  int const order = _order;
678  if (order == 0) {
679  return this->_params[0];
680  } else if (order == 1) {
681  return this->_params[0] + (this->_params[1] * xPrime);
682  }
683  double cshPrev = this->_params[order];
684  double csh = (2 * xPrime * this->_params[order]) + this->_params[order - 1];
685  for (int i = order - 2; i > 0; --i) {
686  double cshNext = (2 * xPrime * csh) + this->_params[i] - cshPrev;
687  cshPrev = csh;
688  csh = cshNext;
689  }
690  return (xPrime * csh) + this->_params[0] - cshPrev;
691  }
692 
693  virtual std::string toString(std::string const& prefix) const {
695  os << "Chebyshev1Function1 [" << _minX << ", " << _maxX << "]: ";
696  os << Function1<ReturnT>::toString(prefix);
697  return os.str();
698  }
699 
700 private:
701  double _minX;
702  double _maxX;
703  double _scale;
704  double _offset;
705  unsigned int _order;
706 
710  void _initialize(double minX, double maxX) {
711  _minX = minX;
712  _maxX = maxX;
713  _scale = 2.0 / (_maxX - _minX);
714  _offset = -(_minX + _maxX) * 0.5;
715  _order = this->getNParameters() - 1;
716  }
717 
718 protected:
719  /* Default constructor: intended only for serialization */
721  : Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
722 
723 private:
724  friend class boost::serialization::access;
725  template <class Archive>
726  void serialize(Archive& ar, unsigned int const) {
727  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
728  ar& make_nvp("minX", this->_minX);
729  ar& make_nvp("minX", this->_minX);
730  ar& make_nvp("maxX", this->_maxX);
731  ar& make_nvp("scale", this->_scale);
732  ar& make_nvp("offset", this->_offset);
733  ar& make_nvp("maxInd", this->_order);
734  }
735 };
736 
762 template <typename ReturnT>
764 public:
770  explicit Chebyshev1Function2(unsigned int order,
772  lsst::afw::geom::Point2D(-1.0, -1.0),
773  lsst::afw::geom::Point2D(1.0, 1.0)))
774  : BasePolynomialFunction2<ReturnT>(order),
775  _oldYPrime(0),
776  _yCheby(this->_order + 1),
777  _xCoeffs(this->_order + 1) {
778  _initialize(xyRange);
779  }
780 
791  lsst::afw::geom::Point2D(-1.0, -1.0),
792  lsst::afw::geom::Point2D(1.0, 1.0)))
793  : BasePolynomialFunction2<ReturnT>(params),
794  _oldYPrime(0),
795  _yCheby(this->_order + 1),
796  _xCoeffs(this->_order + 1) {
797  _initialize(xyRange);
798  }
799 
800  virtual ~Chebyshev1Function2() {}
801 
804  new Chebyshev1Function2(this->_params, this->getXYRange()));
805  }
806 
812  lsst::afw::geom::Point2D(_maxX, _maxY));
813  };
814 
820  virtual Chebyshev1Function2 truncate(int truncOrder
821  ) const {
822  if (truncOrder > this->_order) {
824  os << "truncated order=" << truncOrder << " must be <= original order=" << this->_order;
826  }
827  int truncNParams = this->nParametersFromOrder(truncOrder);
828  std::vector<double> truncParams(this->_params.begin(), this->_params.begin() + truncNParams);
829  return Chebyshev1Function2(truncParams, this->getXYRange());
830  }
831 
832  virtual ReturnT operator()(double x, double y) const {
833  /* Solve as follows:
834  - f(x,y) = Cy0 T0(y') + Cy1 T1(y') + Cy2 T2(y') + Cy3 T3(y') + ...
835  where:
836  Cy0 = P0 T0(x') + P1 T1(x') + P3 T2(x') + P6 T3(x') + ...
837  Cy1 = P2 T0(x') + P4 T1(x') + P7 T2(x') + ...
838  Cy2 = P5 T0(x') + P8 T1(x') + ...
839  Cy3 = P9 T0(x') + ...
840  ...
841 
842  First compute Tn(x') for each n
843  Then use that to compute Cy0, Cy1, ...Cyn
844  Then solve the y Chebyshev polynomial using the Clenshaw algorithm
845  */
846  double const xPrime = (x + _offsetX) * _scaleX;
847  double const yPrime = (y + _offsetY) * _scaleY;
848 
849  const int nParams = static_cast<int>(this->_params.size());
850  const int order = this->_order;
851 
852  if (order == 0) {
853  return this->_params[0]; // No caching required
854  }
855 
856  if ((yPrime != _oldYPrime) || !this->_isCacheValid) {
857  // update cached _yCheby and _xCoeffs
858  _yCheby[0] = 1.0;
859  _yCheby[1] = yPrime;
860  for (int chebyInd = 2; chebyInd <= order; chebyInd++) {
861  _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
862  }
863 
864  for (int coeffInd = 0; coeffInd <= order; coeffInd++) {
865  _xCoeffs[coeffInd] = 0;
866  }
867  for (int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
868  _xCoeffs[coeffInd] += this->_params[paramInd] * _yCheby[endCoeffInd];
869  --coeffInd;
870  ++endCoeffInd;
871  if (coeffInd < 0) {
872  coeffInd = endCoeffInd;
873  endCoeffInd = 0;
874  }
875  }
876 
877  _oldYPrime = yPrime;
878  this->_isCacheValid = true;
879  }
880 
881  // Clenshaw function for solving the Chebyshev polynomial
882  // Non-recursive version from Kresimir Cosic
883  if (order == 1) {
884  return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
885  }
886  double cshPrev = _xCoeffs[order];
887  double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
888  for (int i = order - 2; i > 0; --i) {
889  double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
890  cshPrev = csh;
891  csh = cshNext;
892  }
893  return (xPrime * csh) + _xCoeffs[0] - cshPrev;
894  }
895 
896  virtual std::string toString(std::string const& prefix) const {
898  os << "Chebyshev1Function2 [";
899  os << this->_order << ", " << this->getXYRange() << "]";
900  os << Function2<ReturnT>::toString(prefix);
901  return os.str();
902  }
903 
904  virtual bool isPersistable() const { return true; }
905 
906 protected:
907  virtual std::string getPersistenceName() const;
908 
909  virtual void write(afw::table::io::OutputArchiveHandle& handle) const;
910 
911 private:
912  mutable double _oldYPrime;
913  mutable std::vector<double> _yCheby;
914  mutable std::vector<double> _xCoeffs;
915  double _minX;
916  double _minY;
917  double _maxX;
918  double _maxY;
919  double _scaleX;
920  double _scaleY;
921  double _offsetX;
922  double _offsetY;
923 
927  void _initialize(lsst::afw::geom::Box2D const& xyRange) {
928  _minX = xyRange.getMinX();
929  _minY = xyRange.getMinY();
930  _maxX = xyRange.getMaxX();
931  _maxY = xyRange.getMaxY();
932  _scaleX = 2.0 / (_maxX - _minX);
933  _scaleY = 2.0 / (_maxY - _minY);
934  _offsetX = -(_minX + _maxX) * 0.5;
935  _offsetY = -(_minY + _maxY) * 0.5;
936  }
937 
938 protected:
939  /* Default constructor: intended only for serialization */
941  : BasePolynomialFunction2<ReturnT>(),
942  _oldYPrime(0),
943  _yCheby(0),
944  _xCoeffs(0),
945  _minX(0.0),
946  _minY(0.0),
947  _maxX(0.0),
948  _maxY(0.0),
949  _scaleX(1.0),
950  _scaleY(1.0),
951  _offsetX(0.0),
952  _offsetY(0.0) {}
953 
954 private:
955  friend class boost::serialization::access;
956  template <class Archive>
957  void serialize(Archive& ar, unsigned int const version) {
958  ar& make_nvp("fn2", boost::serialization::base_object<BasePolynomialFunction2<ReturnT>>(*this));
959  ar& make_nvp("minX", this->_minX);
960  ar& make_nvp("minY", this->_minY);
961  ar& make_nvp("maxX", this->_maxX);
962  ar& make_nvp("maxY", this->_maxY);
963  ar& make_nvp("scaleX", this->_scaleX);
964  ar& make_nvp("scaleY", this->_scaleY);
965  ar& make_nvp("offsetX", this->_offsetX);
966  ar& make_nvp("offsetY", this->_offsetY);
967  ar& make_nvp("xCheby", this->_yCheby); // sets size of _yCheby; name is historical
968  _xCoeffs.resize(_yCheby.size());
969  }
970 };
971 
984 template <typename ReturnT>
985 class LanczosFunction1 : public Function1<ReturnT> {
986 public:
990  explicit LanczosFunction1(unsigned int n,
991  double xOffset = 0.0)
992  : Function1<ReturnT>(1),
993  _invN(1.0 / static_cast<double>(n)) {
994  this->_params[0] = xOffset;
995  }
996 
997  virtual ~LanczosFunction1() {}
998 
1000  return std::shared_ptr<Function1<ReturnT>>(new LanczosFunction1(this->getOrder(), this->_params[0]));
1001  }
1002 
1003  virtual ReturnT operator()(double x) const {
1004  double xArg1 = (x - this->_params[0]) * lsst::afw::geom::PI;
1005  double xArg2 = xArg1 * _invN;
1006  if (std::fabs(xArg1) > 1.0e-5) {
1007  return static_cast<ReturnT>(std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2));
1008  } else {
1009  return static_cast<ReturnT>(1);
1010  }
1011  }
1012 
1016  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1017 
1018  virtual std::string toString(std::string const& prefix) const {
1019  std::ostringstream os;
1020  os << "LanczosFunction1 [" << this->getOrder() << "]: ";
1021  ;
1022  os << Function1<ReturnT>::toString(prefix);
1023  return os.str();
1024  }
1025 
1026 private:
1027  double _invN; // == 1/n
1028 
1029 protected:
1030  /* Default constructor: intended only for serialization */
1031  explicit LanczosFunction1() : Function1<ReturnT>(1), _invN(1.0) {}
1032 
1033 private:
1034  friend class boost::serialization::access;
1035  template <class Archive>
1036  void serialize(Archive& ar, unsigned int const version) {
1037  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
1038  ar& make_nvp("invN", this->_invN);
1039  }
1040 };
1041 
1054 template <typename ReturnT>
1055 class LanczosFunction2 : public Function2<ReturnT> {
1056 public:
1060  explicit LanczosFunction2(unsigned int n,
1061  double xOffset = 0.0,
1062  double yOffset = 0.0)
1063  : Function2<ReturnT>(2),
1064  _invN(1.0 / static_cast<double>(n)) {
1065  this->_params[0] = xOffset;
1066  this->_params[1] = yOffset;
1067  }
1068 
1069  virtual ~LanczosFunction2() {}
1070 
1073  new LanczosFunction2(this->getOrder(), this->_params[0], this->_params[1]));
1074  }
1075 
1076  virtual ReturnT operator()(double x, double y) const {
1077  double xArg1 = (x - this->_params[0]) * lsst::afw::geom::PI;
1078  double xArg2 = xArg1 * _invN;
1079  double xFunc = 1;
1080  if (std::fabs(xArg1) > 1.0e-5) {
1081  xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1082  }
1083  double yArg1 = (y - this->_params[1]) * lsst::afw::geom::PI;
1084  double yArg2 = yArg1 * _invN;
1085  double yFunc = 1;
1086  if (std::fabs(yArg1) > 1.0e-5) {
1087  yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1088  }
1089  return static_cast<ReturnT>(xFunc * yFunc);
1090  }
1091 
1095  unsigned int getOrder() const { return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1096 
1097  virtual std::string toString(std::string const& prefix) const {
1098  std::ostringstream os;
1099  os << "LanczosFunction2 [" << this->getOrder() << "]: ";
1100  ;
1101  os << Function2<ReturnT>::toString(prefix);
1102  return os.str();
1103  }
1104 
1105 private:
1106  double _invN;
1107 
1108 protected:
1109  /* Default constructor: intended only for serialization */
1110  explicit LanczosFunction2() : Function2<ReturnT>(2), _invN(1.0) {}
1111 
1112 private:
1113  friend class boost::serialization::access;
1114  template <class Archive>
1115  void serialize(Archive& ar, unsigned int const version) {
1116  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
1117  ar& make_nvp("invN", this->_invN);
1118  }
1119 };
1120 }
1121 }
1122 } // lsst::afw::math
1123 
1124 #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:342
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.
2-dimensional separable Lanczos function
A Function taking two arguments.
Definition: Function.h:266
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:335
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)
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:183
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:343
double getMinX() const
Definition: Box.h:338
A Function taking one argument.
Definition: Function.h:207
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)
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:110
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:339
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.