comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/BackJumpCorrector.java @ 1672:0b6dac664bbb

Moved some generic double array code from BackjumpCorrector to DoubleUtil flys-artifacts/trunk@2885 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 04 Oct 2011 15:29:39 +0000
parents c09c9e05ecfa
children b5209452f6bb
comparison
equal deleted inserted replaced
1671:da872168a899 1672:0b6dac664bbb
14 import org.apache.commons.math.exception.MathIllegalArgumentException; 14 import org.apache.commons.math.exception.MathIllegalArgumentException;
15 15
16 import org.apache.log4j.Logger; 16 import org.apache.log4j.Logger;
17 17
18 import de.intevation.flys.artifacts.model.Calculation; 18 import de.intevation.flys.artifacts.model.Calculation;
19
20 import de.intevation.flys.utils.DoubleUtil;
19 21
20 public class BackJumpCorrector 22 public class BackJumpCorrector
21 implements Serializable 23 implements Serializable
22 { 24 {
23 private static Logger log = Logger.getLogger(BackJumpCorrector.class); 25 private static Logger log = Logger.getLogger(BackJumpCorrector.class);
45 public boolean doCorrection( 47 public boolean doCorrection(
46 double [] km, 48 double [] km,
47 double [] ws, 49 double [] ws,
48 Calculation errors 50 Calculation errors
49 ) { 51 ) {
50 boolean wsUp = isIncreasing(ws); 52 boolean wsUp = DoubleUtil.isIncreasing(ws);
51 53
52 if (wsUp) { 54 if (wsUp) {
53 km = swapClone(km); 55 km = DoubleUtil.swapClone(km);
54 ws = swapClone(ws); 56 ws = DoubleUtil.swapClone(ws);
55 } 57 }
56 58
57 boolean kmUp = isIncreasing(km); 59 boolean kmUp = DoubleUtil.isIncreasing(km);
58 60
59 if (!kmUp) { 61 if (!kmUp) {
60 km = sumDiffs(km); 62 km = DoubleUtil.sumDiffs(km);
61 } 63 }
62 64
63 if (log.isDebugEnabled()) { 65 if (log.isDebugEnabled()) {
64 log.debug("BackJumpCorrector.doCorrection ------- enter"); 66 log.debug("BackJumpCorrector.doCorrection ------- enter");
65 log.debug(" km increasing: " + isIncreasing(km)); 67 log.debug(" km increasing: " + DoubleUtil.isIncreasing(km));
66 log.debug(" ws increasing: " + isIncreasing(ws)); 68 log.debug(" ws increasing: " + DoubleUtil.isIncreasing(ws));
67 log.debug("BackJumpCorrector.doCorrection ------- leave"); 69 log.debug("BackJumpCorrector.doCorrection ------- leave");
68 } 70 }
69 71
70 boolean hasBackJumps = doCorrectionClean(km, ws, errors); 72 boolean hasBackJumps = doCorrectionClean(km, ws, errors);
71 73
72 if (hasBackJumps && wsUp) { 74 if (hasBackJumps && wsUp) {
73 // mirror back 75 // mirror back
74 swap(corrected); 76 DoubleUtil.swap(corrected);
75 } 77 }
76 78
77 return hasBackJumps; 79 return hasBackJumps;
78 } 80 }
79 81
243 } 245 }
244 } // for all km 246 } // for all km
245 247
246 return !backjumps.isEmpty(); 248 return !backjumps.isEmpty();
247 } 249 }
248
249 public static final boolean isIncreasing(double [] array) {
250 int inc = 0;
251 int dec = 0;
252 int sweet = (array.length-1)/2;
253 for (int i = 1; i < array.length; ++i) {
254 if (array[i] > array[i-1]) {
255 if (++inc > sweet) {
256 return true;
257 }
258 }
259 else if (++dec > sweet) {
260 return false;
261 }
262 }
263 return inc > sweet;
264 }
265
266 public static final double [] swap(double [] array) {
267 int lo = 0;
268 int hi = array.length-1;
269 while (hi > lo) {
270 double t = array[lo];
271 array[lo] = array[hi];
272 array[hi] = t;
273 ++lo;
274 --hi;
275 }
276
277 return array;
278 }
279
280 public static final double [] swapClone(double [] in) {
281 double [] out = new double[in.length];
282
283 for (int j = out.length-1, i = 0; j >= 0;) {
284 out[j--] = in[i++];
285 }
286
287 return out;
288 }
289
290 public static final double [] sumDiffs(double [] in) {
291 double [] out = new double[in.length];
292
293 for (int i = 1; i < out.length; ++i) {
294 out[i] = out[i-1] + Math.abs(in[i-1] - in[i]);
295 }
296
297 return out;
298 }
299 } 250 }
300 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 251 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org