comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearFunction.java @ 1119:7c4f81f74c47

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

http://dive4elements.wald.intevation.org