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