comparison artifacts/src/main/java/org/dive4elements/river/utils/DoubleUtil.java @ 6434:898afcce1d0a

Partial fix for flys/1303
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 26 Jun 2013 13:33:15 +0200
parents af13ceeba52a
children eb4ca9a7eaca
comparison
equal deleted inserted replaced
6433:2874e16c2cc6 6434:898afcce1d0a
11 import org.dive4elements.river.artifacts.math.Linear; 11 import org.dive4elements.river.artifacts.math.Linear;
12 12
13 import gnu.trove.TDoubleArrayList; 13 import gnu.trove.TDoubleArrayList;
14 14
15 import java.util.Arrays; 15 import java.util.Arrays;
16 import java.util.Comparator;
16 17
17 import org.apache.log4j.Logger; 18 import org.apache.log4j.Logger;
18 19
19 /** Utils to deal with Double precision values. */ 20 /** Utils to deal with Double precision values. */
20 public class DoubleUtil 21 public class DoubleUtil
21 { 22 {
22 /** Private logger. */ 23 /** Private logger. */
23 private static Logger log = Logger.getLogger(DoubleUtil.class); 24 private static Logger log = Logger.getLogger(DoubleUtil.class);
24 25
25 public static final double DEFAULT_STEP_PRECISION = 1e6; 26 public static final double DEFAULT_STEP_PRECISION = 1e6;
27
28 public static final Comparator<double []> DOUBLE_PAIR_CMP =
29 new Comparator<double []>() {
30 @Override
31 public int compare(double [] a, double [] b) {
32 double diff = a[0] - b[0];
33 if (diff < 0d) return -1;
34 if (diff > 0d) return +1;
35 return 0;
36 }
37 };
26 38
27 /** EPSILON for comparison of double precision values. */ 39 /** EPSILON for comparison of double precision values. */
28 public static final double EPSILON = 1e-4; 40 public static final double EPSILON = 1e-4;
29 41
30 private DoubleUtil() { 42 private DoubleUtil() {
223 if (d > max) max = d; 235 if (d > max) max = d;
224 } 236 }
225 return max; 237 return max;
226 } 238 }
227 239
240
241
242 /** Sort a and b with a as key. b is ordered accordingly */
243 public static final void sortByFirst(double [] a, double [] b) {
244 // XXX: Not efficient but bulletproof.
245 double [][] pairs = new double[a.length][2];
246 for (int i = 0; i < a.length; ++i) {
247 double [] p = pairs[i];
248 p[0] = a[i];
249 p[1] = b[i];
250
251 }
252 Arrays.sort(pairs, DOUBLE_PAIR_CMP);
253 for (int i = 0; i < a.length; ++i) {
254 double [] p = pairs[i];
255 a[i] = p[0];
256 b[i] = p[1];
257 }
258 }
259
228 public static void removeNaNs(TDoubleArrayList [] arrays) { 260 public static void removeNaNs(TDoubleArrayList [] arrays) {
229 261
230 int dest = 0; 262 int dest = 0;
231 263
232 int A = arrays.length; 264 int A = arrays.length;

http://dive4elements.wald.intevation.org