Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
brent.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_BRENT_H_)
21 #define __MITSUBA_CORE_BRENT_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <boost/function.hpp>
25 
27 
28 /**
29  * \brief Brent's method nonlinear zero finder
30  *
31  * The implementation is transcribed from the Apache Commons
32  * Java implementation. The supplied function is required to be
33  * continuous, but not necessarily smooth.
34  *
35  * \ingroup libcore
36  */
38 public:
39  /// Return value of \ref BrentSolver::solve()
40  struct Result {
41  bool success;
42  size_t iterations;
45 
46  /// Create a new result instance
47  inline Result(bool success, size_t iterations, Float x, Float y)
48  : success(success), iterations(iterations), x(x), y(y) { }
49 
50  /// Return a string representation of the result
51  inline std::string toString() const {
52  std::ostringstream oss;
53  oss << "BrentSolver::Result["
54  << "success=" << success << ", "
55  << "iterations=" << iterations << ", "
56  << "x=" << x << ", "
57  << "y=" << y << "]";
58  return oss.str();
59  }
60  };
61 
62  /**
63  * \brief Create a new Brent-style solver with the
64  * specified accuracy requirements
65  *
66  * \param maxIterations
67  * Max. number of successive iterations (default: 100)
68  * \param absAccuracy
69  * Absolute accuracy requirement -- the iterations will stop
70  * when |f(x)| < absAccuracy.
71  * \param absAccuracyPos
72  * Absolute accuracy requirement of the position -- the
73  * iterations will stop when |minX-maxX| < absAccuracyPos.
74  * \param absAccuracyPos
75  * Absolute accuracy requirement of the position -- the
76  * iterations will stop when |minX-maxX|/minX < relAccuracyPos.
77  */
78  inline BrentSolver(size_t maxIterations = 100,
79  Float absAccuracy = 1e-6f,
80  Float absAccuracyPos = 1e-6f,
81  Float relAccuracyPos = 1e-6f)
82  : m_maxIterations(maxIterations),
83  m_absAccuracy(absAccuracy),
84  m_absAccuracyPos(absAccuracyPos),
85  m_relAccuracyPos(relAccuracyPos) { }
86 
87  /**
88  * \brief Find a zero in the given interval.
89  *
90  * Requires that the values of the function at the endpoints
91  * have opposite signs.
92  *
93  * \param min the lower bound for the interval.
94  * \param max the upper bound for the interval.
95  * \return the value where the function is zero
96  */
97  Result solve(const boost::function<Float (Float)> &func,
98  Float min, Float max) const;
99 
100  /**
101  * \brief Find a zero in the given interval with an initial guess
102  *
103  * Requires that the values of the function at the endpoints
104  * have opposite signs (note that it is allowed to have endpoints
105  * with the same sign if the initial point has opposite sign
106  * function-wise).
107  *
108  * \param min the lower bound for the interval.
109  * \param max the upper bound for the interval.
110  * \param initial the start value to use (must be set to min
111  * if no initial point is known)
112  * \return the value where the function is zero
113  */
114  Result solve(const boost::function<Float (Float)> &func,
115  Float min, Float max, Float initial) const;
116 
117  /**
118  * Find a zero starting search according to the three provided points.
119  *
120  * \param x0 old approximation for the root
121  * \param y0 function value at the approximation for the root
122  * \param x1 last calculated approximation for the root
123  * \param y1 function value at the last calculated approximation
124  * for the root
125  * \param x2 bracket point (must be set to x0 if no bracket point is
126  * known, this will force starting with linear interpolation)
127  * \param y2 function value at the bracket point.
128  * \return the value where the function is zero
129  */
130  Result solve(const boost::function<Float (Float)> &func,
131  Float x0, Float y0,
132  Float x1, Float y1,
133  Float x2, Float y2) const;
134 protected:
139 };
140 
142 
143 #endif /* __MITSUBA_CORE_BRENT_H_ */
Return value of BrentSolver::solve()
Definition: brent.h:40
size_t iterations
Definition: brent.h:42
Brent&#39;s method nonlinear zero finder.
Definition: brent.h:37
#define MTS_EXPORT_CORE
Definition: getopt.h:29
Float m_absAccuracyPos
Definition: brent.h:137
Result(bool success, size_t iterations, Float x, Float y)
Create a new result instance.
Definition: brent.h:47
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
BrentSolver(size_t maxIterations=100, Float absAccuracy=1e-6f, Float absAccuracyPos=1e-6f, Float relAccuracyPos=1e-6f)
Create a new Brent-style solver with the specified accuracy requirements.
Definition: brent.h:78
Float y
Definition: brent.h:44
Float m_absAccuracy
Definition: brent.h:136
Float x
Definition: brent.h:43
Float m_relAccuracyPos
Definition: brent.h:138
size_t m_maxIterations
Definition: brent.h:135
bool success
Definition: brent.h:41
std::string toString() const
Return a string representation of the result.
Definition: brent.h:51
#define MTS_NAMESPACE_END
Definition: platform.h:138