4fd800b3a8 2011-02-23 kinaba: // ternery search (for shita-ni-totsu) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double x = ~min~; 4fd800b3a8 2011-02-23 kinaba: double w = ~max~; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<~iteration~; ++i) // or, while( w-x > ~eps~ ) be careful for precision! 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: double y = 2*x/3 + w/3; 4fd800b3a8 2011-02-23 kinaba: double z = x/3 + 2*w/3; 4fd800b3a8 2011-02-23 kinaba: double fx = f(x); 4fd800b3a8 2011-02-23 kinaba: double fy = f(y); 4fd800b3a8 2011-02-23 kinaba: double fz = f(z); 4fd800b3a8 2011-02-23 kinaba: double fw = f(w); 4fd800b3a8 2011-02-23 kinaba: if( fx < fy ) w = y; 4fd800b3a8 2011-02-23 kinaba: else if( fz > fw ) x = z; 4fd800b3a8 2011-02-23 kinaba: else if( fy < fz ) w = z; 4fd800b3a8 2011-02-23 kinaba: else x = y; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }