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>
184 return static_cast<ReturnT
>((_multFac / this->
_params[0]) *
190 os <<
"GaussianFunction1 [" << _multFac <<
"]: ";
191 os << Function1<ReturnT>::toString(
prefix);
196 const double _multFac;
203 friend class boost::serialization::access;
204 template <
class Archive>
205 void serialize(Archive& ar,
unsigned int const version) {
224 template <
typename ReturnT>
252 if (_angle != this->
_params[2]) {
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])) *
259 ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
264 os <<
"GaussianFunction2: ";
265 os << Function2<ReturnT>::toString(
prefix);
294 void _updateCache()
const {
299 const double _multFac;
300 mutable double _angle;
301 mutable double _sinAngle;
302 mutable double _cosAngle;
314 friend class boost::serialization::access;
315 template <
class Archive>
316 void serialize(Archive& ar,
unsigned int const version) {
318 ar& make_nvp(
"angle", this->_angle);
319 ar& make_nvp(
"sinAngle", this->_sinAngle);
320 ar& make_nvp(
"cosAngle", this->_cosAngle);
338 template <
typename ReturnT>
366 double radSq = (x *
x) + (y * y);
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)))));
377 os <<
"DoubleGaussianFunction2 [" << _multFac <<
"]: ";
378 os << Function2<ReturnT>::toString(
prefix);
390 const double _multFac;
397 friend class boost::serialization::access;
398 template <
class Archive>
399 void serialize(Archive& ar,
unsigned int const version) {
411 template <
typename ReturnT>
431 if (params.
size() < 1) {
433 "PolynomialFunction1 called with empty vector");
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];
455 return static_cast<ReturnT
>(retVal);
465 os <<
"PolynomialFunction1 []: ";
466 os << Function1<ReturnT>::toString(
prefix);
475 friend class boost::serialization::access;
476 template <
class Archive>
477 void serialize(Archive& ar,
unsigned int const version) {
496 template <
typename ReturnT>
549 const int maxXCoeffInd = this->_order;
554 int paramInd =
static_cast<int>(this->
_params.
size()) - 1;
558 for (
int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
559 _xCoeffs[xCoeffInd] = this->
_params[paramInd];
563 for (
int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
564 _xCoeffs[xCoeffInd] = (_xCoeffs[xCoeffInd] *
y) + this->
_params[paramInd];
566 if (xCoeffInd >= endXCoeffInd) {
577 double retVal = _xCoeffs[maxXCoeffInd];
578 for (
int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
579 retVal = (retVal *
x) + _xCoeffs[xCoeffInd];
581 return static_cast<ReturnT
>(retVal);
592 os <<
"PolynomialFunction2 [" << this->_order <<
"]: ";
593 os << Function2<ReturnT>::toString(
prefix);
605 mutable double _oldY;
613 friend class boost::serialization::access;
614 template <
class Archive>
615 void serialize(Archive& ar,
unsigned int const version) {
617 ar& make_nvp(
"yCoeffs", this->_xCoeffs);
637 template <
typename ReturnT>
649 _initialize(minX, maxX);
663 if (params.
size() < 1) {
665 "Chebyshev1Function1 called with empty vector");
667 _initialize(minX, maxX);
698 double xPrime = (x + _offset) * _scale;
702 int const order = _order;
705 }
else if (order == 1) {
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;
715 return (xPrime * csh) + this->
_params[0] - cshPrev;
720 os <<
"Chebyshev1Function1 [" << _minX <<
", " << _maxX <<
"]: ";
721 os << Function1<ReturnT>::toString(
prefix);
735 void _initialize(
double minX,
double maxX) {
738 _scale = 2.0 / (_maxX - _minX);
739 _offset = -(_minX + _maxX) * 0.5;
746 :
Function1<ReturnT>(1), _minX(0.0), _maxX(0.0), _scale(1.0), _offset(0.0), _order(0) {}
749 friend class boost::serialization::access;
750 template <
class Archive>
751 void serialize(Archive& ar,
unsigned int const) {
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);
787 template <
typename ReturnT>
801 _yCheby(this->_order + 1),
802 _xCoeffs(this->_order + 1) {
803 _initialize(xyRange);
820 _yCheby(this->_order + 1),
821 _xCoeffs(this->_order + 1) {
822 _initialize(xyRange);
850 if (truncOrder > this->_order) {
852 os <<
"truncated order=" << truncOrder <<
" must be <= original order=" << this->_order;
855 int truncNParams = this->nParametersFromOrder(truncOrder);
874 double const xPrime = (x + _offsetX) * _scaleX;
875 double const yPrime = (y + _offsetY) * _scaleY;
877 const int nParams =
static_cast<int>(this->
_params.
size());
878 const int order = this->_order;
888 for (
int chebyInd = 2; chebyInd <= order; chebyInd++) {
889 _yCheby[chebyInd] = (2 * yPrime * _yCheby[chebyInd - 1]) - _yCheby[chebyInd - 2];
892 for (
int coeffInd = 0; coeffInd <= order; coeffInd++) {
893 _xCoeffs[coeffInd] = 0;
895 for (
int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
896 _xCoeffs[coeffInd] += this->
_params[paramInd] * _yCheby[endCoeffInd];
900 coeffInd = endCoeffInd;
912 return _xCoeffs[0] + (_xCoeffs[1] * xPrime);
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;
921 return (xPrime * csh) + _xCoeffs[0] - cshPrev;
926 os <<
"Chebyshev1Function2 [";
927 os << this->_order <<
", " << this->getXYRange() <<
"]";
928 os << Function2<ReturnT>::toString(
prefix);
940 mutable double _oldYPrime;
960 _scaleX = 2.0 / (_maxX - _minX);
961 _scaleY = 2.0 / (_maxY - _minY);
962 _offsetX = -(_minX + _maxX) * 0.5;
963 _offsetY = -(_minY + _maxY) * 0.5;
983 friend class boost::serialization::access;
984 template <
class Archive>
985 void serialize(Archive& ar,
unsigned int const version) {
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);
1012 template <
typename ReturnT>
1019 double xOffset = 0.0)
1020 :
Function1<ReturnT>(1), _invN(1.0 / static_cast<double>(n)) {
1036 double xArg2 = xArg1 * _invN;
1038 return static_cast<ReturnT
>(
std::sin(xArg1) *
std::sin(xArg2) / (xArg1 * xArg2));
1040 return static_cast<ReturnT
>(1);
1047 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1051 os <<
"LanczosFunction1 [" << this->getOrder() <<
"]: ";
1053 os << Function1<ReturnT>::toString(
prefix);
1065 friend class boost::serialization::access;
1066 template <
class Archive>
1067 void serialize(Archive& ar,
unsigned int const version) {
1069 ar& make_nvp(
"invN", this->_invN);
1085 template <
typename ReturnT>
1092 double xOffset = 0.0,
1093 double yOffset = 0.0)
1094 :
Function2<ReturnT>(2), _invN(1.0 / static_cast<double>(n)) {
1112 double xArg2 = xArg1 * _invN;
1118 double yArg2 = yArg1 * _invN;
1123 return static_cast<ReturnT
>(xFunc * yFunc);
1129 unsigned int getOrder()
const {
return static_cast<unsigned int>(0.5 + (1.0 / _invN)); };
1133 os <<
"LanczosFunction2 [" << this->getOrder() <<
"]: ";
1135 os << Function2<ReturnT>::toString(
prefix);
1147 friend class boost::serialization::access;
1148 template <
class Archive>
1149 void serialize(Archive& ar,
unsigned int const version) {
1151 ar& make_nvp(
"invN", this->_invN);
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.
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.
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.
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.
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
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.
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.
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
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.
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.
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.
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.
unsigned int getNParameters() const
Return the number of function parameters.
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 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.
table::Key< double > sigma1
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.