File: crosslaplacian_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (112 lines) | stat: -rw-r--r-- 2,559 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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 (
	"testing"

	"gonum.org/v1/gonum/floats/scalar"
	"gonum.org/v1/gonum/mat"
)

type CrossLaplacianTester interface {
	Func(x, y []float64) float64
	CrossLaplacian(x, y []float64) float64
}

type WrapperCL struct {
	Tester HessianTester
}

func (WrapperCL) constructZ(x, y []float64) []float64 {
	z := make([]float64, len(x)+len(y))
	copy(z, x)
	copy(z[len(x):], y)
	return z
}

func (w WrapperCL) Func(x, y []float64) float64 {
	z := w.constructZ(x, y)
	return w.Tester.Func(z)
}

func (w WrapperCL) CrossLaplacian(x, y []float64) float64 {
	z := w.constructZ(x, y)
	hess := mat.NewSymDense(len(z), nil)
	w.Tester.Hess(hess, z)
	// The CrossLaplacian is the trace of the off-diagonal block of the Hessian.
	var l float64
	for i := 0; i < len(x); i++ {
		l += hess.At(i, i+len(x))
	}
	return l
}

func TestCrossLaplacian(t *testing.T) {
	t.Parallel()
	for cas, test := range []struct {
		l        CrossLaplacianTester
		x, y     []float64
		settings *Settings
		tol      float64
	}{
		{
			l:   WrapperCL{Watson{}},
			x:   []float64{0.2, 0.3},
			y:   []float64{0.1, 0.4},
			tol: 1e-3,
		},
		{
			l:   WrapperCL{Watson{}},
			x:   []float64{2, 3, 1},
			y:   []float64{1, 4, 1},
			tol: 1e-3,
		},
		{
			l:   WrapperCL{ConstFunc(6)},
			x:   []float64{2, -3, 1},
			y:   []float64{1, 4, -5},
			tol: 1e-6,
		},
		{
			l:   WrapperCL{LinearFunc{w: []float64{10, 6, -1, 5}, c: 5}},
			x:   []float64{3, 1},
			y:   []float64{8, 6},
			tol: 1e-6,
		},
		{
			l: WrapperCL{QuadFunc{
				a: mat.NewSymDense(4, []float64{
					10, 2, 1, 9,
					2, 5, -3, 4,
					1, -3, 6, 2,
					9, 4, 2, -14,
				}),
				b: mat.NewVecDense(4, []float64{3, -2, -1, 4}),
				c: 5,
			}},
			x:   []float64{-1.6, -3},
			y:   []float64{1.8, 3.4},
			tol: 1e-6,
		},
	} {
		got := CrossLaplacian(test.l.Func, test.x, test.y, test.settings)
		want := test.l.CrossLaplacian(test.x, test.y)
		if !scalar.EqualWithinAbsOrRel(got, want, test.tol, test.tol) {
			t.Errorf("Cas %d: CrossLaplacian mismatch serial. got %v, want %v", cas, got, want)
		}

		// Test that concurrency works.
		settings := test.settings
		if settings == nil {
			settings = &Settings{}
		}
		settings.Concurrent = true
		got2 := CrossLaplacian(test.l.Func, test.x, test.y, settings)
		if !scalar.EqualWithinAbsOrRel(got, got2, 1e-6, 1e-6) {
			t.Errorf("Cas %d: Laplacian mismatch. got %v, want %v", cas, got2, got)
		}
	}
}