changeset 1671:da872168a899

Added code to interpolate double values in a sorted array flys-artifacts/trunk@2883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 04 Oct 2011 15:23:31 +0000
parents fc9719cddbc6
children 0b6dac664bbb
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java
diffstat 2 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Oct 04 15:14:09 2011 +0000
+++ b/flys-artifacts/ChangeLog	Tue Oct 04 15:23:31 2011 +0000
@@ -1,3 +1,9 @@
+2011-10-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/utils/DoubleUtil.java (interpolateSorted):
+	  Added code to linear interpolate double values in a sorted array.
+	  Keys and values are given as double arrays. Keys need to be sorted.
+
 2011-10-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/states/WDifferencesState.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java	Tue Oct 04 15:14:09 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java	Tue Oct 04 15:23:31 2011 +0000
@@ -2,6 +2,8 @@
 
 import java.util.Arrays;
 
+import de.intevation.flys.artifacts.math.Linear;
+
 
 public class DoubleUtil
 {
@@ -53,5 +55,31 @@
 
         return values;
     }
+
+    public static final double interpolateSorted(
+        double [] xs,
+        double [] ys,
+        double x
+    ) {
+        int lo = 0, hi = xs.length-1;
+
+        int mid = -1;
+
+        while (lo <= hi) {
+            mid = (lo + hi) >> 1;
+            double mx = xs[mid];
+                 if (x < mx) hi = mid - 1;
+            else if (x > mx) lo = mid + 1;
+            else return ys[mid];
+        }
+        if (mid < lo) {
+            return lo >= xs.length
+                ? Double.NaN
+                : Linear.linear(x, xs[mid], xs[mid+1], ys[mid], ys[mid+1]);
+        }
+        return hi < 0
+            ? Double.NaN
+            : Linear.linear(x, xs[mid-1], xs[mid], ys[mid-1], ys[mid]);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org