    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements.  See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache License, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License.  You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the License for the specific language governing permissions and
    015     * limitations under the License.
    016     */
    017
    018    package org.apache.commons.math.optimization.fitting;
    019
    020    import java.io.Serializable;
    021
    022    import org.apache.commons.math.analysis.UnivariateRealFunction;
    023    import org.apache.commons.math.exception.DimensionMismatchException;
    024    import org.apache.commons.math.exception.util.LocalizedFormats;
    025    import org.apache.commons.math.exception.ZeroException;
    026    import org.apache.commons.math.exception.NullArgumentException;
    027
    028    /**
    029     * The derivative of {@link GaussianFunction}.  Specifically:
    030     * <p>
    031     * <tt>f'(x) = (-b / (d^2)) * (x - c) * exp(-((x - c)^2) / (2*(d^2)))</tt>
    032     * <p>
    033     * Notation key:
    034     * <ul>
    035     * <li><tt>x^n</tt>: <tt>x</tt> raised to the power of <tt>n</tt>
    036     * <li><tt>exp(x)</tt>: <i>e</i><tt>^x</tt>
    037     * </ul>
    038     *
    039     * @since 2.2
    040     * @version $Revision: 1037327 $ $Date: 2010-11-20 21:57:37 +0100 (sam. 20 nov. 2010) $
    041     */
    042    public class GaussianDerivativeFunction implements UnivariateRealFunction, Serializable {
    043
    044        /** Serializable version identifier. */
    045        private static final long serialVersionUID = -6500229089670174766L;
    046
    047        /** Parameter b of this function. */
    048        private final double b;
    049
    050        /** Parameter c of this function. */
    051        private final double c;
    052
    053        /** Square of the parameter d of this function. */
    054        private final double d2;
    055
    056        /**
    057         * Constructs an instance with the specified parameters.
    058         *
    059         * @param b <tt>b</tt> parameter value
    060         * @param c <tt>c</tt> parameter value
    061         * @param d <tt>d</tt> parameter value
    062         *
    063         * @throws IllegalArgumentException if <code>d</code> is 0
    064         */
    065        public GaussianDerivativeFunction(double b, double c, double d) {
    066            if (d == 0.0) {
    067                throw new ZeroException();
    068            }
    069            this.b = b;
    070            this.c = c;
    071            this.d2 = d * d;
    072        }
    073
    074        /**
    075         * Constructs an instance with the specified parameters.
    076         *
    077         * @param parameters <tt>b</tt>, <tt>c</tt>, and <tt>d</tt> parameter values
    078         *
    079         * @throws IllegalArgumentException if <code>parameters</code> is null,
    080         *         <code>parameters</code> length is not 3, or if
    081         *         <code>parameters[2]</code> is 0
    082         */
    083        public GaussianDerivativeFunction(double[] parameters) {
    084            if (parameters == null) {
    085                throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
    086            }
    087            if (parameters.length != 3) {
    088                throw new DimensionMismatchException(3, parameters.length);
    089            }
    090            if (parameters[2] == 0.0) {
    091                throw new ZeroException();
    092            }
    093            this.b = parameters[0];
    094            this.c = parameters[1];
    095            this.d2 = parameters[2] * parameters[2];
    096        }
    097
    098        /** {@inheritDoc} */
    099        public double value(double x) {
    100            final double xMc = x - c;
    101            return (-b / d2) * xMc * Math.exp(-(xMc * xMc) / (2.0 * d2));
    102        }
    103
    104    }