1 package org.catacomb.util; 2 3 4 public class ArrayUtil { 5 6 7 public static int getIndexInArray(String s, String[] sa) { 8 int iret = -1; 9 10 if (s != null) { 11 for (int i = 0; i < sa.length; i++) { 12 if (sa[i].equals(s)) { 13 iret = i; 14 break; 15 } 16 } 17 } 18 return iret; 19 } 20 21 public static double[] copyDArray(double[] xp) { 22 int np = xp.length; 23 double[] ret = new double[np]; 24 for (int i = 0; i < np; i++) { 25 ret[i] = xp[i]; 26 } 27 return ret; 28 } 29 30 31 public static String[] reverseStringArray(String[] sa) { 32 int n = sa.length; 33 String[] ret = new String[n]; 34 for (int i = 0; i < n; i++) { 35 ret[i] = sa[n - 1- i]; 36 } 37 return ret; 38 } 39 40 public static double minD(double[] d) { 41 double ret = 0.; 42 if (d != null && d.length > 0) { 43 ret = d[0]; 44 for (int i = 1; i < d.length; i++) { 45 if (d[i] < ret) { 46 ret = d[i]; 47 } 48 } 49 } 50 return ret; 51 } 52 53 54 public static double maxD(double[] d) { 55 double ret = 0.; 56 if (d != null && d.length > 0) { 57 ret = d[0]; 58 for (int i = 1; i < d.length; i++) { 59 if (d[i] > ret) { 60 ret = d[i]; 61 } 62 } 63 } 64 return ret; 65 } 66 67 68 69 public static double min(double[][] dd) { 70 double ret = Double.NaN; 71 if (dd != null) { 72 for (int i = 0; i < dd.length; i++) { 73 double[] a = dd[i]; 74 if (a != null) { 75 76 77 if (Double.isNaN(ret) && a.length > 0) { 78 ret = a[0]; 79 } 80 81 for (int j = 0; j < a.length; j++) { 82 if (a[j] < ret) { 83 ret = a[j]; 84 } 85 } 86 } 87 } 88 } 89 return ret; 90 } 91 92 public static double max(double[][] dd) { 93 double ret = Double.NaN; 94 if (dd != null) { 95 for (int i = 0; i < dd.length; i++) { 96 double[] a = dd[i]; 97 if (a != null) { 98 99 100 if (Double.isNaN(ret) && a.length > 0) { 101 ret = a[0]; 102 } 103 104 for (int j = 0; j < a.length; j++) { 105 if (a[j] > ret) { 106 ret = a[j]; 107 } 108 } 109 } 110 } 111 } 112 return ret; 113 } 114 115 116 public static double avg(double[] d) { 117 double sum = 0.; 118 int n = 0; 119 if (d != null) { 120 for (int i = 0; i < d.length; i++) { 121 n += 1; 122 sum += d[i]; 123 } 124 } 125 double ret = sum / n; 126 return ret; 127 } 128 129 130 public static double sd(double[] d) { 131 double sumsq = 0.; 132 double sum = 0; 133 int n = 0; 134 if (d != null) { 135 for (int i = 0; i < d.length; i++) { 136 n += 1; 137 sum += d[i]; 138 sumsq += d[i] * d[i]; 139 } 140 } 141 double avg = sum / n; 142 double ret = Math.sqrt(sumsq / n - avg * avg); 143 return ret; 144 145 } 146 147 148 149 150 public static double avg(double[][] dd) { 151 double ret = 0.; 152 int n = 0; 153 if (dd != null) { 154 for (int i = 0; i < dd.length; i++) { 155 double[] a = dd[i]; 156 if (a != null) { 157 for (int j = 0; j < a.length; i++) { 158 n += 1; 159 ret += a[j]; 160 } 161 } 162 } 163 } 164 ret /= n; 165 return ret; 166 } 167 168 169 public static double sd(double[][] dd) { 170 double sumsq = 0.; 171 double sum = 0; 172 int n = 0; 173 if (dd != null) { 174 for (int i = 0; i < dd.length; i++) { 175 double[] a = dd[i]; 176 if (a != null) { 177 for (int j = 0; j < a.length; i++) { 178 n += 1; 179 sum += a[j]; 180 sumsq += a[j] * a[j]; 181 } 182 } 183 } 184 } 185 186 double avg = sum / n; 187 double ret = Math.sqrt(sumsq - n * avg); 188 return ret; 189 190 } 191 192 193 } 194