comparison flys-artifacts/src/main/java/de/intevation/flys/utils/DoubleUtil.java @ 3812:f788d2d901d6

merged flys-artifacts/pre2.6-2011-12-05
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:53 +0200
parents 0b6dac664bbb
children 0dd58ab7e118
comparison
equal deleted inserted replaced
3808:5fab0fe3c445 3812:f788d2d901d6
1 package de.intevation.flys.utils;
2
3 import java.util.Arrays;
4
5 import de.intevation.flys.artifacts.math.Linear;
6
7
8 public class DoubleUtil
9 {
10 public static final double DEFAULT_STEP_PRECISION = 1e6;
11
12 private DoubleUtil() {
13 }
14
15 public static final double [] explode(double from, double to, double step) {
16 return explode(from, to, step, DEFAULT_STEP_PRECISION);
17 }
18
19 public static final double round(double x, double precision) {
20 return Math.round(x * precision)/precision;
21 }
22
23 public static final double round(double x) {
24 return Math.round(x * DEFAULT_STEP_PRECISION)/DEFAULT_STEP_PRECISION;
25 }
26
27 public static final double [] explode(
28 double from,
29 double to,
30 double step,
31 double precision
32 ) {
33 double lower = from;
34
35 double diff = to - from;
36 double tmp = diff / step;
37 int num = (int)Math.abs(Math.ceil(tmp)) + 1;
38
39 double [] values = new double[num];
40
41 if (from > to) {
42 step = -step;
43 }
44
45 double max = Math.max(from, to);
46
47 for (int idx = 0; idx < num; idx++) {
48 if (lower > max) {
49 return Arrays.copyOfRange(values, 0, idx);
50 }
51
52 values[idx] = round(lower, precision);
53 lower += step;
54 }
55
56 return values;
57 }
58
59 public static final double interpolateSorted(
60 double [] xs,
61 double [] ys,
62 double x
63 ) {
64 int lo = 0, hi = xs.length-1;
65
66 int mid = -1;
67
68 while (lo <= hi) {
69 mid = (lo + hi) >> 1;
70 double mx = xs[mid];
71 if (x < mx) hi = mid - 1;
72 else if (x > mx) lo = mid + 1;
73 else return ys[mid];
74 }
75 if (mid < lo) {
76 return lo >= xs.length
77 ? Double.NaN
78 : Linear.linear(x, xs[mid], xs[mid+1], ys[mid], ys[mid+1]);
79 }
80 return hi < 0
81 ? Double.NaN
82 : Linear.linear(x, xs[mid-1], xs[mid], ys[mid-1], ys[mid]);
83 }
84
85 public static final boolean isIncreasing(double [] array) {
86 int inc = 0;
87 int dec = 0;
88 int sweet = (array.length-1)/2;
89 for (int i = 1; i < array.length; ++i) {
90 if (array[i] > array[i-1]) {
91 if (++inc > sweet) {
92 return true;
93 }
94 }
95 else if (++dec > sweet) {
96 return false;
97 }
98 }
99 return inc > sweet;
100 }
101
102 public static final double [] swap(double [] array) {
103 int lo = 0;
104 int hi = array.length-1;
105 while (hi > lo) {
106 double t = array[lo];
107 array[lo] = array[hi];
108 array[hi] = t;
109 ++lo;
110 --hi;
111 }
112
113 return array;
114 }
115
116 public static final double [] swapClone(double [] in) {
117 double [] out = new double[in.length];
118
119 for (int j = out.length-1, i = 0; j >= 0;) {
120 out[j--] = in[i++];
121 }
122
123 return out;
124 }
125
126 public static final double [] sumDiffs(double [] in) {
127 double [] out = new double[in.length];
128
129 for (int i = 1; i < out.length; ++i) {
130 out[i] = out[i-1] + Math.abs(in[i-1] - in[i]);
131 }
132
133 return out;
134 }
135 }
136 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org