github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dorg2l.go (about)

     1  // Copyright ©2016 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  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/gonum/blas/blas64"
    13  )
    14  
    15  type Dorg2ler interface {
    16  	Dorg2l(m, n, k int, a []float64, lda int, tau, work []float64)
    17  	Dgeql2er
    18  }
    19  
    20  func Dorg2lTest(t *testing.T, impl Dorg2ler) {
    21  	rnd := rand.New(rand.NewSource(1))
    22  	for _, test := range []struct {
    23  		m, n, k, lda int
    24  	}{
    25  		{5, 4, 3, 0},
    26  		{5, 4, 4, 0},
    27  		{3, 3, 2, 0},
    28  		{5, 5, 5, 0},
    29  	} {
    30  		m := test.m
    31  		n := test.n
    32  		k := test.k
    33  		lda := test.lda
    34  		if lda == 0 {
    35  			lda = n
    36  		}
    37  
    38  		a := make([]float64, m*lda)
    39  		for i := range a {
    40  			a[i] = rnd.NormFloat64()
    41  		}
    42  		tau := nanSlice(max(m, n))
    43  		work := make([]float64, n)
    44  		impl.Dgeql2(m, n, a, lda, tau, work)
    45  
    46  		aCopy := make([]float64, len(a))
    47  		copy(aCopy, a)
    48  		impl.Dorg2l(m, n, k, a, lda, tau[n-k:], work)
    49  		if !hasOrthonormalColumns(m, n, a, lda) {
    50  			t.Errorf("Q is not orthonormal. m = %v, n = %v, k = %v", m, n, k)
    51  		}
    52  	}
    53  }
    54  
    55  // hasOrthornormalColumns checks that the columns of a are orthonormal.
    56  func hasOrthonormalColumns(m, n int, a []float64, lda int) bool {
    57  	for i := 0; i < n; i++ {
    58  		for j := i; j < n; j++ {
    59  			dot := blas64.Dot(m,
    60  				blas64.Vector{Inc: lda, Data: a[i:]},
    61  				blas64.Vector{Inc: lda, Data: a[j:]},
    62  			)
    63  			if i == j {
    64  				if math.Abs(dot-1) > 1e-10 {
    65  					return false
    66  				}
    67  			} else {
    68  				if math.Abs(dot) > 1e-10 {
    69  					return false
    70  				}
    71  			}
    72  		}
    73  	}
    74  	return true
    75  }