diff/fd: implement Hessian finite difference, and code cleanups. (#109)

* diff/fd: implement Hessian finite difference, and code cleanups.

This commit primarily adds the Hessian function for finding a finite difference approximation to the Hessian. At the same time, it combines duplicated functionality across the difference routines so that the preludes to all the difference routines look similar
This commit is contained in:
Brendan Tracey
2017-07-28 13:46:27 -06:00
committed by GitHub
parent ffd939f8ca
commit eeb363530d
9 changed files with 815 additions and 227 deletions

View File

@@ -0,0 +1,87 @@
// Copyright ©2017 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fd
import (
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/mat"
)
// ConstFunc is a constant function returning the value held by the type.
type ConstFunc float64
func (c ConstFunc) Func(x []float64) float64 {
return float64(c)
}
func (c ConstFunc) Grad(grad, x []float64) {
for i := range grad {
grad[i] = 0
}
}
func (c ConstFunc) Hess(dst mat.MutableSymmetric, x []float64) {
n := len(x)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
dst.SetSym(i, j, 0)
}
}
}
// LinearFunc is a linear function returning w*x+c.
type LinearFunc struct {
w []float64
c float64
}
func (l LinearFunc) Func(x []float64) float64 {
return floats.Dot(l.w, x) + l.c
}
func (l LinearFunc) Grad(grad, x []float64) {
copy(grad, l.w)
}
func (l LinearFunc) Hess(dst mat.MutableSymmetric, x []float64) {
n := len(x)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
dst.SetSym(i, j, 0)
}
}
}
// QuadFunc is a quadratic function returning 0.5*x'*a*x + b*x + c.
type QuadFunc struct {
a *mat.SymDense
b *mat.Vector
c float64
}
func (q QuadFunc) Func(x []float64) float64 {
v := mat.NewVector(len(x), x)
var tmp mat.Vector
tmp.MulVec(q.a, v)
return 0.5*mat.Dot(&tmp, v) + mat.Dot(q.b, v) + q.c
}
func (q QuadFunc) Grad(grad, x []float64) {
var tmp mat.Vector
v := mat.NewVector(len(x), x)
tmp.MulVec(q.a, v)
for i := range grad {
grad[i] = tmp.At(i, 0) + q.b.At(i, 0)
}
}
func (q QuadFunc) Hess(dst mat.MutableSymmetric, x []float64) {
n := len(x)
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
dst.SetSym(i, j, q.a.At(i, j))
}
}
}