Index: trunk/lucene-search-2/test/org/wikimedia/lsearch/util/MathFuncTest.java |
— | — | @@ -4,15 +4,45 @@ |
5 | 5 | import java.io.File; |
6 | 6 | import java.io.FileNotFoundException; |
7 | 7 | import java.io.FileReader; |
| 8 | +import java.io.IOException; |
8 | 9 | import java.util.ArrayList; |
9 | | -import java.util.Arrays; |
10 | | - |
11 | 10 | import org.wikimedia.lsearch.util.MathFunc; |
12 | 11 | |
13 | | -public class MathFuncTest { |
| 12 | + |
| 13 | +/** |
| 14 | + * This class is test MathFunc which is not used in the project |
| 15 | + * The test is visual i.e. putput not a success failure |
| 16 | + * also neither MathFunc nor MathFuncTest are referenced in the project |
| 17 | + * |
| 18 | + * @deprecated |
| 19 | + * |
| 20 | + * if the class isn't deprecated then the following should be addressed |
| 21 | + * configure test file location from command line |
| 22 | + * convert to unit test |
| 23 | + * convert to output to log4j for non console execution |
| 24 | + |
| 25 | + * |
| 26 | + */ |
| 27 | +public class MathFuncTest |
| 28 | +{ |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * prints the entries of an integer and a double arrays side by side |
| 33 | + * |
| 34 | + * |
| 35 | + * @param p the integers |
| 36 | + * @param val the doubles |
| 37 | + */ |
14 | 38 | public static void print(int[] p, double[] val){ |
| 39 | + |
| 40 | + //for each integer in p |
15 | 41 | for(int i=0;i<p.length-1;i++){ |
| 42 | + |
| 43 | + //print the value of val[i] |
16 | 44 | System.out.print(MathFunc.avg(val,p[i],p[i+1])+" -> "); |
| 45 | + |
| 46 | + //prints all a sequence of values from val |
17 | 47 | for(int j=p[i];j<p[i+1];j++){ |
18 | 48 | System.out.print(val[j]+" "); |
19 | 49 | } |
— | — | @@ -21,19 +51,38 @@ |
22 | 52 | System.out.println(); |
23 | 53 | } |
24 | 54 | |
25 | | - public static void main(String[] args) throws Exception{ |
| 55 | + /** |
| 56 | + * 1. loads data from "./test-data/mathfunc.test" |
| 57 | + * 2. |
| 58 | + * |
| 59 | + * @param args |
| 60 | + * @throws FileNotFoundException |
| 61 | + */ |
| 62 | + public static void main(String[] args) throws FileNotFoundException { |
26 | 63 | double[] val = {39.2,13.45,12.67,10.25,8.84,8.66,8.31,8.19,8.06,7.99,6.39,6.19,6,5.92,5.85}; |
27 | 64 | String testfile = "./test-data/mathfunc.test"; |
28 | 65 | int[] p = MathFunc.partitionList(val,3); |
29 | 66 | print(p,val); |
30 | 67 | |
| 68 | + |
| 69 | + //load data from file |
31 | 70 | System.out.println("From "+testfile); |
32 | 71 | BufferedReader r = new BufferedReader(new FileReader(new File(testfile))); |
33 | 72 | String line; |
34 | 73 | ArrayList<Double> val2a = new ArrayList<Double>(); |
35 | | - while((line = r.readLine()) != null){ |
36 | | - val2a.add(new Double(line)); |
| 74 | + try { |
| 75 | + while((line = r.readLine()) != null){ |
| 76 | + val2a.add(new Double(line)); |
| 77 | + } |
| 78 | + } catch (NumberFormatException e) { |
| 79 | + // TODO badly formated file |
| 80 | + e.printStackTrace(); |
| 81 | + } catch (IOException e) { |
| 82 | + // TODO could not read file |
| 83 | + e.printStackTrace(); |
| 84 | + |
37 | 85 | } |
| 86 | + |
38 | 87 | double[] val2 = new double[val2a.size()]; |
39 | 88 | for(int i=0;i<val2.length;i++) |
40 | 89 | val2[i] = val2a.get(i); |
— | — | @@ -42,6 +91,5 @@ |
43 | 92 | double[] val3 = {1.5192982456140351, 1.222988282514404, 1.053690036900369, 1.053690036900369, 1.003690036900369, 0.5229882825144041}; |
44 | 93 | print(MathFunc.partitionList(val3,5),val3); |
45 | 94 | |
46 | | - |
47 | 95 | } |
48 | 96 | } |
Index: trunk/lucene-search-2/src/org/wikimedia/lsearch/util/MathFunc.java |
— | — | @@ -1,19 +1,48 @@ |
2 | 2 | package org.wikimedia.lsearch.util; |
3 | 3 | |
4 | | -public class MathFunc { |
5 | | - |
6 | | - /** Calculate average value starting from start to end (end excluded) */ |
| 4 | +/** |
| 5 | + * this class is used only in its own tests. |
| 6 | + * |
| 7 | + * @deprecated |
| 8 | + * |
| 9 | + * |
| 10 | + */ |
| 11 | +public class MathFunc |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * Calculate arithmetic mean value of an array of doubles. |
| 16 | + * |
| 17 | + * note: only called from MathFuncTest. |
| 18 | + * |
| 19 | + * @param val - array of values to average. |
| 20 | + * @param start - array index to start. |
| 21 | + * @param end - array index to end with (excluded). |
| 22 | + * @return the average. |
| 23 | + * |
| 24 | + */ |
7 | 25 | public static double avg(double[] val, int start, int end){ |
| 26 | + |
8 | 27 | double s = 0; |
9 | 28 | for(int i=start;i<end;i++) |
10 | 29 | s+=val[i]; |
11 | 30 | return s/(end-start); |
12 | 31 | } |
13 | | - |
14 | 32 | /** |
15 | 33 | * Approximate the graph of function with num horizontal lines |
16 | 34 | * (const functions), so to minimize the maximal deviation |
17 | | - * @return list of discontinuities (begin points of horizontal lines) |
| 35 | + * |
| 36 | + * input output for n =4 |
| 37 | + * * ** |
| 38 | + * * ** *** |
| 39 | + * * * * *** ** |
| 40 | + * ** * |
| 41 | + * |
| 42 | + * |
| 43 | + * |
| 44 | + * @param val the heights of the function being approximated |
| 45 | + * @param num number of discrete lines |
| 46 | + * @return list of discontinuities (begin points of horizontal lines) |
18 | 47 | */ |
19 | 48 | public static int[] partitionList(double[] val, int num){ |
20 | 49 | //System.out.println("Doing: "+Arrays.toString(val)); |
— | — | @@ -74,6 +103,7 @@ |
75 | 104 | return part; |
76 | 105 | } |
77 | 106 | |
| 107 | + |
78 | 108 | private static void extend(int[] part, int[] newpart, double[] val, int num) { |
79 | 109 | for(int j=0;j<num;j++) |
80 | 110 | newpart[j] = part[j]; |