lsst.afw  14.0-23-g38c323faa+1
Function.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_AFW_MATH_FUNCTION_H
26 #define LSST_AFW_MATH_FUNCTION_H
27 /*
28  * Define the basic Function classes.
29  */
30 #include <stdexcept>
31 #include <sstream>
32 #include <vector>
33 
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"
39 
40 #include "lsst/daf/base/Citizen.h"
41 #include "lsst/pex/exceptions.h"
42 
44 
45 namespace lsst {
46 namespace afw {
47 namespace math {
48 
49 using boost::serialization::make_nvp;
50 
80 template <typename ReturnT>
82  public afw::table::io::PersistableFacade<Function<ReturnT>>,
84 public:
90  explicit Function(unsigned int nParams)
91  : lsst::daf::base::Citizen(typeid(this)),
92  _params(nParams),
93  _isCacheValid(false) {}
94 
98  explicit Function(std::vector<double> const& params)
99  : lsst::daf::base::Citizen(typeid(this)),
100  _params(params),
101  _isCacheValid(false) {}
102 
103  virtual ~Function() {}
104 
110  unsigned int getNParameters() const { return _params.size(); }
111 
117  virtual double getParameter(unsigned int ind)
118  const {
119  return _params[ind];
120  }
121 
127  std::vector<double> const& getParameters() const { return _params; }
128 
136  virtual bool isLinearCombination() const { return false; }
137 
141  void setParameter(unsigned int ind,
142  double newValue)
143  {
144  _isCacheValid = false;
145  _params[ind] = newValue;
146  }
147 
154  void setParameters(std::vector<double> const& params)
155  {
156  if (_params.size() != params.size()) {
157  throw LSST_EXCEPT(
159  (boost::format("params has %d entries instead of %d") % params.size() % _params.size())
160  .str());
161  }
162  _isCacheValid = false;
163  _params = params;
164  }
165 
171  virtual std::string toString(std::string const&) const {
173  os << "parameters: [ ";
174  for (std::vector<double>::const_iterator i = _params.begin(); i != _params.end(); ++i) {
175  if (i != _params.begin()) os << ", ";
176  os << *i;
177  }
178  os << " ]";
179  return os.str();
180  }
181 
182 protected:
184  mutable bool _isCacheValid;
185 
186  virtual std::string getPythonModule() const { return "lsst.afw.math"; }
187 
188  /* Default constructor: intended only for serialization */
189  explicit Function() : lsst::daf::base::Citizen(typeid(this)), _params(0), _isCacheValid(false) {}
190 
191 private: // serialization support
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);
196  }
197 };
198 
206 template <typename ReturnT>
207 class Function1 : public afw::table::io::PersistableFacade<Function1<ReturnT>>, public Function<ReturnT> {
208 public:
214  explicit Function1(unsigned int nParams)
215  : Function<ReturnT>(nParams) {}
216 
220  explicit Function1(std::vector<double> const& params)
221  : Function<ReturnT>(params) {}
222 
223  virtual ~Function1() {}
224 
236  virtual std::shared_ptr<Function1<ReturnT>> clone() const = 0;
237 
238  virtual ReturnT operator()(double x) const = 0;
239 
240  virtual std::string toString(std::string const& prefix = "") const {
241  return std::string("Function1: ") + Function<ReturnT>::toString(prefix);
242  }
243 
244  virtual void computeCache(int const n) {}
245 
246 protected:
247  /* Default constructor: intended only for serialization */
248  explicit Function1() : Function<ReturnT>() {}
249 
250 private: // serialization
251  friend class boost::serialization::access;
252  template <class Archive>
253  void serialize(Archive& ar, unsigned const int version) {
254  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
255  }
256 };
257 
265 template <typename ReturnT>
266 class Function2 : public afw::table::io::PersistableFacade<Function2<ReturnT>>, public Function<ReturnT> {
267 public:
273  explicit Function2(unsigned int nParams)
274  : Function<ReturnT>(nParams) {}
275 
281  explicit Function2(std::vector<double> const& params)
282  : Function<ReturnT>(params) {}
283 
284  virtual ~Function2() {}
285 
297  virtual std::shared_ptr<Function2<ReturnT>> clone() const = 0;
298 
299  virtual ReturnT operator()(double x, double y) const = 0;
300 
301  virtual std::string toString(std::string const& prefix = "") const {
302  return std::string("Function2: ") + Function<ReturnT>::toString(prefix);
303  }
307  virtual std::vector<double> getDFuncDParameters(double, double) const {
309  "getDFuncDParameters is not implemented for this class");
310  }
311 
312 protected:
313  /* Default constructor: intended only for serialization */
314  explicit Function2() : Function<ReturnT>() {}
315 
316 private:
317  friend class boost::serialization::access;
318  template <class Archive>
319  void serialize(Archive& ar, unsigned const int version) {
320  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
321  }
322 };
323 
334 template <typename ReturnT>
335 class BasePolynomialFunction2 : public Function2<ReturnT> {
336 public:
344  explicit BasePolynomialFunction2(unsigned int order)
345  : Function2<ReturnT>(BasePolynomialFunction2::nParametersFromOrder(order)),
346  _order(order) {}
347 
357  : Function2<ReturnT>(params),
358  _order(BasePolynomialFunction2::orderFromNParameters(static_cast<int>(params.size()))) {}
359 
361 
365  int getOrder() const { return _order; }
366 
367  virtual bool isLinearCombination() const { return true; }
368 
374  static int nParametersFromOrder(int order) {
375  if (order < 0) {
377  os << "order=" << order << " invalid: must be >= 0";
379  }
380  return (order + 1) * (order + 2) / 2;
381  }
382 
397  static int orderFromNParameters(int nParameters) {
398  int order = static_cast<int>(
399  0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
400  if (nParameters != BasePolynomialFunction2::nParametersFromOrder(order)) {
402  os << "nParameters=" << nParameters << " invalid: order is not an integer";
404  }
405  return order;
406  }
407 
416  virtual std::vector<double> getDFuncDParameters(double x, double y) const {
417  unsigned int const numParams = this->getNParameters(); // Number of parameters
418  std::vector<double> deriv(numParams); // Derivatives, to return
419 
421  this->clone(); // Dummy function to evaluate for derivatives
422  for (unsigned int i = 0; i < numParams; ++i) {
423  dummy->setParameter(i, 0.0);
424  }
425 
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);
430  }
431 
432  return deriv;
433  }
434 
435 protected:
436  int _order;
437 
438  /* Default constructor: intended only for serialization */
439  explicit BasePolynomialFunction2() : Function2<ReturnT>(1), _order(0) {}
440 
441 private:
442  friend class boost::serialization::access;
443  template <class Archive>
444  void serialize(Archive& ar, unsigned const int version) {
445  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
446  ar& make_nvp("order", _order);
447  }
448 };
449 
453 template <typename ReturnT>
454 class NullFunction1 : public Function1<ReturnT> {
455 public:
456  explicit NullFunction1() : Function1<ReturnT>(0) {}
459  }
460 
461 private:
462  ReturnT operator()(double) const { return static_cast<ReturnT>(0); }
463 
464 private:
465  friend class boost::serialization::access;
466  template <class Archive>
467  void serialize(Archive& ar, unsigned int const version) {
468  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
469  }
470 };
471 
475 template <typename ReturnT>
476 class NullFunction2 : public Function2<ReturnT> {
477 public:
478  explicit NullFunction2() : Function2<ReturnT>(0) {}
481  }
482 
483 private:
484  ReturnT operator()(double, double) const { return static_cast<ReturnT>(0); }
485 
486 private:
487  friend class boost::serialization::access;
488  template <class Archive>
489  void serialize(Archive& ar, unsigned int const version) {
490  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
491  }
492 };
493 }
494 }
495 } // lsst::afw::math
496 
497 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
int _order
order of polynomial
Definition: Function.h:436
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:136
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:367
std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:479
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
Definition: Function.h:273
std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:457
T end(T... args)
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Definition: Function.h:356
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
Definition: Function.h:214
A Function taking two arguments.
Definition: Function.h:266
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:335
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
BasePolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
Definition: Function.h:344
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
Definition: Function.h:374
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:476
std::vector< double > _params
Definition: Function.h:183
A base class for image defects.
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
Definition: Function.h:117
std::vector< double > const & getParameters() const
Return all function parameters.
Definition: Function.h:127
virtual std::vector< double > getDFuncDParameters(double, double) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:307
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
Definition: Function.h:90
A Function taking one argument.
Definition: Function.h:207
T str(T... args)
double x
Basic Function class.
Definition: Function.h:81
a class used in function calls to indicate that no Function1 is being provided
Definition: Function.h:454
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Function.h:186
T size(T... args)
Function(std::vector< double > const &params)
Construct a Function given the function parameters.
Definition: Function.h:98
#define LSST_EXCEPT(type,...)
Function2(std::vector< double > const &params)
Construct a Function2 given the function parameters.
Definition: Function.h:281
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:416
void setParameters(std::vector< double > const &params)
Set all function parameters.
Definition: Function.h:154
Citizen(const std::type_info &)
T begin(T... args)
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
Definition: Function.h:240
int getOrder() const
Get the polynomial order.
Definition: Function.h:365
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Definition: Function.h:141
Function1(std::vector< double > const &params)
Construct a Function1 given the function parameters.
Definition: Function.h:220
virtual void computeCache(int const n)
Definition: Function.h:244
T sqrt(T... args)
virtual std::string toString(std::string const &) const
Return a string representation of the function.
Definition: Function.h:171
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:173
static int orderFromNParameters(int nParameters)
Compute polynomial order from the number of parameters.
Definition: Function.h:397
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:110
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
Definition: Function.h:301