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  import org.hipparchus.exception.LocalizedCoreFormats;
20  import org.hipparchus.exception.MathIllegalArgumentException;
21  import org.hipparchus.linear.Array2DRowRealMatrix;
22  import org.hipparchus.linear.ArrayRealVector;
23  import org.hipparchus.linear.RealMatrix;
24  import org.hipparchus.linear.RealVector;
25  import org.hipparchus.util.MathUtils;
26  
27  /** Given P, Q, d, implements \(\frac{1}{2}x^T P X + Q^T x + d\).
28   * The gradient is P x + Q^T, and the Hessian is P
29   * @since 3.1
30   */
31  public class QuadraticFunction extends TwiceDifferentiableFunction {
32  
33      /** Square matrix of weights for quadratic terms. */
34      private final RealMatrix p;
35  
36      /** Vector of weights for linear terms. */
37      private final RealVector q;
38  
39      /** Constant term. */
40      private final double d;
41  
42      /** Dimension of the vector. */
43      private final int n;
44  
45      /** Construct quadratic function \(\frac{1}{2}x^T P X + Q^T x + d\).
46       * @param p square matrix of weights for quadratic terms.
47       * Typically expected to be positive definite or positive semi-definite.
48       * @param q vector of weights for linear terms.
49       * @param d constant term
50       */
51      public QuadraticFunction(RealMatrix p, RealVector q, double d) {
52          this.n = q.getDimension();
53          if (n < 1) {
54              throw new MathIllegalArgumentException(LocalizedCoreFormats.INSUFFICIENT_DIMENSION, d, 1);
55          }
56          MathUtils.checkDimension(p.getRowDimension(), n);
57          this.p = p.copy();
58          this.q = q.copy();
59          this.d = d;
60     }
61  
62      /** Construct quadratic function \(\frac{1}{2}x^T P X + Q^T x + d\).
63       * @param h square matrix of weights for quadratic terms.
64       * Typically expected to be positive definite or positive semi-definite.
65       * @param c vector of weights for linear terms.
66       * @param d constant term
67       */
68      public QuadraticFunction(double[][] h, double[] c, double d) {
69          this(new Array2DRowRealMatrix(h), new ArrayRealVector(c), d);
70      }
71  
72      /** Get square matrix of weights for quadratic terms.
73       * @return square matrix of weights for quadratic terms
74       */
75      public RealMatrix getP() {
76          return this.p;
77      }
78  
79      /** Get vector of weights for linear terms.
80       * @return vector of weights for linear terms
81       */
82      public RealVector getQ() {
83          return this.q;
84      }
85  
86      /** Get constant term.
87       * @return constant term
88       */
89      public double getD() {
90          return d;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public int dim() {
96          return n;
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public double value(final RealVector x) {
102         return 0.5 * p.operate(x).dotProduct(x) + q.dotProduct(x) + d;
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public RealVector gradient(final RealVector x) {
108         return p.operate(x).add(q);
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public RealMatrix hessian(final RealVector x) {
114         return p.copy();
115     }
116 
117 }