github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/testlapack/dgelqf.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  	"github.com/jingcheng-WU/gonum/floats"
    13  )
    14  
    15  type Dgelqfer interface {
    16  	Dgelq2er
    17  	Dgelqf(m, n int, a []float64, lda int, tau, work []float64, lwork int)
    18  }
    19  
    20  func DgelqfTest(t *testing.T, impl Dgelqfer) {
    21  	const tol = 1e-12
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for c, test := range []struct {
    24  		m, n, lda int
    25  	}{
    26  		{10, 5, 0},
    27  		{5, 10, 0},
    28  		{10, 10, 0},
    29  		{300, 5, 0},
    30  		{3, 500, 0},
    31  		{200, 200, 0},
    32  		{300, 200, 0},
    33  		{204, 300, 0},
    34  		{1, 3000, 0},
    35  		{3000, 1, 0},
    36  		{10, 5, 30},
    37  		{5, 10, 30},
    38  		{10, 10, 30},
    39  		{300, 5, 500},
    40  		{3, 500, 600},
    41  		{200, 200, 300},
    42  		{300, 200, 300},
    43  		{204, 300, 400},
    44  		{1, 3000, 4000},
    45  		{3000, 1, 4000},
    46  	} {
    47  		m := test.m
    48  		n := test.n
    49  		lda := test.lda
    50  		if lda == 0 {
    51  			lda = n
    52  		}
    53  		// Allocate m×n matrix A and fill it with random numbers.
    54  		a := make([]float64, m*lda)
    55  		for i := range a {
    56  			a[i] = rnd.NormFloat64()
    57  		}
    58  		// Store a copy of A for later comparison.
    59  		aCopy := make([]float64, len(a))
    60  		copy(aCopy, a)
    61  
    62  		// Allocate a slice for scalar factors of elementary reflectors
    63  		// and fill it with random numbers.
    64  		tau := make([]float64, n)
    65  		for i := 0; i < n; i++ {
    66  			tau[i] = rnd.NormFloat64()
    67  		}
    68  
    69  		// Compute the expected result using unblocked LQ algorithm and
    70  		// store it want.
    71  		want := make([]float64, len(a))
    72  		copy(want, a)
    73  		impl.Dgelq2(m, n, want, lda, tau, make([]float64, m))
    74  
    75  		for _, wl := range []worklen{minimumWork, mediumWork, optimumWork} {
    76  			copy(a, aCopy)
    77  
    78  			var lwork int
    79  			switch wl {
    80  			case minimumWork:
    81  				lwork = m
    82  			case mediumWork:
    83  				work := make([]float64, 1)
    84  				impl.Dgelqf(m, n, a, lda, tau, work, -1)
    85  				lwork = int(work[0]) - 2*m
    86  			case optimumWork:
    87  				work := make([]float64, 1)
    88  				impl.Dgelqf(m, n, a, lda, tau, work, -1)
    89  				lwork = int(work[0])
    90  			}
    91  			work := make([]float64, lwork)
    92  
    93  			// Compute the LQ factorization of A.
    94  			impl.Dgelqf(m, n, a, lda, tau, work, len(work))
    95  			// Compare the result with Dgelq2.
    96  			if !floats.EqualApprox(want, a, tol) {
    97  				t.Errorf("Case %v, workspace type %v, unexpected result", c, wl)
    98  			}
    99  		}
   100  	}
   101  }