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 */
017package org.apache.commons.math3.optim.nonlinear.vector;
018
019import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
020import org.apache.commons.math3.optim.ConvergenceChecker;
021import org.apache.commons.math3.optim.OptimizationData;
022import org.apache.commons.math3.optim.PointVectorValuePair;
023import org.apache.commons.math3.exception.TooManyEvaluationsException;
024import org.apache.commons.math3.exception.DimensionMismatchException;
025
026/**
027 * Base class for implementing optimizers for multivariate vector
028 * differentiable functions.
029 * It contains boiler-plate code for dealing with Jacobian evaluation.
030 * It assumes that the rows of the Jacobian matrix iterate on the model
031 * functions while the columns iterate on the parameters; thus, the numbers
032 * of rows is equal to the dimension of the {@link Target} while the
033 * number of columns is equal to the dimension of the
034 * {@link org.apache.commons.math3.optim.InitialGuess InitialGuess}.
035 *
036 * @since 3.1
037 * @deprecated All classes and interfaces in this package are deprecated.
038 * The optimizers that were provided here were moved to the
039 * {@link org.apache.commons.math3.fitting.leastsquares} package
040 * (cf. MATH-1008).
041 */
042@Deprecated
043public abstract class JacobianMultivariateVectorOptimizer
044    extends MultivariateVectorOptimizer {
045    /**
046     * Jacobian of the model function.
047     */
048    private MultivariateMatrixFunction jacobian;
049
050    /**
051     * @param checker Convergence checker.
052     */
053    protected JacobianMultivariateVectorOptimizer(ConvergenceChecker<PointVectorValuePair> checker) {
054        super(checker);
055    }
056
057    /**
058     * Computes the Jacobian matrix.
059     *
060     * @param params Point at which the Jacobian must be evaluated.
061     * @return the Jacobian at the specified point.
062     */
063    protected double[][] computeJacobian(final double[] params) {
064        return jacobian.value(params);
065    }
066
067    /**
068     * {@inheritDoc}
069     *
070     * @param optData Optimization data. In addition to those documented in
071     * {@link MultivariateVectorOptimizer#optimize(OptimizationData...)}
072     * MultivariateOptimizer}, this method will register the following data:
073     * <ul>
074     *  <li>{@link ModelFunctionJacobian}</li>
075     * </ul>
076     * @return {@inheritDoc}
077     * @throws TooManyEvaluationsException if the maximal number of
078     * evaluations is exceeded.
079     * @throws DimensionMismatchException if the initial guess, target, and weight
080     * arguments have inconsistent dimensions.
081     */
082    @Override
083    public PointVectorValuePair optimize(OptimizationData... optData)
084        throws TooManyEvaluationsException,
085               DimensionMismatchException {
086        // Set up base class and perform computation.
087        return super.optimize(optData);
088    }
089
090    /**
091     * Scans the list of (required and optional) optimization data that
092     * characterize the problem.
093     *
094     * @param optData Optimization data.
095     * The following data will be looked for:
096     * <ul>
097     *  <li>{@link ModelFunctionJacobian}</li>
098     * </ul>
099     */
100    @Override
101    protected void parseOptimizationData(OptimizationData... optData) {
102        // Allow base class to register its own data.
103        super.parseOptimizationData(optData);
104
105        // The existing values (as set by the previous call) are reused if
106        // not provided in the argument list.
107        for (OptimizationData data : optData) {
108            if (data instanceof ModelFunctionJacobian) {
109                jacobian = ((ModelFunctionJacobian) data).getModelFunctionJacobian();
110                // If more data must be parsed, this statement _must_ be
111                // changed to "continue".
112                break;
113            }
114        }
115    }
116}