teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.utils; felix@1163: felix@1163: import java.util.Random; felix@1163: sascha@1678: import gnu.trove.TDoubleArrayList; felix@1163: felix@1163: public class DataUtil felix@1163: { aheinecke@7081: public static boolean guessDataIncreasing(TDoubleArrayList data) { aheinecke@7081: return guessDataIncreasing(data, 0.05f); felix@1163: } felix@1163: aheinecke@7081: /** Guess if data1 and data2 both grow in the same direction */ aheinecke@7081: public static boolean guessSameDirectionData(TDoubleArrayList data1, aheinecke@7081: TDoubleArrayList data2) { aheinecke@7081: boolean d1dir = DataUtil.guessDataIncreasing(data1, 0.05f); aheinecke@7081: boolean d2dir = DataUtil.guessDataIncreasing(data2, 0.05f); aheinecke@7081: int size = data1.size(); aheinecke@7081: return ((d1dir && d2dir) || (!d1dir && !d2dir)) && size > 1; aheinecke@7081: } aheinecke@7081: tom@8856: public static boolean guessDataIncreasing( tom@8856: TDoubleArrayList data, tom@8856: float factor tom@8856: ) { sascha@1678: int N = data.size(); felix@1163: if (N < 2) return false; sascha@3076: felix@1163: int samples = (int)(factor*N) + 1; sascha@3076: felix@1163: int up = 0; sascha@3076: felix@1163: Random rand = new Random(); sascha@3076: felix@1163: for (int i = 0; i < samples; ++i) { felix@1163: int pos2 = rand.nextInt(N-1) + 1; felix@1163: int pos1 = rand.nextInt(pos2); sascha@1678: double w1 = data.getQuick(pos1); sascha@1678: double w2 = data.getQuick(pos2); felix@1163: if (w2 > w1) ++up; felix@1163: } sascha@3076: felix@1163: return up > samples/2; felix@1163: } felix@1163: } felix@1982: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :