OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / lapack / testlapack / dgelq2.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 Dgelq2er interface {
19         Dgelq2(m, n int, a []float64, lda int, tau, work []float64)
20 }
21
22 func Dgelq2Test(t *testing.T, impl Dgelq2er) {
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                 {1, 12, 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                 {1, 12, 20},
47         } {
48                 n := test.n
49                 m := test.m
50                 lda := test.lda
51                 if lda == 0 {
52                         lda = test.n
53                 }
54                 k := min(m, n)
55                 tau := make([]float64, k)
56                 for i := range tau {
57                         tau[i] = rnd.Float64()
58                 }
59                 work := make([]float64, m)
60                 for i := range work {
61                         work[i] = rnd.Float64()
62                 }
63                 a := make([]float64, m*lda)
64                 for i := 0; i < m*lda; i++ {
65                         a[i] = rnd.Float64()
66                 }
67                 aCopy := make([]float64, len(a))
68                 copy(aCopy, a)
69                 impl.Dgelq2(m, n, a, lda, tau, work)
70
71                 Q := constructQ("LQ", m, n, a, lda, tau)
72
73                 // Check that Q is orthonormal
74                 for i := 0; i < Q.Rows; i++ {
75                         nrm := blas64.Nrm2(Q.Cols, blas64.Vector{Inc: 1, Data: Q.Data[i*Q.Stride:]})
76                         if math.Abs(nrm-1) > 1e-14 {
77                                 t.Errorf("Q not normal. Norm is %v", nrm)
78                         }
79                         for j := 0; j < i; j++ {
80                                 dot := blas64.Dot(Q.Rows,
81                                         blas64.Vector{Inc: 1, Data: Q.Data[i*Q.Stride:]},
82                                         blas64.Vector{Inc: 1, Data: Q.Data[j*Q.Stride:]},
83                                 )
84                                 if math.Abs(dot) > 1e-14 {
85                                         t.Errorf("Q not orthogonal. Dot is %v", dot)
86                                 }
87                         }
88                 }
89
90                 L := blas64.General{
91                         Rows:   m,
92                         Cols:   n,
93                         Stride: n,
94                         Data:   make([]float64, m*n),
95                 }
96                 for i := 0; i < m; i++ {
97                         for j := 0; j <= min(i, n-1); j++ {
98                                 L.Data[i*L.Stride+j] = a[i*lda+j]
99                         }
100                 }
101
102                 ans := blas64.General{
103                         Rows:   m,
104                         Cols:   n,
105                         Stride: lda,
106                         Data:   make([]float64, m*lda),
107                 }
108                 copy(ans.Data, aCopy)
109                 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, L, Q, 0, ans)
110                 if !floats.EqualApprox(aCopy, ans.Data, 1e-14) {
111                         t.Errorf("Case %v, LQ mismatch. Want %v, got %v.", c, aCopy, ans.Data)
112                 }
113         }
114 }