comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearFunction.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents a645bd23c1c8
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.math;
2
3 import org.apache.commons.math.FunctionEvaluationException;
4
5 import org.apache.commons.math.analysis.UnivariateRealFunction;
6
7 import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
8
9 /**
10 * Models a linear function to be usable in the fitting framework of
11 * the Apache Commons Mathematics Library.
12 *
13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
14 */
15 public class LinearFunction
16 implements ParametricRealFunction
17 {
18 /**
19 * Instance to prevent needless creations of instances.
20 */
21 public static final LinearFunction INSTANCE = new LinearFunction();
22
23 /**
24 * Specialized class to be useful in function evaluating
25 * in the Apache Commons Mathematics Library.
26 */
27 public static class Univariate
28 implements UnivariateRealFunction
29 {
30 /**
31 * The linear scaling factor.
32 */
33 protected double m;
34 /**
35 * The linear offset.
36 */
37 protected double b;
38
39 /**
40 * Default constructor.
41 */
42 public Univariate() {
43 }
44
45 /**
46 * Constructor to create a Univariate with the linear parameters
47 * of the line between (x1, y1) and (x2, y2).
48 * @param x1 The x coordinate of the first point.
49 * @param y1 The y coordinate of the first point.
50 * @param x2 The x coordinate of the second point.
51 * @param y2 The y coordinate of the second point.
52 */
53 public Univariate(double x1, double y1, double x2, double y2) {
54 if (y1 == y2) {
55 m = 0d;
56 b = (x1 + x2)*0.5d;
57 }
58 else {
59 m = (x1 - x2)/(y1 - y2);
60 b = y1 - m*x1;
61 }
62 }
63
64 public double value(double x) {
65 return m*x + b;
66 }
67 } // class Univariate
68
69 /**
70 * Default constructor.
71 */
72 public LinearFunction() {
73 }
74
75 public double value(double x, double [] parameters)
76 throws FunctionEvaluationException
77 {
78 return x*parameters[0] + parameters[1];
79 }
80
81 public double [] gradient(double x, double [] parameters)
82 throws FunctionEvaluationException
83 {
84 return new double [] { x, 1f };
85 }
86 }
87 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org