QuadraticFunction.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.exception.LocalizedCoreFormats;
  19. import org.hipparchus.exception.MathIllegalArgumentException;
  20. import org.hipparchus.linear.Array2DRowRealMatrix;
  21. import org.hipparchus.linear.ArrayRealVector;
  22. import org.hipparchus.linear.RealMatrix;
  23. import org.hipparchus.linear.RealVector;
  24. import org.hipparchus.util.MathUtils;

  25. /** Given P, Q, d, implements \(\frac{1}{2}x^T P X + Q^T x + d\).
  26.  * The gradient is P x + Q^T, and the Hessian is P
  27.  * @since 3.1
  28.  */
  29. public class QuadraticFunction extends TwiceDifferentiableFunction {

  30.     /** Square matrix of weights for quadratic terms. */
  31.     private final RealMatrix p;

  32.     /** Vector of weights for linear terms. */
  33.     private final RealVector q;

  34.     /** Constant term. */
  35.     private final double d;

  36.     /** Dimension of the vector. */
  37.     private final int n;

  38.     /** Construct quadratic function \(\frac{1}{2}x^T P X + Q^T x + d\).
  39.      * @param p square matrix of weights for quadratic terms.
  40.      * Typically expected to be positive definite or positive semi-definite.
  41.      * @param q vector of weights for linear terms.
  42.      * @param d constant term
  43.      */
  44.     public QuadraticFunction(RealMatrix p, RealVector q, double d) {
  45.         this.n = q.getDimension();
  46.         if (n < 1) {
  47.             throw new MathIllegalArgumentException(LocalizedCoreFormats.INSUFFICIENT_DIMENSION, d, 1);
  48.         }
  49.         MathUtils.checkDimension(p.getRowDimension(), n);
  50.         this.p = p.copy();
  51.         this.q = q.copy();
  52.         this.d = d;
  53.    }

  54.     /** Construct quadratic function \(\frac{1}{2}x^T P X + Q^T x + d\).
  55.      * @param h square matrix of weights for quadratic terms.
  56.      * Typically expected to be positive definite or positive semi-definite.
  57.      * @param c vector of weights for linear terms.
  58.      * @param d constant term
  59.      */
  60.     public QuadraticFunction(double[][] h, double[] c, double d) {
  61.         this(new Array2DRowRealMatrix(h), new ArrayRealVector(c), d);
  62.     }

  63.     /** Get square matrix of weights for quadratic terms.
  64.      * @return square matrix of weights for quadratic terms
  65.      */
  66.     public RealMatrix getP() {
  67.         return this.p;
  68.     }

  69.     /** Get vector of weights for linear terms.
  70.      * @return vector of weights for linear terms
  71.      */
  72.     public RealVector getQ() {
  73.         return this.q;
  74.     }

  75.     /** Get constant term.
  76.      * @return constant term
  77.      */
  78.     public double getD() {
  79.         return d;
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override
  83.     public int dim() {
  84.         return n;
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public double value(final RealVector x) {
  89.         return 0.5 * p.operate(x).dotProduct(x) + q.dotProduct(x) + d;
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public RealVector gradient(final RealVector x) {
  94.         return p.operate(x).add(q);
  95.     }

  96.     /** {@inheritDoc} */
  97.     @Override
  98.     public RealMatrix hessian(final RealVector x) {
  99.         return p.copy();
  100.     }

  101. }