Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 56f62b5209f5 |
children | 1df6984628c3 |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
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 getValue(int row, String column) { | |
125 int idx = columnIndex(column); | |
126 return idx >= 0 | |
127 ? columns[idx].getQuick(row) | |
128 : Double.NaN; | |
129 } | |
130 | |
131 public double [] get(int row, String [] columns) { | |
132 return get(row, columns, new double[columns.length]); | |
133 } | |
134 | |
135 public double [] get(int row, String [] columns, double [] values) { | |
136 for (int i = 0; i < columns.length; ++i) { | |
137 int idx = columnIndex(columns[i]); | |
138 values[i] = idx < 0 | |
139 ? Double.NaN | |
140 : this.columns[idx].getQuick(row); | |
141 } | |
142 | |
143 return values; | |
144 } | |
145 | |
146 public void get(int row, int [] columnIndices, double [] values) { | |
147 for (int i = 0; i < columnIndices.length; ++i) { | |
148 int index = columnIndices[i]; | |
149 values[i] = index >= 0 && index < columns.length | |
150 ? columns[index].getQuick(row) | |
151 : Double.NaN; | |
152 } | |
153 } | |
154 | |
155 public int binarySearch(String columnName, double value) { | |
156 return binarySearch(columnIndex(columnName), value); | |
157 } | |
158 | |
159 /** | |
160 * Performes a binary search in the column identified by its | |
161 * index. | |
162 * @return Index of found element or negative insertion point (shifted by one) | |
163 */ | |
164 public int binarySearch(int columnIndex, double value) { | |
165 TDoubleArrayList column = columns[columnIndex]; | |
166 return column.binarySearch(value); | |
167 } | |
168 | |
169 public int binarySearch(String columnName, double value, double epsilon) { | |
170 return binarySearch(columnIndex(columnName), value, epsilon); | |
171 } | |
172 | |
173 public int binarySearch(int columnIndex, double value, double epsilon) { | |
174 if (epsilon < 0d) epsilon = -epsilon; | |
175 double vl = value - epsilon; | |
176 double vh = value + epsilon; | |
177 | |
178 TDoubleArrayList column = columns[columnIndex]; | |
179 int lo = 0, hi = column.size()-1; | |
180 while (hi >= lo) { | |
181 int mid = (lo + hi) >> 1; | |
182 double v = column.getQuick(mid); | |
183 if (v < vl) lo = mid + 1; | |
184 else if (v > vh) hi = mid - 1; | |
185 else return mid; | |
186 } | |
187 | |
188 return -(lo + 1); | |
189 } | |
190 | |
191 public double [] interpolate(int columnIndex, double key) { | |
192 return interpolate(columnIndex, key, new double[columns.length]); | |
193 } | |
194 | |
195 public double [] interpolate(String columnName, double key) { | |
196 return interpolate( | |
197 columnIndex(columnName), key, new double[columns.length]); | |
198 } | |
199 | |
200 public double [] interpolate( | |
201 String columnName, | |
202 double key, | |
203 double [] values | |
204 ) { | |
205 return interpolate(columnIndex(columnName), key, values); | |
206 } | |
207 | |
208 public double [] interpolate( | |
209 int columnIndex, | |
210 double key, | |
211 double [] values | |
212 ) { | |
213 int row = binarySearch(columnIndex, key, EPSILON); | |
214 | |
215 if (row >= 0) { // direct hit | |
216 for (int i = 0; i < values.length; ++i) { | |
217 values[i] = columns[i].getQuick(row); | |
218 } | |
219 } | |
220 else { | |
221 row = -row - 1; | |
222 if (row < 1 || row >= size()) { | |
223 return null; | |
224 } | |
225 double v1 = columns[columnIndex].getQuick(row-1); | |
226 double v2 = columns[columnIndex].getQuick(row); | |
227 double factor = Linear.factor(key, v1, v2); | |
228 for (int i = 0; i < values.length; ++i) { | |
229 values[i] = Linear.weight( | |
230 factor, | |
231 columns[i].getQuick(row-1), | |
232 columns[i].getQuick(row)); | |
233 } | |
234 } | |
235 return values; | |
236 } | |
237 | |
238 | |
239 public double [] interpolate( | |
240 String keyName, | |
241 double key, | |
242 String [] columnNames | |
243 ) { | |
244 int keyIndex = columnIndex(keyName); | |
245 return keyIndex < 0 | |
246 ? null | |
247 : interpolate(keyIndex, key, columnNames); | |
248 } | |
249 | |
250 public double [] interpolate( | |
251 int keyIndex, | |
252 double key, | |
253 String [] columnNames | |
254 ) { | |
255 int row = binarySearch(keyIndex, key, EPSILON); | |
256 | |
257 if (row >= 0) { // direct match | |
258 double [] values = new double[columnNames.length]; | |
259 for (int i = 0; i < values.length; ++i) { | |
260 int ci = columnIndex(columnNames[i]); | |
261 values[i] = ci < 0 | |
262 ? Double.NaN | |
263 : columns[ci].getQuick(row); | |
264 } | |
265 return values; | |
266 } | |
267 | |
268 row = -row - 1; | |
269 if (row < 1 || row >= size()) { | |
270 log.debug("interpolate: row is out of bounds"); | |
271 return null; | |
272 } | |
273 | |
274 double v1 = columns[keyIndex].getQuick(row-1); | |
275 double v2 = columns[keyIndex].getQuick(row); | |
276 double factor = Linear.factor(key, v1, v2); | |
277 | |
278 double [] values = new double[columnNames.length]; | |
279 | |
280 for (int i = 0; i < values.length; ++i) { | |
281 int ci = columnIndex(columnNames[i]); | |
282 values[i] = ci < 0 | |
283 ? Double.NaN | |
284 : Linear.weight( | |
285 factor, | |
286 columns[ci].getQuick(row-1), | |
287 columns[ci].getQuick(row)); | |
288 } | |
289 | |
290 return values; | |
291 } | |
292 | |
293 public boolean isSorted(String columnName) { | |
294 return isSorted(columnIndex(columnName)); | |
295 } | |
296 | |
297 public boolean isSorted(int columnIndex) { | |
298 TDoubleArrayList column = columns[columnIndex]; | |
299 for (int i = 1, N = column.size(); i < N; ++i) { | |
300 if (column.getQuick(i-1) > column.getQuick(i)) { | |
301 return false; | |
302 } | |
303 } | |
304 return true; | |
305 } | |
306 | |
307 public void visit(Visitor visitor) { | |
308 visit(visitor, new double[columns.length]); | |
309 } | |
310 | |
311 public void visit(Visitor visitor, double [] data) { | |
312 for (int i = 0, R = size(); i < R; ++i) { | |
313 for (int j = 0; j < data.length; ++j) { | |
314 data[j] = columns[j].getQuick(i); | |
315 } | |
316 visitor.visit(data); | |
317 } | |
318 } | |
319 } | |
320 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |