comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearFunction.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents d9d933e06875
children 9a828e5a2390
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 package de.intevation.gnv.math;
2
3 import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
4
5 import org.apache.commons.math.FunctionEvaluationException;
6
7 import org.apache.commons.math.analysis.UnivariateRealFunction;
8
9 /**
10 * @author Sascha L. Teichmann (sascha.teichmann@intevation.de)
11 */
12 public class LinearFunction
13 implements ParametricRealFunction
14 {
15 public static final LinearFunction INSTANCE = new LinearFunction();
16
17 public static class Univariate
18 implements UnivariateRealFunction
19 {
20 protected double m;
21 protected double b;
22
23 public Univariate() {
24 }
25
26 public Univariate(double x1, double y1, double x2, double y2) {
27 if (y1 == y2) {
28 m = 0d;
29 b = (x1 + x2)*0.5d;
30 }
31 else {
32 m = (x1 - x2)/(y1 - y2);
33 b = y1 - m*x1;
34 }
35 }
36
37 public double value(double x) {
38 return m*x + b;
39 }
40 } // class Univariate
41
42 public LinearFunction() {
43 }
44
45 public double value(double x, double [] parameters)
46 throws FunctionEvaluationException
47 {
48 return x*parameters[0] + parameters[1];
49 }
50
51 public double [] gradient(double x, double [] parameters)
52 throws FunctionEvaluationException
53 {
54 return new double [] { x, 1f };
55 }
56 }
57 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org