/
dlauu2.go
112 lines (98 loc) · 2.89 KB
/
dlauu2.go
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 ©2018 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 testlapack
import (
"fmt"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/blas"
"gonum.org/v1/gonum/blas/blas64"
)
type Dlauu2er interface {
Dlauu2(uplo blas.Uplo, n int, a []float64, lda int)
}
func Dlauu2Test(t *testing.T, impl Dlauu2er) {
for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower} {
name := "Upper"
if uplo == blas.Lower {
name = "Lower"
}
t.Run(name, func(t *testing.T) {
ns := []int{0, 1, 2, 3, 4, 5, 10, 25}
dlauuTest(t, impl.Dlauu2, uplo, ns)
})
}
}
func dlauuTest(t *testing.T, dlauu func(blas.Uplo, int, []float64, int), uplo blas.Uplo, ns []int) {
const tol = 1e-13
bi := blas64.Implementation()
rnd := rand.New(rand.NewSource(1))
for _, n := range ns {
for _, lda := range []int{max(1, n), n + 11} {
prefix := fmt.Sprintf("n=%v,lda=%v", n, lda)
// Allocate n×n matrix A and fill it with random numbers.
// Only its uplo triangle will be used below.
a := make([]float64, n*lda)
for i := range a {
a[i] = rnd.NormFloat64()
}
// Create a copy of A.
aCopy := make([]float64, len(a))
copy(aCopy, a)
// Compute U*Uᵀ or Lᵀ*L using Dlauu?.
dlauu(uplo, n, a, lda)
if n == 0 {
continue
}
// * Check that the triangle of A opposite to uplo has not been modified.
// * Convert the result of Dlauu? into a dense symmetric matrix.
// * Zero out the triangle in aCopy opposite to uplo.
if uplo == blas.Upper {
if !sameLowerTri(n, aCopy, lda, a, lda) {
t.Errorf("%v: unexpected modification in lower triangle", prefix)
continue
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
a[i*lda+j] = a[j*lda+i]
aCopy[i*lda+j] = 0
}
}
} else {
if !sameUpperTri(n, aCopy, lda, a, lda) {
t.Errorf("%v: unexpected modification in upper triangle", prefix)
continue
}
for i := 0; i < n-1; i++ {
for j := i + 1; j < n; j++ {
a[i*lda+j] = a[j*lda+i]
aCopy[i*lda+j] = 0
}
}
}
// Compute U*Uᵀ or Lᵀ*L using Dgemm with U and L
// represented as dense triangular matrices.
ldwant := n
want := make([]float64, n*ldwant)
if uplo == blas.Upper {
// Use aCopy as a dense representation of the upper triangular U.
u := aCopy
ldu := lda
// Compute U * Uᵀ and store the result into want.
bi.Dgemm(blas.NoTrans, blas.Trans, n, n, n,
1, u, ldu, u, ldu, 0, want, ldwant)
} else {
// Use aCopy as a dense representation of the lower triangular L.
l := aCopy
ldl := lda
// Compute Lᵀ * L and store the result into want.
bi.Dgemm(blas.Trans, blas.NoTrans, n, n, n,
1, l, ldl, l, ldl, 0, want, ldwant)
}
if !equalApprox(n, n, a, lda, want, tol) {
t.Errorf("%v: unexpected result", prefix)
}
}
}
}