Skip to content
This repository has been archived by the owner on Nov 25, 2018. It is now read-only.

Commit

Permalink
fd: move tolerance to Hessian test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-ch committed Mar 30, 2016
1 parent bccebfb commit cfbdb8d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions fd/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,42 @@ func monkeySaddleHess(h *mat64.SymDense, x []float64) {

func TestHessian(t *testing.T) {
rand.Seed(0)
const tol = 1e-4

for i, test := range []struct {
name string
f func([]float64) float64
hess func(h *mat64.SymDense, x []float64)
dim int
tol float64
}{
{
name: "quadratic",
f: quadratic,
hess: quadraticHess,
dim: 1,
tol: 1e-6,
},
{
name: "standard saddle",
f: saddle,
hess: saddleHess,
dim: 2,
tol: 1e-6,
},
{
name: "monkey saddle",
f: monkeySaddle,
hess: monkeySaddleHess,
dim: 2,
tol: 1e-4,
},
} {
for k := 0; k < 10; k++ {
x := randomSlice(test.dim, 10)
want := mat64.NewSymDense(test.dim, nil)
test.hess(want, x)
got := Hessian(nil, test.f, x, nil)
if !mat64.EqualApprox(want, got, tol) {
if !mat64.EqualApprox(want, got, test.tol) {
t.Errorf("case %d (%s): unexpected Hessian with nil dst: want %v, got %v\n", i, test.name, want, got)
}

Expand All @@ -212,7 +215,7 @@ func TestHessian(t *testing.T) {
}
}
Hessian(got, test.f, x, nil)
if !mat64.EqualApprox(want, got, tol) {
if !mat64.EqualApprox(want, got, test.tol) {
t.Errorf("case %d (%s): unexpected Hessian with non-nil dst: want %v, got %v\n", i, test.name, want, got)
}

Expand All @@ -225,7 +228,7 @@ func TestHessian(t *testing.T) {
OriginKnown: true,
OriginValue: test.f(x),
})
if !mat64.EqualApprox(want, got, tol) {
if !mat64.EqualApprox(want, got, test.tol) {
t.Errorf("case %d (%s): unexpected Hessian with known origin: want %v, got %v\n", i, test.name, want, got)
}

Expand All @@ -237,9 +240,23 @@ func TestHessian(t *testing.T) {
Hessian(got, test.f, x, &Settings{
Concurrent: true,
})
if !mat64.EqualApprox(want, got, tol) {
if !mat64.EqualApprox(want, got, test.tol) {
t.Errorf("case %d (%s): unexpected Hessian with concurrency: want %v, got %v\n", i, test.name, want, got)
}

for i := range x {
for j := i; j < test.dim; j++ {
got.SetSym(i, j, math.NaN())
}
}
Hessian(got, test.f, x, &Settings{
Concurrent: true,
OriginKnown: true,
OriginValue: test.f(x),
})
if !mat64.EqualApprox(want, got, test.tol) {
t.Errorf("case %d (%s): unexpected Hessian with known origin and concurrency: want %v, got %v\n", i, test.name, want, got)
}
}
}
}
Expand Down

0 comments on commit cfbdb8d

Please sign in to comment.