lsst.afw  14.0-49-g535656444+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  Function(Function const &) = default;
104  Function(Function &&) = default;
105  Function & operator=(Function const &) = default;
106  Function & operator=(Function &&) = default;
107 
108  virtual ~Function() = default;
109 
115  unsigned int getNParameters() const { return _params.size(); }
116 
122  virtual double getParameter(unsigned int ind)
123  const {
124  return _params[ind];
125  }
126 
132  std::vector<double> const& getParameters() const { return _params; }
133 
141  virtual bool isLinearCombination() const { return false; }
142 
146  void setParameter(unsigned int ind,
147  double newValue)
148  {
149  _isCacheValid = false;
150  _params[ind] = newValue;
151  }
152 
159  void setParameters(std::vector<double> const& params)
160  {
161  if (_params.size() != params.size()) {
162  throw LSST_EXCEPT(
164  (boost::format("params has %d entries instead of %d") % params.size() % _params.size())
165  .str());
166  }
167  _isCacheValid = false;
168  _params = params;
169  }
170 
176  virtual std::string toString(std::string const&) const {
178  os << "parameters: [ ";
179  for (std::vector<double>::const_iterator i = _params.begin(); i != _params.end(); ++i) {
180  if (i != _params.begin()) os << ", ";
181  os << *i;
182  }
183  os << " ]";
184  return os.str();
185  }
186 
187 protected:
189  mutable bool _isCacheValid;
190 
191  virtual std::string getPythonModule() const { return "lsst.afw.math"; }
192 
193  /* Default constructor: intended only for serialization */
194  explicit Function() : lsst::daf::base::Citizen(typeid(this)), _params(0), _isCacheValid(false) {}
195 
196 private: // serialization support
197  friend class boost::serialization::access;
198  template <class Archive>
199  void serialize(Archive& ar, unsigned int const version) {
200  ar& make_nvp("params", _params);
201  }
202 };
203 
211 template <typename ReturnT>
212 class Function1 : public afw::table::io::PersistableFacade<Function1<ReturnT>>, public Function<ReturnT> {
213 public:
219  explicit Function1(unsigned int nParams)
220  : Function<ReturnT>(nParams) {}
221 
225  explicit Function1(std::vector<double> const& params)
226  : Function<ReturnT>(params) {}
227 
228  Function1(Function1 const &) = default;
229  Function1(Function1 &&) = default;
230  Function1 & operator=(Function1 const &) = default;
231  Function1 & operator=(Function1 &&) = default;
232 
233  virtual ~Function1() = default;
234 
246  virtual std::shared_ptr<Function1<ReturnT>> clone() const = 0;
247 
248  virtual ReturnT operator()(double x) const = 0;
249 
250  virtual std::string toString(std::string const& prefix = "") const {
251  return std::string("Function1: ") + Function<ReturnT>::toString(prefix);
252  }
253 
254  virtual void computeCache(int const n) {}
255 
256 protected:
257  /* Default constructor: intended only for serialization */
258  explicit Function1() : Function<ReturnT>() {}
259 
260 private: // serialization
261  friend class boost::serialization::access;
262  template <class Archive>
263  void serialize(Archive& ar, unsigned const int version) {
264  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
265  }
266 };
267 
275 template <typename ReturnT>
276 class Function2 : public afw::table::io::PersistableFacade<Function2<ReturnT>>, public Function<ReturnT> {
277 public:
283  explicit Function2(unsigned int nParams)
284  : Function<ReturnT>(nParams) {}
285 
291  explicit Function2(std::vector<double> const& params)
292  : Function<ReturnT>(params) {}
293 
294  Function2(Function2 const &) = default;
295  Function2(Function2 &&) = default;
296  Function2 & operator=(Function2 const &) = default;
297  Function2 & operator=(Function2 &&) = default;
298 
299  virtual ~Function2() = default;
300 
312  virtual std::shared_ptr<Function2<ReturnT>> clone() const = 0;
313 
314  virtual ReturnT operator()(double x, double y) const = 0;
315 
316  virtual std::string toString(std::string const& prefix = "") const {
317  return std::string("Function2: ") + Function<ReturnT>::toString(prefix);
318  }
322  virtual std::vector<double> getDFuncDParameters(double, double) const {
324  "getDFuncDParameters is not implemented for this class");
325  }
326 
327 protected:
328  /* Default constructor: intended only for serialization */
329  explicit Function2() : Function<ReturnT>() {}
330 
331 private:
332  friend class boost::serialization::access;
333  template <class Archive>
334  void serialize(Archive& ar, unsigned const int version) {
335  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
336  }
337 };
338 
349 template <typename ReturnT>
350 class BasePolynomialFunction2 : public Function2<ReturnT> {
351 public:
359  explicit BasePolynomialFunction2(unsigned int order)
360  : Function2<ReturnT>(BasePolynomialFunction2::nParametersFromOrder(order)),
361  _order(order) {}
362 
372  : Function2<ReturnT>(params),
373  _order(BasePolynomialFunction2::orderFromNParameters(static_cast<int>(params.size()))) {}
374 
379 
380  virtual ~BasePolynomialFunction2() = default;
381 
385  int getOrder() const { return _order; }
386 
387  virtual bool isLinearCombination() const { return true; }
388 
394  static int nParametersFromOrder(int order) {
395  if (order < 0) {
397  os << "order=" << order << " invalid: must be >= 0";
399  }
400  return (order + 1) * (order + 2) / 2;
401  }
402 
417  static int orderFromNParameters(int nParameters) {
418  int order = static_cast<int>(
419  0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
420  if (nParameters != BasePolynomialFunction2::nParametersFromOrder(order)) {
422  os << "nParameters=" << nParameters << " invalid: order is not an integer";
424  }
425  return order;
426  }
427 
436  virtual std::vector<double> getDFuncDParameters(double x, double y) const {
437  unsigned int const numParams = this->getNParameters(); // Number of parameters
438  std::vector<double> deriv(numParams); // Derivatives, to return
439 
441  this->clone(); // Dummy function to evaluate for derivatives
442  for (unsigned int i = 0; i < numParams; ++i) {
443  dummy->setParameter(i, 0.0);
444  }
445 
446  for (unsigned int i = 0; i < numParams; ++i) {
447  dummy->setParameter(i, 1.0);
448  deriv[i] = (*dummy)(x, y);
449  dummy->setParameter(i, 0.0);
450  }
451 
452  return deriv;
453  }
454 
455 protected:
456  int _order;
457 
458  /* Default constructor: intended only for serialization */
459  explicit BasePolynomialFunction2() : Function2<ReturnT>(1), _order(0) {}
460 
461 private:
462  friend class boost::serialization::access;
463  template <class Archive>
464  void serialize(Archive& ar, unsigned const int version) {
465  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
466  ar& make_nvp("order", _order);
467  }
468 };
469 
473 template <typename ReturnT>
474 class NullFunction1 : public Function1<ReturnT> {
475 public:
476  explicit NullFunction1() : Function1<ReturnT>(0) {}
479  }
480 
481 private:
482  ReturnT operator()(double) const { return static_cast<ReturnT>(0); }
483 
484 private:
485  friend class boost::serialization::access;
486  template <class Archive>
487  void serialize(Archive& ar, unsigned int const version) {
488  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
489  }
490 };
491 
495 template <typename ReturnT>
496 class NullFunction2 : public Function2<ReturnT> {
497 public:
498  explicit NullFunction2() : Function2<ReturnT>(0) {}
501  }
502 
503 private:
504  ReturnT operator()(double, double) const { return static_cast<ReturnT>(0); }
505 
506 private:
507  friend class boost::serialization::access;
508  template <class Archive>
509  void serialize(Archive& ar, unsigned int const version) {
510  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
511  }
512 };
513 }
514 }
515 } // lsst::afw::math
516 
517 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
int _order
order of polynomial
Definition: Function.h:456
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:141
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:387
std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:499
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
Definition: Function.h:283
std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:477
T end(T... args)
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Definition: Function.h:371
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
Definition: Function.h:219
A Function taking two arguments.
Definition: Function.h:276
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:350
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
BasePolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
Definition: Function.h:359
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
Definition: Function.h:394
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:496
std::vector< double > _params
Definition: Function.h:188
A base class for image defects.
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
Definition: Function.h:122
std::vector< double > const & getParameters() const
Return all function parameters.
Definition: Function.h:132
virtual std::vector< double > getDFuncDParameters(double, double) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:322
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
Definition: Function.h:90
virtual ~Function()=default
A Function taking one argument.
Definition: Function.h:212
T str(T... args)
Function & operator=(Function const &)=default
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:474
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:191
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:291
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:436
void setParameters(std::vector< double > const &params)
Set all function parameters.
Definition: Function.h:159
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:250
int getOrder() const
Get the polynomial order.
Definition: Function.h:385
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Definition: Function.h:146
Function1(std::vector< double > const &params)
Construct a Function1 given the function parameters.
Definition: Function.h:225
virtual void computeCache(int const n)
Definition: Function.h:254
T sqrt(T... args)
virtual std::string toString(std::string const &) const
Return a string representation of the function.
Definition: Function.h:176
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
static int orderFromNParameters(int nParameters)
Compute polynomial order from the number of parameters.
Definition: Function.h:417
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:115
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
Definition: Function.h:316