25 #ifndef LSST_AFW_MATH_FUNCTIONLIBRARY_H
26 #define LSST_AFW_MATH_FUNCTIONLIBRARY_H
48 using boost::serialization::make_nvp;
61 template<
typename ReturnT>
83 return static_cast<ReturnT
>(x ==
_xo);
86 virtual std::string
toString(std::string
const& prefix=
"")
const {
87 std::ostringstream os;
88 os <<
"IntegerDeltaFunction1 [" <<
_xo <<
"]: ";
89 os << Function1<ReturnT>::toString(prefix);
102 template <
class Archive>
103 void serialize(Archive& ar,
unsigned int const version) {
105 ar & make_nvp(
"xo", this->
_xo);
119 template<
typename ReturnT>
143 return static_cast<ReturnT
>((x ==
_xo) && (y ==
_yo));
146 virtual std::string
toString(std::string
const& prefix)
const {
147 std::ostringstream os;
148 os <<
"IntegerDeltaFunction2 [" <<
_xo <<
", " <<
_yo <<
"]: ";
149 os << Function2<ReturnT>::toString(prefix);
163 template <
class Archive>
164 void serialize(Archive& ar,
unsigned int const version) {
166 ar & make_nvp(
"xo", this->
_xo);
167 ar & make_nvp(
"yo", this->
_yo);
181 template<
typename ReturnT>
204 return static_cast<ReturnT
> (
206 std::exp(- (x * x) / (2.0 * this->
_params[0] * this->
_params[0])));
209 virtual std::string
toString(std::string
const& prefix)
const {
210 std::ostringstream os;
211 os <<
"GaussianFunction1 [" <<
_multFac <<
"]: ";
212 os << Function1<ReturnT>::toString(prefix);
225 template <
class Archive>
226 void serialize(Archive& ar,
unsigned int const version) {
245 template<
typename ReturnT>
279 return static_cast<ReturnT
> (
281 std::exp(- ((pos1 * pos1) / (2.0 * this->
_params[0] * this->
_params[0]))
282 - ((pos2 * pos2) / (2.0 * this->_params[1] * this->_params[1]))));
285 virtual std::string
toString(std::string
const& prefix)
const {
286 std::ostringstream os;
287 os <<
"GaussianFunction2: ";
288 os << Function2<ReturnT>::toString(prefix);
335 template <
class Archive>
336 void serialize(Archive& ar,
unsigned int const version) {
338 ar & make_nvp(
"angle", this->
_angle);
339 ar & make_nvp(
"sinAngle", this->
_sinAngle);
340 ar & make_nvp(
"cosAngle", this->
_cosAngle);
358 template<
typename ReturnT>
387 double radSq = (x *
x) + (y * y);
389 double sigma2Sq = this->_params[1] * this->_params[1];
390 double b = this->_params[2];
391 return static_cast<ReturnT
> (
392 (
_multFac / (sigma1Sq + (b * sigma2Sq))) *
393 (std::exp(-radSq / (2.0 * sigma1Sq))
394 + (b * std::exp(-radSq / (2.0 * sigma2Sq)))));
397 virtual std::string
toString(std::string
const& prefix)
const {
398 std::ostringstream os;
399 os <<
"DoubleGaussianFunction2 [" <<
_multFac <<
"]: ";
400 os << Function2<ReturnT>::toString(prefix);
421 template <
class Archive>
422 void serialize(Archive& ar,
unsigned int const version) {
434 template<
typename ReturnT>
458 std::vector<double> params)
462 if (params.size() < 1) {
463 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
464 "PolynomialFunction1 called with empty vector");
477 int const order =
static_cast<int>(this->
_params.size()) - 1;
478 double retVal = this->
_params[order];
479 for (
int ii = order-1; ii >= 0; --ii) {
480 retVal = (retVal *
x) + this->
_params[ii];
482 return static_cast<ReturnT
>(retVal);
490 virtual std::string
toString(std::string
const& prefix)
const {
491 std::ostringstream os;
492 os <<
"PolynomialFunction1 []: ";
493 os << Function1<ReturnT>::toString(prefix);
503 template <
class Archive>
504 void serialize(Archive& ar,
unsigned int const version) {
523 template<
typename ReturnT>
554 std::vector<double> params)
583 const int maxXCoeffInd = this->
_order;
588 int paramInd =
static_cast<int>(this->
_params.size()) - 1;
592 for (
int xCoeffInd = 0; xCoeffInd <= maxXCoeffInd; ++xCoeffInd, --paramInd) {
597 for (
int xCoeffInd = 0, endXCoeffInd = maxXCoeffInd; paramInd >= 0; --paramInd) {
600 if (xCoeffInd >= endXCoeffInd) {
611 double retVal =
_xCoeffs[maxXCoeffInd];
612 for (
int xCoeffInd = maxXCoeffInd - 1; xCoeffInd >= 0; --xCoeffInd) {
613 retVal = (retVal *
x) +
_xCoeffs[xCoeffInd];
615 return static_cast<ReturnT
>(retVal);
620 virtual std::string
toString(std::string
const& prefix)
const {
621 std::ostringstream os;
622 os <<
"PolynomialFunction2 [" << this->
_order <<
"]: ";
623 os << Function2<ReturnT>::toString(prefix);
645 template <
class Archive>
646 void serialize(Archive& ar,
unsigned int const version) {
648 ar & make_nvp(
"yCoeffs", this->
_xCoeffs);
668 template<
typename ReturnT>
696 std::vector<double> params,
702 if (params.size() < 1) {
703 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError,
704 "Chebyshev1Function1 called with empty vector");
740 }
else if (order == 1) {
743 double cshPrev = this->
_params[order];
744 double csh = (2 * xPrime * this->
_params[order]) + this->
_params[order-1];
745 for (
int i = order - 2; i > 0; --i) {
746 double cshNext = (2 * xPrime * csh) + this->
_params[i] - cshPrev;
750 return (xPrime * csh) + this->
_params[0] - cshPrev;
753 virtual std::string
toString(std::string
const& prefix)
const {
754 std::ostringstream os;
755 os <<
"Chebyshev1Function1 [" <<
_minX <<
", " <<
_maxX <<
"]: ";
756 os << Function1<ReturnT>::toString(prefix);
785 template <
class Archive>
788 ar & make_nvp(
"minX", this->
_minX);
789 ar & make_nvp(
"minX", this->
_minX);
790 ar & make_nvp(
"maxX", this->
_maxX);
791 ar & make_nvp(
"scale", this->
_scale);
792 ar & make_nvp(
"offset", this->
_offset);
793 ar & make_nvp(
"maxInd", this->
_order);
822 template<
typename ReturnT>
854 std::vector<double> params,
890 if (truncOrder > this->
_order) {
891 std::ostringstream os;
892 os <<
"truncated order=" << truncOrder <<
" must be <= original order=" << this->
_order;
893 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
896 std::vector<double> truncParams(this->
_params.begin(), this->
_params.begin() + truncNParams);
917 const int nParams =
static_cast<int>(this->
_params.size());
918 const int order = this->
_order;
928 for (
int chebyInd = 2; chebyInd <= order; chebyInd++) {
932 for (
int coeffInd=0; coeffInd <= order; coeffInd++) {
935 for (
int coeffInd = 0, endCoeffInd = 0, paramInd = 0; paramInd < nParams; paramInd++) {
940 coeffInd = endCoeffInd;
956 for (
int i = order - 2; i > 0; --i) {
957 double cshNext = (2 * xPrime * csh) +
_xCoeffs[i] - cshPrev;
961 return (xPrime * csh) +
_xCoeffs[0] - cshPrev;
964 virtual std::string
toString(std::string
const& prefix)
const {
965 std::ostringstream os;
966 os <<
"Chebyshev1Function2 [";
968 os << Function2<ReturnT>::toString(prefix);
1020 template <
class Archive>
1023 ar & make_nvp(
"minX", this->
_minX);
1024 ar & make_nvp(
"minY", this->
_minY);
1025 ar & make_nvp(
"maxX", this->
_maxX);
1026 ar & make_nvp(
"maxY", this->
_maxY);
1027 ar & make_nvp(
"scaleX", this->
_scaleX);
1028 ar & make_nvp(
"scaleY", this->
_scaleY);
1029 ar & make_nvp(
"offsetX", this->
_offsetX);
1030 ar & make_nvp(
"offsetY", this->
_offsetY);
1031 ar & make_nvp(
"xCheby", this->
_yCheby);
1048 template<
typename ReturnT>
1058 double xOffset = 0.0)
1061 _invN(1.0/static_cast<double>(n))
1074 double xArg2 = xArg1 *
_invN;
1075 if (std::fabs(xArg1) > 1.0e-5) {
1076 return static_cast<ReturnT
>(std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2));
1078 return static_cast<ReturnT
>(1);
1086 return static_cast<unsigned int>(0.5 + (1.0 /
_invN));
1089 virtual std::string
toString(std::string
const& prefix)
const {
1090 std::ostringstream os;
1091 os <<
"LanczosFunction1 [" << this->
getOrder() <<
"]: ";;
1092 os << Function1<ReturnT>::toString(prefix);
1105 template <
class Archive>
1108 ar & make_nvp(
"invN", this->
_invN);
1124 template<
typename ReturnT>
1134 double xOffset = 0.0,
1135 double yOffset = 0.0)
1138 _invN(1.0 / static_cast<double>(n))
1152 double xArg2 = xArg1 *
_invN;
1154 if (std::fabs(xArg1) > 1.0e-5) {
1155 xFunc = std::sin(xArg1) * std::sin(xArg2) / (xArg1 * xArg2);
1158 double yArg2 = yArg1 *
_invN;
1160 if (std::fabs(yArg1) > 1.0e-5) {
1161 yFunc = std::sin(yArg1) * std::sin(yArg2) / (yArg1 * yArg2);
1163 return static_cast<ReturnT
>(xFunc * yFunc);
1170 return static_cast<unsigned int>(0.5 + (1.0 /
_invN));
1173 virtual std::string
toString(std::string
const& prefix)
const {
1174 std::ostringstream os;
1175 os <<
"LanczosFunction2 [" << this->
getOrder() <<
"]: ";;
1176 os << Function2<ReturnT>::toString(prefix);
1189 template <
class Archive>
1192 ar & make_nvp(
"invN", this->
_invN);
1198 #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.
void serialize(Archive &ar, unsigned int const version)
friend class boost::serialization::access
virtual ~PolynomialFunction1()
double _scaleY
y' = (y + _offsetY) * _scaleY
An include file to include the header files for lsst::afw::geom.
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
std::vector< double > _yCheby
working vector: value of Tn(y')
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.
Function2< ReturnT >::Ptr Function2Ptr
1-dimensional Lanczos function
double Guassian (sum of two Gaussians)
virtual ReturnT operator()(double x) const
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.
double _scaleX
x' = (x + _offsetX) * _scaleX
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.
Function2< ReturnT >::Ptr Function2Ptr
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
const double _multFac
precomputed scale factor
virtual Function1Ptr 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.
2-dimensional polynomial function with cross terms
double _oldY
value of y for which _xCoeffs is valid
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 Function2Ptr 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::vector< double > getDFuncDParameters(double x, double y) const
std::vector< double > _xCoeffs
working vector: transformed coeffs of x polynomial
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.
Function1< ReturnT >::Ptr Function1Ptr
void serialize(Archive &ar, unsigned int const version)
LanczosFunction2(unsigned int n, double xOffset=0.0, double yOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.
Function2< ReturnT >::Ptr Function2Ptr
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
unsigned int _order
polynomial order
virtual ReturnT operator()(double x, double y) const
const double _multFac
precomputed scale factor
Base class for 2-dimensional polynomials of the form:
afw::table::Key< double > sigma
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
void _initialize(double minX, double maxX)
initialize private constants
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.
Function1< ReturnT >::Ptr Function1Ptr
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.
void serialize(Archive &ar, unsigned int const version)
virtual ~LanczosFunction1()
std::vector< double > _xCoeffs
working vector
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.
void serialize(Archive &ar, unsigned int const version)
double _minY
minimum allowed y
double const PI
The ratio of a circle's circumference to diameter.
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
void serialize(Archive &ar, unsigned int const version)
friend class boost::serialization::access
virtual ~LanczosFunction2()
void serialize(Archive &ar, unsigned int const version)
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()
boost::shared_ptr< Function2< ReturnT > > Ptr
lsst::afw::geom::Box2D getXYRange() const
Get x,y range.
Function2< ReturnT >::Ptr Function2Ptr
double _minX
minimum allowed x
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
DoubleGaussianFunction2()
A Function taking one argument.
double _maxX
maximum allowed x
void _initialize(lsst::afw::geom::Box2D const &xyRange)
initialize private constants
double _maxX
maximum allowed x
double _scale
x' = (x + _offset) * _scale
void serialize(Archive &ar, unsigned int const version)
void _updateCache() const
Update cached values.
double _angle
cached angle
PolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
const double _multFac
precomputed scale factor
double _minX
minimum allowed x
double _offsetX
x' = (x + _offsetX) * _scaleX
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.
double _sinAngle
cached sin(angle)
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.
#define LSST_EXCEPT(type,...)
friend class boost::serialization::access
unsigned int getNParameters() const
Return the number of function parameters.
Function1< ReturnT >::Ptr Function1Ptr
DoubleGaussianFunction2(double sigma1, double sigma2=0, double ampl2=0)
Construct a Gaussian function with specified x and y sigma.
Function1< ReturnT >::Ptr Function1Ptr
virtual ReturnT operator()(double x) const
void serialize(Archive &ar, unsigned int const version)
Function2< ReturnT >::Ptr Function2Ptr
unsigned int getOrder() const
Get the order of Lanczos function.
afw::table::Key< double > sigma1
unsigned int getOrder() const
Get the order of the Lanczos function.
double _offsetY
y' = (y + _offsetY) * _scaleY
friend class boost::serialization::access
virtual ~GaussianFunction1()
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
unsigned int getOrder() const
Get the polynomial order.
double _cosAngle
cached cos(angle)
virtual ~Chebyshev1Function1()
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
boost::shared_ptr< Function1< ReturnT > > Ptr
Define the basic Function classes.
afw::table::Key< double > b
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 Function2Ptr clone() const
Return a pointer to a deep copy of this function.
void serialize(Archive &ar, unsigned int const version)
virtual ~IntegerDeltaFunction2()
double _maxY
maximum allowed y
A floating-point coordinate rectangle geometry.
double _offset
x' = (x + _offset) * _scale
void serialize(Archive &ar, unsigned int const version)
virtual Function1Ptr 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 Function1Ptr clone() const
Return a pointer to a deep copy of this function.
afw::table::Key< double > sigma2
Function1< ReturnT >::Ptr Function1Ptr
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
Function2< ReturnT >::Ptr Function2Ptr
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
virtual ~GaussianFunction2()
Function2< ReturnT >::Ptr Function2Ptr
virtual ReturnT operator()(double x, double y) const
void serialize(Archive &ar, unsigned int const)
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 Function2Ptr 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.
1-dimensional polynomial function.
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.