JacobianFunction.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.differentiation;

  22. import org.hipparchus.analysis.MultivariateMatrixFunction;

  23. /** Class representing the Jacobian of a multivariate vector function.
  24.  * <p>
  25.  * The rows iterate on the model functions while the columns iterate on the parameters; thus,
  26.  * the numbers of rows is equal to the dimension of the underlying function vector
  27.  * value and the number of columns is equal to the number of free parameters of
  28.  * the underlying function.
  29.  * </p>
  30.  */
  31. public class JacobianFunction implements MultivariateMatrixFunction {

  32.     /** Underlying vector-valued function. */
  33.     private final MultivariateDifferentiableVectorFunction f;

  34.     /** Simple constructor.
  35.      * @param f underlying vector-valued function
  36.      */
  37.     public JacobianFunction(final MultivariateDifferentiableVectorFunction f) {
  38.         this.f = f;
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public double[][] value(double[] point) {

  43.         // set up parameters
  44.         final DSFactory factory = new DSFactory(point.length, 1);
  45.         final DerivativeStructure[] dsX = new DerivativeStructure[point.length];
  46.         for (int i = 0; i < point.length; ++i) {
  47.             dsX[i] = factory.variable(i, point[i]);
  48.         }

  49.         // compute the derivatives
  50.         final DerivativeStructure[] dsY = f.value(dsX);

  51.         // extract the Jacobian
  52.         final double[][] y = new double[dsY.length][point.length];
  53.         final int[] orders = new int[point.length];
  54.         for (int i = 0; i < dsY.length; ++i) {
  55.             for (int j = 0; j < point.length; ++j) {
  56.                 orders[j] = 1;
  57.                 y[i][j] = dsY[i].getPartialDerivative(orders);
  58.                 orders[j] = 0;
  59.             }
  60.         }

  61.         return y;

  62.     }

  63. }