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.
11 "golang.org/x/exp/rand"
13 "gonum.org/v1/gonum/blas"
14 "gonum.org/v1/gonum/blas/blas64"
15 "gonum.org/v1/gonum/floats"
18 type Dgelq2er interface {
19 Dgelq2(m, n int, a []float64, lda int, tau, work []float64)
22 func Dgelq2Test(t *testing.T, impl Dgelq2er) {
23 rnd := rand.New(rand.NewSource(1))
24 for c, test := range []struct {
55 tau := make([]float64, k)
57 tau[i] = rnd.Float64()
59 work := make([]float64, m)
61 work[i] = rnd.Float64()
63 a := make([]float64, m*lda)
64 for i := 0; i < m*lda; i++ {
67 aCopy := make([]float64, len(a))
69 impl.Dgelq2(m, n, a, lda, tau, work)
71 Q := constructQ("LQ", m, n, a, lda, tau)
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)
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:]},
84 if math.Abs(dot) > 1e-14 {
85 t.Errorf("Q not orthogonal. Dot is %v", dot)
94 Data: make([]float64, m*n),
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]
102 ans := blas64.General{
106 Data: make([]float64, m*lda),
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)