gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dorg2r.go (about) 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 "testing" 9 10 "golang.org/x/exp/rand" 11 12 "gonum.org/v1/gonum/floats/scalar" 13 ) 14 15 type Dorg2rer interface { 16 Dgeqrfer 17 Dorg2r(m, n, k int, a []float64, lda int, tau []float64, work []float64) 18 } 19 20 func Dorg2rTest(t *testing.T, impl Dorg2rer) { 21 rnd := rand.New(rand.NewSource(1)) 22 for ti, test := range []struct { 23 m, n, k, lda int 24 }{ 25 {3, 3, 0, 0}, 26 {4, 3, 0, 0}, 27 {3, 3, 2, 0}, 28 {4, 3, 2, 0}, 29 30 {5, 5, 0, 20}, 31 {5, 5, 3, 20}, 32 {10, 5, 0, 20}, 33 {10, 5, 2, 20}, 34 } { 35 m := test.m 36 n := test.n 37 lda := test.lda 38 if lda == 0 { 39 lda = test.n 40 } 41 // Allocate m×n matrix A and fill it with random numbers. 42 a := make([]float64, m*lda) 43 for i := range a { 44 a[i] = rnd.NormFloat64() 45 } 46 47 // Compute the QR decomposition of A. 48 tau := make([]float64, min(m, n)) 49 work := make([]float64, 1) 50 impl.Dgeqrf(m, n, a, lda, tau, work, -1) 51 work = make([]float64, int(work[0])) 52 impl.Dgeqrf(m, n, a, lda, tau, work, len(work)) 53 54 // Compute the matrix Q explicitly using the first k elementary reflectors. 55 k := test.k 56 if k == 0 { 57 k = n 58 } 59 q := constructQK("QR", m, n, k, a, lda, tau) 60 61 // Compute the matrix Q using Dorg2r. 62 impl.Dorg2r(m, n, k, a, lda, tau, work) 63 64 // Check that the first n columns of both results match. 65 same := true 66 loop: 67 for i := 0; i < m; i++ { 68 for j := 0; j < n; j++ { 69 if !scalar.EqualWithinAbsOrRel(q.Data[i*q.Stride+j], a[i*lda+j], 1e-12, 1e-12) { 70 same = false 71 break loop 72 } 73 } 74 } 75 if !same { 76 t.Errorf("Case %v: Q mismatch", ti) 77 } 78 } 79 }