TwiceDifferentiableFunction.java

  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. import org.hipparchus.analysis.MultivariateFunction;
  19. import org.hipparchus.linear.RealMatrix;
  20. import org.hipparchus.linear.RealVector;
  21. import org.hipparchus.linear.ArrayRealVector;

  22. /** A MultivariateFunction that also has a defined gradient and Hessian.
  23.  * @since 3.1
  24.  */
  25. public abstract class TwiceDifferentiableFunction implements MultivariateFunction {
  26.     /**
  27.      * Returns the dimensionality of the function domain.
  28.      * If dim() returns (n) then this function expects an n-vector as its input.
  29.      * @return the expected dimension of the function's domain
  30.      */
  31.     public abstract int dim();

  32.     /**
  33.      * Returns the value of this function at (x)
  34.      *
  35.      * @param x a point to evaluate this function at.
  36.      * @return the value of this function at (x)
  37.      */
  38.     public abstract double value(RealVector x);

  39.     /**
  40.      * Returns the gradient of this function at (x)
  41.      *
  42.      * @param x a point to evaluate this gradient at
  43.      * @return the gradient of this function at (x)
  44.      */
  45.     public abstract RealVector gradient(RealVector x);

  46.     /**
  47.      * The Hessian of this function at (x)
  48.      *
  49.      * @param x a point to evaluate this Hessian at
  50.      * @return the Hessian of this function at (x)
  51.      */
  52.     public abstract RealMatrix hessian(RealVector x);

  53.     /**
  54.      * Returns the value of this function at (x)
  55.      *
  56.      * @param x a point to evaluate this function at.
  57.      * @return the value of this function at (x)
  58.      */
  59.     @Override
  60.     public double value(final double[] x) {
  61.         return value(new ArrayRealVector(x, false));
  62.     }

  63.     /**
  64.      * Returns the gradient of this function at (x)
  65.      *
  66.      * @param x a point to evaluate this gradient at
  67.      * @return the gradient of this function at (x)
  68.      */
  69.     public RealVector gradient(final double[] x) {
  70.         return gradient(new ArrayRealVector(x, false));
  71.     }

  72.     /**
  73.      * The Hessian of this function at (x)
  74.      *
  75.      * @param x a point to evaluate this Hessian at
  76.      * @return the Hessian of this function at (x)
  77.      */
  78.     public RealMatrix hessian(final double[] x) {
  79.         return hessian(new ArrayRealVector(x, false));
  80.     }
  81. }