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
|
// run
// Copyright 2018 The Go 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 main
const (
Upper = true
blas_Upper = 121
badTriangle = "bad triangle"
)
// Triangular represents a triangular matrix. Triangular matrices are always square.
type Triangular interface {
// Triangular returns the number of rows/columns in the matrix and its
// orientation.
Tryangle() (mmmm int, kynd bool)
Triangle() (mmmm int, kynd bool)
}
// blas64_Triangular represents a triangular matrix using the conventional storage scheme.
type blas64_Triangular struct {
Stride int
Uplo int
}
// TriDense represents an upper or lower triangular matrix in dense storage
// format.
type TriDense struct {
mat blas64_Triangular
}
func NewTriDense() *TriDense {
return &TriDense{
mat: blas64_Triangular{
Stride: 3,
Uplo: blas_Upper,
},
}
}
func (t *TriDense) isUpper() bool {
return isUpperUplo(t.mat.Uplo)
}
func (t *TriDense) triKind() bool {
return isUpperUplo(t.mat.Uplo)
}
func isUpperUplo(u int) bool {
switch u {
case blas_Upper:
return true
default:
panic(badTriangle)
}
}
func (t *TriDense) IsZero() bool {
return t.mat.Stride == 0
}
//go:noinline
func (t *TriDense) ScaleTri(f float64, a Triangular) {
n, kind := a.Triangle()
if kind == false {
println("ScaleTri n, kind=", n, ", ", kind, " (FAIL, expected true)")
}
}
//go:noinline
func (t *TriDense) ScaleTry(f float64, a Triangular) {
n, kind := a.Tryangle()
if kind == false {
println("ScaleTry n, kind=", n, ", ", kind, " (FAIL, expected true)")
}
}
// Triangle failed (before fix)
func (t *TriDense) Triangle() (nnnn int, kind bool) {
return 3, !t.IsZero() && t.triKind()
}
// Tryangle works -- difference is not-named output parameters.
func (t *TriDense) Tryangle() (int, bool) {
return 3, !t.IsZero() && t.triKind()
}
func main() {
ta := NewTriDense()
n, kind := ta.Triangle()
if kind == false {
println(" main n, kind=", n, ", ", kind, " (FAIL, expected true)")
}
ta.ScaleTri(1, ta)
ta.ScaleTry(1, ta)
}
|