25 #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H 26 #define LSST_AFW_MATH_FUNCTIONLIBRARY_H 41 using boost::serialization::make_nvp;
53 template <
typename ReturnT>
63 virtual std::shared_ptr<Function1<ReturnT>>
clone()
const {
67 virtual ReturnT
operator()(
double x)
const {
return static_cast<ReturnT
>(x == _xo); }
69 virtual std::string
toString(std::string
const& prefix =
"")
const {
70 std::ostringstream os;
71 os <<
"IntegerDeltaFunction1 [" << _xo <<
"]: ";
72 os << Function1<ReturnT>::toString(prefix);
85 template <
class Archive>
86 void serialize(Archive& ar,
unsigned int const version) {
88 ar& make_nvp(
"xo", this->_xo);
102 template <
typename ReturnT>
112 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
117 return static_cast<ReturnT
>((x == _xo) && (y == _yo));
120 virtual std::string
toString(std::string
const& prefix)
const {
121 std::ostringstream os;
122 os <<
"IntegerDeltaFunction2 [" << _xo <<
", " << _yo <<
"]: ";
123 os << Function2<ReturnT>::toString(prefix);
136 friend class boost::serialization::access;
137 template <
class Archive>
138 void serialize(Archive& ar,
unsigned int const version) {
140 ar& make_nvp(
"xo", this->_xo);
141 ar& make_nvp(
"yo", this->_yo);
155 template <
typename ReturnT>
168 virtual std::shared_ptr<Function1<ReturnT>>
clone()
const {
173 return static_cast<ReturnT
>((_multFac / this->
_params[0]) *
174 std::exp(-(x * x) / (2.0 * this->
_params[0] * this->
_params[0])));
177 virtual std::string
toString(std::string
const& prefix)
const {
178 std::ostringstream os;
179 os <<
"GaussianFunction1 [" << _multFac <<
"]: ";
180 os << Function1<ReturnT>::toString(prefix);
185 const double _multFac;
192 friend class boost::serialization::access;
193 template <
class Archive>
194 void serialize(Archive& ar,
unsigned int const version) {
213 template <
typename ReturnT>
223 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)) {
232 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
233 return std::shared_ptr<Function2<ReturnT>>(
238 if (_angle != this->
_params[2]) {
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]))));
248 virtual std::string
toString(std::string
const& prefix)
const {
249 std::ostringstream os;
250 os <<
"GaussianFunction2: ";
251 os << Function2<ReturnT>::toString(prefix);
280 void _updateCache()
const {
282 _sinAngle = std::sin(_angle);
283 _cosAngle = std::cos(_angle);
285 const double _multFac;
286 mutable double _angle;
287 mutable double _sinAngle;
288 mutable double _cosAngle;
294 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)),
300 friend class boost::serialization::access;
301 template <
class Archive>
302 void serialize(Archive& ar,
unsigned int const version) {
304 ar& make_nvp(
"angle", this->_angle);
305 ar& make_nvp(
"sinAngle", this->_sinAngle);
306 ar& make_nvp(
"cosAngle", this->_cosAngle);
324 template <
typename ReturnT>
335 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)) {
343 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
344 return std::shared_ptr<Function2<ReturnT>>(
349 double radSq = (x *
x) + (y * y);
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)))));
358 virtual std::string
toString(std::string
const& prefix)
const {
359 std::ostringstream os;
360 os <<
"DoubleGaussianFunction2 [" << _multFac <<
"]: ";
361 os << Function2<ReturnT>::toString(prefix);
373 const double _multFac;
380 friend class boost::serialization::access;
381 template <
class Archive>
382 void serialize(Archive& ar,
unsigned int const version) {
394 template <
typename ReturnT>
414 if (params.size() < 1) {
415 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
416 "PolynomialFunction1 called with empty vector");
422 virtual std::shared_ptr<Function1<ReturnT>>
clone()
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];
434 return static_cast<ReturnT
>(retVal);
442 virtual std::string
toString(std::string
const& prefix)
const {
443 std::ostringstream os;
444 os <<
"PolynomialFunction1 []: ";
445 os << Function1<ReturnT>::toString(prefix);
454 friend class boost::serialization::access;
455 template <
class Archive>
456 void serialize(Archive& ar,
unsigned int const version) {
475 template <
typename ReturnT>
488 _xCoeffs(this->_order + 1) {}
501 std::vector<double> params)
505 _xCoeffs(this->_order + 1) {}
509 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
528 const int maxXCoeffInd = this->_order;
533 int paramInd =
static_cast<int>(this->
_params.size()) - 1;
537 for (
int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
538 _xCoeffs[xCoeffInd] = this->
_params[paramInd];
542 for (
int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
543 _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->
_params[paramInd];
545 if (xCoeffInd >= endXCoeffInd) {
556 double retVal = _xCoeffs[maxXCoeffInd];
557 for (
int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
558 retVal = (retVal *
x) + _xCoeffs[xCoeffInd];
560 return static_cast<ReturnT
>(retVal);
567 virtual std::vector<double> getDFuncDParameters(
double x,
double y)
const;
569 virtual std::string
toString(std::string
const& prefix)
const {
570 std::ostringstream os;
571 os <<
"PolynomialFunction2 [" << this->_order <<
"]: ";
572 os << Function2<ReturnT>::toString(prefix);
584 mutable double _oldY;
585 mutable std::vector<double> _xCoeffs;
592 friend class boost::serialization::access;
593 template <
class Archive>
594 void serialize(Archive& ar,
unsigned int const version) {
596 ar& make_nvp(
"yCoeffs", this->_xCoeffs);
616 template <
typename ReturnT>
628 _initialize(minX, maxX);
642 if (params.size() < 1) {
643 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
644 "Chebyshev1Function1 called with empty vector");
646 _initialize(minX, maxX);
651 virtual std::shared_ptr<Function1<ReturnT>>
clone()
const {
673 double xPrime = (x + _offset) * _scale;
677 int const order = _order;
680 }
else if (order == 1) {
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;
690 return (xPrime * csh) + this->
_params[0] - cshPrev;
693 virtual std::string
toString(std::string
const& prefix)
const {
694 std::ostringstream os;
695 os <<
"Chebyshev1Function1 [" << _minX <<
", " << _maxX <<
"]: ";
696 os << Function1<ReturnT>::toString(prefix);
710 void _initialize(
double minX,
double maxX) {
713 _scale = 2.0 / (_maxX - _minX);
714 _offset = -(_minX + _maxX) * 0.5;
721 :
Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
724 friend class boost::serialization::access;
725 template <
class Archive>
726 void serialize(Archive& ar,
unsigned int const) {
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);
762 template <
typename ReturnT>
776 _yCheby(this->_order + 1),
777 _xCoeffs(this->_order + 1) {
778 _initialize(xyRange);
795 _yCheby(this->_order + 1),
796 _xCoeffs(this->_order + 1) {
797 _initialize(xyRange);
802 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
803 return std::shared_ptr<Function2<ReturnT>>(
822 if (truncOrder > this->_order) {
823 std::ostringstream os;
824 os <<
"truncated order=" << truncOrder <<
" must be <= original order=" << this->_order;
825 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
827 int truncNParams = this->nParametersFromOrder(truncOrder);
828 std::vector<double> truncParams(this->
_params.begin(), this->
_params.begin() + truncNParams);
846 double const xPrime = (x + _offsetX) * _scaleX;
847 double const yPrime = (y + _offsetY) * _scaleY;
849 const int nParams =
static_cast<int>(this->
_params.size());
850 const int order = this->_order;
860 for (
int chebyInd = 2; chebyInd <= order; chebyInd++) {
861 _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
864 for (
int coeffInd = 0; coeffInd <= order; coeffInd++) {
865 _xCoeffs[coeffInd] = 0;
867 for (
int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
868 _xCoeffs[coeffInd] += this->
_params[paramInd] * _yCheby[endCoeffInd];
872 coeffInd = endCoeffInd;
884 return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
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;
893 return (xPrime * csh) + _xCoeffs[0] - cshPrev;
896 virtual std::string
toString(std::string
const& prefix)
const {
897 std::ostringstream os;
898 os <<
"Chebyshev1Function2 [";
899 os << this->_order <<
", " << this->getXYRange() <<
"]";
900 os << Function2<ReturnT>::toString(prefix);
912 mutable double _oldYPrime;
913 mutable std::vector<double> _yCheby;
914 mutable std::vector<double> _xCoeffs;
932 _scaleX = 2.0 / (_maxX - _minX);
933 _scaleY = 2.0 / (_maxY - _minY);
934 _offsetX = -(_minX + _maxX) * 0.5;
935 _offsetY = -(_minY + _maxY) * 0.5;
955 friend class boost::serialization::access;
956 template <
class Archive>
957 void serialize(Archive& ar,
unsigned int const version) {
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);
968 _xCoeffs.resize(_yCheby.size());
984 template <
typename ReturnT>
991 double xOffset = 0.0)
993 _invN(1.0 / static_cast<double>(n)) {
999 virtual std::shared_ptr<Function1<ReturnT>>
clone()
const {
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));
1009 return static_cast<ReturnT
>(1);
1016 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1018 virtual std::string
toString(std::string
const& prefix)
const {
1019 std::ostringstream os;
1020 os <<
"LanczosFunction1 [" << this->getOrder() <<
"]: ";
1022 os << Function1<ReturnT>::toString(prefix);
1034 friend class boost::serialization::access;
1035 template <
class Archive>
1036 void serialize(Archive& ar,
unsigned int const version) {
1038 ar& make_nvp(
"invN", this->_invN);
1054 template <
typename ReturnT>
1061 double xOffset = 0.0,
1062 double yOffset = 0.0)
1064 _invN(1.0 / static_cast<double>(n)) {
1071 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const {
1072 return std::shared_ptr<Function2<ReturnT>>(
1078 double xArg2 = xArg1 * _invN;
1080 if (std::fabs(xArg1) > 1.0e-5) {
1081 xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1084 double yArg2 = yArg1 * _invN;
1086 if (std::fabs(yArg1) > 1.0e-5) {
1087 yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1089 return static_cast<ReturnT
>(xFunc * yFunc);
1095 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1097 virtual std::string
toString(std::string
const& prefix)
const {
1098 std::ostringstream os;
1099 os <<
"LanczosFunction2 [" << this->getOrder() <<
"]: ";
1101 os << Function2<ReturnT>::toString(prefix);
1113 friend class boost::serialization::access;
1114 template <
class Archive>
1115 void serialize(Archive& ar,
unsigned int const version) {
1117 ar& make_nvp(
"invN", this->_invN);
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.
virtual ~PolynomialFunction1()
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.
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
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.
virtual ~DoubleGaussianFunction2()
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.
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.
Base class for 2-dimensional polynomials of the form:
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.
double constexpr PI
The ratio of a circle's circumference to diameter.
virtual ~LanczosFunction1()
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
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 ~LanczosFunction2()
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 ~PolynomialFunction2()
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.
DoubleGaussianFunction2()
A Function taking one argument.
virtual ReturnT operator()(double x) const
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.
afw::table::Key< double > sigma
virtual ~Chebyshev1Function2()
virtual ReturnT operator()(double x, double y) const
virtual ReturnT operator()(double x, double y) const
virtual ~IntegerDeltaFunction1()
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.
DoubleGaussianFunction2(double sigma1, double sigma2=0, double ampl2=0)
Construct a Gaussian function with specified x and y sigma.
virtual ~GaussianFunction1()
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 ~Chebyshev1Function1()
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.
virtual ~IntegerDeltaFunction2()
A floating-point coordinate rectangle geometry.
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.
virtual ~GaussianFunction2()
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.
unsigned int getOrder() const
Get the order of the Lanczos function.
1-dimensional polynomial function.
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.