/*****************************************************************
** Misc - miscellaneous static methods
**
**
** (c) Scott MacKenzie, 2000                             
******************************************************************/
public class Misc
{
   // find the minimum value in an array
   public static double min(double[] n)
   {
      double min = n[0];
      for (int j = 1; j < n.length; j++)
         if (n[j] < min)
            min = n[j];
      return min;
   }

   // find the minimum value in an array
   public static double min(double[] n, int length)
   {
      double min = n[0];
      for (int j = 1; j < length; j++)
         if (n[j] < min)
            min = n[j];
      return min;
   }

   // find the maximum value in a array
   public static double max(double[] n, int length)
   {
      double max = n[0];
      for (int j = 1; j < length; j++)
         if (n[j] > max)
            max = n[j];
      return max;
   }

   // find the maximum value in a array
   public static double max(double[] n)
   {
      double max = n[0];
      for (int j = 1; j < n.length; j++)
         if (n[j] > max)
            max = n[j];
      return max;
   }

   // calculate the mean of the values in an array
   public static double mean(double n[], int length)
   {
      double mean = 0.0;
      for (int j = 0; j < length; j++)
         mean += n[j];   
      return mean / length;
   }

   // calculate the mean of the values in an array
   public static double mean(double n[])
   {
      double mean = 0.0;
      for (int j = 0; j < n.length; j++)
         mean += n[j];   
      return mean / n.length;
   }

   // calculate the standard deviation of values in an array
   public static double sd(double[] n, int length)
   {
      double m = mean(n, length);
      double t = 0.0;
      for (int j = 0; j < length; j++)
         t += (m - n[j]) * (m - n[j]);
      return Math.sqrt(t / (length - 1.0));
   }

   // calculate the standard deviation of values in an array
   public static double sd(double[] n)
   {
      double m = mean(n);
      double t = 0.0;
      for (int j = 0; j < n.length; j++)
         t += (m - n[j]) * (m - n[j]);
      return Math.sqrt(t / (n.length - 1.0));
   }

   // format integer 'i' as a string of length 'len' (pad left with spaces)
   public static String formatInt(int i, int len)
   {
      String s = "" + i;
      while (s.length() < len)
         s = " " + s;
      return s;
   }

   // format double 'd' as a string of length 'len' & 'dp' decimal places
   public static String formatDouble(double d, int len, int dp)
   {
      String fx = (dp <= 0) ? "" + Math.round(d) : "" + trim(d, dp);
      while (fx.substring(fx.indexOf(".") + 1, fx.length()).length() < dp)
         fx += "0";
      while (fx.length() < len)
         fx = " " + fx;
      return fx;
   }

   // trim double 'd' to have 'dp' decimal places
   public static double trim(double d, int dp)
   {
      double factor = Math.pow(10.0, dp);
      return (int)(d * factor) / factor;
   }

}

