LSSTApplications  19.0.0-14-gb0260a2+64befe7c1c,20.0.0+0b340dd7c4,20.0.0+1540ce6389,20.0.0+7c6b12c2f9,20.0.0+ae956f52c5,20.0.0+be870186d9,20.0.0+e2e26847c2,20.0.0-1-g04bee24+7c6b12c2f9,20.0.0-1-g10df615+fbe45b1aff,20.0.0-1-g253301a+7c6b12c2f9,20.0.0-1-g2b7511a+46a6078777,20.0.0-1-g4d801e7+9d50d6c21a,20.0.0-1-g5b95a8c+8ebfc75efb,20.0.0-1-gb058bd0+46a6078777,20.0.0-1-gb88604f+acecce4127,20.0.0-1-gc96f8cb+016e330d09,20.0.0-1-gedffbd8+83814edeba,20.0.0-10-g0891cd99+a5f14e9acb,20.0.0-10-g9a20bd332+576ca7b471,20.0.0-17-gcdbda88+0e26bfb1e6,20.0.0-2-g4dae9ad+016e330d09,20.0.0-2-g61b8584+85c46248f3,20.0.0-2-gb780d76+f45b7d88f4,20.0.0-21-g9bbb7f7+016e330d09,20.0.0-22-gc512666+f30f3883a7,20.0.0-23-g8900aa8+68630f7098,20.0.0-3-g1653f94+85c46248f3,20.0.0-3-g4cc78c6+63636aeed8,20.0.0-3-g750bffe+e05f822de9,20.0.0-3-gbd60e8c+ff10c6d78d,20.0.0-32-g15a0e07c+a4c9d0309b,20.0.0-4-g97dc21a+68630f7098,20.0.0-4-gfea843c+f45b7d88f4,20.0.0-5-g357b56b+f45b7d88f4,20.0.0-6-g9a5b7a1+b0f8c2be93,20.0.0-60-gad89340+e4dd172200,20.0.0-7-gcda7bf1+673816f70c,w.2020.43
LSSTDataManagementBasePackage
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 
37 #include "lsst/pex/exceptions.h"
38 
40 
41 namespace lsst {
42 namespace afw {
43 namespace math {
44 
50 template <typename T>
51 bool constexpr IS_NOTHROW_INIT = noexcept(static_cast<T>(1.0));
52 
82 template <typename ReturnT>
83 class Function : public afw::table::io::PersistableFacade<Function<ReturnT>>,
85 public:
91  explicit Function(unsigned int nParams)
92  : _params(nParams), _isCacheValid(false) {}
93 
97  explicit Function(std::vector<double> const& params)
98  : _params(params), _isCacheValid(false) {}
99 
100  Function(Function const&) = default;
101  Function(Function&&) = default;
102  Function& operator=(Function const&) = default;
103  Function& operator=(Function&&) = default;
104 
105  ~Function() noexcept override = default;
106 
112  unsigned int getNParameters() const noexcept { return _params.size(); }
113 
119  virtual double getParameter(unsigned int ind)
120  const {
121  return _params[ind];
122  }
123 
129  std::vector<double> const& getParameters() const noexcept { return _params; }
130 
138  virtual bool isLinearCombination() const noexcept { return false; }
139 
143  void setParameter(unsigned int ind,
144  double newValue)
145  {
146  _isCacheValid = false;
147  _params[ind] = newValue;
148  }
149 
156  void setParameters(std::vector<double> const& params)
157  {
158  if (_params.size() != params.size()) {
159  throw LSST_EXCEPT(
161  (boost::format("params has %d entries instead of %d") % params.size() % _params.size())
162  .str());
163  }
164  _isCacheValid = false;
165  _params = params;
166  }
167 
173  virtual std::string toString(std::string const&) const {
175  os << "parameters: [ ";
176  for (std::vector<double>::const_iterator i = _params.begin(); i != _params.end(); ++i) {
177  if (i != _params.begin()) os << ", ";
178  os << *i;
179  }
180  os << " ]";
181  return os.str();
182  }
183 
184 protected:
186  mutable bool _isCacheValid;
187 
188  std::string getPythonModule() const override { return "lsst.afw.math"; }
189 
190  /* Default constructor: intended only for serialization */
191  explicit Function() : _params(0), _isCacheValid(false) {}
192 };
193 
201 template <typename ReturnT>
202 class Function1 : public afw::table::io::PersistableFacade<Function1<ReturnT>>, public Function<ReturnT> {
203 public:
209  explicit Function1(unsigned int nParams)
210  : Function<ReturnT>(nParams) {}
211 
215  explicit Function1(std::vector<double> const& params)
216  : Function<ReturnT>(params) {}
217 
218  Function1(Function1 const&) = default;
219  Function1(Function1&&) = default;
220  Function1& operator=(Function1 const&) = default;
222 
223  ~Function1() noexcept override = default;
224 
236  virtual std::shared_ptr<Function1<ReturnT>> clone() const = 0;
237 
238  virtual ReturnT operator()(double x) const = 0;
239 
240  std::string toString(std::string const& prefix = "") const override {
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 
258 template <typename ReturnT>
259 class Function2 : public afw::table::io::PersistableFacade<Function2<ReturnT>>, public Function<ReturnT> {
260 public:
266  explicit Function2(unsigned int nParams)
267  : Function<ReturnT>(nParams) {}
268 
274  explicit Function2(std::vector<double> const& params)
275  : Function<ReturnT>(params) {}
276 
277  Function2(Function2 const&) = default;
278  Function2(Function2&&) = default;
279  Function2& operator=(Function2 const&) = default;
281 
282  ~Function2() noexcept override = default;
283 
295  virtual std::shared_ptr<Function2<ReturnT>> clone() const = 0;
296 
297  virtual ReturnT operator()(double x, double y) const = 0;
298 
299  std::string toString(std::string const& prefix = "") const override {
300  return std::string("Function2: ") + Function<ReturnT>::toString(prefix);
301  }
305  virtual std::vector<double> getDFuncDParameters(double, double) const {
307  "getDFuncDParameters is not implemented for this class");
308  }
309 
310 protected:
311  /* Default constructor: intended only for serialization */
312  explicit Function2() : Function<ReturnT>() {}
313 };
314 
325 template <typename ReturnT>
326 class BasePolynomialFunction2 : public Function2<ReturnT> {
327 public:
335  explicit BasePolynomialFunction2(unsigned int order)
336  : Function2<ReturnT>(BasePolynomialFunction2::nParametersFromOrder(order)), _order(order) {}
337 
347  : Function2<ReturnT>(params),
348  _order(BasePolynomialFunction2::orderFromNParameters(static_cast<int>(params.size()))) {}
349 
354 
355  ~BasePolynomialFunction2() noexcept override = default;
356 
360  int getOrder() const noexcept { return _order; }
361 
362  bool isLinearCombination() const noexcept override { return true; }
363 
369  static int nParametersFromOrder(int order) {
370  if (order < 0) {
372  os << "order=" << order << " invalid: must be >= 0";
374  }
375  return (order + 1) * (order + 2) / 2;
376  }
377 
392  static int orderFromNParameters(int nParameters) {
393  int order = static_cast<int>(
394  0.5 + ((-3.0 + (std::sqrt(1.0 + (8.0 * static_cast<double>(nParameters))))) / 2.0));
395  if (nParameters != BasePolynomialFunction2::nParametersFromOrder(order)) {
397  os << "nParameters=" << nParameters << " invalid: order is not an integer";
399  }
400  return order;
401  }
402 
411  std::vector<double> getDFuncDParameters(double x, double y) const override {
412  unsigned int const numParams = this->getNParameters(); // Number of parameters
413  std::vector<double> deriv(numParams); // Derivatives, to return
414 
416  this->clone(); // Dummy function to evaluate for derivatives
417  for (unsigned int i = 0; i < numParams; ++i) {
418  dummy->setParameter(i, 0.0);
419  }
420 
421  for (unsigned int i = 0; i < numParams; ++i) {
422  dummy->setParameter(i, 1.0);
423  deriv[i] = (*dummy)(x, y);
424  dummy->setParameter(i, 0.0);
425  }
426 
427  return deriv;
428  }
429 
430 protected:
431  int _order;
432 
433  /* Default constructor: intended only for serialization */
434  explicit BasePolynomialFunction2() : Function2<ReturnT>(1), _order(0) {}
435 };
436 
440 template <typename ReturnT>
441 class NullFunction1 : public Function1<ReturnT> {
442 public:
443  explicit NullFunction1() : Function1<ReturnT>(0) {}
446  }
447 
448 private:
449  ReturnT operator()(double) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
450  return static_cast<ReturnT>(0);
451  }
452 };
453 
457 template <typename ReturnT>
458 class NullFunction2 : public Function2<ReturnT> {
459 public:
460  explicit NullFunction2() : Function2<ReturnT>(0) {}
463  }
464 
465 private:
466  ReturnT operator()(double, double) const noexcept(IS_NOTHROW_INIT<ReturnT>) override {
467  return static_cast<ReturnT>(0);
468  }
469 };
470 } // namespace math
471 } // namespace afw
472 } // namespace lsst
473 
474 #endif // #ifndef LSST_AFW_MATH_FUNCTION_H
y
int y
Definition: SpanSet.cc:49
lsst::afw::math::Function1::Function1
Function1()
Definition: Function.h:248
lsst::afw::math::BasePolynomialFunction2::_order
int _order
order of polynomial
Definition: Function.h:431
Persistable.h
lsst::afw::math::Function::operator=
Function & operator=(Function const &)=default
lsst::afw::math::Function::getPythonModule
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Function.h:188
std::string
STL class.
lsst::afw::math::NullFunction1::clone
std::shared_ptr< Function1< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
Definition: Function.h:444
std::shared_ptr
STL class.
lsst::afw::math::BasePolynomialFunction2::orderFromNParameters
static int orderFromNParameters(int nParameters)
Compute polynomial order from the number of parameters.
Definition: Function.h:392
lsst::afw::math::Function2::operator=
Function2 & operator=(Function2 const &)=default
lsst::afw::math::Function1::Function1
Function1(std::vector< double > const &params)
Construct a Function1 given the function parameters.
Definition: Function.h:215
lsst::afw::math::BasePolynomialFunction2::~BasePolynomialFunction2
~BasePolynomialFunction2() noexcept override=default
lsst::afw::math::Function2::clone
virtual std::shared_ptr< Function2< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
std::vector< double >
std::vector::size
T size(T... args)
lsst::afw::math::BasePolynomialFunction2::getDFuncDParameters
std::vector< double > getDFuncDParameters(double x, double y) const override
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:411
lsst::afw::math::Function2::operator=
Function2 & operator=(Function2 &&)=default
lsst::afw::math::Function2::Function2
Function2()
Definition: Function.h:312
lsst.pex::exceptions::NotFoundError
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
lsst::afw::math::Function1::clone
virtual std::shared_ptr< Function1< ReturnT > > clone() const =0
Return a pointer to a deep copy of this function.
lsst::afw::math::Function::getParameters
std::vector< double > const & getParameters() const noexcept
Return all function parameters.
Definition: Function.h:129
lsst::afw::math::Function2::getDFuncDParameters
virtual std::vector< double > getDFuncDParameters(double, double) const
Return the derivative of the Function with respect to its parameters.
Definition: Function.h:305
lsst::afw::math::BasePolynomialFunction2::nParametersFromOrder
static int nParametersFromOrder(int order)
Compute number of parameters from polynomial order.
Definition: Function.h:369
lsst::afw::math::NullFunction1::NullFunction1
NullFunction1()
Definition: Function.h:443
lsst::afw
Definition: imageAlgorithm.dox:1
std::stringstream
STL class.
lsst::afw::math::BasePolynomialFunction2
Base class for 2-dimensional polynomials of the form:
Definition: Function.h:326
lsst::afw::math::BasePolynomialFunction2::operator=
BasePolynomialFunction2 & operator=(BasePolynomialFunction2 const &)=default
lsst::afw::math::IS_NOTHROW_INIT
constexpr bool IS_NOTHROW_INIT
Test that a Function's return value is nothrow-castable to T.
Definition: Function.h:51
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::math::NullFunction2::clone
std::shared_ptr< Function2< ReturnT > > clone() const override
Return a pointer to a deep copy of this function.
Definition: Function.h:461
lsst::afw::math::BasePolynomialFunction2::BasePolynomialFunction2
BasePolynomialFunction2(unsigned int order)
Construct a polynomial function of specified order.
Definition: Function.h:335
lsst::afw::math::BasePolynomialFunction2::BasePolynomialFunction2
BasePolynomialFunction2()
Definition: Function.h:434
lsst::afw::math::BasePolynomialFunction2::BasePolynomialFunction2
BasePolynomialFunction2(BasePolynomialFunction2 &&)=default
std::sqrt
T sqrt(T... args)
lsst::afw::math::Function1::Function1
Function1(Function1 const &)=default
lsst::afw::math::Function1::computeCache
virtual void computeCache(int const n)
Definition: Function.h:244
lsst::afw::math::Function::isLinearCombination
virtual bool isLinearCombination() const noexcept
Is the function a linear combination of its parameters?
Definition: Function.h:138
lsst::afw::math::Function1::toString
std::string toString(std::string const &prefix="") const override
Return a string representation of the function.
Definition: Function.h:240
lsst::afw::math::Function2::~Function2
~Function2() noexcept override=default
lsst::afw::math::Function::Function
Function(Function const &)=default
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst::afw::math::NullFunction2
a class used in function calls to indicate that no Function2 is being provided
Definition: Function.h:458
lsst::afw::math::BasePolynomialFunction2::operator=
BasePolynomialFunction2 & operator=(BasePolynomialFunction2 &&)=default
lsst::afw::math::Function1::~Function1
~Function1() noexcept override=default
lsst::afw::math::Function2::Function2
Function2(unsigned int nParams)
Construct a Function2 given the number of function parameters.
Definition: Function.h:266
lsst::afw::math::Function::setParameters
void setParameters(std::vector< double > const &params)
Set all function parameters.
Definition: Function.h:156
lsst::afw::math::Function::Function
Function(std::vector< double > const &params)
Construct a Function given the function parameters.
Definition: Function.h:97
lsst::afw::math::Function::operator=
Function & operator=(Function &&)=default
lsst::afw::math::Function::Function
Function(unsigned int nParams)
Construct a Function given the number of function parameters.
Definition: Function.h:91
lsst::afw::math::BasePolynomialFunction2::BasePolynomialFunction2
BasePolynomialFunction2(BasePolynomialFunction2 const &)=default
lsst::afw::math::Function::Function
Function()
Definition: Function.h:191
lsst::afw::math::Function2::Function2
Function2(Function2 &&)=default
lsst::afw::math::Function::getParameter
virtual double getParameter(unsigned int ind) const
Get one function parameter without range checking.
Definition: Function.h:119
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::math::BasePolynomialFunction2::isLinearCombination
bool isLinearCombination() const noexcept override
Is the function a linear combination of its parameters?
Definition: Function.h:362
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::ostringstream
STL class.
lsst::afw::math::Function::~Function
~Function() noexcept override=default
lsst::afw::math::Function::_params
std::vector< double > _params
Definition: Function.h:185
lsst::afw::math::BasePolynomialFunction2::BasePolynomialFunction2
BasePolynomialFunction2(std::vector< double > params)
Construct a polynomial function with specified parameters.
Definition: Function.h:346
lsst::afw::math::Function2
A Function taking two arguments.
Definition: Function.h:259
lsst::afw::table::io::Persistable
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
os
std::ostream * os
Definition: Schema.cc:746
exceptions.h
lsst::afw::math::NullFunction1
a class used in function calls to indicate that no Function1 is being provided
Definition: Function.h:441
lsst::afw::math::Function2::Function2
Function2(std::vector< double > const &params)
Construct a Function2 given the function parameters.
Definition: Function.h:274
lsst::afw::math::Function::Function
Function(Function &&)=default
lsst.pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
lsst::afw::math::Function::setParameter
void setParameter(unsigned int ind, double newValue)
Set one function parameter without range checking.
Definition: Function.h:143
std::vector::begin
T begin(T... args)
lsst::afw::math::Function::getNParameters
unsigned int getNParameters() const noexcept
Return the number of function parameters.
Definition: Function.h:112
lsst::afw::math::Function1::Function1
Function1(unsigned int nParams)
Construct a Function1 given the number of function parameters.
Definition: Function.h:209
std
STL namespace.
lsst::afw::table::io::PersistableFacade
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
lsst::afw::math::Function::toString
virtual std::string toString(std::string const &) const
Return a string representation of the function.
Definition: Function.h:173
lsst::afw::math::BasePolynomialFunction2::getOrder
int getOrder() const noexcept
Get the polynomial order.
Definition: Function.h:360
std::vector::end
T end(T... args)
lsst::afw::math::Function::_isCacheValid
bool _isCacheValid
Definition: Function.h:186
lsst::afw::math::Function1::operator=
Function1 & operator=(Function1 const &)=default
lsst::afw::math::Function2::toString
std::string toString(std::string const &prefix="") const override
Return a string representation of the function.
Definition: Function.h:299
lsst::afw::math::Function
Basic Function class.
Definition: Function.h:84
lsst::afw::math::Function1
A Function taking one argument.
Definition: Function.h:202
lsst::afw::math::Function1::operator=
Function1 & operator=(Function1 &&)=default
lsst::afw::math::Function2::Function2
Function2(Function2 const &)=default
lsst::afw::math::Function1::Function1
Function1(Function1 &&)=default
prefix
std::string prefix
Definition: SchemaMapper.cc:79
lsst::afw::math::NullFunction2::NullFunction2
NullFunction2()
Definition: Function.h:460