1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }