lsst.afw  15.0-17-g0b88e3da8
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 
81 template <typename ReturnT>
83  public afw::table::io::PersistableFacade<Function<ReturnT>>,
85 public:
91  explicit Function(unsigned int nParams)
92  : lsst::daf::base::Citizen(typeid(this)),
93  _params(nParams),
94  _isCacheValid(false) {}
95 
99  explicit Function(std::vector<double> const& params)
100  : lsst::daf::base::Citizen(typeid(this)),
101  _params(params),
102  _isCacheValid(false) {}
103 
104  Function(Function const &) = default;
105  Function(Function &&) = default;
106  Function & operator=(Function const &) = default;
107  Function & operator=(Function &&) = default;
108 
109  virtual ~Function() = default;
110 
116  unsigned int getNParameters() const { return _params.size(); }
117 
123  virtual double getParameter(unsigned int ind)
124  const {
125  return _params[ind];
126  }
127 
133  std::vector<double> const& getParameters() const { return _params; }
134 
142  virtual bool isLinearCombination() const { return false; }
143 
147  void setParameter(unsigned int ind,
148  double newValue)
149  {
150  _isCacheValid = false;
151  _params[ind] = newValue;
152  }
153 
160  void setParameters(std::vector<double> const& params)
161  {
162  if (_params.size() != params.size()) {
163  throw LSST_EXCEPT(
165  (boost::format("params has %d entries instead of %d") % params.size() % _params.size())
166  .str());
167  }
168  _isCacheValid = false;
169  _params = params;
170  }
171 
177  virtual std::string toString(std::string const&) const {
179  os << "parameters: [ ";
180  for (std::vector<double>::const_iterator i = _params.begin(); i != _params.end(); ++i) {
181  if (i != _params.begin()) os << ", ";
182  os << *i;
183  }
184  os << " ]";
185  return os.str();
186  }
187 
188 protected:
190  mutable bool _isCacheValid;
191 
192  virtual std::string getPythonModule() const { return "lsst.afw.math"; }
193 
194  /* Default constructor: intended only for serialization */
195  explicit Function() : lsst::daf::base::Citizen(typeid(this)), _params(0), _isCacheValid(false) {}
196 
197 private: // serialization support
198  friend class boost::serialization::access;
199  template <class Archive>
200  void serialize(Archive& ar, unsigned int const version) {
201  ar& make_nvp("params", _params);
202  }
203 };
204 
212 template <typename ReturnT>
213 class Function1 : public afw::table::io::PersistableFacade<Function1<ReturnT>>, public Function<ReturnT> {
214 public:
220  explicit Function1(unsigned int nParams)
221  : Function<ReturnT>(nParams) {}
222 
226  explicit Function1(std::vector<double> const& params)
227  : Function<ReturnT>(params) {}
228 
229  Function1(Function1 const &) = default;
230  Function1(Function1 &&) = default;
231  Function1 & operator=(Function1 const &) = default;
232  Function1 & operator=(Function1 &&) = default;
233 
234  virtual ~Function1() = default;
235 
247  virtual std::shared_ptr<Function1<ReturnT>> clone() const = 0;
248 
249  virtual ReturnT operator()(double x) const = 0;
250 
251  virtual std::string toString(std::string const& prefix = "") const {
252  return std::string("Function1: ") + Function<ReturnT>::toString(prefix);
253  }
254 
255  virtual void computeCache(int const n) {}
256 
257 protected:
258  /* Default constructor: intended only for serialization */
259  explicit Function1() : Function<ReturnT>() {}
260 
261 private: // serialization
262  friend class boost::serialization::access;
263  template <class Archive>
264  void serialize(Archive& ar, unsigned const int version) {
265  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
266  }
267 };
268 
276 template <typename ReturnT>
277 class Function2 : public afw::table::io::PersistableFacade<Function2<ReturnT>>, public Function<ReturnT> {
278 public:
284  explicit Function2(unsigned int nParams)
285  : Function<ReturnT>(nParams) {}
286 
292  explicit Function2(std::vector<double> const& params)
293  : Function<ReturnT>(params) {}
294 
295  Function2(Function2 const &) = default;
296  Function2(Function2 &&) = default;
297  Function2 & operator=(Function2 const &) = default;
298  Function2 & operator=(Function2 &&) = default;
299 
300  virtual ~Function2() = default;
301 
313  virtual std::shared_ptr<Function2<ReturnT>> clone() const = 0;
314 
315  virtual ReturnT operator()(double x, double y) const = 0;
316 
317  virtual std::string toString(std::string const& prefix = "") const {
318  return std::string("Function2: ") + Function<ReturnT>::toString(prefix);
319  }
323  virtual std::vector<double> getDFuncDParameters(double, double) const {
325  "getDFuncDParameters is not implemented for this class");
326  }
327 
328 protected:
329  /* Default constructor: intended only for serialization */
330  explicit Function2() : Function<ReturnT>() {}
331 
332 private:
333  friend class boost::serialization::access;
334  template <class Archive>
335  void serialize(Archive& ar, unsigned const int version) {
336  ar& make_nvp("fn", boost::serialization::base_object<Function<ReturnT>>(*this));
337  }
338 };
339 
350 template <typename ReturnT>
351 class BasePolynomialFunction2 : public Function2<ReturnT> {
352 public:
360  explicit BasePolynomialFunction2(unsigned int order)
361  : Function2<ReturnT>(BasePolynomialFunction2::nParametersFromOrder(order)),
362  _order(order) {}
363 
373  : Function2<ReturnT>(params),
374  _order(BasePolynomialFunction2::orderFromNParameters(static_cast<int>(params.size()))) {}
375 
380 
381  virtual ~BasePolynomialFunction2() = default;
382 
386  int getOrder() const { return _order; }
387 
388  virtual bool isLinearCombination() const { return true; }
389 
395  static int nParametersFromOrder(int order) {
396  if (order < 0) {
398  os << "order=" << order << " invalid: must be >= 0";
400  }
401  return (order + 1) * (order + 2) / 2;
402  }
403 
418  static int orderFromNParameters(int nParameters) {
419  int order = static_cast<int>(
420  0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
421  if (nParameters != BasePolynomialFunction2::nParametersFromOrder(order)) {
423  os << "nParameters=" << nParameters << " invalid: order is not an integer";
425  }
426  return order;
427  }
428 
437  virtual std::vector<double> getDFuncDParameters(double x, double y) const {
438  unsigned int const numParams = this->getNParameters(); // Number of parameters
439  std::vector<double> deriv(numParams); // Derivatives, to return
440 
442  this->clone(); // Dummy function to evaluate for derivatives
443  for (unsigned int i = 0; i < numParams; ++i) {
444  dummy->setParameter(i, 0.0);
445  }
446 
447  for (unsigned int i = 0; i < numParams; ++i) {
448  dummy->setParameter(i, 1.0);
449  deriv[i] = (*dummy)(x, y);
450  dummy->setParameter(i, 0.0);
451  }
452 
453  return deriv;
454  }
455 
456 protected:
457  int _order;
458 
459  /* Default constructor: intended only for serialization */
460  explicit BasePolynomialFunction2() : Function2<ReturnT>(1), _order(0) {}
461 
462 private:
463  friend class boost::serialization::access;
464  template <class Archive>
465  void serialize(Archive& ar, unsigned const int version) {
466  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
467  ar& make_nvp("order", _order);
468  }
469 };
470 
474 template <typename ReturnT>
475 class NullFunction1 : public Function1<ReturnT> {
476 public:
477  explicit NullFunction1() : Function1<ReturnT>(0) {}
480  }
481 
482 private:
483  ReturnT operator()(double) const { return static_cast<ReturnT>(0); }
484 
485 private:
486  friend class boost::serialization::access;
487  template <class Archive>
488  void serialize(Archive& ar, unsigned int const version) {
489  ar& make_nvp("fn1", boost::serialization::base_object<Function1<ReturnT>>(*this));
490  }
491 };
492 
496 template <typename ReturnT>
497 class NullFunction2 : public Function2<ReturnT> {
498 public:
499  explicit NullFunction2() : Function2<ReturnT>(0) {}
502  }
503 
504 private:
505  ReturnT operator()(double, double) const { return static_cast<ReturnT>(0); }
506 
507 private:
508  friend class boost::serialization::access;
509  template <class Archive>
510  void serialize(Archive& ar, unsigned int const version) {
511  ar& make_nvp("fn2", boost::serialization::base_object<Function2<ReturnT>>(*this));
512  }
513 };
514 }
515 }
516 } // lsst::afw::math
517 
518 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
int _order
order of polynomial
Definition: Function.h:457
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:142
virtual bool isLinearCombination() const
Is the function a linear combination of its parameters?
Definition: Function.h:388
std::shared_ptr< Function2< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:500
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
Definition: Function.h:284
std::shared_ptr< Function1< ReturnT > > clone() const
Return a pointer to a deep copy of this function.
Definition: Function.h:478
T end(T... args)
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Definition: Function.h:372
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
Definition: Function.h:220
A Function taking two arguments.
Definition: Function.h:277
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:351
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:360
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
Definition: Function.h:395
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:497
std::vector< double > _params
Definition: Function.h:189
A base class for image defects.
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
Definition: Function.h:123
std::vector< double > const & getParameters() const
Return all function parameters.
Definition: Function.h:133
virtual std::vector< double > getDFuncDParameters(double, double) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:323
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
Definition: Function.h:91
virtual ~Function()=default
A Function taking one argument.
Definition: Function.h:213
T str(T... args)
Function & operator=(Function const &)=default
double x
Basic Function class.
Definition: Function.h:82
a class used in function calls to indicate that no Function1 is being provided
Definition: Function.h:475
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:192
T size(T... args)
Function(std::vector< double > const &params)
Construct a Function given the function parameters.
Definition: Function.h:99
#define LSST_EXCEPT(type,...)
Function2(std::vector< double > const &params)
Construct a Function2 given the function parameters.
Definition: Function.h:292
virtual std::vector< double > getDFuncDParameters(double x, double y) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:437
void setParameters(std::vector< double > const &params)
Set all function parameters.
Definition: Function.h:160
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:251
int getOrder() const
Get the polynomial order.
Definition: Function.h:386
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Definition: Function.h:147
std::string prefix
Definition: SchemaMapper.cc:79
Function1(std::vector< double > const &params)
Construct a Function1 given the function parameters.
Definition: Function.h:226
virtual void computeCache(int const n)
Definition: Function.h:255
T sqrt(T... args)
virtual std::string toString(std::string const &) const
Return a string representation of the function.
Definition: Function.h:177
std::ostream * os
Definition: Schema.cc:736
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:418
unsigned int getNParameters() const
Return the number of function parameters.
Definition: Function.h:116
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:317