/Users/lyon/j4p/src/math/Mat2.java

1    package math; 
2     
3    import collections.sortable.Cshort; 
4    import collections.sortable.QuickSort; 
5     
6    import java.util.Vector; 
7     
8     
9    public abstract class Mat2 { 
10    
11       public static void main(String args[]) { 
12           float f[][] = new float[3][3]; 
13           for (int i = 0; i < f.length; i++) 
14               for (int j = 0; j < f[0].length; j++) 
15                   f[i][j] = 1; 
16           Mat2.normalize(f); 
17           //Mat.printKernel(f,"mean3"); 
18       } 
19    
20       public static void print(double a[][]) { 
21           for (int i = 0; i < a.length; i++) { 
22               for (int j = 0; j < a[0].length; j++) 
23                   System.out.print(a[i][j] + " "); 
24               System.out.println(); 
25           } 
26       } 
27    
28       public static void printStats(String title, 
29                                     float a[][]) { 
30           System.out.println(title); 
31           printStats(a); 
32       } 
33    
34       public static void printStats(float a[][]) { 
35           float min = Float.MAX_VALUE; 
36           float max = Float.MIN_VALUE; 
37           float aBar = 0; 
38    
39    
40           double N = a.length * a[0].length; 
41           for (int x = 0; x < a.length; x++) 
42               for (int y = 0; y < a[0].length; y++) { 
43                   aBar += a[x][y]; 
44    
45                   min = Math.min(a[x][y], min); 
46                   max = Math.max(a[x][y], max); 
47               } 
48    
49    
50           aBar /= N; 
51    
52           System.out.println(" aBar=" + aBar + 
53                              " a min=" + min + 
54                              " a max=" + max + 
55                              " a.length=" + a.length + 
56                              " a[0].length=" + a[0].length); 
57    
58       } 
59    
60       public static void print(float a[][]) { 
61           for (int i = 0; i < a.length; i++) { 
62               for (int j = 0; j < a[0].length; j++) 
63                   System.out.print(a[i][j] + " "); 
64               System.out.println(); 
65           } 
66       } 
67    
68       public static double sum(double a[][]) { 
69           double s = 0.0; 
70           for (int i = 0; i < a.length; i++) 
71               for (int j = 0; j < a[i].length; j++) 
72                   s += a[i][j]; 
73           return s; 
74       } 
75    
76       public static double sum(float a[][]) { 
77           double s = 0.0; 
78           for (int i = 0; i < a.length; i++) 
79               for (int j = 0; j < a[0].length; j++) 
80                   s += a[i][j]; 
81           return s; 
82       } 
83    
84       public static double sum(short a[][]) { 
85           double s = 0.0; 
86           if (a == null) return 0; 
87           for (int i = 0; i < a.length; i++) 
88               for (int j = 0; j < a[0].length; j++) 
89                   s += a[i][j]; 
90           return s; 
91       } 
92    
93       public static short clip(short i) { 
94           if (i < 0) 
95               return 0; 
96    
97           if (i > 255) 
98               return 255; 
99    
100          return i; 
101      } 
102   
103      public static short[][] clip(short i[][]) { 
104          short i2[][] = new short[i.length][i[0].length]; 
105          for (int x = 0; x < i2.length; x++) 
106              for (int y = 0; y < i2[0].length; y++) 
107                  i2[x][y] = clip(i[x][y]); 
108          return i2; 
109      } 
110   
111      public static void normalize(double a[][]) { 
112          scale(a, 1.0 / sum(a)); 
113      } 
114   
115      public static float[][] normalize( 
116              short a[][]) { 
117          return scale(a, (float) (1.0f / sum(a))); 
118      } 
119   
120      public static void normalize(float a[][]) { 
121          scale(a, 1.0 / sum(a)); 
122      } 
123   
124      public static double average(double a[][]) { 
125          int n = a.length * a[0].length; 
126          return sum(a) / n; 
127      } 
128   
129      public static double average(float a[][]) { 
130          int n = a.length * a[0].length; 
131          return sum(a) / n; 
132      } 
133   
134      public static double average(short a[][]) { 
135          int n = a.length * a[0].length; 
136          return sum(a) / n; 
137      } 
138   
139      public static void threshold(short a[][], 
140                                   short thresh) { 
141          for (int i = 0; i < a.length; i++) 
142              for (int j = 0; j < a[0].length; j++) 
143                  if (a[i][j] < thresh) 
144                      a[i][j] = 0; 
145                  else 
146                      a[i][j] = 255; 
147      } 
148   
149      public static void threshold(short a[][]) { 
150          threshold(a, (short) average(a)); 
151      } 
152   
153      public static void printKernel(float k[][], 
154                                     String name) { 
155          float g; 
156          System.out.println("\npublic void " + 
157                             name + 
158                             "(){\n" 
159                             + "\tfloat k[][] = {"); 
160          int w = k.length; 
161          int h = k[0].length; 
162   
163          for (int y = 0; y < h; y++) { 
164              System.out.print("\t{"); 
165              for (int x = 0; x < w - 1; x++) { 
166                  g = k[x][y]; 
167                  if (g < 10) 
168                      System.out.print("  "); 
169                  else if (g < 100) System.out.print( 
170                          " "); 
171                  System.out.print(g + "f, "); 
172              } 
173              String s = k[w - 1][y] + "f}"; 
174              if (y < h - 1) 
175                  s = s + ","; 
176              else 
177                  s = s + "};"; 
178              System.out.println(s); 
179          } 
180   
181          //String s="\n\tconvolve(k);\n}"; 
182          //s = "//sum="+sum(k)+s; 
183   
184          //System.out.println(s); 
185   
186      } 
187   
188      public static void printArray(double k[][], 
189                                    String name) { 
190          double g; 
191          int w = k.length; 
192          int h = k[0].length; 
193          System.out.println("w=" + w + " h=" + h); 
194          System.out.println(name + "(){\n" 
195                             + "\tfloat k[][] = {"); 
196   
197          for (int x = 0; x < w; x++) { 
198              System.out.print("{"); 
199              for (int y = 0; y < h; y++) { 
200                  g = k[x][y]; 
201                  if (g < 10) 
202                      System.out.print("  "); 
203                  else if (g < 100) System.out.print( 
204                          " "); 
205                  System.out.print(g); 
206              } 
207              System.out.println("}"); 
208          } 
209   
210      } 
211   
212      public static void printKernel(double k[][], 
213                                     String name) { 
214          double g; 
215          int w = k.length; 
216          int h = k[0].length; 
217          System.out.println("w=" + w + " h=" + h); 
218          System.out.println("\npublic void " + 
219                             name + 
220                             "(){\n" 
221                             + "\tfloat k[][] = {"); 
222   
223          for (int y = 0; y < h; y++) { 
224              System.out.print("\t{"); 
225              for (int x = 0; x < w - 1; x++) { 
226                  g = k[x][y]; 
227                  if (g < 10) 
228                      System.out.print("  "); 
229                  else if (g < 100) System.out.print( 
230                          " "); 
231                  System.out.print(g + "f, "); 
232              } 
233              String s = " " + k[w - 1][y] + "f }"; 
234              if (y < h - 1) 
235                  s = s + ","; 
236              else 
237                  s = s + "};"; 
238              System.out.println(s); 
239          } 
240   
241          String s = "\n\tconvolve(k);\n}"; 
242          s = "//sum=" + sum(k) + s; 
243   
244          System.out.println(s); 
245   
246      } 
247   
248      public static void printKernel(short k[][], 
249                                     String name) { 
250          int g; 
251          System.out.println("\npublic void " + 
252                             name + 
253                             "(){\n" 
254                             + 
255                             "\tfloat s =(float)" + 
256                             Mat2.sum(k) + 
257                             ";\n" 
258                             + "\tfloat k[][] = {"); 
259          int w = k.length; 
260          int h = k[0].length; 
261   
262          for (int y = 0; y < h; y++) { 
263              System.out.print("\t{"); 
264              for (int x = 0; x < w - 1; x++) { 
265                  g = k[x][y]; 
266                  System.out.print(g + "/s,"); 
267              } 
268              String s = k[w - 1][y] + "/s}"; 
269              if (y < h - 1) 
270                  s = s + ","; 
271              else 
272                  s = s + "};"; 
273              System.out.println(s); 
274          } 
275   
276          String s = "\n\tconvolve(k);\n}"; 
277          System.out.println(s); 
278   
279      } 
280   
281      public static float[][] scale(short a[][], 
282                                    float k) { 
283          if (a == null) return null; 
284          float f[][] = new float[a.length][a[0].length]; 
285          for (int i = 0; i < a.length; i++) { 
286              for (int j = 0; j < a[0].length; j++) 
287                  f[i][j] = k * a[i][j]; 
288          } 
289          return f; 
290   
291      } 
292   
293      public static void scale(double a[][], 
294                               double k) { 
295          System.out.println( 
296                  "scale(double a[][], double k)"); 
297          for (int i = 0; i < a.length; i++) { 
298              for (int j = 0; j < a[0].length; j++) 
299                  a[i][j] *= k; 
300          } 
301   
302      } 
303   
304      public static void scale(float a[][], 
305                               float k) { 
306          if (a == null) return; 
307          for (int i = 0; i < a.length; i++) { 
308              for (int j = 0; j < a[0].length; j++) 
309                  a[i][j] = a[i][j] * k; 
310          } 
311   
312      } 
313   
314      public static void scale(float a[][], 
315                               double k) { 
316  //System.out.println("scale(float a[][], double k)"); 
317  //  System.out.println("k="+k); 
318          scale(a, (float) k); 
319      } 
320   
321      public static float[][] shortToFloat( 
322              short a[][]) { 
323          int w = a.length; 
324          int h = a[0].length; 
325          float c[][] = new float[w][h]; 
326          for (int i = 0; i < w; i++) 
327              for (int j = 0; j < h; j++) 
328                  c[i][j] = a[i][j]; 
329          return c; 
330      } 
331   
332      public static short[][] copyArray( 
333              short a[][]) { 
334          int w = a.length; 
335          int h = a[0].length; 
336          short c[][] = new short[w][h]; 
337          for (int i = 0; i < w; i++) 
338              for (int j = 0; j < h; j++) 
339                  c[i][j] = a[i][j]; 
340          return c; 
341      } 
342   
343      public static double variance(int a[]) { 
344          double xBar = mean(a); 
345          double sum = 0; 
346          double dx = 0; 
347          for (int i = 0; i < a.length; i++) { 
348              dx = a[i] - xBar; 
349              sum += dx * dx; 
350          } 
351          return sum / a.length; 
352      } 
353   
354      public static double mean(int a[]) { 
355          double sum = 0; 
356          for (int i = 0; i < a.length; i++) 
357              sum += a[i]; 
358          return sum / a.length; 
359      } 
360   
361      public static double coefficientOfVariation( 
362              int a[]) { 
363          double aBar = mean(a); 
364          double aBar2 = aBar * aBar; 
365          return Math.sqrt(variance(a) / aBar2); 
366      } 
367   
368      public static void testVariance() { 
369          int a[] = {1, 2, 3, 5, 4, 3, 2, 5, 6, 7}; 
370          System.out.println( 
371                  "The variance =" + variance(a)); 
372      } 
373   
374      public static void testCoefficientOfVariation() { 
375          int a[] = {0, 85, 87, 90, 100}; 
376          System.out.println("coefficientOfVariation({0,85,87,90,100}) =" 
377                             + 
378                             coefficientOfVariation( 
379                                     a)); 
380          int b[] = {95, 85, 87, 90, 100}; 
381          System.out.println("The coefficientOfVariation({95,85,87,90,100}) =" 
382                             + 
383                             coefficientOfVariation( 
384                                     b)); 
385      } 
386   
387      public static boolean outlierHere(int a[]) { 
388          return (coefficientOfVariation(a) > .1); 
389      } 
390   
391      public static void testOutlier() { 
392          int a[] = {0, 85, 87, 90, 100}; 
393          int b[] = {95, 85, 87, 90, 100}; 
394          System.out.println("dog ate my homework ={0,85,87,90,100}" 
395                             + outlierHere(a)); 
396          System.out.println("dog ate my homework ={95,85,87,90,100}" 
397                             + outlierHere(b)); 
398      } 
399   
400      public static void intQuickSort(int a[], 
401                                      int lo0, 
402                                      int hi0) { 
403          // Based on the QuickSort method by 
404          // James Gosling from Sun's SortDemo applet 
405   
406          int lo = lo0; 
407          int hi = hi0; 
408          int mid, t; 
409   
410          if (hi0 > lo0) { 
411              mid = a[(lo0 + hi0) / 2]; 
412              while (lo <= hi) { 
413                  while ((lo < hi0) && 
414                         (a[lo] < mid)) 
415                      ++lo; 
416                  while ((hi > lo0) && 
417                         (a[hi] > mid)) 
418                      --hi; 
419                  if (lo <= hi) { 
420                      t = a[lo]; 
421                      a[lo] = a[hi]; 
422                      a[hi] = t; 
423                      ++lo; 
424                      --hi; 
425                  } 
426              } 
427              if (lo0 < hi) 
428                  intQuickSort(a, lo0, hi); 
429              if (lo < hi0) 
430                  intQuickSort(a, lo, hi0); 
431   
432          } 
433      } 
434   
435   
436      public static void intQuickSort(int a[]) { 
437          intQuickSort(a, 0, a.length - 1); 
438      } 
439   
440      public static double mean(short a[][]) { 
441          double sum = 0; 
442          for (int x = 0; x < a.length; x++) 
443              for (int y = 0; y < a[0].length; y++) { 
444                  sum += a[x][y]; 
445              } 
446          return sum / (a.length * a[0].length); 
447      } 
448   
449      public static double variance(short a[][]) { 
450          double xBar = mean(a); 
451          double sum = 0; 
452          double dx = 0; 
453          for (int x = 0; x < a.length; x++) 
454              for (int y = 0; y < a[0].length; y++) { 
455                  dx = a[x][y] - xBar; 
456                  sum += dx * dx; 
457              } 
458          return sum / (a.length * a[0].length); 
459      } 
460   
461      public static double[] getAverage(double a[], 
462                                        double b[], 
463                                        double c[]) { 
464          double avg[] = new double[a.length]; 
465   
466          for (int i = 0; i < a.length; i++) { 
467              avg[i] = (a[i] + b[i] + c[i]) / 3.0; 
468          } 
469          return avg; 
470      } 
471   
472      public static short[][] copy(short r[][]) { 
473          short c[][] = new short[r.length][r[0].length]; 
474          int w = r.length; 
475          int h = r[0].length; 
476          for (int x = 0; x < w; x++) 
477              for (int y = 0; y < h; y++) 
478                  c[x][y] = r[x][y]; 
479          return c; 
480      } 
481   
482      public static short getMin(short a[]) { 
483          short min = 255; 
484          for (int i = 0; i < a.length; i++) 
485              if (a[i] < min) 
486                  min = a[i]; 
487          return min; 
488      } 
489   
490      public static short getMax(short a[]) { 
491          short max = -255; 
492          for (int i = 0; i < a.length; i++) 
493              if (a[i] > max) 
494                  max = a[i]; 
495          return max; 
496      } 
497   
498      public static short median(Vector v) { 
499          QuickSort.sort(v, 
500                         new Vector(), 
501                         0, 
502                         v.size(), 
503                         true); 
504          return 
505                  ((Cshort) v.elementAt( 
506                          v.size() / 2)).getValue(); 
507      } 
508   
509      public static void testQuickSort() { 
510          int a[] = {1, 2, 3, 5, 4, 3, 2, 5, 6, 7}; 
511          intQuickSort(a); 
512          for (int i = 0; i < a.length; i++) 
513              System.out.println(a[i]); 
514      } 
515   
516      public static int numberOfNonZeros( 
517              short k[][]) { 
518          int umax = k.length; 
519          int vmax = k[0].length; 
520          int sum = 0; 
521   
522          for (int x = 0; x < umax; x++) 
523              for (int y = 0; y < vmax; y++) 
524                  if (k[x][y] != 0) sum++; 
525          return sum; 
526      } 
527   
528      public static void printMedian(short k[][], 
529                                     String name) { 
530          //printMaple(k); 
531          System.out.println("\npublic void " + 
532                             name + 
533                             "(){\n" 
534                             + "\tfloat k[][] = {"); 
535          int w = k.length; 
536          int h = k[0].length; 
537   
538          for (int y = 0; y < h; y++) { 
539              System.out.print("\t{"); 
540              for (int x = 0; x < w - 1; x++) 
541                  System.out.print(k[x][y] + ", "); 
542              String s = k[w - 1][y] + "}"; 
543              if (y < h - 1) 
544                  s = s + ","; 
545              else 
546                  s = s + "};"; 
547              System.out.println(s); 
548          } 
549   
550          String s = "\n\tmedian(k);\n}"; 
551          System.out.println(s); 
552   
553      } 
554   
555      static void printMaple(short a[][]) { 
556          printMaple(shortToFloat(a)); 
557      } 
558   
559      public static void printMaple(float a[][]) { 
560          //linalg[matrix](3,3,[1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9,1/9]); 
561          int w = a.length; 
562          int h = a[0].length; 
563          System.out.println("evalf(linalg[matrix](" + 
564                             w 
565                             + "," + h + ",["); 
566          for (int i = 0; i < w; i++) 
567              for (int j = 0; j < h; j++) { 
568                  System.out.print(a[i][j]); 
569                  if (i * j == (w - 1) * (h - 1)) break; 
570                  System.out.print(","); 
571              } 
572          System.out.println("]));"); 
573      } 
574   
575      public static short[][] subtract(short a[][], 
576                                       short b[][]) { 
577   
578          int w = a.length; 
579          int h = a[0].length; 
580   
581          short c[][] = new short[w][h]; 
582          for (int x = 0; x < w; x++) 
583              for (int y = 0; y < h; y++) { 
584                  int s = a[x][y] - b[x][y]; 
585                  c[x][y] = (short) Math.abs(s); 
586              } 
587          return c; 
588      } 
589   
590      public static short[][] resample(short a[][], 
591                                       int ratio) { 
592          int w = a.length; 
593          int h = a[0].length; 
594          int nw = w / ratio; 
595          int nh = h / ratio; 
596          short c[][] = new short[nw][nh]; 
597          for (int i = 0; i < w; i++) 
598              for (int j = 0; j < h; j++) 
599                  c[i / ratio][j / ratio] = 
600                  a[i][j]; 
601          return c; 
602      } 
603   
604  }