View Javadoc

1   package org.textensor.util;
2   
3   
4   public class ArrayUtil {
5   
6   
7       public static double[] log(double[] d, double dzero) {
8           double[] ret = new double[d.length];
9           for (int i = 0; i < d.length; i++) {
10              if (d[i] <= 0.) {
11                  ret[i] = dzero;
12              } else {
13                  ret[i] = Math.log(d[i]);
14              }
15          }
16          return ret;
17      }
18  
19      public static double[][] log(double[][] d, double dzero) {
20          double[][] ret = new double[d.length][];
21          for (int i = 0; i < d.length; i++) {
22              ret[i] = log(d[i], dzero);
23          }
24          return ret;
25      }
26  
27  
28      public static int findBracket(double[] v, double x) {
29          if (v == null || v.length == 0) {
30              return -1;
31          }
32  
33          int n = v.length;
34          int ret = 0;
35          /*AB: v[0] is cumulative.  Thus, if x < v[0] the spine goes into region 0 (don't exit).
36           * Also, if x > v[n-1] that is bad - exit (don't return n-1) */
37          if (x <= v[0]) {
38              ret = 0;
39          } else if (x > v[n-1]) {
40              ret = -1;
41          } else {
42  
43              /* AB, jan 25, 2012: this part used x<= v[ic] (num methods uses x>=v[ic], and found the wrong index */
44              int ia = 0;
45              int ib = n-1;
46              while (ib - ia > 1) {
47                  int ic = (ia + ib) / 2;
48                  if (x >= v[ic]) {
49                      ia = ic;
50                  } else {
51                      ib = ic;
52                  }
53              }
54              /* AB, jan 25, 2012: ia+1 corrects for ia being the lower limit in a cumulative array */
55              ret = ia+1;
56          }
57          return ret;
58      }
59  
60      public static double[] span(double xa, double xb, int nel) {
61          double[] ret = new double[nel+1];
62          double dx = (xb - xa) / nel;
63          for (int i = 0; i < nel+1; i++) {
64              ret[i] = xa + i * dx;
65          }
66          return ret;
67      }
68  
69      public static double[] interpInAtFor(double[] aw, double[] ax, double[] xbd) {
70          int np = xbd.length;
71          int ns = ax.length;
72  
73          double[] ret = new double[np];
74          for (int i = 0; i < np; i++) {
75              double x = xbd[i];
76  
77              // REFAC - could be smarter if sure xbd monotonic;
78              int ipos = 0;
79              while (ipos < ns-2 && x > ax[ipos+1]) {
80                  ipos += 1;
81              }
82              double f = (x - ax[ipos]) / (ax[ipos+1] - ax[ipos]);
83  
84              ret[i] = f * aw[ipos+1] + (1.-f) * aw[ipos];
85  
86          }
87          return ret;
88  
89      }
90  
91      public static boolean arraysMatch(String[] a, String[] b) {
92          boolean ret = false;
93          if (a.length == b.length) {
94              ret = true;
95              for (int i = 0; i < a.length; i++) {
96                  if (a[i].equals(b[i])) {
97                      // ok
98                  } else {
99                      ret = false;
100                 }
101             }
102 
103         }
104 
105         return ret;
106     }
107 
108 }