Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 79dd823733e2 |
children | 56f62b5209f5 |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import de.intevation.flys.artifacts.math.Linear; | |
4 | |
5 import gnu.trove.TDoubleArrayList; | |
6 | |
7 import java.io.Serializable; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 public class Parameters | |
12 implements Serializable | |
13 { | |
14 private static Logger log = Logger.getLogger(Parameters.class); | |
15 | |
16 public interface Visitor { | |
17 | |
18 void visit(double [] row); | |
19 | |
20 } // interface Visitor | |
21 | |
22 public static final double EPSILON = 1e-4; | |
23 | |
24 protected String [] columnNames; | |
25 protected TDoubleArrayList [] columns; | |
26 | |
27 public Parameters() { | |
28 } | |
29 | |
30 public Parameters(String [] columnNames) { | |
31 if (columnNames == null || columnNames.length < 1) { | |
32 throw new IllegalArgumentException("columnNames too short."); | |
33 } | |
34 this.columnNames = columnNames; | |
35 columns = new TDoubleArrayList[columnNames.length]; | |
36 for (int i = 0; i < columns.length; ++i) { | |
37 columns[i] = new TDoubleArrayList(); | |
38 } | |
39 } | |
40 | |
41 public int columnIndex(String name) { | |
42 for (int i = 0; i < columnNames.length; ++i) { | |
43 if (columnNames[i].equals(name)) { | |
44 return i; | |
45 } | |
46 } | |
47 if (log.isDebugEnabled()) { | |
48 log.debug("columnIndex: " + name + " not found in columnNames"); | |
49 } | |
50 return -1; | |
51 } | |
52 | |
53 public int newRow() { | |
54 | |
55 int N = columns[0].size(); | |
56 | |
57 for (int i = 0; i < columns.length; ++i) { | |
58 columns[i].add(Double.NaN); | |
59 } | |
60 | |
61 return N; | |
62 } | |
63 | |
64 public double get(int row, int index) { | |
65 return columns[index].getQuick(row); | |
66 } | |
67 | |
68 public double get(int i, String columnName) { | |
69 int index = columnIndex(columnName); | |
70 return index >= 0 | |
71 ? columns[index].getQuick(i) | |
72 : Double.NaN; | |
73 } | |
74 | |
75 public void set(int row, int index, double value) { | |
76 columns[index].setQuick(row, value); | |
77 } | |
78 | |
79 public void set(int i, String columnName, double value) { | |
80 int idx = columnIndex(columnName); | |
81 if (idx >= 0) { | |
82 columns[idx].setQuick(i, value); | |
83 } | |
84 } | |
85 | |
86 public boolean set(int row, int [] indices, double [] values) { | |
87 boolean invalid = false; | |
88 for (int i = 0; i < indices.length; ++i) { | |
89 double v = values[i]; | |
90 if (Double.isNaN(v)) { | |
91 invalid = true; | |
92 } | |
93 else { | |
94 columns[indices[i]].setQuick(row, v); | |
95 } | |
96 } | |
97 return invalid; | |
98 } | |
99 | |
100 public int size() { | |
101 return columns[0].size(); | |
102 } | |
103 | |
104 public int getNumberColumns() { | |
105 return columnNames.length; | |
106 } | |
107 | |
108 public String [] getColumnNames() { | |
109 return columnNames; | |
110 } | |
111 | |
112 public void removeNaNs() { | |
113 W.removeNaNs(columns); | |
114 } | |
115 | |
116 public int [] columnIndices(String [] columns) { | |
117 int [] indices = new int[columns.length]; | |
118 for (int i = 0; i < columns.length; ++i) { | |
119 indices[i] = columnIndex(columns[i]); | |
120 } | |
121 return indices; | |
122 } | |
123 | |
124 public double [] get(int row, String [] columns) { | |
125 return get(row, columns, new double[columns.length]); | |
126 } | |
127 | |
128 public double [] get(int row, String [] columns, double [] values) { | |
129 for (int i = 0; i < columns.length; ++i) { | |
130 int idx = columnIndex(columns[i]); | |
131 values[i] = idx < 0 | |
132 ? Double.NaN | |
133 : this.columns[idx].getQuick(row); | |
134 } | |
135 | |
136 return values; | |
137 } | |
138 | |
139 public void get(int row, int [] columnIndices, double [] values) { | |
140 for (int i = 0; i < columnIndices.length; ++i) { | |
141 int index = columnIndices[i]; | |
142 values[i] = index >= 0 && index < columns.length | |
143 ? columns[index].getQuick(row) | |
144 : Double.NaN; | |
145 } | |
146 } | |
147 | |
148 public int binarySearch(String columnName, double value) { | |
149 return binarySearch(columnIndex(columnName), value); | |
150 } | |
151 | |
152 /** | |
153 * Performes a binary search in the column identified by its | |
154 * index. | |
155 * @return Index of found element or negative insertion point (shifted by one) | |
156 */ | |
157 public int binarySearch(int columnIndex, double value) { | |
158 TDoubleArrayList column = columns[columnIndex]; | |
159 return column.binarySearch(value); | |
160 } | |
161 | |
162 public int binarySearch(String columnName, double value, double epsilon) { | |
163 return binarySearch(columnIndex(columnName), value, epsilon); | |
164 } | |
165 | |
166 public int binarySearch(int columnIndex, double value, double epsilon) { | |
167 if (epsilon < 0d) epsilon = -epsilon; | |
168 double vl = value - epsilon; | |
169 double vh = value + epsilon; | |
170 | |
171 TDoubleArrayList column = columns[columnIndex]; | |
172 int lo = 0, hi = column.size()-1; | |
173 while (hi >= lo) { | |
174 int mid = (lo + hi) >> 1; | |
175 double v = column.getQuick(mid); | |
176 if (v < vl) lo = mid + 1; | |
177 else if (v > vh) hi = mid - 1; | |
178 else return mid; | |
179 } | |
180 | |
181 return -(lo + 1); | |
182 } | |
183 | |
184 public double [] interpolate(int columnIndex, double key) { | |
185 return interpolate(columnIndex, key, new double[columns.length]); | |
186 } | |
187 | |
188 public double [] interpolate(String columnName, double key) { | |
189 return interpolate( | |
190 columnIndex(columnName), key, new double[columns.length]); | |
191 } | |
192 | |
193 public double [] interpolate( | |
194 String columnName, | |
195 double key, | |
196 double [] values | |
197 ) { | |
198 return interpolate(columnIndex(columnName), key, values); | |
199 } | |
200 | |
201 public double [] interpolate( | |
202 int columnIndex, | |
203 double key, | |
204 double [] values | |
205 ) { | |
206 int row = binarySearch(columnIndex, key, EPSILON); | |
207 | |
208 if (row >= 0) { // direct hit | |
209 for (int i = 0; i < values.length; ++i) { | |
210 values[i] = columns[i].getQuick(row); | |
211 } | |
212 } | |
213 else { | |
214 row = -row - 1; | |
215 if (row < 1 || row >= size()) { | |
216 return null; | |
217 } | |
218 double v1 = columns[columnIndex].getQuick(row-1); | |
219 double v2 = columns[columnIndex].getQuick(row); | |
220 double factor = Linear.factor(key, v1, v2); | |
221 for (int i = 0; i < values.length; ++i) { | |
222 values[i] = Linear.weight( | |
223 factor, | |
224 columns[i].getQuick(row-1), | |
225 columns[i].getQuick(row)); | |
226 } | |
227 } | |
228 return values; | |
229 } | |
230 | |
231 | |
232 public double [] interpolate( | |
233 String keyName, | |
234 double key, | |
235 String [] columnNames | |
236 ) { | |
237 int keyIndex = columnIndex(keyName); | |
238 return keyIndex < 0 | |
239 ? null | |
240 : interpolate(keyIndex, key, columnNames); | |
241 } | |
242 | |
243 public double [] interpolate( | |
244 int keyIndex, | |
245 double key, | |
246 String [] columnNames | |
247 ) { | |
248 int row = binarySearch(keyIndex, key, EPSILON); | |
249 | |
250 if (row >= 0) { // direct match | |
251 double [] values = new double[columnNames.length]; | |
252 for (int i = 0; i < values.length; ++i) { | |
253 int ci = columnIndex(columnNames[i]); | |
254 values[i] = ci < 0 | |
255 ? Double.NaN | |
256 : columns[ci].getQuick(row); | |
257 } | |
258 return values; | |
259 } | |
260 | |
261 row = -row - 1; | |
262 if (row < 1 || row >= size()) { | |
263 log.debug("interpolate: row is out of bounds"); | |
264 return null; | |
265 } | |
266 | |
267 double v1 = columns[keyIndex].getQuick(row-1); | |
268 double v2 = columns[keyIndex].getQuick(row); | |
269 double factor = Linear.factor(key, v1, v2); | |
270 | |
271 double [] values = new double[columnNames.length]; | |
272 | |
273 for (int i = 0; i < values.length; ++i) { | |
274 int ci = columnIndex(columnNames[i]); | |
275 values[i] = ci < 0 | |
276 ? Double.NaN | |
277 : Linear.weight( | |
278 factor, | |
279 columns[ci].getQuick(row-1), | |
280 columns[ci].getQuick(row)); | |
281 } | |
282 | |
283 return values; | |
284 } | |
285 | |
286 public boolean isSorted(String columnName) { | |
287 return isSorted(columnIndex(columnName)); | |
288 } | |
289 | |
290 public boolean isSorted(int columnIndex) { | |
291 TDoubleArrayList column = columns[columnIndex]; | |
292 for (int i = 1, N = column.size(); i < N; ++i) { | |
293 if (column.getQuick(i-1) > column.getQuick(i)) { | |
294 return false; | |
295 } | |
296 } | |
297 return true; | |
298 } | |
299 | |
300 public void visit(Visitor visitor) { | |
301 visit(visitor, new double[columns.length]); | |
302 } | |
303 | |
304 public void visit(Visitor visitor, double [] data) { | |
305 for (int i = 0, R = size(); i < R; ++i) { | |
306 for (int j = 0; j < data.length; ++j) { | |
307 data[j] = columns[j].getQuick(i); | |
308 } | |
309 visitor.visit(data); | |
310 } | |
311 } | |
312 } | |
313 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |