OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / lapack / testlapack / dgeqr2.go
1 // Copyright ©2015 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package testlapack
6
7 import (
8         "math"
9         "testing"
10
11         "golang.org/x/exp/rand"
12
13         "gonum.org/v1/gonum/blas"
14         "gonum.org/v1/gonum/blas/blas64"
15         "gonum.org/v1/gonum/floats"
16 )
17
18 type Dgeqr2er interface {
19         Dgeqr2(m, n int, a []float64, lda int, tau []float64, work []float64)
20 }
21
22 func Dgeqr2Test(t *testing.T, impl Dgeqr2er) {
23         rnd := rand.New(rand.NewSource(1))
24         for c, test := range []struct {
25                 m, n, lda int
26         }{
27                 {1, 1, 0},
28                 {2, 2, 0},
29                 {3, 2, 0},
30                 {2, 3, 0},
31                 {1, 12, 0},
32                 {2, 6, 0},
33                 {3, 4, 0},
34                 {4, 3, 0},
35                 {6, 2, 0},
36                 {12, 1, 0},
37                 {1, 1, 20},
38                 {2, 2, 20},
39                 {3, 2, 20},
40                 {2, 3, 20},
41                 {1, 12, 20},
42                 {2, 6, 20},
43                 {3, 4, 20},
44                 {4, 3, 20},
45                 {6, 2, 20},
46                 {12, 1, 20},
47         } {
48                 n := test.n
49                 m := test.m
50                 lda := test.lda
51                 if lda == 0 {
52                         lda = test.n
53                 }
54                 a := make([]float64, m*lda)
55                 for i := range a {
56                         a[i] = rnd.Float64()
57                 }
58                 aCopy := make([]float64, len(a))
59                 k := min(m, n)
60                 tau := make([]float64, k)
61                 for i := range tau {
62                         tau[i] = rnd.Float64()
63                 }
64                 work := make([]float64, n)
65                 for i := range work {
66                         work[i] = rnd.Float64()
67                 }
68                 copy(aCopy, a)
69                 impl.Dgeqr2(m, n, a, lda, tau, work)
70
71                 // Test that the QR factorization has completed successfully. Compute
72                 // Q based on the vectors.
73                 q := constructQ("QR", m, n, a, lda, tau)
74
75                 // Check that q is orthonormal
76                 for i := 0; i < m; i++ {
77                         nrm := blas64.Nrm2(m, blas64.Vector{Inc: 1, Data: q.Data[i*m:]})
78                         if math.Abs(nrm-1) > 1e-14 {
79                                 t.Errorf("Case %v, q not normal", c)
80                         }
81                         for j := 0; j < i; j++ {
82                                 dot := blas64.Dot(m, blas64.Vector{Inc: 1, Data: q.Data[i*m:]}, blas64.Vector{Inc: 1, Data: q.Data[j*m:]})
83                                 if math.Abs(dot) > 1e-14 {
84                                         t.Errorf("Case %v, q not orthogonal", c)
85                                 }
86                         }
87                 }
88                 // Check that A = Q * R
89                 r := blas64.General{
90                         Rows:   m,
91                         Cols:   n,
92                         Stride: n,
93                         Data:   make([]float64, m*n),
94                 }
95                 for i := 0; i < m; i++ {
96                         for j := i; j < n; j++ {
97                                 r.Data[i*n+j] = a[i*lda+j]
98                         }
99                 }
100                 atmp := blas64.General{
101                         Rows:   m,
102                         Cols:   n,
103                         Stride: lda,
104                         Data:   make([]float64, m*lda),
105                 }
106                 copy(atmp.Data, a)
107                 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, q, r, 0, atmp)
108                 if !floats.EqualApprox(atmp.Data, aCopy, 1e-14) {
109                         t.Errorf("Q*R != a")
110                 }
111         }
112 }