Hex Artifact Content
Not logged in

Artifact c1b48b3999a9f65ad3f00bf5275c72bf7eb06f19:


0000: 2f 2f 20 74 65 72 6e 65 72 79 20 73 65 61 72 63  // ternery searc
0010: 68 20 28 66 6f 72 20 73 68 69 74 61 2d 6e 69 2d  h (for shita-ni-
0020: 74 6f 74 73 75 29 0a 7b 0a 09 64 6f 75 62 6c 65  totsu).{..double
0030: 20 78 20 3d 20 7e 6d 69 6e 7e 3b 0a 09 64 6f 75   x = ~min~;..dou
0040: 62 6c 65 20 77 20 3d 20 7e 6d 61 78 7e 3b 0a 09  ble w = ~max~;..
0050: 66 6f 72 28 69 6e 74 20 69 3d 30 3b 20 69 3c 7e  for(int i=0; i<~
0060: 69 74 65 72 61 74 69 6f 6e 7e 3b 20 2b 2b 69 29  iteration~; ++i)
0070: 20 2f 2f 20 6f 72 2c 20 77 68 69 6c 65 28 20 77   // or, while( w
0080: 2d 78 20 3e 20 7e 65 70 73 7e 20 29 20 62 65 20  -x > ~eps~ ) be 
0090: 63 61 72 65 66 75 6c 20 66 6f 72 20 70 72 65 63  careful for prec
00a0: 69 73 69 6f 6e 21 0a 09 7b 0a 09 09 64 6f 75 62  ision!..{...doub
00b0: 6c 65 20 79 20 3d 20 32 2a 78 2f 33 20 2b 20 77  le y = 2*x/3 + w
00c0: 2f 33 3b 0a 09 09 64 6f 75 62 6c 65 20 7a 20 3d  /3;...double z =
00d0: 20 78 2f 33 20 2b 20 32 2a 77 2f 33 3b 0a 09 09   x/3 + 2*w/3;...
00e0: 64 6f 75 62 6c 65 20 66 78 20 3d 20 66 28 78 29  double fx = f(x)
00f0: 3b 0a 09 09 64 6f 75 62 6c 65 20 66 79 20 3d 20  ;...double fy = 
0100: 66 28 79 29 3b 0a 09 09 64 6f 75 62 6c 65 20 66  f(y);...double f
0110: 7a 20 3d 20 66 28 7a 29 3b 0a 09 09 64 6f 75 62  z = f(z);...doub
0120: 6c 65 20 66 77 20 3d 20 66 28 77 29 3b 0a 09 09  le fw = f(w);...
0130: 69 66 28 20 66 78 20 3c 20 66 79 20 29 20 20 20  if( fx < fy )   
0140: 20 20 20 77 20 3d 20 79 3b 0a 09 09 65 6c 73 65     w = y;...else
0150: 20 69 66 28 20 66 7a 20 3e 20 66 77 20 29 20 78   if( fz > fw ) x
0160: 20 3d 20 7a 3b 0a 09 09 65 6c 73 65 20 69 66 28   = z;...else if(
0170: 20 66 79 20 3c 20 66 7a 20 29 20 77 20 3d 20 7a   fy < fz ) w = z
0180: 3b 0a 09 09 65 6c 73 65 20 20 20 20 20 20 20 20  ;...else        
0190: 20 20 20 20 20 20 20 78 20 3d 20 79 3b 0a 09 7d         x = y;..}
01a0: 0a 7d 0a                                         .}.