25 #ifndef LSST_AFW_MATH_FUNCTION_H 26 #define LSST_AFW_MATH_FUNCTION_H 34 #include "boost/format.hpp" 35 #include "boost/serialization/nvp.hpp" 36 #include "boost/serialization/vector.hpp" 37 #include "boost/serialization/void_cast.hpp" 38 #include "boost/serialization/export.hpp" 40 #include "lsst/daf/base/Citizen.h" 41 #include "lsst/pex/exceptions.h" 49 using boost::serialization::make_nvp;
80 template <
typename ReturnT>
81 class Function :
public lsst::daf::base::Citizen,
91 :
lsst::daf::base::Citizen(typeid(this)),
98 explicit Function(std::vector<double>
const& params)
99 :
lsst::daf::base::Citizen(typeid(this)),
156 if (
_params.size() != params.size()) {
158 pexExcept::InvalidParameterError,
159 (boost::format(
"params has %d entries instead of %d") % params.size() %
_params.size())
171 virtual std::string
toString(std::string
const&)
const {
172 std::stringstream os;
173 os <<
"parameters: [ ";
174 for (std::vector<double>::const_iterator i =
_params.begin(); i !=
_params.end(); ++i) {
175 if (i !=
_params.begin()) os <<
", ";
189 explicit Function() :
lsst::daf::base::Citizen(typeid(this)), _params(0), _isCacheValid(false) {}
192 friend class boost::serialization::access;
193 template <
class Archive>
194 void serialize(Archive& ar,
unsigned int const version) {
195 ar& make_nvp(
"params", _params);
206 template <
typename ReturnT>
236 virtual std::shared_ptr<Function1<ReturnT>>
clone()
const = 0;
238 virtual ReturnT operator()(
double x)
const = 0;
240 virtual std::string
toString(std::string
const& prefix =
"")
const {
251 friend class boost::serialization::access;
252 template <
class Archive>
253 void serialize(Archive& ar,
unsigned const int version) {
265 template <
typename ReturnT>
297 virtual std::shared_ptr<Function2<ReturnT>>
clone()
const = 0;
299 virtual ReturnT operator()(
double x,
double y)
const = 0;
301 virtual std::string
toString(std::string
const& prefix =
"")
const {
308 throw LSST_EXCEPT(lsst::pex::exceptions::NotFoundError,
309 "getDFuncDParameters is not implemented for this class");
317 friend class boost::serialization::access;
318 template <
class Archive>
319 void serialize(Archive& ar,
unsigned const int version) {
334 template <
typename ReturnT>
376 std::ostringstream os;
377 os <<
"order=" << order <<
" invalid: must be >= 0";
378 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
380 return (order + 1) * (order + 2) / 2;
398 int order =
static_cast<int>(
399 0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
401 std::ostringstream os;
402 os <<
"nParameters=" << nParameters <<
" invalid: order is not an integer";
403 throw LSST_EXCEPT(lsst::pex::exceptions::InvalidParameterError, os.str());
418 std::vector<double> deriv(numParams);
420 std::shared_ptr<Function2<ReturnT>> dummy =
422 for (
unsigned int i = 0; i < numParams; ++i) {
423 dummy->setParameter(i, 0.0);
426 for (
unsigned int i = 0; i < numParams; ++i) {
427 dummy->setParameter(i, 1.0);
428 deriv[i] = (*dummy)(
x, y);
429 dummy->setParameter(i, 0.0);
442 friend class boost::serialization::access;
443 template <
class Archive>
444 void serialize(Archive& ar,
unsigned const int version) {
446 ar& make_nvp(
"order", _order);
453 template <
typename ReturnT>
457 std::shared_ptr<Function1<ReturnT>>
clone()
const {
458 return std::shared_ptr<Function1<ReturnT>>(
new NullFunction1());
462 ReturnT operator()(
double)
const {
return static_cast<ReturnT
>(0); }
465 friend class boost::serialization::access;
466 template <
class Archive>
467 void serialize(Archive& ar,
unsigned int const version) {
475 template <
typename ReturnT>
479 std::shared_ptr<Function2<ReturnT>>
clone()
const {
480 return std::shared_ptr<Function2<ReturnT>>(
new NullFunction2());
484 ReturnT operator()(
double,
double)
const {
return static_cast<ReturnT
>(0); }
487 friend class boost::serialization::access;
488 template <
class Archive>
489 void serialize(Archive& ar,
unsigned int const version) {
497 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
int _order
order of polynomial
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
A Function taking two arguments.
Base class for 2-dimensional polynomials of the form:
A base class for objects that can be persisted via afw::table::io Archive classes.
BasePolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
a class used in function calls to indicate that no Function2 is being provided
std::vector< double > _params
A base class for image defects.
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
std::vector< double > const & getParameters() const
Return all function parameters.
virtual std::vector< double > getDFuncDParameters(double, double) const
Return the derivative of the Function with respect to its parameters.
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
A Function taking one argument.
a class used in function calls to indicate that no Function1 is being provided
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Function(std::vector< double > const ¶ms)
Construct a Function given the function parameters.
Function2(std::vector< double > const ¶ms)
Construct a Function2 given the function parameters.
virtual ~BasePolynomialFunction2()
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the derivative of the Function with respect to its parameters.
void setParameters(std::vector< double > const ¶ms)
Set all function parameters.
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
int getOrder() const
Get the polynomial order.
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Function1(std::vector< double > const ¶ms)
Construct a Function1 given the function parameters.
virtual void computeCache(int const n)
virtual std::string toString(std::string const &) const
Return a string representation of the function.
A CRTP facade class for subclasses of Persistable.
static int orderFromNParameters(int nParameters)
Compute polynomial order from the number of parameters.
unsigned int getNParameters() const
Return the number of function parameters.
BasePolynomialFunction2()
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.