gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dlapll.go (about)

     1  // Copyright ©2017 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  	"gonum.org/v1/gonum/lapack"
    14  )
    15  
    16  type Dlapller interface {
    17  	Dgesvder
    18  	Dlapll(n int, x []float64, incX int, y []float64, incY int) float64
    19  }
    20  
    21  func DlapllTest(t *testing.T, impl Dlapller) {
    22  	rnd := rand.New(rand.NewSource(1))
    23  	for i, m := range []int{5, 6, 9, 300, 400, 600} {
    24  		n := 2
    25  		lda := n
    26  		// Allocate m×2 matrix A and fill it with random numbers.
    27  		a := make([]float64, m*lda)
    28  		for i := range a {
    29  			a[i] = rnd.NormFloat64()
    30  		}
    31  		// Store a copy of A for later comparison.
    32  		aCopy := make([]float64, len(a))
    33  		copy(aCopy, a)
    34  
    35  		// Compute the smallest singular value of A.
    36  		got := impl.Dlapll(m, a[0:], lda, a[1:], lda)
    37  
    38  		// Compute singular values of A independently by Dgesvd.
    39  		s := make([]float64, min(m, n))
    40  		work := make([]float64, 1)
    41  		impl.Dgesvd(lapack.SVDNone, lapack.SVDNone, m, n, aCopy, lda, s, nil, 1, nil, 1, work, -1)
    42  		work = make([]float64, int(work[0]))
    43  		impl.Dgesvd(lapack.SVDNone, lapack.SVDNone, m, n, aCopy, lda, s, nil, 1, nil, 1, work, len(work))
    44  		// Take the smallest singular value.
    45  		want := s[len(s)-1]
    46  
    47  		if !scalar.EqualWithinAbsOrRel(got, want, 1e-14, 1e-14) {
    48  			t.Errorf("Case %d: unexpected smallest singular value, got:%f want:%f", i, got, want)
    49  		}
    50  	}
    51  }