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
void serialize(Archive &ar, unsigned int const version)
2-dimensional weighted sum of Chebyshev polynomials of the first kind.
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
double _scale
x' = (x + _offset) * _scale
const double _multFac
precomputed scale factor
int _order
order of polynomial
virtual ReturnT operator()(double x, double y) const
Function1< ReturnT >::Ptr Function1Ptr
friend class boost::serialization::access
An include file to include the header files for lsst::afw::geom.
void serialize(Archive &ar, unsigned int const version)
friend class boost::serialization::access
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.
void serialize(Archive &ar, unsigned int const version)
double _minX
minimum allowed x
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
DoubleGaussianFunction2()
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
1-dimensional Lanczos function
virtual ReturnT operator()(double x, double y) const
double Guassian (sum of two Gaussians)
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.
IntegerDeltaFunction1(double xo)
Construct an integer delta function with specified xo, yo.
An object passed to Persistable::write to allow it to persist itself.
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.
virtual ReturnT operator()(double x) const
unsigned int getOrder() const
Get the order of Lanczos function.
void serialize(Archive &ar, unsigned int const version)
friend class boost::serialization::access
virtual ReturnT operator()(double x, double y) const
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
2-dimensional polynomial function with cross terms
virtual ~DoubleGaussianFunction2()
virtual ReturnT operator()(double x) const
lsst::afw::geom::Box2D getXYRange() const
Get x,y range.
1-dimensional weighted sum of Chebyshev polynomials of the first kind.
Function2< ReturnT >::Ptr Function2Ptr
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.
void serialize(Archive &ar, unsigned int const version)
PolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
double _offsetX
x' = (x + _offsetX) * _scaleX
2-dimensional integer delta function.
DoubleGaussianFunction2(double sigma1, double sigma2=0, double ampl2=0)
Construct a Gaussian function with specified x and y sigma.
Function1< ReturnT >::Ptr Function1Ptr
Chebyshev1Function1(std::vector< double > params, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial with specified parameters and range.
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this 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.
unsigned int _order
polynomial order
unsigned int getOrder() const
Get the polynomial order.
virtual ~GaussianFunction2()
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual ~LanczosFunction1()
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
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.
Function2< ReturnT >::Ptr Function2Ptr
Function2< ReturnT >::Ptr Function2Ptr
2-dimensional separable Lanczos function
A Function taking two arguments.
friend class boost::serialization::access
PolynomialFunction1(std::vector< double > params)
Construct a polynomial function with the specified parameters.
GaussianFunction1(double sigma)
Construct a Gaussian function with specified sigma.
friend class boost::serialization::access
double _oldY
value of y for which _xCoeffs is valid
friend class boost::serialization::access
void serialize(Archive &ar, unsigned int const version)
Function2< ReturnT >::Ptr Function2Ptr
virtual ReturnT operator()(double x, double y) const
Base class for 2-dimensional polynomials of the form:
unsigned int getOrder() const
Get the polynomial order.
afw::table::Key< double > sigma
unsigned int getNParameters() const
Return the number of function parameters.
void serialize(Archive &ar, unsigned int const version)
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
std::vector< double > _xCoeffs
working vector: transformed coeffs of x polynomial
const double _multFac
precomputed scale factor
double _scaleY
y' = (y + _offsetY) * _scaleY
void serialize(Archive &ar, unsigned int const version)
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
void serialize(Archive &ar, unsigned int const version)
virtual ~LanczosFunction2()
double _maxY
maximum allowed y
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
IntegerDeltaFunction2(double xo, double yo)
Construct an integer delta function with specified xo, yo.
double _cosAngle
cached cos(angle)
PolynomialFunction1(unsigned int order)
Construct a polynomial function of the specified order.
double _scaleX
x' = (x + _offsetX) * _scaleX
virtual ~IntegerDeltaFunction1()
void serialize(Archive &ar, unsigned int const)
virtual ReturnT operator()(double x, double y) const
1-dimensional integer delta function.
friend class boost::serialization::access
friend class boost::serialization::access
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
virtual ~Chebyshev1Function1()
virtual bool isPersistable() const
Return true if this particular object can be persisted using afw::table::io.
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Function2< ReturnT >::Ptr Function2Ptr
A Function taking one argument.
LanczosFunction1(unsigned int n, double xOffset=0.0)
Construct a Lanczos function of specified order and x,y offset.
std::vector< double > _xCoeffs
working vector
virtual Function1Ptr 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.
virtual ReturnT operator()(double x, double y) const
double _offsetY
y' = (y + _offsetY) * _scaleY
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
double _sinAngle
cached sin(angle)
virtual std::vector< double > getDFuncDParameters(double x, double y) const
virtual ~GaussianFunction1()
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.
virtual ~PolynomialFunction1()
double _maxX
maximum allowed x
double _offset
x' = (x + _offset) * _scale
Function1< ReturnT >::Ptr Function1Ptr
virtual ReturnT operator()(double x) const
Function1< ReturnT >::Ptr Function1Ptr
Function2< ReturnT >::Ptr Function2Ptr
virtual Chebyshev1Function2 truncate(int truncOrder) const
Return a truncated copy of lower (or equal) order.
double _minY
minimum allowed y
virtual ~Chebyshev1Function2()
#define LSST_EXCEPT(type,...)
friend class boost::serialization::access
Function2< ReturnT >::Ptr Function2Ptr
std::vector< double > _yCheby
working vector: value of Tn(y')
virtual ReturnT operator()(double x) const
std::vector< double > _params
void _initialize(double minX, double maxX)
initialize private constants
PolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
double getMaxX() const
Get maximum allowed x.
afw::table::Key< double > sigma1
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
const double _multFac
precomputed scale factor
friend class boost::serialization::access
virtual ~PolynomialFunction2()
Chebyshev1Function1(unsigned int order, double minX=-1, double maxX=1)
Construct a Chebyshev polynomial of specified order and range.
double getMinX() const
Get minimum allowed x.
virtual ~IntegerDeltaFunction2()
Define the basic Function classes.
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.
double _angle
cached angle
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
void _updateCache() const
Update cached values.
afw::table::Key< double > b
double _minX
minimum allowed x
virtual void write(afw::table::io::OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
unsigned int getOrder() const
Get the order of the Lanczos function.
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
double const PI
The ratio of a circle's circumference to diameter.
A floating-point coordinate rectangle geometry.
boost::shared_ptr< Function2< ReturnT > > Ptr
virtual Function1Ptr clone() const
Return a pointer to a deep copy of this function.
double _maxX
maximum allowed x
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
friend class boost::serialization::access
void _initialize(lsst::afw::geom::Box2D const &xyRange)
initialize private constants
virtual ReturnT operator()(double x, double y) const
afw::table::Key< double > sigma2
boost::shared_ptr< Function1< ReturnT > > Ptr
void serialize(Archive &ar, unsigned int const version)
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.
virtual std::string toString(std::string const &prefix) const
Return a string representation of the function.
Function1< ReturnT >::Ptr Function1Ptr
virtual Function2Ptr clone() const
Return a pointer to a deep copy of this function.
1-dimensional polynomial function.
GaussianFunction2(double sigma1, double sigma2, double angle=0.0)
Construct a 2-dimensional Gaussian function.