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 noexcept(IS_NOTHROW_INIT<ReturnT>) {
72 return static_cast<ReturnT
>(
x == _xo);
77 os <<
"IntegerDeltaFunction1 [" << _xo <<
"]: ";
78 os << Function1<ReturnT>::toString(
prefix);
91 template <
class Archive>
92 void serialize(Archive& ar,
unsigned int const version) {
94 ar& make_nvp(
"xo", this->_xo);
108 template <
typename ReturnT>
126 virtual ReturnT
operator()(
double x,
double y)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
127 return static_cast<ReturnT
>((
x == _xo) && (
y == _yo));
132 os <<
"IntegerDeltaFunction2 [" << _xo <<
", " << _yo <<
"]: ";
133 os << Function2<ReturnT>::toString(
prefix);
146 friend class boost::serialization::access;
147 template <
class Archive>
148 void serialize(Archive& ar,
unsigned int const version) {
150 ar& make_nvp(
"xo", this->_xo);
151 ar& make_nvp(
"yo", this->_yo);
165 template <
typename ReturnT>
185 virtual ReturnT
operator()(
double x)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
186 return static_cast<ReturnT
>((_multFac / this->
_params[0]) *
192 os <<
"GaussianFunction1 [" << _multFac <<
"]: ";
193 os << Function1<ReturnT>::toString(
prefix);
198 const double _multFac;
205 friend class boost::serialization::access;
206 template <
class Archive>
207 void serialize(Archive& ar,
unsigned int const version) {
226 template <
typename ReturnT>
253 virtual ReturnT
operator()(
double x,
double y)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
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 noexcept {
301 const double _multFac;
302 mutable double _angle;
303 mutable double _sinAngle;
304 mutable double _cosAngle;
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>
367 virtual ReturnT
operator()(
double x,
double y)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
368 double radSq = (
x *
x) + (
y *
y);
370 double sigma2Sq = this->_params[1] * this->_params[1];
371 double b = this->_params[2];
372 return static_cast<ReturnT
>(
373 (_multFac / (sigma1Sq + (b * sigma2Sq))) *
374 (
std::exp(-radSq / (2.0 * sigma1Sq)) + (b *
std::exp(-radSq / (2.0 * sigma2Sq)))));
379 os <<
"DoubleGaussianFunction2 [" << _multFac <<
"]: ";
380 os << Function2<ReturnT>::toString(
prefix);
392 const double _multFac;
399 friend class boost::serialization::access;
400 template <
class Archive>
401 void serialize(Archive& ar,
unsigned int const version) {
413 template <
typename ReturnT>
433 if (params.
size() < 1) {
435 "PolynomialFunction1 called with empty vector");
451 virtual ReturnT
operator()(
double x)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
452 int const order =
static_cast<int>(this->
_params.
size()) - 1;
453 double retVal = this->
_params[order];
454 for (
int ii = order - 1; ii >= 0; --ii) {
455 retVal = (retVal *
x) + this->
_params[ii];
457 return static_cast<ReturnT
>(retVal);
467 os <<
"PolynomialFunction1 []: ";
468 os << Function1<ReturnT>::toString(
prefix);
477 friend class boost::serialization::access;
478 template <
class Archive>
479 void serialize(Archive& ar,
unsigned int const version) {
498 template <
typename ReturnT>
536 virtual ReturnT
operator()(
double x,
double y)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
551 const int maxXCoeffInd = this->_order;
556 int paramInd =
static_cast<int>(this->
_params.
size()) - 1;
560 for (
int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
561 _xCoeffs[xCoeffInd] = this->
_params[paramInd];
565 for (
int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
566 _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] *
y) + this->
_params[paramInd];
568 if (xCoeffInd >= endXCoeffInd) {
579 double retVal = _xCoeffs[maxXCoeffInd];
580 for (
int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
581 retVal = (retVal *
x) + _xCoeffs[xCoeffInd];
583 return static_cast<ReturnT
>(retVal);
594 os <<
"PolynomialFunction2 [" << this->_order <<
"]: ";
595 os << Function2<ReturnT>::toString(
prefix);
607 mutable double _oldY;
615 friend class boost::serialization::access;
616 template <
class Archive>
617 void serialize(Archive& ar,
unsigned int const version) {
619 ar& make_nvp(
"yCoeffs", this->_xCoeffs);
639 template <
typename ReturnT>
651 _initialize(minX, maxX);
665 if (params.
size() < 1) {
667 "Chebyshev1Function1 called with empty vector");
669 _initialize(minX, maxX);
685 double getMinX() const noexcept {
return _minX; };
690 double getMaxX() const noexcept {
return _maxX; };
700 double xPrime = (x + _offset) * _scale;
704 int const order = _order;
707 }
else if (order == 1) {
710 double cshPrev = this->
_params[order];
711 double csh = (2 * xPrime * this->
_params[order]) + this->
_params[order - 1];
712 for (
int i = order - 2; i > 0; --i) {
713 double cshNext = (2 * xPrime * csh) + this->
_params[i] - cshPrev;
717 return (xPrime * csh) + this->
_params[0] - cshPrev;
722 os <<
"Chebyshev1Function1 [" << _minX <<
", " << _maxX <<
"]: ";
723 os << Function1<ReturnT>::toString(
prefix);
737 void _initialize(
double minX,
double maxX) {
740 _scale = 2.0 / (_maxX - _minX);
741 _offset = -(_minX + _maxX) * 0.5;
748 :
Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
751 friend class boost::serialization::access;
752 template <
class Archive>
753 void serialize(Archive& ar,
unsigned int const) {
755 ar& make_nvp(
"minX", this->_minX);
756 ar& make_nvp(
"minX", this->_minX);
757 ar& make_nvp(
"maxX", this->_maxX);
758 ar& make_nvp(
"scale", this->_scale);
759 ar& make_nvp(
"offset", this->_offset);
760 ar& make_nvp(
"maxInd", this->_order);
789 template <
typename ReturnT>
803 _yCheby(this->_order + 1),
804 _xCoeffs(this->_order + 1) {
805 _initialize(xyRange);
822 _yCheby(this->_order + 1),
823 _xCoeffs(this->_order + 1) {
824 _initialize(xyRange);
852 if (truncOrder > this->_order) {
854 os <<
"truncated order=" << truncOrder <<
" must be <= original order=" << this->_order;
857 int truncNParams = this->nParametersFromOrder(truncOrder);
876 double const xPrime = (x + _offsetX) * _scaleX;
877 double const yPrime = (y + _offsetY) * _scaleY;
879 const int nParams =
static_cast<int>(this->
_params.
size());
880 const int order = this->_order;
890 for (
int chebyInd = 2; chebyInd <= order; chebyInd++) {
891 _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
894 for (
int coeffInd = 0; coeffInd <= order; coeffInd++) {
895 _xCoeffs[coeffInd] = 0;
897 for (
int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
898 _xCoeffs[coeffInd] += this->
_params[paramInd] * _yCheby[endCoeffInd];
902 coeffInd = endCoeffInd;
914 return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
916 double cshPrev = _xCoeffs[order];
917 double csh = (2 * xPrime * _xCoeffs[order]) + _xCoeffs[order - 1];
918 for (
int i = order - 2; i > 0; --i) {
919 double cshNext = (2 * xPrime * csh) + _xCoeffs[i] - cshPrev;
923 return (xPrime * csh) + _xCoeffs[0] - cshPrev;
928 os <<
"Chebyshev1Function2 [";
929 os << this->_order <<
", " << this->getXYRange() <<
"]";
930 os << Function2<ReturnT>::toString(
prefix);
942 mutable double _oldYPrime;
962 _scaleX = 2.0 / (_maxX - _minX);
963 _scaleY = 2.0 / (_maxY - _minY);
964 _offsetX = -(_minX + _maxX) * 0.5;
965 _offsetY = -(_minY + _maxY) * 0.5;
985 friend class boost::serialization::access;
986 template <
class Archive>
987 void serialize(Archive& ar,
unsigned int const version) {
989 ar& make_nvp(
"minX", this->_minX);
990 ar& make_nvp(
"minY", this->_minY);
991 ar& make_nvp(
"maxX", this->_maxX);
992 ar& make_nvp(
"maxY", this->_maxY);
993 ar& make_nvp(
"scaleX", this->_scaleX);
994 ar& make_nvp(
"scaleY", this->_scaleY);
995 ar& make_nvp(
"offsetX", this->_offsetX);
996 ar& make_nvp(
"offsetY", this->_offsetY);
997 ar& make_nvp(
"xCheby", this->_yCheby);
1014 template <
typename ReturnT>
1021 double xOffset = 0.0)
1022 :
Function1<ReturnT>(1), _invN(1.0 / static_cast<double>(n)) {
1036 virtual ReturnT
operator()(
double x)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
1038 double xArg2 = xArg1 * _invN;
1040 return static_cast<ReturnT
>(
std::sin(xArg1) *
std::sin(xArg2) / (xArg1 * xArg2));
1042 return static_cast<ReturnT
>(1);
1049 unsigned int getOrder() const noexcept {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1053 os <<
"LanczosFunction1 [" << this->getOrder() <<
"]: ";
1055 os << Function1<ReturnT>::toString(
prefix);
1067 friend class boost::serialization::access;
1068 template <
class Archive>
1069 void serialize(Archive& ar,
unsigned int const version) {
1071 ar& make_nvp(
"invN", this->_invN);
1087 template <
typename ReturnT>
1094 double xOffset = 0.0,
1095 double yOffset = 0.0)
1096 :
Function2<ReturnT>(2), _invN(1.0 / static_cast<double>(n)) {
1112 virtual ReturnT
operator()(
double x,
double y)
const noexcept(IS_NOTHROW_INIT<ReturnT>) {
1114 double xArg2 = xArg1 * _invN;
1120 double yArg2 = yArg1 * _invN;
1125 return static_cast<ReturnT
>(xFunc * yFunc);
1131 unsigned int getOrder() const noexcept {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1135 os <<
"LanczosFunction2 [" << this->getOrder() <<
"]: ";
1137 os << Function2<ReturnT>::toString(
prefix);
1149 friend class boost::serialization::access;
1150 template <
class Archive>
1151 void serialize(Archive& ar,
unsigned int const version) {
1153 ar& make_nvp(
"invN", this->_invN);
1160 #endif // #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H virtual bool isLinearCombination() const noexcept
Is the function a linear combination of its parameters?
double getMinY() const noexcept
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
virtual ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >)
unsigned int getNParameters() const noexcept
Return the number of function parameters.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
double getMaxX() const noexcept
Get maximum allowed x.
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
unsigned int getOrder() const noexcept
Get the order of the Lanczos function.
virtual ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >)
An object passed to Persistable::write to allow it to persist itself.
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
virtual ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >)
double getMinX() const noexcept
Get minimum allowed x.
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
double getMinX() const noexcept
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual bool isLinearCombination() const noexcept
Is the function a linear combination of its parameters?
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, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >)
double getMaxX() const noexcept
Chebyshev1Function1(unsigned int order, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial of specified order and range.
2-dimensional integer delta function.
virtual Chebyshev1Function2 truncate(int truncOrder) const
Return a truncated copy of lower (or equal) order.
IntegerDeltaFunction1(double xo)
Construct an integer delta function with specified xo.
Chebyshev1Function2(std::vector< double > params, lsst::geom::Box2D const &xyRange=lsst::geom::Box2D(lsst::geom::Point2D(-1.0, -1.0), lsst::geom::Point2D(1.0, 1.0)))
Construct a Chebyshev polynomial with specified parameters and range.
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.
virtual ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >)
virtual ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >)
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Base class for 2-dimensional polynomials of the form:
afw::table::Key< double > sigma
double getMaxY() const noexcept
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 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.
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
unsigned int getOrder() const noexcept
Get the order of Lanczos function.
virtual ReturnT operator()(double x) const
DoubleGaussianFunction2()
A Function taking one argument.
virtual ~IntegerDeltaFunction1() noexcept=default
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 noexcept(IS_NOTHROW_INIT< ReturnT >)
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.
lsst::geom::Box2D getXYRange() const noexcept
Get x,y range.
unsigned int getOrder() const noexcept
Get the polynomial order.
table::Key< double > angle
virtual bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
virtual bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
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 isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
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< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual ReturnT operator()(double x) const noexcept(IS_NOTHROW_INIT< ReturnT >)
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
table::Key< double > ampl2
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.
table::Key< double > sigma2
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 ReturnT operator()(double x, double y) const noexcept(IS_NOTHROW_INIT< ReturnT >)
unsigned int getOrder() const noexcept
Get the polynomial order.
virtual bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
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.