00001 package org.math.plot.utils;
00002
00003 import java.util.Collections;
00004 import java.util.HashMap;
00005 import java.util.List;
00006
00013 public class Array {
00014
00015 public static HashMap<Double, String> reverseStringMap(HashMap<String, Double> map) {
00016 if (map == null)
00017 return null;
00018 HashMap<Double, String> reverseMap = new HashMap<Double, String>();
00019 for (String key : map.keySet())
00020 reverseMap.put(map.get(key), key);
00021 return reverseMap;
00022 }
00023
00024 private static HashMap<String, Double> mapStringArray(double minvalue, double step, List<String> array) {
00025 if (array == null)
00026 return null;
00027 Collections.sort(array);
00028 HashMap<String, Double> map = new HashMap<String, Double>(array.size());
00029 double v = minvalue;
00030 for (String string : array)
00031 if (!map.containsKey(string)) {
00032 map.put(string, v);
00033 v += step;
00034 }
00035 return map;
00036 }
00037
00038 public static String toString(HashMap hash) {
00039 StringBuffer sb = new StringBuffer();
00040 for (Object key : hash.keySet())
00041 sb.append(key + " > " + hash.get(key) + "\n");
00042 return sb.toString();
00043 }
00044
00045 public static boolean isDouble(String s) {
00046 try {
00047 Double.parseDouble(s);
00048 } catch (NumberFormatException ne) {
00049 return false;
00050 }
00051 return true;
00052 }
00053
00054 public static HashMap<String, Double> mapStringArray(List<String> array) {
00055 return mapStringArray(0, 1, array);
00056 }
00057
00058
00059
00060 public static String cat(Object[] array) {
00061 return cat(" ", array);
00062 }
00063
00064 public static String cat(String separator, Object[] array) {
00065 String o = "";
00066 for (int i = 0; i < array.length - 1; i++)
00067 o += array[i].toString() + separator;
00068 o += array[array.length - 1].toString();
00069 return o;
00070 }
00071
00072 public static String cat(String columnsSeparator, String rowsSeparator, Object[][] array) {
00073 String o = "";
00074 for (int i = 0; i < array.length - 1; i++)
00075 o += cat(columnsSeparator, array[i]) + rowsSeparator;
00076 o += cat(columnsSeparator, array[array.length - 1]);
00077 return o;
00078 }
00079
00080 public static String cat(Object[][] array) {
00081 return cat(" ", "\n", array);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 public static String[] duplicate(int m, String c) {
00097 String[] o = new String[m];
00098 for (int i = 0; i < o.length; i++)
00099 o[i] = c;
00100 return o;
00101 }
00102
00103 public static int[] duplicate(int m, int c) {
00104 int[] o = new int[m];
00105 for (int i = 0; i < o.length; i++)
00106 o[i] = c;
00107 return o;
00108 }
00109
00110 public static double[][] one(int m, int n) {
00111 return one(m, n, 1.0);
00112 }
00113
00114 public static double[][] one(int m, int n, double c) {
00115 double[][] o = new double[m][n];
00116 for (int i = 0; i < o.length; i++)
00117 for (int j = 0; j < o[i].length; j++)
00118 o[i][j] = c;
00119 return o;
00120 }
00121
00122 public static double[] one(int m) {
00123 return one(m, 1.0);
00124 }
00125
00126 public static double[] one(int m, double c) {
00127 double[] o = new double[m];
00128 for (int i = 0; i < o.length; i++)
00129 o[i] = c;
00130 return o;
00131 }
00132
00133 public static double[][] increment(int m, int n, double begin, double pitch) {
00134 double[][] array = new double[m][n];
00135 for (int i = 0; i < m; i++) {
00136 for (int j = 0; j < n; j++) {
00137 array[i][j] = begin + i * pitch;
00138 }
00139 }
00140 return array;
00141 }
00142
00143 public static double[] increment(int m, double begin, double pitch) {
00144 double[] array = new double[m];
00145 for (int i = 0; i < m; i++) {
00146 array[i] = begin + i * pitch;
00147 }
00148 return array;
00149 }
00150
00151
00152
00153 public static double[] copy(double[] M) {
00154 double[] array = new double[M.length];
00155 System.arraycopy(M, 0, array, 0, M.length);
00156 return array;
00157 }
00158
00159 public static double[][] copy(double[][] M) {
00160 double[][] array = new double[M.length][M[0].length];
00161 for (int i = 0; i < array.length; i++)
00162 System.arraycopy(M[i], 0, array[i], 0, M[i].length);
00163 return array;
00164 }
00165
00166 public static double[][] getSubMatrixRangeCopy(double[][] M, int i1, int i2, int j1, int j2) {
00167 double[][] array = new double[i2 - i1 + 1][j2 - j1 + 1];
00168 for (int i = 0; i < i2 - i1 + 1; i++)
00169 System.arraycopy(M[i + i1], j1, array[i], 0, j2 - j1 + 1);
00170 ;
00171 return array;
00172 }
00173
00174 public static double[][] getColumnsRangeCopy(double[][] M, int j1, int j2) {
00175 double[][] array = new double[M.length][j2 - j1 + 1];
00176 for (int i = 0; i < M.length; i++)
00177 System.arraycopy(M[i], j1, array[i], 0, j2 - j1 + 1);
00178 return array;
00179 }
00180
00181 public static double[][] getColumnsCopy(double[][] M, int... J) {
00182 double[][] array = new double[M.length][J.length];
00183 for (int i = 0; i < M.length; i++)
00184 for (int j = 0; j < J.length; j++)
00185 array[i][j] = M[i][J[j]];
00186 return array;
00187 }
00188
00189 public static double[] getColumnCopy(double[][] M, int j) {
00190 double[] array = new double[M.length];
00191 for (int i = 0; i < M.length; i++)
00192 array[i] = M[i][j];
00193 return array;
00194 }
00195
00196 public static double[] getColumnCopy(double[][][] M, int j, int k) {
00197 double[] array = new double[M.length];
00198 for (int i = 0; i < M.length; i++)
00199 array[i] = M[i][j][k];
00200 return array;
00201 }
00202
00203 public static double[][] getRowsCopy(double[][] M, int... I) {
00204 double[][] array = new double[I.length][M[0].length];
00205 for (int i = 0; i < I.length; i++)
00206 System.arraycopy(M[I[i]], 0, array[i], 0, M[I[i]].length);
00207 return array;
00208 }
00209
00210 public static double[] getRowCopy(double[][] M, int i) {
00211 double[] array = new double[M[0].length];
00212 System.arraycopy(M[i], 0, array, 0, M[i].length);
00213 return array;
00214 }
00215
00216 public static double[][] getRowsRangeCopy(double[][] M, int i1, int i2) {
00217 double[][] array = new double[i2 - i1 + 1][M[0].length];
00218 for (int i = 0; i < i2 - i1 + 1; i++)
00219 System.arraycopy(M[i + i1], 0, array[i], 0, M[i + i1].length);
00220 return array;
00221 }
00222
00223 public static double[] getRangeCopy(double[] M, int j1, int j2) {
00224 double[] array = new double[j2 - j1 + 1];
00225 System.arraycopy(M, j1, array, 0, j2 - j1 + 1);
00226 return array;
00227 }
00228
00229 public static double[] getCopy(double[] M, int... I) {
00230 double[] array = new double[I.length];
00231 for (int i = 0; i < I.length; i++)
00232 array[i] = M[I[i]];
00233 return array;
00234 }
00235
00236 public static int getColumnDimension(double[][] M, int i) {
00237 return M[i].length;
00238 }
00239
00240 public static double[][] mergeRows(double[]... x) {
00241 double[][] array = new double[x.length][];
00242 for (int i = 0; i < array.length; i++) {
00243 array[i] = new double[x[i].length];
00244 System.arraycopy(x[i], 0, array[i], 0, array[i].length);
00245 }
00246 return array;
00247 }
00248
00249 public static double[][] mergeColumns(double[]... x) {
00250 double[][] array = new double[x[0].length][x.length];
00251 for (int i = 0; i < array.length; i++)
00252 for (int j = 0; j < array[i].length; j++)
00253 array[i][j] = x[j][i];
00254 return array;
00255 }
00256
00257 public static double[] merge(double[]... x) {
00258 int[] xlength_array = new int[x.length];
00259 xlength_array[0] = x[0].length;
00260 for (int i = 1; i < x.length; i++)
00261 xlength_array[i] = x[i].length + xlength_array[i - 1];
00262 double[] array = new double[xlength_array[x.length - 1]];
00263 System.arraycopy(x[0], 0, array, 0, x[0].length);
00264 for (int i = 1; i < x.length; i++)
00265 System.arraycopy(x[i], 0, array, xlength_array[i - 1], x[i].length);
00266 return array;
00267 }
00268
00269 public static double[][] insertColumns(double[][] x, double[][] y, int J) {
00270 double[][] array = new double[x.length][x[0].length + y[0].length];
00271 for (int i = 0; i < array.length; i++) {
00272 System.arraycopy(x[i], 0, array[i], 0, J);
00273 System.arraycopy(y[i], 0, array[i], J, y[i].length);
00274 System.arraycopy(x[i], J, array[i], J + y[i].length, x[i].length - J);
00275 }
00276 return array;
00277 }
00278
00279 public static double[][] insertColumn(double[][] x, double[] y, int J) {
00280 double[][] array = new double[x.length][x[0].length + 1];
00281 for (int i = 0; i < array.length; i++) {
00282 System.arraycopy(x[i], 0, array[i], 0, J);
00283 array[i][J] = y[i];
00284 System.arraycopy(x[i], J, array[i], J + 1, x[i].length - J);
00285 }
00286 return array;
00287 }
00288
00289 public static double[][] insertRows(double[][] x, double[][] y, int I) {
00290 double[][] array = new double[x.length + y.length][x[0].length];
00291 for (int i = 0; i < I; i++)
00292 System.arraycopy(x[i], 0, array[i], 0, x[i].length);
00293 for (int i = 0; i < y.length; i++)
00294 System.arraycopy(y[i], 0, array[i + I], 0, y[i].length);
00295 for (int i = 0; i < x.length - I; i++)
00296 System.arraycopy(x[i + I], 0, array[i + I + y.length], 0, x[i].length);
00297 return array;
00298 }
00299
00300 public static double[][] insertRow(double[][] x, double[] y, int I) {
00301 double[][] array = new double[x.length + 1][x[0].length];
00302 for (int i = 0; i < I; i++)
00303 System.arraycopy(x[i], 0, array[i], 0, x[i].length);
00304 System.arraycopy(y, 0, array[I], 0, y.length);
00305 for (int i = 0; i < x.length - I; i++)
00306 System.arraycopy(x[i + I], 0, array[i + I + 1], 0, x[i].length);
00307 return array;
00308 }
00309
00310 public static double[] insert(double[] x, int I, double... y) {
00311 double[] array = new double[x.length + y.length];
00312 System.arraycopy(x, 0, array, 0, I);
00313 System.arraycopy(y, 0, array, I, y.length);
00314 System.arraycopy(x, I, array, I + y.length, x.length - I);
00315 return array;
00316 }
00317
00318 public static double[][] deleteColumnsRange(double[][] x, int J1, int J2) {
00319 double[][] array = new double[x.length][x[0].length - (J2 - J1 + 1)];
00320 for (int i = 0; i < array.length; i++) {
00321 System.arraycopy(x[i], 0, array[i], 0, J1);
00322 System.arraycopy(x[i], J2 + 1, array[i], J1, x[i].length - (J2 + 1));
00323 }
00324 return array;
00325 }
00326
00327 public static double[][] deleteColumns(double[][] x, int... J) {
00328 double[][] array = new double[x.length][x[0].length - J.length];
00329 for (int i = 0; i < array.length; i++) {
00330 System.arraycopy(x[i], 0, array[i], 0, J[0]);
00331 for (int j = 0; j < J.length - 1; j++)
00332 System.arraycopy(x[i], J[j] + 1, array[i], J[j] - j, J[j + 1] - J[j] - 1);
00333 System.arraycopy(x[i], J[J.length - 1] + 1, array[i], J[J.length - 1] - J.length + 1, x[i].length - J[J.length - 1] - 1);
00334 }
00335 return array;
00336 }
00337
00338 public static double[][] deleteRowsRange(double[][] x, int I1, int I2) {
00339 double[][] array = new double[x.length - (I2 - I1 + 1)][x[0].length];
00340 for (int i = 0; i < I1; i++)
00341 System.arraycopy(x[i], 0, array[i], 0, x[i].length);
00342 for (int i = 0; i < x.length - I2 - 1; i++)
00343 System.arraycopy(x[i + I2 + 1], 0, array[i + I1], 0, x[i].length);
00344 return array;
00345 }
00346
00347 public static double[][] deleteRows(double[][] x, int... I) {
00348 double[][] array = new double[x.length - I.length][x[0].length];
00349 for (int i = 0; i < I[0]; i++)
00350 System.arraycopy(x[i], 0, array[i], 0, x[i].length);
00351 for (int j = 0; j < I.length - 1; j++)
00352 for (int i = I[j] + 1; i < I[j + 1]; i++)
00353 System.arraycopy(x[i], 0, array[i - j], 0, x[i].length);
00354 for (int i = I[I.length - 1] + 1; i < x.length; i++)
00355 System.arraycopy(x[i], 0, array[i - I.length], 0, x[i].length);
00356 return array;
00357 }
00358
00359 public static double[] deleteRange(double[] x, int J1, int J2) {
00360 double[] array = new double[x.length - (J2 - J1 + 1)];
00361 System.arraycopy(x, 0, array, 0, J1);
00362 System.arraycopy(x, J2 + 1, array, J1, x.length - (J2 + 1));
00363 return array;
00364 }
00365
00366 public static double[] delete(double[] x, int... J) {
00367 double[] array = new double[x.length - J.length];
00368 System.arraycopy(x, 0, array, 0, J[0]);
00369 for (int j = 0; j < J.length - 1; j++)
00370 System.arraycopy(x, J[j] + 1, array, J[j] - j, J[j + 1] - J[j] - 1);
00371 System.arraycopy(x, J[J.length - 1] + 1, array, J[J.length - 1] - J.length + 1, x.length - J[J.length - 1] - 1);
00372 return array;
00373 }
00374
00375 public static double[][] buildXY(double Xmin, double Xmax, double[] Y) {
00376 int n = Y.length;
00377 double[][] XY = new double[n][2];
00378 for (int i = 0; i < n; i++) {
00379 XY[i][0] = Xmin + (Xmax - Xmin) * (double) i / (double) (n - 1);
00380 XY[i][1] = Y[i];
00381 }
00382 return XY;
00383 }
00384
00385 public static double[][] buildXY(double[] X, double[] Y) {
00386 return mergeColumns(X, Y);
00387 }
00388
00389
00390
00391 public static double[] min(double[][] M) {
00392 double[] min = new double[M[0].length];
00393 for (int j = 0; j < min.length; j++) {
00394 min[j] = M[0][j];
00395 for (int i = 1; i < M.length; i++)
00396 min[j] = Math.min(min[j], M[i][j]);
00397 }
00398 return min;
00399 }
00400
00401 public static int min(int... M) {
00402 int min = M[0];
00403 for (int i = 1; i < M.length; i++)
00404 min = Math.min(min, M[i]);
00405 return min;
00406 }
00407
00408 public static int max(int... M) {
00409 int max = M[0];
00410 for (int i = 1; i < M.length; i++)
00411 max = Math.max(max, M[i]);
00412 return max;
00413 }
00414
00415 public static double min(double... M) {
00416 double min = M[0];
00417 for (int i = 1; i < M.length; i++)
00418 min = Math.min(min, M[i]);
00419 return min;
00420 }
00421
00422 public static double[] max(double[][] M) {
00423 double[] max = new double[M[0].length];
00424 for (int j = 0; j < max.length; j++) {
00425 max[j] = M[0][j];
00426 for (int i = 1; i < M.length; i++)
00427 max[j] = Math.max(max[j], M[i][j]);
00428 }
00429 return max;
00430 }
00431
00432 public static double max(double... M) {
00433 double max = M[0];
00434 for (int i = 1; i < M.length; i++)
00435 max = Math.max(max, M[i]);
00436 return max;
00437 }
00438
00439 public static int[] minIndex(double[][] M) {
00440 int[] minI = new int[M[0].length];
00441 for (int j = 0; j < minI.length; j++) {
00442 minI[j] = 0;
00443 for (int i = 1; i < M.length; i++)
00444 if (M[i][j] < M[minI[j]][j])
00445 minI[j] = i;
00446
00447 }
00448 return minI;
00449 }
00450
00451 public static int minIndex(double... M) {
00452 int minI = 0;
00453 for (int i = 1; i < M.length; i++)
00454 if (M[i] < M[minI])
00455 minI = i;
00456 return minI;
00457 }
00458
00459 public static int[] maxIndex(double[][] M) {
00460 int[] maxI = new int[M[0].length];
00461 for (int j = 0; j < maxI.length; j++) {
00462 maxI[j] = 0;
00463 for (int i = 1; i < M.length; i++)
00464 if (M[i][j] > M[maxI[j]][j])
00465 maxI[j] = i;
00466 }
00467 return maxI;
00468 }
00469
00470 public static int maxIndex(double... M) {
00471 int maxI = 0;
00472 for (int i = 1; i < M.length; i++)
00473 if (M[i] > M[maxI])
00474 maxI = i;
00475 return maxI;
00476 }
00477
00478
00479
00480 public static String toString(double[]... v) {
00481 StringBuffer str = new StringBuffer();
00482 for (int i = 0; i < v.length; i++) {
00483 for (int j = 0; j < v[i].length; j++)
00484 str.append(v[i][j] + " ");
00485 if (i < v.length - 1)
00486 str.append("\n");
00487 }
00488 return str.toString();
00489 }
00490
00491
00492
00493 public static void throwError(String msg) {
00494 throw new IllegalArgumentException(msg);
00495 }
00496
00497 public static void checkColumnDimension(double[][] M, int n) {
00498 for (int i = 0; i < M.length; i++)
00499 if (M[i].length != n)
00500 throwError("row " + i + " have " + M[i].length + " columns instead of " + n + " columns expected.");
00501 }
00502
00503 public static boolean isColumnDimension(double[][] M, int n) {
00504 for (int i = 0; i < M.length; i++)
00505 if (M[i].length != n)
00506 return false;
00507 return true;
00508 }
00509
00510 public static void checkRowDimension(double[][] M, int m) {
00511 if (M.length != m)
00512 throwError("columns have " + M.length + " rows instead of " + m + " rows expected.");
00513 }
00514
00515 public static boolean isRowDimension(double[][] M, int m) {
00516 if (M.length != m)
00517 return false;
00518 return true;
00519 }
00520
00521 public static void checkLength(double[] M, int n) {
00522 if (M.length != n)
00523 throwError("row have " + M.length + " elements instead of " + n + " elements expected.");
00524 }
00525
00526 public static boolean isLength(double[] M, int n) {
00527 if (M.length != n)
00528 return false;
00529 return true;
00530 }
00531
00532 }