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.linear.Array2DRowRealMatrix;
20  import org.hipparchus.linear.ArrayRealVector;
21  import org.hipparchus.linear.RealMatrix;
22  import org.hipparchus.linear.RealVector;
23  
24  public class RosenbrockFunction extends TwiceDifferentiableFunction {
25  
26      @Override
27      public int dim() {
28          return 2;
29      }
30  
31      @Override
32      public double value(RealVector x) {
33          return (1.0-x.getEntry(0))*(1.0-x.getEntry(0))+100.0*(x.getEntry(1)-x.getEntry(0)*x.getEntry(0))*(x.getEntry(1)-x.getEntry(0)*x.getEntry(0));
34      }
35  
36      @Override
37      public RealVector gradient(RealVector x) {
38          RealVector grad=new ArrayRealVector(x.getDimension());
39          grad.setEntry(0,-2.0*(1.0-x.getEntry(0))-400.0*(x.getEntry(1)-x.getEntry(0)*x.getEntry(0))*x.getEntry(0));
40          grad.setEntry(1,200.0*(x.getEntry(1)-x.getEntry(0)*x.getEntry(0)));
41          return grad;
42      }
43  
44      @Override
45      public RealMatrix hessian(RealVector x) {
46          RealMatrix h=new Array2DRowRealMatrix(2,2);
47          double a00=800.0*x.getEntry(0)*x.getEntry(0)-400.0*(x.getEntry(1)-x.getEntry(0)*x.getEntry(0))+2;
48          double a01=-400.0*x.getEntry(0);
49          double a10=-400*x.getEntry(0);
50          double a11=200;
51          h.setEntry(0,0, a00);
52          h.setEntry(0,1, a01);
53          h.setEntry(1,0, a10);
54          h.setEntry(1,1, a11);
55          return h;
56      }
57  
58  }