View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.analysis.differentiation;
23  
24  import org.hipparchus.analysis.MultivariateMatrixFunction;
25  
26  /** Class representing the Jacobian of a multivariate vector function.
27   * <p>
28   * The rows iterate on the model functions while the columns iterate on the parameters; thus,
29   * the numbers of rows is equal to the dimension of the underlying function vector
30   * value and the number of columns is equal to the number of free parameters of
31   * the underlying function.
32   * </p>
33   */
34  public class JacobianFunction implements MultivariateMatrixFunction {
35  
36      /** Underlying vector-valued function. */
37      private final MultivariateDifferentiableVectorFunction f;
38  
39      /** Simple constructor.
40       * @param f underlying vector-valued function
41       */
42      public JacobianFunction(final MultivariateDifferentiableVectorFunction f) {
43          this.f = f;
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public double[][] value(double[] point) {
49  
50          // set up parameters
51          final DSFactory factory = new DSFactory(point.length, 1);
52          final DerivativeStructure[] dsX = new DerivativeStructure[point.length];
53          for (int i = 0; i < point.length; ++i) {
54              dsX[i] = factory.variable(i, point[i]);
55          }
56  
57          // compute the derivatives
58          final DerivativeStructure[] dsY = f.value(dsX);
59  
60          // extract the Jacobian
61          final double[][] y = new double[dsY.length][point.length];
62          final int[] orders = new int[point.length];
63          for (int i = 0; i < dsY.length; ++i) {
64              for (int j = 0; j < point.length; ++j) {
65                  orders[j] = 1;
66                  y[i][j] = dsY[i].getPartialDerivative(orders);
67                  orders[j] = 0;
68              }
69          }
70  
71          return y;
72  
73      }
74  
75  }