[Dive4elements-commits] [PATCH] A little optimization and handle the case that there are duplicates in the values

Wald Commits scm-commit at wald.intevation.org
Mon Dec 3 21:16:33 CET 2012


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1354565775 -3600
# Node ID d35d316049e8738db511770641006aa55699ff79
# Parent  63368dcc3f941d46d558fc3da2603ace32298c2b
A little optimization and handle the case that there are duplicates in the values.

diff -r 63368dcc3f94 -r d35d316049e8 flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java	Mon Dec 03 18:44:48 2012 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/MovingAverage.java	Mon Dec 03 21:16:15 2012 +0100
@@ -10,49 +10,56 @@
 
     public static double[][] simple(double[][] values, double radius) {
         TreeMap<Double, Double> map = toMap(values);
-        double[][] result = new double[values.length][values[0].length];
+        int N = map.size();
+        double [] xs = new double[N];
+        double [] ys = new double[N];
         int ndx = 0;
         for (double x: map.keySet()) {
             SortedMap<Double, Double> range =
                 map.subMap(x-radius, true, x+radius, true);
             double avg = 0d;
-            for (Double v: range.values()) {
+            for (double v: range.values()) {
                 avg += v;
             }
             avg /= range.size();
-            result[0][ndx] = x;
-            result[1][ndx] = avg;
+            xs[ndx] = x;
+            ys[ndx] = avg;
             ndx++;
         }
-        return result;
+        return new double [][] { xs, ys };
     }
 
     public static double[][] weighted(double[][] values, double radius) {
         TreeMap<Double, Double> map = toMap(values);
-        double[][] result = new double[values.length][values[0].length];
+        int N = map.size();
+        double [] xs = new double[N];
+        double [] ys = new double[N];
         int ndx = 0;
+        double _1radius = 1d/radius;
         for (double x: map.keySet()) {
             double avg = 0d;
             double weights = 0d;
             for (Map.Entry<Double, Double> e:
                 map.subMap(x-radius, false, x+radius, false).entrySet()
             ) {
-                double weight = 1d - Math.abs(x - e.getKey())/radius;
+                double weight = 1d - Math.abs(x - e.getKey())*_1radius;
                 avg += weight*e.getValue();
                 weights += weight;
             }
             avg /= weights;
-            result[0][ndx] = x;
-            result[1][ndx] = avg;
+            xs[ndx] = x;
+            ys[ndx] = avg;
             ndx++;
         }
-        return result;
+        return new double [][] { xs, ys };
     }
 
     private static TreeMap<Double, Double> toMap(double[][] values) {
         TreeMap<Double, Double> map = new TreeMap<Double, Double>();
-        for(int i = 0; i < values[0].length; i++) {
-            map.put(values[0][i], values[1][i]);
+        double [] xs = values[0];
+        double [] ys = values[1];
+        for (int i = 0; i < xs.length; i++) {
+            map.put(xs[i], ys[i]);
         }
         return map;
     }


More information about the Dive4elements-commits mailing list