Thursday, October 30, 2014

Java Stdstats Pro



IFS.java

public class IFS 
{
public static void main(String[] args) 
{
                 int T = Integer.parseInt(args[0]);
                          double[] dist = StdArrayIO.readDouble1D();
                  double[][] cx = StdArrayIO.readDouble2D();
   double[][] cy = StdArrayIO.readDouble2D();
                 double x = 0.0, y = 0.0;
                          for (int t = 0; t < T; t++) 
   { 
            int r = StdRandom.discrete(dist); 
 
            double x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2]; 
            double y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2]; 
            x = x0; 
            y = y0; 
                 StdDraw.point(x, y); 
                          if (t % 100 == 0) 
            StdDraw.show(10);
        } 
        StdDraw.show(0);
    } 
} 
 
StdStats.java

public final class StdStats
{
             private StdStats() { }
             public static double max(double[] a)
{
        double max = Double.Negative_infinity;
        for (int i = 0; i < a.length; i++)
         {
            if (Double.isNaN(a[i])) return Double.NaN;
            if (a[i] > max) max = a[i];
        }
        return max;
    }
 public static double max(double[] a, int lo, int hi)
{
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        double max = Double.Negative_infinity;
        for (int i = lo; i <= hi; i++)
         {
            if (Double.isNaN(a[i])) return Double.NaN;
            if (a[i] > max) max = a[i];
        }
        return max;
    }
    public static int max(int[] a)
   {
        int max = Integer.Min_value;
        for (int i = 0; i < a.length; i++)
    {
            if (a[i] > max) max = a[i];
        }
        return max;
    }
    public static double min(double[] a)
   {
        double min = Double.Positive_infinity;
        for (int i = 0; i < a.length; i++)
     {
            if (Double.isNaN(a[i])) return Double.NaN;
            if (a[i] < min) min = a[i];
        }
        return min;
    }
   public static double min(double[] a, int lo, int hi)
   {
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        double min = Double.Positive_infinity;
        for (int i = lo; i <= hi; i++)
        {
            if (Double.isNaN(a[i])) return Double.NaN;
            if (a[i] < min) min = a[i];
        }
        return min;
    }
    public static int min(int[] a)
    {
        int min = Integer.Max_value;
        for (int i = 0; i < a.length; i++)
       {
            if (a[i] < min) min = a[i];
        }
        return min;
    }
    public static double mean(double[] a)
   {
        if (a.length == 0) return Double.NaN;
        double sum = sum(a);
        return sum / a.length;
    }
  public static double mean(double[] a, int lo, int hi)
 {
        int length = hi - lo + 1;
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        if (length == 0) return Double.NaN;
        double sum = sum(a, lo, hi);
        return sum / length;
    }
    public static double mean(int[] a)
   {
        if (a.length == 0) return Double.NaN;
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
      {
            sum = sum + a[i];
        }
        return sum / a.length;
    }
    public static double var(double[] a)
    {
        if (a.length == 0) return Double.NaN;
        double avg = mean(a);
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
        {
            sum += (a[i] - avg) * (a[i] - avg);
        }
        return sum / (a.length - 1);
    }
    public static double var(double[] a, int lo, int hi)
    {
        int length = hi - lo + 1;
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        if (length == 0) return Double.NaN;
        double avg = mean(a, lo, hi);
        double sum = 0.0;
        for (int i = lo; i <= hi; i++)
         {
            sum += (a[i] - avg) * (a[i] - avg);
        }
        return sum / (length - 1);
    }
    public static double var(int[] a)
    {
        if (a.length == 0) return Double.NaN;
        double avg = mean(a);
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
        {
            sum += (a[i] - avg) * (a[i] - avg);
        }
        return sum / (a.length - 1);
    }
                  public static double varp(double[] a)
                    {
        if (a.length == 0) return Double.NaN;
        double avg = mean(a);
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
         {
            sum += (a[i] - avg) * (a[i] - avg);
        }
        return sum / a.length;
    }
    public static double varp(double[] a, int lo, int hi)
    {
        int length = hi - lo + 1;
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        if (length == 0) return Double.NaN;
        double avg = mean(a, lo, hi);
        double sum = 0.0;
        for (int i = lo; i <= hi; i++) {
            sum += (a[i] - avg) * (a[i] - avg);
        }
        return sum / length;
    }
                public static double stddev(double[] a)
    {
        return Math.sqrt(var(a));
    }
    public static double stddev(double[] a, int lo, int hi)
    {
        return Math.sqrt(var(a, lo, hi));
    }
    public static double stddev(int[] a)
   {
        return Math.sqrt(var(a));
    }
    public static double stddevp(double[] a)
    {
        return Math.sqrt(varp(a));
    }
    public static double stddevp(double[] a, int lo, int hi)
   {
        return Math.sqrt(varp(a, lo, hi));
    }
    public static double sum(double[] a)
   {
        double sum = 0.0;
        for (int i = 0; i < a.length; i++)
       {
            sum += a[i];
        }
        return sum;
    }
   public static double sum(double[] a, int lo, int hi)
   {
        if (lo < 0 || hi >= a.length || lo > hi)
            throw new RuntimeException("Subarray indices out of bounds");
        double sum = 0.0;
        for (int i = lo; i <= hi; i++)
       {
            sum += a[i];
        }
        return sum;
    }
    public static int sum(int[] a)
    {
        int sum = 0;
        for (int i = 0; i < a.length; i++)
       {
            sum += a[i];
        }
        return sum;
    }
    public static void plotPoints(double[] a)
    {
        int N = a.length;
        StdDraw.setXscale(0, N-1);
        StdDraw.setPenRadius(1.0 / (3.0 * N));
        for (int i = 0; i < N; i++)
         {
            StdDraw.point(i, a[i]);
        }
    }
          public static void plotLines(double[] a)
                  {
                     int N = a.length;
                     StdDraw.setXscale(0, N-1);
                     StdDraw.setPenRadius();
                     for (int i = 1; i < N; i++)
                    {
                       StdDraw.line(i-1, a[i-1], i, a[i]);
                    }
               }
        public static void plotBars(double[] a)
             {
                   int N = a.length;
                   StdDraw.setXscale(0, N-1);
                   for (int i = 0; i < N; i++)
                  {
                   StdDraw.filledRectangle(i, a[i]/2, .25, a[i]/2);
                   }
                }
          public static void main(String[] args)
    {
        double[] a = StdArrayIO.readDouble1D();
        StdOut.printf("       min %10.3f\n", min(a));
        StdOut.printf("      mean %10.3f\n", mean(a));
        StdOut.printf("       max %10.3f\n", max(a));
        StdOut.printf("       sum %10.3f\n", sum(a));
        StdOut.printf("    stddev %10.3f\n", stddev(a));
        StdOut.printf("       var %10.3f\n", var(a));
        StdOut.printf("   stddevp %10.3f\n", stddevp(a));
        StdOut.printf("      varp %10.3f\n", varp(a));
    }
}

No comments:

Post a Comment