View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.optim.nonlinear.vector.constrained;
18  
19  
20  
21  import org.hipparchus.analysis.MultivariateFunction;
22  import org.hipparchus.linear.RealMatrix;
23  import org.hipparchus.linear.RealVector;
24  import org.hipparchus.linear.ArrayRealVector;
25  
26  /** A MultivariateFunction that also has a defined gradient and Hessian.
27   * @since 3.1
28   */
29  public abstract class TwiceDifferentiableFunction implements MultivariateFunction {
30      /**
31       * Returns the dimensionality of the function domain.
32       * If dim() returns (n) then this function expects an n-vector as its input.
33       * @return the expected dimension of the function's domain
34       */
35      public abstract int dim();
36  
37      /**
38       * Returns the value of this function at (x)
39       *
40       * @param x a point to evaluate this function at.
41       * @return the value of this function at (x)
42       */
43      public abstract double value(RealVector x);
44  
45      /**
46       * Returns the gradient of this function at (x)
47       *
48       * @param x a point to evaluate this gradient at
49       * @return the gradient of this function at (x)
50       */
51      public abstract RealVector gradient(RealVector x);
52  
53      /**
54       * The Hessian of this function at (x)
55       *
56       * @param x a point to evaluate this Hessian at
57       * @return the Hessian of this function at (x)
58       */
59      public abstract RealMatrix hessian(RealVector x);
60  
61      /**
62       * Returns the value of this function at (x)
63       *
64       * @param x a point to evaluate this function at.
65       * @return the value of this function at (x)
66       */
67      @Override
68      public double value(final double[] x) {
69          return value(new ArrayRealVector(x, false));
70      }
71  
72      /**
73       * Returns the gradient of this function at (x)
74       *
75       * @param x a point to evaluate this gradient at
76       * @return the gradient of this function at (x)
77       */
78      public RealVector gradient(final double[] x) {
79          return gradient(new ArrayRealVector(x, false));
80      }
81  
82      /**
83       * The Hessian of this function at (x)
84       *
85       * @param x a point to evaluate this Hessian at
86       * @return the Hessian of this function at (x)
87       */
88      public RealMatrix hessian(final double[] x) {
89          return hessian(new ArrayRealVector(x, false));
90      }
91  }