ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: sascha@357: package de.intevation.gnv.math; sascha@357: sascha@357: import org.apache.commons.math.FunctionEvaluationException; sascha@357: sascha@433: import org.apache.commons.math.analysis.UnivariateRealFunction; sascha@433: sascha@779: import org.apache.commons.math.optimization.fitting.ParametricRealFunction; sascha@779: sascha@357: /** sascha@807: * Models a linear function to be usable in the fitting framework of sascha@805: * the Apache Commons Mathematics Library. sascha@805: * sascha@780: * @author Sascha L. Teichmann sascha@357: */ sascha@357: public class LinearFunction sascha@357: implements ParametricRealFunction sascha@357: { sascha@805: /** sascha@805: * Instance to prevent needless creations of instances. sascha@805: */ sascha@357: public static final LinearFunction INSTANCE = new LinearFunction(); sascha@357: sascha@805: /** sascha@805: * Specialized class to be useful in function evaluating sascha@805: * in the Apache Commons Mathematics Library. sascha@805: */ sascha@778: public static class Univariate sascha@433: implements UnivariateRealFunction sascha@433: { sascha@805: /** sascha@805: * The linear scaling factor. sascha@805: */ sascha@433: protected double m; sascha@805: /** sascha@805: * The linear offset. sascha@805: */ sascha@433: protected double b; sascha@433: sascha@805: /** sascha@805: * Default constructor. sascha@805: */ sascha@433: public Univariate() { sascha@433: } sascha@433: sascha@805: /** sascha@805: * Constructor to create a Univariate with the linear parameters sascha@805: * of the line between (x1, y1) and (x2, y2). sascha@805: * @param x1 The x coordinate of the first point. sascha@805: * @param y1 The y coordinate of the first point. sascha@805: * @param x2 The x coordinate of the second point. sascha@805: * @param y2 The y coordinate of the second point. sascha@805: */ 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@805: /** sascha@805: * Default constructor. sascha@805: */ sascha@357: public LinearFunction() { sascha@357: } sascha@357: sascha@357: public double value(double x, double [] parameters) sascha@778: throws FunctionEvaluationException sascha@357: { sascha@357: return x*parameters[0] + parameters[1]; sascha@357: } sascha@357: sascha@778: public double [] gradient(double x, double [] parameters) sascha@778: 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 :