forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dorg2r.go
79 lines (68 loc) · 1.67 KB
/
dorg2r.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
// Copyright ©2015 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 (
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats/scalar"
)
type Dorg2rer interface {
Dgeqrfer
Dorg2r(m, n, k int, a []float64, lda int, tau []float64, work []float64)
}
func Dorg2rTest(t *testing.T, impl Dorg2rer) {
rnd := rand.New(rand.NewSource(1))
for ti, test := range []struct {
m, n, k, lda int
}{
{3, 3, 0, 0},
{4, 3, 0, 0},
{3, 3, 2, 0},
{4, 3, 2, 0},
{5, 5, 0, 20},
{5, 5, 3, 20},
{10, 5, 0, 20},
{10, 5, 2, 20},
} {
m := test.m
n := test.n
lda := test.lda
if lda == 0 {
lda = test.n
}
// Allocate m×n matrix A and fill it with random numbers.
a := make([]float64, m*lda)
for i := range a {
a[i] = rnd.NormFloat64()
}
// Compute the QR decomposition of A.
tau := make([]float64, min(m, n))
work := make([]float64, 1)
impl.Dgeqrf(m, n, a, lda, tau, work, -1)
work = make([]float64, int(work[0]))
impl.Dgeqrf(m, n, a, lda, tau, work, len(work))
// Compute the matrix Q explicitly using the first k elementary reflectors.
k := test.k
if k == 0 {
k = n
}
q := constructQK("QR", m, n, k, a, lda, tau)
// Compute the matrix Q using Dorg2r.
impl.Dorg2r(m, n, k, a, lda, tau, work)
// Check that the first n columns of both results match.
same := true
loop:
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if !scalar.EqualWithinAbsOrRel(q.Data[i*q.Stride+j], a[i*lda+j], 1e-12, 1e-12) {
same = false
break loop
}
}
}
if !same {
t.Errorf("Case %v: Q mismatch", ti)
}
}
}