28 #define NUMERATOR(dab,dcb,fa,fb,fc) fb*(dab*fc*(fc-fb)-fa*dcb*(fa-fb))
29 #define DENOMINATOR(fa,fb,fc) (fc-fb)*(fa-fb)*(fa-fc)
45 Function1D(
const double &a,
const double &b):p1(a),p2(b) {}
50 class Function1D_7param
53 Function1D_7param(
const double &a,
const double &b,
const double &c,
54 const double &d,
const double &e,
const double &f,
55 const double &g):p1(a),p2(b),p3(c),p4(d),p5(e),p6(f),p7(g) {}
57 const double p1,p2,p3,p4,p5,p6,p7;
67 FZero(
const double &a_,
const double &c_):a(a_),c(c_) {}
69 template <
class Functor>
70 double FindZero(
const Functor &f);
79 template <
class Functor>
80 double FZero::FindZero(
const Functor &f)
82 static const int max_iterations = 500;
83 static const double tolerance = 1e-15;
85 double fa = f(a), b = 0.5 * ( a + c ), fc = f(c), fb = fa * fc;
86 double delta, dab, dcb;
93 if ( fb > 0.0 ) {
return 0.0; }
94 else {
return ( fa == 0.0 ) ? a : c;}
100 delta = a; a = c; c = delta; delta = fa; fa = fc; fc = delta;
111 for ( i = 0; i < max_iterations; i++) {
115 if ( ( c - a ) < tolerance )
return 0.5 * ( a + c);
124 if ( ( c - a ) < tolerance )
return 0.5 * ( a + c);
125 if ( ( b - a ) < tolerance ) {
127 a = b; fa = fb; b = 0.5 * ( a + c );
continue;
138 if ( ( c - b ) < tolerance ) {
140 c = b; fc = fb; b = 0.5 * ( a + c );
continue;
149 if ( ( fa > fb ) && ( fb > fc ) ) {
150 delta = DENOMINATOR(fa,fb,fc);
151 if ( delta != 0.0 ) {
154 delta = NUMERATOR(dab,dcb,fa,fb,fc) / delta;
160 if ( delta > dab && delta < dcb ) {
161 if ( fb > 0.0 ) { a = b; fa = fb; }
162 else if ( fb < 0.0 ) { c = b; fc = fb; }
172 fb > 0 ? ( a = b, fa = fb ) : ( c = b, fc = fb );
184 for ( i = 0; i < max_iterations; i++) {
185 if ( ( c - a ) < tolerance )
return 0.5 * ( a + c);
188 if ( ( b - a ) < tolerance ) {
190 a = b; fa = fb; b = 0.5 * ( a + c );
continue;
196 if ( ( c - b ) < tolerance ) {
198 c = b; fc = fb; b = 0.5 * ( a + c );
continue;
204 if ( ( fa < fb ) && ( fb < fc ) ) {
205 delta = DENOMINATOR(fa,fb,fc);
206 if ( delta != 0.0 ) {
209 delta = NUMERATOR(dab,dcb,fa,fb,fc) / delta;
210 if ( delta > dab && delta < dcb ) {
211 if ( fb < 0.0 ) { a = b; fa = fb; }
212 else if ( fb > 0.0 ) { c = b; fc = fb; }
219 fb < 0 ? ( a = b, fa = fb ) : ( c = b, fc = fb );