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    package org.apache.commons.math3.analysis.differentiation;
018    
019    import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
020    
021    /** Class representing the Jacobian of a multivariate vector function.
022     * <p>
023     * The rows iterate on the model functions while the columns iterate on the parameters; thus,
024     * the numbers of rows is equal to the dimension of the underlying function vector
025     * value and the number of columns is equal to the number of free parameters of
026     * the underlying function.
027     * </p>
028     * @version $Id: JacobianFunction.java 1416643 2012-12-03 19:37:14Z tn $
029     * @since 3.1
030     */
031    public class JacobianFunction implements MultivariateMatrixFunction {
032    
033        /** Underlying vector-valued function. */
034        private final MultivariateDifferentiableVectorFunction f;
035    
036        /** Simple constructor.
037         * @param f underlying vector-valued function
038         */
039        public JacobianFunction(final MultivariateDifferentiableVectorFunction f) {
040            this.f = f;
041        }
042    
043        /** {@inheritDoc} */
044        public double[][] value(double[] point)
045            throws IllegalArgumentException {
046    
047            // set up parameters
048            final DerivativeStructure[] dsX = new DerivativeStructure[point.length];
049            for (int i = 0; i < point.length; ++i) {
050                dsX[i] = new DerivativeStructure(point.length, 1, i, point[i]);
051            }
052    
053            // compute the derivatives
054            final DerivativeStructure[] dsY = f.value(dsX);
055    
056            // extract the Jacobian
057            final double[][] y = new double[dsY.length][point.length];
058            final int[] orders = new int[point.length];
059            for (int i = 0; i < dsY.length; ++i) {
060                for (int j = 0; j < point.length; ++j) {
061                    orders[j] = 1;
062                    y[i][j] = dsY[i].getPartialDerivative(orders);
063                    orders[j] = 0;
064                }
065            }
066    
067            return y;
068    
069        }
070    
071    }