lsst.afw  16.0-12-gf08c31002
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 <cmath>
31 #include <stdexcept>
32 #include <sstream>
33 #include <vector>
34 
35 #include "boost/format.hpp"
36 #include "boost/serialization/nvp.hpp"
37 #include "boost/serialization/vector.hpp"
38 #include "boost/serialization/void_cast.hpp"
39 #include "boost/serialization/export.hpp"
40 
41 #include "lsst/daf/base/Citizen.h"
42 #include "lsst/pex/exceptions.h"
43 
45 
46 namespace lsst {
47 namespace afw {
48 namespace math {
49 
50 using boost::serialization::make_nvp;
51 
57 template <typename T>
58 bool constexpr IS_NOTHROW_INIT = noexcept(static_cast<T>(1.0));
59 
89 template <typename ReturnT>
91  public afw::table::io::PersistableFacade<Function<ReturnT>>,
93 public:
99  explicit Function(unsigned int nParams)
100  : lsst::daf::base::Citizen(typeid(this)), _params(nParams), _isCacheValid(false) {}
101 
105  explicit Function(std::vector<double> const& params)
106  : lsst::daf::base::Citizen(typeid(this)), _params(params), _isCacheValid(false) {}
107 
108  Function(Function const&) = default;
109  Function(Function&&) = default;
110  Function& operator=(Function const&) = default;
111  Function& operator=(Function&&) = default;
112 
113  virtual ~Function() noexcept = default;
114 
120  unsigned int getNParameters() const noexcept { return _params.size(); }
121 
127  virtual double getParameter(unsigned int ind)
128  const {
129  return _params[ind];
130  }
131 
137  std::vector<double> const& getParameters() const noexcept { return _params; }
138 
146  virtual bool isLinearCombination() const noexcept { return false; }
147 
151  void setParameter(unsigned int ind,
152  double newValue)
153  {
154  _isCacheValid = false;
155  _params[ind] = newValue;
156  }
157 
164  void setParameters(std::vector<double> const& params)
165  {
166  if (_params.size() != params.size()) {
167  throw LSST_EXCEPT(
169  (boost::format("params has %d entries instead of %d") % params.size() % _params.size())
170  .str());
171  }
172  _isCacheValid = false;
173  _params = params;
174  }
175 
181  virtual std::string toString(std::string const&) const {
183  os << "parameters: [ ";
184  for (std::vector<double>::const_iterator i = _params.begin(); i != _params.end(); ++i) {
185  if (i != _params.begin()) os << ", ";
186  os << *i;
187  }
188  os << " ]";
189  return os.str();
190  }
191 
192 protected:
194  mutable bool _isCacheValid;
195 
196  virtual std::string getPythonModule() const { return "lsst.afw.math"; }
197 
198  /* Default constructor: intended only for serialization */
199  explicit Function() : lsst::daf::base::Citizen(typeid(this)), _params(0), _isCacheValid(false) {}
200 
201 private: // serialization support
202  friend class boost::serialization::access;
203  template <class Archive>
204  void serialize(Archive& ar, unsigned int const version) {
205  ar& make_nvp("params", _params);
206  }
207 };
208 
216 template <typename ReturnT>
217 class Function1 : public afw::table::io::PersistableFacade<Function1<ReturnT>>, public Function<ReturnT> {
218 public:
224  explicit Function1(unsigned int nParams)
225  : Function<ReturnT>(nParams) {}
226 
230  explicit Function1(std::vector<double> const& params)
231  : Function<ReturnT>(params) {}
232 
233  Function1(Function1 const&) = default;
234  Function1(Function1&&) = default;
235  Function1& operator=(Function1 const&) = default;
236  Function1& operator=(Function1&&) = default;
237 
238  virtual ~Function1() noexcept = default;
239 
251  virtual std::shared_ptr<Function1<ReturnT>> clone() const = 0;
252 
253  virtual ReturnT operator()(double x) const = 0;
254 
255  virtual std::string toString(std::string const& prefix = "") const {
256  return std::string("Function1: ") + Function<ReturnT>::toString(prefix);
257  }
258 
259  virtual void computeCache(int const n) {}
260 
261 protected:
262  /* Default constructor: intended only for serialization */
263  explicit Function1() : Function<ReturnT>() {}
264 
265 private: // serialization
266  friend class boost::serialization::access;
267  template <class Archive>
268  void serialize(Archive& ar, unsigned const int version) {
269  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
270  }
271 };
272 
280 template <typename ReturnT>
281 class Function2 : public afw::table::io::PersistableFacade<Function2<ReturnT>>, public Function<ReturnT> {
282 public:
288  explicit Function2(unsigned int nParams)
289  : Function<ReturnT>(nParams) {}
290 
296  explicit Function2(std::vector<double> const& params)
297  : Function<ReturnT>(params) {}
298 
299  Function2(Function2 const&) = default;
300  Function2(Function2&&) = default;
301  Function2& operator=(Function2 const&) = default;
302  Function2& operator=(Function2&&) = default;
303 
304  virtual ~Function2() noexcept = default;
305 
317  virtual std::shared_ptr<Function2<ReturnT>> clone() const = 0;
318 
319  virtual ReturnT operator()(double x, double y) const = 0;
320 
321  virtual std::string toString(std::string const& prefix = "") const {
322  return std::string("Function2: ") + Function<ReturnT>::toString(prefix);
323  }
327  virtual std::vector<double> getDFuncDParameters(double, double) const {
329  "getDFuncDParameters is not implemented for this class");
330  }
331 
332 protected:
333  /* Default constructor: intended only for serialization */
334  explicit Function2() : Function<ReturnT>() {}
335 
336 private:
337  friend class boost::serialization::access;
338  template <class Archive>
339  void serialize(Archive& ar, unsigned const int version) {
340  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
341  }
342 };
343 
354 template <typename ReturnT>
355 class BasePolynomialFunction2 : public Function2<ReturnT> {
356 public:
364  explicit BasePolynomialFunction2(unsigned int order)
365  : Function2<ReturnT>(BasePolynomialFunction2::nParametersFromOrder(order)), _order(order) {}
366 
376  : Function2<ReturnT>(params),
377  _order(BasePolynomialFunction2::orderFromNParameters(static_cast<int>(params.size()))) {}
378 
383 
384  virtual ~BasePolynomialFunction2() noexcept = default;
385 
389  int getOrder() const noexcept { return _order; }
390 
391  virtual bool isLinearCombination() const noexcept { return true; }
392 
398  static int nParametersFromOrder(int order) {
399  if (order < 0) {
401  os << "order=" << order << " invalid: must be >= 0";
403  }
404  return (order + 1) * (order + 2) / 2;
405  }
406 
421  static int orderFromNParameters(int nParameters) {
422  int order = static_cast<int>(
423  0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
424  if (nParameters != BasePolynomialFunction2::nParametersFromOrder(order)) {
426  os << "nParameters=" << nParameters << " invalid: order is not an integer";
428  }
429  return order;
430  }
431 
440  virtual std::vector<double> getDFuncDParameters(double x, double y) const {
441  unsigned int const numParams = this->getNParameters(); // Number of parameters
442  std::vector<double> deriv(numParams); // Derivatives, to return
443 
445  this->clone(); // Dummy function to evaluate for derivatives
446  for (unsigned int i = 0; i < numParams; ++i) {
447  dummy->setParameter(i, 0.0);
448  }
449 
450  for (unsigned int i = 0; i < numParams; ++i) {
451  dummy->setParameter(i, 1.0);
452  deriv[i] = (*dummy)(x, y);
453  dummy->setParameter(i, 0.0);
454  }
455 
456  return deriv;
457  }
458 
459 protected:
460  int _order;
461 
462  /* Default constructor: intended only for serialization */
463  explicit BasePolynomialFunction2() : Function2<ReturnT>(1), _order(0) {}
464 
465 private:
466  friend class boost::serialization::access;
467  template <class Archive>
468  void serialize(Archive& ar, unsigned const int version) {
469  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
470  ar& make_nvp("order", _order);
471  }
472 };
473 
477 template <typename ReturnT>
478 class NullFunction1 : public Function1<ReturnT> {
479 public:
480  explicit NullFunction1() : Function1<ReturnT>(0) {}
483  }
484 
485 private:
486  ReturnT operator()(double) const noexcept(IS_NOTHROW_INIT<ReturnT>) { return static_cast<ReturnT>(0); }
487 
488 private:
489  friend class boost::serialization::access;
490  template <class Archive>
491  void serialize(Archive& ar, unsigned int const version) {
492  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
493  }
494 };
495 
499 template <typename ReturnT>
500 class NullFunction2 : public Function2<ReturnT> {
501 public:
502  explicit NullFunction2() : Function2<ReturnT>(0) {}
505  }
506 
507 private:
508  ReturnT operator()(double, double) const noexcept(IS_NOTHROW_INIT<ReturnT>) {
509  return static_cast<ReturnT>(0);
510  }
511 
512 private:
513  friend class boost::serialization::access;
514  template <class Archive>
515  void serialize(Archive& ar, unsigned int const version) {
516  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
517  }
518 };
519 } // namespace math
520 } // namespace afw
521 } // namespace lsst
522 
523 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
unsigned int getNParameters() const noexcept
Return the number of function parameters.
Definition: Function.h:120
int _order
order of polynomial
Definition: Function.h:460
bool constexpr IS_NOTHROW_INIT
Test that a Function&#39;s return value is nothrow-castable to T.
Definition: Function.h:58
std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:503
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
Definition: Function.h:288
std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:481
T end(T... args)
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Definition: Function.h:375
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
Definition: Function.h:224
A Function taking two arguments.
Definition: Function.h:281
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:355
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:364
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
Definition: Function.h:398
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:500
std::vector< double > _params
Definition: Function.h:193
A base class for image defects.
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
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:327
std::vector< double > const & getParameters() const noexcept
Return all function parameters.
Definition: Function.h:137
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
Definition: Function.h:99
virtual bool isLinearCombination() const noexcept
Is the function a linear combination of its parameters?
Definition: Function.h:146
A Function taking one argument.
Definition: Function.h:217
T str(T... args)
Function & operator=(Function const &)=default
double x
Basic Function class.
Definition: Function.h:90
a class used in function calls to indicate that no Function1 is being provided
Definition: Function.h:478
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:196
virtual bool isLinearCombination() const noexcept
Is the function a linear combination of its parameters?
Definition: Function.h:391
T size(T... args)
Function(std::vector< double > const &params)
Construct a Function given the function parameters.
Definition: Function.h:105
#define LSST_EXCEPT(type,...)
Function2(std::vector< double > const &params)
Construct a Function2 given the function parameters.
Definition: Function.h:296
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:440
void setParameters(std::vector< double > const &params)
Set all function parameters.
Definition: Function.h:164
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:255
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Definition: Function.h:151
std::string prefix
Definition: SchemaMapper.cc:79
Function1(std::vector< double > const &params)
Construct a Function1 given the function parameters.
Definition: Function.h:230
int getOrder() const noexcept
Get the polynomial order.
Definition: Function.h:389
virtual void computeCache(int const n)
Definition: Function.h:259
T sqrt(T... args)
virtual std::string toString(std::string const &) const
Return a string representation of the function.
Definition: Function.h:181
std::ostream * os
Definition: Schema.cc:737
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:421
int y
Definition: SpanSet.cc:44
virtual std::string toString(std::string const &prefix="") const
Return a string representation of the function.
Definition: Function.h:321
virtual ~Function() noexcept=default