/**
 * A partial implementation of a utility class for performing vector arithmetic.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class VectorMath
{
  /**
   * The requirements of a Metric.
   */
  public interface Metric
  {
    /**
     * Calculate the distance between two vectors.
     * 
     * @param v  One vector
     * @param w  The other vector
     * @return   The distance between v and w
     */
    public abstract double distance(double[] v, double[] w);
  }    
  
  
  /**
   * Find the closest vector in a collection of destinations to the given
   * origin vector.
   * 
   * @param d The Metric to use to calculate the distance
   * @param origin The origin vector
   * @param destinations The set of destination vectors
   * @return A reference to the closes vector in the colelction of destinations
   */
  public static double[] closest(Metric d, double[] origin, double[]... destinations)
  {
    double   min = Double.POSITIVE_INFINITY;
    double[] argmin = null;
    
    for (double[] dest: destinations)
    {
      double temp = d.distance(origin, dest);
      if (temp < min)
      {
        min = temp;
        argmin = dest;
      }
    }
    
    return argmin;
  }
  
}
