lsst.afw  15.0-16-g953e39cab
Functor.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2015 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 #include <cmath>
26 #include <iostream>
27 
28 #include "lsst/pex/exceptions.h"
29 #include "lsst/afw/geom/Functor.h"
30 
31 namespace lsst {
32 namespace afw {
33 namespace geom {
34 
35 Functor::Functor(std::string const& name) : daf::base::Citizen(typeid(this)), _name(name) {}
36 
37 Functor::Functor(Functor const &) = default;
38 Functor::Functor(Functor &&) = default;
39 Functor &Functor::operator=(Functor const &) = default;
40 Functor &Functor::operator=(Functor &&) = default;
41 
42 double Functor::inverse(double y, double tol, unsigned int maxiter) const {
43  // Sanity checks for tol and maxiter.
44  if (tol > 1 || tol <= 0) {
45  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError, "tol out-of-range, tol <=0 or tol > 1");
46  }
47  if (maxiter < 1) {
48  throw LSST_EXCEPT(lsst::pex::exceptions::OutOfRangeError, "maxiter out-of-range, maxiter < 1");
49  }
50  // Use Newton-Raphson method to find the inverse.
51  double x = y;
52  for (unsigned int iter = 0; iter < maxiter; iter++) {
53  double dx = y - operator()(x);
54  if (std::fabs(dx) < tol) {
55  return x;
56  }
57  x += dx / derivative(x);
58  }
60  "max iteration count exceeded for subclass " + _name);
61  return 0;
62 }
63 
64 } // namespace geom
65 } // namespace afw
66 } // namespace lsst
virtual double operator()(double x) const =0
std::string const & _name
Definition: Mask.cc:576
Functor & operator=(Functor const &)
STL class.
virtual double inverse(double y, double tol=1e-10, unsigned int maxiter=1000) const
Definition: Functor.cc:42
A base class for image defects.
virtual double derivative(double x) const =0
double x
T fabs(T... args)
#define LSST_EXCEPT(type,...)
Abstract base class for function objects.
Definition: Functor.h:50
Functor(std::string const &name)
Definition: Functor.cc:35
int y
Definition: SpanSet.cc:43