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);
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>
163 _multFac(1.0 / std::sqrt(lsst::afw::geom::
TWOPI)) {
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;
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)),
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;
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);
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);
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;
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) {}
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());
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 [";
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;
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);
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);
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
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
friend class boost::serialization::access
virtual ~PolynomialFunction1()
int _order
order of polynomial
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
virtual ReturnT operator()(double x) const
friend class boost::serialization::access
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
double getMaxX() const
Get maximum allowed x.
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
virtual ReturnT operator()(double x) const
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.
An object passed to Persistable::write to allow it to persist itself.
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
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
friend class boost::serialization::access
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.
2-dimensional polynomial function with cross terms
PolynomialFunction1(std::vector< double > params)
Construct a polynomial function with the specified parameters.
double getMinX() const
Get minimum allowed x.
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 std::string toString(std::string const &prefix) const
Return a string representation of the function.
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 ReturnT operator()(double x, double y) const
IntegerDeltaFunction1(double xo)
Construct an integer delta function with specified xo, yo.
virtual ~DoubleGaussianFunction2()
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the coefficients of the Function's parameters, evaluated at (x, y) I.e.
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
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
friend class boost::serialization::access
friend class boost::serialization::access
virtual ReturnT operator()(double x, double y) const
Base class for 2-dimensional polynomials of the form:
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
double constexpr PI
The ratio of a circle's circumference to diameter.
unsigned int getOrder() const
Get the polynomial order.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
virtual ~LanczosFunction1()
virtual std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
std::vector< double > _params
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual ReturnT operator()(double x, double y) const
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.
IntegerDeltaFunction2(double xo, double yo)
Construct an integer delta function with specified xo, yo.
1-dimensional integer delta function.
friend class boost::serialization::access
virtual ReturnT operator()(double x, double y) const
friend class boost::serialization::access
virtual ~LanczosFunction2()
virtual Chebyshev1Function2 truncate(int truncOrder) const
Return a truncated copy of lower (or equal) order.
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.
virtual ~PolynomialFunction2()
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
lsst::afw::geom::Box2D getXYRange() const
Get x,y range.
DoubleGaussianFunction2()
A Function taking one argument.
PolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
afw::table::Key< double > sigma
virtual ReturnT operator()(double x, double y) const
virtual ~Chebyshev1Function2()
virtual ReturnT operator()(double x) const
virtual ~IntegerDeltaFunction1()
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
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.
friend class boost::serialization::access
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.
DoubleGaussianFunction2(double sigma1, double sigma2=0, double ampl2=0)
Construct a Gaussian function with specified x and y sigma.
virtual ReturnT operator()(double x) const
unsigned int getOrder() const
Get the order of Lanczos function.
unsigned int getOrder() const
Get the order of the Lanczos function.
friend class boost::serialization::access
virtual ~GaussianFunction1()
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
unsigned int getOrder() const
Get the polynomial order.
virtual ~Chebyshev1Function1()
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
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 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 ~IntegerDeltaFunction2()
A floating-point coordinate rectangle geometry.
virtual std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.
friend class boost::serialization::access
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 ReturnT operator()(double x) const
virtual ~GaussianFunction2()
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
1-dimensional polynomial function.
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.