comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/math/fitting/App.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/math/fitting/App.java@5642a83420f2
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.artifacts.math.fitting;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.ArrayList;
6 import java.util.TreeMap;
7 import java.util.Comparator;
8
9 import java.io.IOException;
10 import java.io.BufferedReader;
11 import java.io.Reader;
12 import java.io.InputStreamReader;
13
14 import org.apache.commons.math.optimization.fitting.CurveFitter;
15
16 import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
17
18 import org.apache.commons.math.MathException;
19
20 public class App
21 {
22 public static final double EPS = 1e-5;
23
24 public static final String FUNCTION_NAME =
25 System.getProperty("function", "linear");
26
27 public static final Comparator<Double> EPS_CMP =
28 new Comparator<Double>() {
29 @Override
30 public int compare(Double a, Double b) {
31 double diff = a - b;
32 if (diff < -EPS) return -1;
33 if (diff > EPS) return +1;
34 return 0;
35 }
36 };
37
38 public static final List<Double []>readPoints(Reader reader)
39 throws IOException
40 {
41 Map<Double, Double> map = new TreeMap<Double, Double>(EPS_CMP);
42
43 BufferedReader input = new BufferedReader(reader);
44
45 String line;
46 while ((line = input.readLine()) != null) {
47 if ((line = line.trim()).length() == 0 || line.startsWith("#")) {
48 continue;
49 }
50
51 String [] parts = line.split("\\s+");
52
53 if (parts.length < 2) {
54 continue;
55 }
56
57 try {
58 Double x = Double.valueOf(parts[0]);
59 Double y = Double.valueOf(parts[1]);
60
61 Double old = map.put(x, y);
62
63 if (old != null) {
64 System.err.println("duplicate x: " + x);
65 }
66 }
67 catch (NumberFormatException nfe) {
68 nfe.printStackTrace();
69 }
70 }
71
72 List<Double []> list = new ArrayList<Double []>(map.size());
73
74 for (Map.Entry<Double, Double> entry: map.entrySet()) {
75 list.add(new Double [] { entry.getKey(), entry.getValue() });
76 }
77
78 return list;
79 }
80
81 public static void main(String [] args) {
82
83 Function function = FunctionFactory
84 .getInstance()
85 .getFunction(FUNCTION_NAME);
86
87 if (function == null) {
88 System.err.println("Cannot find function '" + FUNCTION_NAME + "'.");
89 System.exit(1);
90 }
91
92 List<Double []> points = null;
93
94 try {
95 points = readPoints(new InputStreamReader(System.in));
96 }
97 catch (IOException ioe) {
98 ioe.printStackTrace();
99 System.exit(1);
100 }
101
102 LevenbergMarquardtOptimizer lmo = new LevenbergMarquardtOptimizer();
103
104 CurveFitter cf = new CurveFitter(lmo);
105
106 for (Double [] point: points) {
107 cf.addObservedPoint(point[0], point[1]);
108 }
109
110 double [] parameters = null;
111
112 try {
113 parameters = cf.fit(function, function.getInitialGuess());
114 }
115 catch (MathException me) {
116 me.printStackTrace();
117 System.exit(1);
118 }
119
120 String [] parameterNames = function.getParameterNames();
121
122 for (int i = 0; i < parameterNames.length; ++i) {
123 System.err.println(parameterNames[i] + ": " + parameters[i]);
124 }
125 }
126 }
127 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org