00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package org.antlr.runtime.misc;
00029
00030 import java.io.*;
00031
00039 public class Stats {
00040 public static final String ANTLRWORKS_DIR = "antlrworks";
00041
00054 public static double stddev(int[] X) {
00055 int m = X.length;
00056 if ( m<=1 ) {
00057 return 0;
00058 }
00059 double xbar = avg(X);
00060 double s2 = 0.0;
00061 for (int i=0; i<m; i++){
00062 s2 += (X[i] - xbar)*(X[i] - xbar);
00063 }
00064 s2 = s2/(m-1);
00065 return Math.sqrt(s2);
00066 }
00067
00069 public static double avg(int[] X) {
00070 double xbar = 0.0;
00071 int m = X.length;
00072 if ( m==0 ) {
00073 return 0;
00074 }
00075 for (int i=0; i<m; i++){
00076 xbar += X[i];
00077 }
00078 if ( xbar>=0.0 ) {
00079 return xbar / m;
00080 }
00081 return 0.0;
00082 }
00083
00084 public static int min(int[] X) {
00085 int min = Integer.MAX_VALUE;
00086 int m = X.length;
00087 if ( m==0 ) {
00088 return 0;
00089 }
00090 for (int i=0; i<m; i++){
00091 if ( X[i] < min ) {
00092 min = X[i];
00093 }
00094 }
00095 return min;
00096 }
00097
00098 public static int max(int[] X) {
00099 int max = Integer.MIN_VALUE;
00100 int m = X.length;
00101 if ( m==0 ) {
00102 return 0;
00103 }
00104 for (int i=0; i<m; i++){
00105 if ( X[i] > max ) {
00106 max = X[i];
00107 }
00108 }
00109 return max;
00110 }
00111
00112 public static int sum(int[] X) {
00113 int s = 0;
00114 int m = X.length;
00115 if ( m==0 ) {
00116 return 0;
00117 }
00118 for (int i=0; i<m; i++){
00119 s += X[i];
00120 }
00121 return s;
00122 }
00123
00124 public static void writeReport(String filename, String data) throws IOException {
00125 String absoluteFilename = getAbsoluteFileName(filename);
00126 File f = new File(absoluteFilename);
00127 File parent = f.getParentFile();
00128 parent.mkdirs();
00129
00130 FileOutputStream fos = new FileOutputStream(f, true);
00131 BufferedOutputStream bos = new BufferedOutputStream(fos);
00132 PrintStream ps = new PrintStream(bos);
00133 ps.println(data);
00134 ps.close();
00135 bos.close();
00136 fos.close();
00137 }
00138
00139 public static String getAbsoluteFileName(String filename) {
00140 return System.getProperty("user.home")+File.separator+
00141 ANTLRWORKS_DIR +File.separator+
00142 filename;
00143 }
00144 }