sascha@655: package de.intevation.flys.utils;
sascha@655: 
ingo@1666: import java.util.Arrays;
ingo@1666: 
sascha@1671: import de.intevation.flys.artifacts.math.Linear;
sascha@1671: 
ingo@1666: 
sascha@655: public class DoubleUtil
sascha@655: {
sascha@655:     public static final double DEFAULT_STEP_PRECISION = 1e6;
sascha@655: 
sascha@655:     private DoubleUtil() {
sascha@655:     }
sascha@655: 
sascha@655:     public static final double [] explode(double from, double to, double step) {
sascha@655:         return explode(from, to, step, DEFAULT_STEP_PRECISION);
sascha@655:     }
sascha@655: 
sascha@655:     public static final double round(double x, double precision) {
sascha@655:         return Math.round(x * precision)/precision;
sascha@655:     }
sascha@655: 
sascha@655:     public static final double round(double x) {
sascha@655:         return Math.round(x * DEFAULT_STEP_PRECISION)/DEFAULT_STEP_PRECISION;
sascha@655:     }
sascha@655: 
sascha@655:     public static final double [] explode(
sascha@655:         double from,
sascha@655:         double to,
sascha@655:         double step,
sascha@655:         double precision
sascha@655:     ) {
sascha@655:         double lower = from;
sascha@655: 
sascha@655:         double diff = to - from;
sascha@655:         double tmp  = diff / step;
sascha@655:         int    num = (int)Math.abs(Math.ceil(tmp)) + 1;
sascha@655: 
sascha@655:         double [] values = new double[num];
sascha@655: 
sascha@655:         if (from > to) {
sascha@655:             step = -step;
sascha@655:         }
sascha@655: 
ingo@1666:         double max = Math.max(from, to);
ingo@1666: 
sascha@655:         for (int idx = 0; idx < num; idx++) {
ingo@1666:             if (lower > max) {
ingo@1666:                 return Arrays.copyOfRange(values, 0, idx);
ingo@1666:             }
ingo@1666: 
sascha@655:             values[idx] = round(lower, precision);
sascha@655:             lower      += step;
sascha@655:         }
sascha@655: 
sascha@655:         return values;
sascha@655:     }
sascha@1671: 
sascha@1671:     public static final double interpolateSorted(
sascha@1671:         double [] xs,
sascha@1671:         double [] ys,
sascha@1671:         double x
sascha@1671:     ) {
sascha@1671:         int lo = 0, hi = xs.length-1;
sascha@1671: 
sascha@1671:         int mid = -1;
sascha@1671: 
sascha@1671:         while (lo <= hi) {
sascha@1671:             mid = (lo + hi) >> 1;
sascha@1671:             double mx = xs[mid];
sascha@1671:                  if (x < mx) hi = mid - 1;
sascha@1671:             else if (x > mx) lo = mid + 1;
sascha@1671:             else return ys[mid];
sascha@1671:         }
sascha@1671:         if (mid < lo) {
sascha@1671:             return lo >= xs.length
sascha@1671:                 ? Double.NaN
sascha@1671:                 : Linear.linear(x, xs[mid], xs[mid+1], ys[mid], ys[mid+1]);
sascha@1671:         }
sascha@1671:         return hi < 0
sascha@1671:             ? Double.NaN
sascha@1671:             : Linear.linear(x, xs[mid-1], xs[mid], ys[mid-1], ys[mid]);
sascha@1671:     }
sascha@1672: 
sascha@1672:     public static final boolean isIncreasing(double [] array) {
sascha@1672:         int inc = 0;
sascha@1672:         int dec = 0;
sascha@1672:         int sweet = (array.length-1)/2;
sascha@1672:         for (int i = 1; i < array.length; ++i) {
sascha@1672:             if (array[i] > array[i-1]) {
sascha@1672:                 if (++inc > sweet) {
sascha@1672:                     return true;
sascha@1672:                 }
sascha@1672:             }
sascha@1672:             else if (++dec > sweet) {
sascha@1672:                 return false;
sascha@1672:             }
sascha@1672:         }
sascha@1672:         return inc > sweet;
sascha@1672:     }
sascha@1672: 
sascha@1672:     public static final double [] swap(double [] array) {
sascha@1672:         int lo = 0;
sascha@1672:         int hi = array.length-1;
sascha@1672:         while (hi > lo) {
sascha@1672:             double t  = array[lo];
sascha@1672:             array[lo] = array[hi];
sascha@1672:             array[hi] = t;
sascha@1672:             ++lo;
sascha@1672:             --hi;
sascha@1672:         }
sascha@1672: 
sascha@1672:         return array;
sascha@1672:     }
sascha@1672: 
sascha@1672:     public static final double [] swapClone(double [] in) {
sascha@1672:         double [] out = new double[in.length];
sascha@1672: 
sascha@1672:         for (int j = out.length-1, i = 0; j >= 0;) {
sascha@1672:             out[j--] = in[i++];
sascha@1672:         }
sascha@1672: 
sascha@1672:         return out;
sascha@1672:     }
sascha@1672: 
sascha@1672:     public static final double [] sumDiffs(double [] in) {
sascha@1672:         double [] out = new double[in.length];
sascha@1672: 
sascha@1672:         for (int i = 1; i < out.length; ++i) {
sascha@1672:             out[i] = out[i-1] + Math.abs(in[i-1] - in[i]);
sascha@1672:         }
sascha@1672: 
sascha@1672:         return out;
sascha@1672:     }
sascha@655: }
sascha@655: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :