sascha@357: package de.intevation.gnv.math; sascha@357: sascha@357: import org.apache.commons.math.optimization.fitting.ParametricRealFunction; sascha@357: sascha@357: import org.apache.commons.math.FunctionEvaluationException; sascha@357: sascha@433: import org.apache.commons.math.analysis.UnivariateRealFunction; sascha@433: sascha@357: /** sascha@514: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) sascha@357: */ sascha@357: public class LinearFunction sascha@357: implements ParametricRealFunction sascha@357: { sascha@357: public static final LinearFunction INSTANCE = new LinearFunction(); sascha@357: sascha@433: public static class Univariate sascha@433: implements UnivariateRealFunction sascha@433: { sascha@433: protected double m; sascha@433: protected double b; sascha@433: sascha@433: public Univariate() { sascha@433: } sascha@433: sascha@433: public Univariate(double x1, double y1, double x2, double y2) { sascha@433: if (y1 == y2) { sascha@433: m = 0d; sascha@433: b = (x1 + x2)*0.5d; sascha@433: } sascha@433: else { sascha@433: m = (x1 - x2)/(y1 - y2); sascha@433: b = y1 - m*x1; sascha@433: } sascha@433: } sascha@433: sascha@433: public double value(double x) { sascha@433: return m*x + b; sascha@433: } sascha@433: } // class Univariate sascha@433: sascha@357: public LinearFunction() { sascha@357: } sascha@357: sascha@357: public double value(double x, double [] parameters) sascha@357: throws FunctionEvaluationException sascha@357: { sascha@357: return x*parameters[0] + parameters[1]; sascha@357: } sascha@357: sascha@357: public double [] gradient(double x, double [] parameters) sascha@357: throws FunctionEvaluationException sascha@357: { sascha@357: return new double [] { x, 1f }; sascha@357: } sascha@357: } sascha@514: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :