25 #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H 26 #define LSST_AFW_MATH_FUNCTIONLIBRARY_H 41 using boost::serialization::make_nvp;
53 template <
typename ReturnT>
71 virtual ReturnT
operator()(
double x)
const {
return static_cast<ReturnT
>(x == _xo); }
75 os <<
"IntegerDeltaFunction1 [" << _xo <<
"]: ";
76 os << Function1<ReturnT>::toString(prefix);
89 template <
class Archive>
90 void serialize(Archive& ar,
unsigned int const version) {
92 ar& make_nvp(
"xo", this->_xo);
106 template <
typename ReturnT>
125 return static_cast<ReturnT
>((x == _xo) && (y == _yo));
130 os <<
"IntegerDeltaFunction2 [" << _xo <<
", " << _yo <<
"]: ";
131 os << Function2<ReturnT>::toString(prefix);
144 friend class boost::serialization::access;
145 template <
class Archive>
146 void serialize(Archive& ar,
unsigned int const version) {
148 ar& make_nvp(
"xo", this->_xo);
149 ar& make_nvp(
"yo", this->_yo);
163 template <
typename ReturnT>
185 return static_cast<ReturnT
>((_multFac / this->
_params[0]) *
191 os <<
"GaussianFunction1 [" << _multFac <<
"]: ";
192 os << Function1<ReturnT>::toString(prefix);
197 const double _multFac;
204 friend class boost::serialization::access;
205 template <
class Archive>
206 void serialize(Archive& ar,
unsigned int const version) {
225 template <
typename ReturnT>
235 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)) {
254 if (_angle != this->
_params[2]) {
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])) *
261 ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
266 os <<
"GaussianFunction2: ";
267 os << Function2<ReturnT>::toString(prefix);
296 void _updateCache()
const {
301 const double _multFac;
302 mutable double _angle;
303 mutable double _sinAngle;
304 mutable double _cosAngle;
310 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)),
316 friend class boost::serialization::access;
317 template <
class Archive>
318 void serialize(Archive& ar,
unsigned int const version) {
320 ar& make_nvp(
"angle", this->_angle);
321 ar& make_nvp(
"sinAngle", this->_sinAngle);
322 ar& make_nvp(
"cosAngle", this->_cosAngle);
340 template <
typename ReturnT>
351 _multFac(1.0 / (
lsst::afw::geom::
TWOPI)) {
369 double radSq = (x * x) + (y * y);
371 double sigma2Sq = this->_params[1] * this->_params[1];
372 double b = this->_params[2];
373 return static_cast<ReturnT
>(
374 (_multFac / (sigma1Sq + (b * sigma2Sq))) *
375 (
std::exp(-radSq / (2.0 * sigma1Sq)) + (b *
std::exp(-radSq / (2.0 * sigma2Sq)))));
380 os <<
"DoubleGaussianFunction2 [" << _multFac <<
"]: ";
381 os << Function2<ReturnT>::toString(prefix);
393 const double _multFac;
400 friend class boost::serialization::access;
401 template <
class Archive>
402 void serialize(Archive& ar,
unsigned int const version) {
414 template <
typename ReturnT>
434 if (params.
size() < 1) {
436 "PolynomialFunction1 called with empty vector");
453 int const order =
static_cast<int>(this->
_params.
size()) - 1;
454 double retVal = this->
_params[order];
455 for (
int ii = order - 1; ii >= 0; --ii) {
456 retVal = (retVal * x) + this->
_params[ii];
458 return static_cast<ReturnT
>(retVal);
468 os <<
"PolynomialFunction1 []: ";
469 os << Function1<ReturnT>::toString(prefix);
478 friend class boost::serialization::access;
479 template <
class Archive>
480 void serialize(Archive& ar,
unsigned int const version) {
499 template <
typename ReturnT>
512 _xCoeffs(this->_order + 1) {}
529 _xCoeffs(this->_order + 1) {}
556 const int maxXCoeffInd = this->_order;
561 int paramInd =
static_cast<int>(this->
_params.
size()) - 1;
565 for (
int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
566 _xCoeffs[xCoeffInd] = this->
_params[paramInd];
570 for (
int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
571 _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] * y) + this->
_params[paramInd];
573 if (xCoeffInd >= endXCoeffInd) {
584 double retVal = _xCoeffs[maxXCoeffInd];
585 for (
int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
586 retVal = (retVal * x) + _xCoeffs[xCoeffInd];
588 return static_cast<ReturnT
>(retVal);
599 os <<
"PolynomialFunction2 [" << this->_order <<
"]: ";
600 os << Function2<ReturnT>::toString(prefix);
612 mutable double _oldY;
620 friend class boost::serialization::access;
621 template <
class Archive>
622 void serialize(Archive& ar,
unsigned int const version) {
624 ar& make_nvp(
"yCoeffs", this->_xCoeffs);
644 template <
typename ReturnT>
656 _initialize(minX, maxX);
670 if (params.
size() < 1) {
672 "Chebyshev1Function1 called with empty vector");
674 _initialize(minX, maxX);
705 double xPrime = (x + _offset) * _scale;
709 int const order = _order;
712 }
else if (order == 1) {
715 double cshPrev = this->
_params[order];
716 double csh = (2 * xPrime * this->
_params[order]) + this->
_params[order - 1];
717 for (
int i = order - 2; i > 0; --i) {
718 double cshNext = (2 * xPrime * csh) + this->
_params[i] - cshPrev;
722 return (xPrime * csh) + this->
_params[0] - cshPrev;
727 os <<
"Chebyshev1Function1 [" << _minX <<
", " << _maxX <<
"]: ";
728 os << Function1<ReturnT>::toString(prefix);
742 void _initialize(
double minX,
double maxX) {
745 _scale = 2.0 / (_maxX - _minX);
746 _offset = -(_minX + _maxX) * 0.5;
753 :
Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
756 friend class boost::serialization::access;
757 template <
class Archive>
758 void serialize(Archive& ar,
unsigned int const) {
760 ar& make_nvp(
"minX", this->_minX);
761 ar& make_nvp(
"minX", this->_minX);
762 ar& make_nvp(
"maxX", this->_maxX);
763 ar& make_nvp(
"scale", this->_scale);
764 ar& make_nvp(
"offset", this->_offset);
765 ar& make_nvp(
"maxInd", this->_order);
794 template <
typename ReturnT>
808 _yCheby(this->_order + 1),
809 _xCoeffs(this->_order + 1) {
810 _initialize(xyRange);
827 _yCheby(this->_order + 1),
828 _xCoeffs(this->_order + 1) {
829 _initialize(xyRange);
858 if (truncOrder > this->_order) {
860 os <<
"truncated order=" << truncOrder <<
" must be <= original order=" << this->_order;
863 int truncNParams = this->nParametersFromOrder(truncOrder);
882 double const xPrime = (x + _offsetX) * _scaleX;
883 double const yPrime = (y + _offsetY) * _scaleY;
885 const int nParams =
static_cast<int>(this->
_params.
size());
886 const int order = this->_order;
896 for (
int chebyInd = 2; chebyInd <= order; chebyInd++) {
897 _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
900 for (
int coeffInd = 0; coeffInd <= order; coeffInd++) {
901 _xCoeffs[coeffInd] = 0;
903 for (
int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
904 _xCoeffs[coeffInd] += this->
_params[paramInd] * _yCheby[endCoeffInd];
908 coeffInd = endCoeffInd;
920 return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
922 double cshPrev = _xCoeffs[order];
923 double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
924 for (
int i = order - 2; i > 0; --i) {
925 double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
929 return (xPrime * csh) + _xCoeffs[0] - cshPrev;
934 os <<
"Chebyshev1Function2 [";
935 os << this->_order <<
", " << this->getXYRange() <<
"]";
936 os << Function2<ReturnT>::toString(prefix);
948 mutable double _oldYPrime;
968 _scaleX = 2.0 / (_maxX - _minX);
969 _scaleY = 2.0 / (_maxY - _minY);
970 _offsetX = -(_minX + _maxX) * 0.5;
971 _offsetY = -(_minY + _maxY) * 0.5;
991 friend class boost::serialization::access;
992 template <
class Archive>
993 void serialize(Archive& ar,
unsigned int const version) {
995 ar& make_nvp(
"minX", this->_minX);
996 ar& make_nvp(
"minY", this->_minY);
997 ar& make_nvp(
"maxX", this->_maxX);
998 ar& make_nvp(
"maxY", this->_maxY);
999 ar& make_nvp(
"scaleX", this->_scaleX);
1000 ar& make_nvp(
"scaleY", this->_scaleY);
1001 ar& make_nvp(
"offsetX", this->_offsetX);
1002 ar& make_nvp(
"offsetY", this->_offsetY);
1003 ar& make_nvp(
"xCheby", this->_yCheby);
1020 template <
typename ReturnT>
1027 double xOffset = 0.0)
1029 _invN(1.0 / static_cast<double>(n)) {
1045 double xArg2 = xArg1 * _invN;
1047 return static_cast<ReturnT
>(
std::sin(xArg1) *
std::sin(xArg2) / (xArg1 * xArg2));
1049 return static_cast<ReturnT
>(1);
1056 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1060 os <<
"LanczosFunction1 [" << this->getOrder() <<
"]: ";
1062 os << Function1<ReturnT>::toString(prefix);
1074 friend class boost::serialization::access;
1075 template <
class Archive>
1076 void serialize(Archive& ar,
unsigned int const version) {
1078 ar& make_nvp(
"invN", this->_invN);
1094 template <
typename ReturnT>
1101 double xOffset = 0.0,
1102 double yOffset = 0.0)
1104 _invN(1.0 / static_cast<double>(n)) {
1122 double xArg2 = xArg1 * _invN;
1128 double yArg2 = yArg1 * _invN;
1133 return static_cast<ReturnT
>(xFunc * yFunc);
1139 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1143 os <<
"LanczosFunction2 [" << this->getOrder() <<
"]: ";
1145 os << Function2<ReturnT>::toString(prefix);
1157 friend class boost::serialization::access;
1158 template <
class Archive>
1159 void serialize(Archive& ar,
unsigned int const version) {
1161 ar& make_nvp(
"invN", this->_invN);
1168 #endif // #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H 2-dimensional weighted sum of Chebyshev polynomials of the first kind.
virtual ReturnT operator()(double x) const
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
double getMaxX() const
Get maximum allowed x.
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
virtual ReturnT operator()(double x, double y) const
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
virtual ReturnT operator()(double x, double y) const
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual ReturnT operator()(double x, double y) const
An object passed to Persistable::write to allow it to persist itself.
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
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 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.
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.
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 ~IntegerDeltaFunction1()=default
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.
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.
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.
#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.
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.
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 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.