• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 namespace Antlr.Runtime.Misc {
34     using System.Collections.Generic;
35     using Antlr.Runtime.JavaExtensions;
36 
37     using Math = System.Math;
38 
39     /** <summary>Stats routines needed by profiler etc...</summary>
40      *
41      *  <remarks>
42      *  note that these routines return 0.0 if no values exist in the X[]
43      *  which is not "correct", but it is useful so I don't generate NaN
44      *  in my output
45      *  </remarks>
46      */
47     public class Stats {
48         public const string ANTLRWORKS_DIR = "antlrworks";
49 
50         /** <summary>Compute the sample (unbiased estimator) standard deviation following:</summary>
51          *
52          *  <remarks>
53          *  Computing Deviations: Standard Accuracy
54          *  Tony F. Chan and John Gregg Lewis
55          *  Stanford University
56          *  Communications of ACM September 1979 of Volume 22 the ACM Number 9
57          *
58          *  The "two-pass" method from the paper; supposed to have better
59          *  numerical properties than the textbook summation/sqrt.  To me
60          *  this looks like the textbook method, but I ain't no numerical
61          *  methods guy.
62          *  </remarks>
63          */
Stddev(int[] X)64         public static double Stddev(int[] X) {
65             int m = X.Length;
66             if (m <= 1) {
67                 return 0;
68             }
69             double xbar = EnumerableExtensions.Average(X);
70             double s2 = 0.0;
71             for (int i = 0; i < m; i++) {
72                 s2 += (X[i] - xbar) * (X[i] - xbar);
73             }
74             s2 = s2 / (m - 1);
75             return Math.Sqrt(s2);
76         }
Stddev(List<int> X)77         public static double Stddev(List<int> X) {
78             int m = X.Count;
79             if (m <= 1) {
80                 return 0;
81             }
82             double xbar = EnumerableExtensions.Average(X);
83             double s2 = 0.0;
84             for (int i = 0; i < m; i++) {
85                 s2 += (X[i] - xbar) * (X[i] - xbar);
86             }
87             s2 = s2 / (m - 1);
88             return Math.Sqrt(s2);
89         }
90 
91 #if DEBUG
92         /** <summary>Compute the sample mean</summary> */
93         [System.Obsolete]
avg(int[] X)94         public static double avg(int[] X) {
95             return EnumerableExtensions.Average(EnumerableExtensions.DefaultIfEmpty(X, 0));
96         }
97 
98         [System.Obsolete]
min(int[] X)99         public static int min(int[] X) {
100             return EnumerableExtensions.Min(EnumerableExtensions.DefaultIfEmpty(X, int.MaxValue));
101         }
102 
103         [System.Obsolete]
max(int[] X)104         public static int max(int[] X) {
105             return EnumerableExtensions.Max(EnumerableExtensions.DefaultIfEmpty(X, int.MinValue));
106         }
107 
108         [System.Obsolete]
sum(int[] X)109         public static int sum(int[] X) {
110             return EnumerableExtensions.Sum(X);
111         }
112 #endif
113 
WriteReport(string filename, string data)114         public static void WriteReport(string filename, string data) {
115             string absoluteFilename = GetAbsoluteFileName(filename);
116 
117             System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(absoluteFilename));
118             System.IO.File.AppendAllText(absoluteFilename, data);
119         }
120 
GetAbsoluteFileName(string filename)121         public static string GetAbsoluteFileName(string filename) {
122             string personalFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
123             return personalFolder + System.IO.Path.DirectorySeparatorChar +
124                         ANTLRWORKS_DIR + System.IO.Path.DirectorySeparatorChar +
125                         filename;
126         }
127     }
128 }
129