1 #ifndef VTESTBED_CORE_BISECTION_HH 2 #define VTESTBED_CORE_BISECTION_HH 4 #include <vtestbed/base/blitz.hh> 12 template <
class T,
int N,
class F>
13 blitz::TinyVector<T,N>
15 blitz::TinyVector<T,N> a,
16 blitz::TinyVector<T,N> b,
18 const blitz::TinyVector<T,N>& arg_eps,
19 const blitz::TinyVector<T,N>& func_eps,
22 typedef blitz::TinyVector<T,N> vec;
24 if (all(abs(fa) < func_eps)) {
25 #if defined(VTB_DEBUG_BISECTION) 36 if (all(abs(fb) < func_eps)) {
37 #if defined(VTB_DEBUG_BISECTION) 49 for (
int i=1; i<=max_iter; ++i) {
52 for (
int j=0; j<N; ++j) {
53 if (fa(j)*fc(j) < 0) {
56 }
else if (fb(j)*fc(j) < 0) {
60 if (abs(fa(j)) < abs(fb(j))) {
68 #if defined(VTB_DEBUG_BISECTION) 79 if (all(abs(b-a) < arg_eps) && all(abs(fc) < func_eps)) {
95 template <
class T,
class F>
97 bisection(T a, T b, F func, T arg_eps, T func_eps,
int max_iter) {
100 if (abs(fa) < func_eps) {
104 if (abs(fb) < func_eps) {
108 for (
int i=0; i<max_iter; ++i) {
114 }
else if (fb*fc < 0) {
118 if (abs(fa) < abs(fb)) {
125 #if defined(VTB_DEBUG_BISECTON) 131 << std::setw(w) << fa
132 << std::setw(w) << fb
133 << std::setw(w) << fc
136 if (abs(b-a) < arg_eps && abs(fc) < func_eps) {
143 template <
class T,
int N>
147 using vec = blitz::TinyVector<T,N>;
150 vec _argeps{T{1e-3}};
151 vec _funceps{T{1e-3}};
152 int _niterations = 100;
166 _niterations(niterations)
169 template <
class Func>
171 operator()(
const vec& lbound,
const vec& ubound, Func func)
const {
172 return bisection<T,N,Func>(
183 argument_precision(
const vec& eps) {
188 function_precision(
const vec& eps) {
189 this->_funceps = eps;
192 inline int max_iterations()
const noexcept {
return this->_niterations; }
193 inline void max_iterations(
int n) { this->_niterations = n; }
203 int _niterations = 100;
210 Bisection(
const T& argeps,
const T& funceps,
int niterations):
211 _argeps(argeps), _funceps(funceps), _niterations(niterations) {}
213 template <
class Func>
215 operator()(
const T& lbound,
const T& ubound, Func func)
const {
216 return bisection<T,Func>(
227 argument_precision(T eps) {
232 function_precision(T eps) {
233 this->_funceps = eps;
237 max_iterations()
const noexcept {
238 return this->_niterations;
242 max_iterations(
int n) {
243 this->_niterations = n;
252 #endif // vim:filetype=cpp