Artifact Content
Not logged in

Artifact c1b48b3999a9f65ad3f00bf5275c72bf7eb06f19


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