func (Watson) Hess(hess mat64.MutableSymmetric, x []float64) { dim := len(x) if dim != hess.Symmetric() { panic("incorrect size of the Hessian") } for j := 0; j < dim; j++ { for k := j; k < dim; k++ { hess.SetSym(j, k, 0) } } for i := 1; i <= 29; i++ { d1 := float64(i) / 29 d2 := 1.0 var s1 float64 for j := 1; j < dim; j++ { s1 += float64(j) * d2 * x[j] d2 *= d1 } d2 = 1.0 var s2 float64 for _, v := range x { s2 += d2 * v d2 *= d1 } t := s1 - s2*s2 - 1 s3 := 2 * d1 * s2 d2 = 2 / d1 th := 2 * d1 * d1 * t for j := 0; j < dim; j++ { v := float64(j) - s3 d3 := 1 / d1 for k := 0; k <= j; k++ { hess.SetSym(k, j, hess.At(k, j)+d2*d3*(v*(float64(k)-s3)-th)) d3 *= d1 } d2 *= d1 } } t1 := x[1] - x[0]*x[0] - 1 hess.SetSym(0, 0, hess.At(0, 0)+8*x[0]*x[0]+2-4*t1) hess.SetSym(0, 1, hess.At(0, 1)-4*x[0]) hess.SetSym(1, 1, hess.At(1, 1)+2) }
func (BrownAndDennis) Hess(hess mat64.MutableSymmetric, x []float64) { if len(x) != 4 { panic("dimension of the problem must be 4") } if len(x) != hess.Symmetric() { panic("incorrect size of the Hessian") } for i := 0; i < 4; i++ { for j := i; j < 4; j++ { hess.SetSym(i, j, 0) } } for i := 1; i <= 20; i++ { d1 := float64(i) / 5 d2 := math.Sin(d1) t1 := x[0] + d1*x[1] - math.Exp(d1) t2 := x[2] + d2*x[3] - math.Cos(d1) t := t1*t1 + t2*t2 s3 := 2 * t1 * t2 r1 := t + 2*t1*t1 r2 := t + 2*t2*t2 hess.SetSym(0, 0, hess.At(0, 0)+r1) hess.SetSym(0, 1, hess.At(0, 1)+d1*r1) hess.SetSym(1, 1, hess.At(1, 1)+d1*d1*r1) hess.SetSym(0, 2, hess.At(0, 2)+s3) hess.SetSym(1, 2, hess.At(1, 2)+d1*s3) hess.SetSym(2, 2, hess.At(2, 2)+r2) hess.SetSym(0, 3, hess.At(0, 3)+d2*s3) hess.SetSym(1, 3, hess.At(1, 3)+d1*d2*s3) hess.SetSym(2, 3, hess.At(2, 3)+d2*r2) hess.SetSym(3, 3, hess.At(3, 3)+d2*d2*r2) } for i := 0; i < 4; i++ { for j := i; j < 4; j++ { hess.SetSym(i, j, 4*hess.At(i, j)) } } }