File: dot_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 (173 lines) | stat: -rw-r--r-- 4,847 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 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 f32_test

import (
	"fmt"
	"math"
	"testing"

	"gonum.org/v1/gonum/floats/scalar"
	. "gonum.org/v1/gonum/internal/asm/f32"
)

var dotTests = []struct {
	x, y     []float32
	sWant    float32 // single-precision
	dWant    float64 // double-precision
	sWantRev float32 // single-precision increment
	dWantRev float64 // double-precision increment
	n        int
	ix, iy   int
}{
	{ // 0
		x:     []float32{},
		y:     []float32{},
		n:     0,
		sWant: 0, dWant: 0,
		sWantRev: 0, dWantRev: 0,
		ix: 0, iy: 0,
	},
	{ // 1
		x:     []float32{0},
		y:     []float32{0},
		n:     1,
		sWant: 0, dWant: 0,
		sWantRev: 0, dWantRev: 0,
		ix: 0, iy: 0,
	},
	{ // 2
		x:     []float32{1},
		y:     []float32{1},
		n:     1,
		sWant: 1, dWant: 1,
		sWantRev: 1, dWantRev: 1,
		ix: 0, iy: 0,
	},
	{ // 3
		x:     []float32{1, 2, 3, 4, 5, 6, 7, 8},
		y:     []float32{2, 2, 2, 2, 2, 2, 2, 2},
		n:     8,
		sWant: 72, dWant: 72,
		sWantRev: 72, dWantRev: 72,
		ix: 1, iy: 1,
	},
	{ // 4
		x:     []float32{math.MaxFloat32},
		y:     []float32{2},
		n:     1,
		sWant: inf, dWant: 2 * float64(math.MaxFloat32),
		sWantRev: inf, dWantRev: 2 * float64(math.MaxFloat32),
		ix: 0, iy: 0,
	},
	{ // 5
		x:     []float32{1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2},
		y:     []float32{3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2},
		n:     20,
		sWant: 70, dWant: 70,
		sWantRev: 80, dWantRev: 80,
		ix: 0, iy: 0,
	},
}

func TestDotUnitary(t *testing.T) {
	const xGdVal, yGdVal = 0.5, 0.25
	for i, test := range dotTests {
		for _, align := range align2 {
			prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
			xgLn, ygLn := 8+align.x, 8+align.y
			xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
			x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
			res := DotUnitary(x, y)
			if !same(res, test.sWant) {
				t.Errorf(msgRes, prefix, res, test.sWant)
			}
			if !isValidGuard(xg, xGdVal, xgLn) {
				t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
			}
			if !isValidGuard(yg, yGdVal, ygLn) {
				t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
			}
		}
	}
}

func TestDotInc(t *testing.T) {
	const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
	for i, test := range dotTests {
		for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
			xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
			x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
			want := test.sWant
			var ix, iy int
			if inc.x < 0 {
				ix = -inc.x * (test.n - 1)
			}
			if inc.y < 0 {
				iy = -inc.y * (test.n - 1)
			}
			if inc.x*inc.y < 0 {
				want = test.sWantRev
			}
			prefix := fmt.Sprintf("Test %v (x:%v y:%v) (ix:%v iy:%v)", i, inc.x, inc.y, ix, iy)
			res := DotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
			if !same(res, want) {
				t.Errorf(msgRes, prefix, res, want)
			}
			checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
			checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
		}
	}
}

func TestDdotUnitary(t *testing.T) {
	const xGdVal, yGdVal = 0.5, 0.25
	for i, test := range dotTests {
		for _, align := range align2 {
			prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
			xgLn, ygLn := 8+align.x, 8+align.y
			xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
			x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
			res := DdotUnitary(x, y)
			if !scalar.Same(res, test.dWant) {
				t.Errorf(msgRes, prefix, res, test.dWant)
			}
			if !isValidGuard(xg, xGdVal, xgLn) {
				t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
			}
			if !isValidGuard(yg, yGdVal, ygLn) {
				t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
			}
		}
	}
}

func TestDdotInc(t *testing.T) {
	const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
	for i, test := range dotTests {
		for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
			prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, inc.x, inc.y)
			xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
			x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
			want := test.dWant
			var ix, iy int
			if inc.x < 0 {
				ix = -inc.x * (test.n - 1)
			}
			if inc.y < 0 {
				iy = -inc.y * (test.n - 1)
			}
			if inc.x*inc.y < 0 {
				want = test.dWantRev
			}
			res := DdotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
			if !scalar.Same(res, want) {
				t.Errorf(msgRes, prefix, res, want)
			}
			checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
			checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
		}
	}
}