/**
 * The Cyclic Coordinates method for minimizing a function of one
 * variable
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CyclicCoordinates
{
    
    /**
     * Minimize a function
     *
     * @param f     The objective function
     * @param init  The initial solution
     * @param tol   The convergence tolerance (for the norm of x^k - x^k-1)
     * @param maxit The maximum number of iterations to perform
     * @return      The argmin over [a_0,b_0] of the function theta
     */
    public static double[] argmin(Objective f, double[] init, 
                                  double tol, int maxit)
    {
       double    change, lambda;
       double[]  y;        
       int       i, k, n;
        

       // Initialization
       k = 0;
       n = init.length;
       i = 0;

       change = tol + 1.0;
       y = new double[n];


       for (i=0; i <= n-1; i++)
       {
          f.setdi(i,0.0);
          f.setxi(i,init[i]);
          y[i] = init[i];
       }




       // Iterations
       while ((change >= tol) && (k < maxit))
       {
          k++;
          change = 0.0;
          for (i=0; i <= n-1; i++)
          {
             f.setdi(i,1.0);
             if (i >= 1) f.setdi(i-1,0.0);

             lambda = GoldenSection.argmin(f,-20.0,20.0,0.001);

             f.setxi(i,f.getxi(i) + lambda);                    
             change += lambda*lambda;
          }

          f.setdi(n-1,0.0);

          for (i=0; i <= n-1; i++)
          {
             y[i] = f.getxi(i);
          }
       }

       return y;
    }
}
