github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/testlapack/dgtsv.go (about)

     1  // Copyright ©2020 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  	"fmt"
     9  	"math"
    10  	"testing"
    11  
    12  	"golang.org/x/exp/rand"
    13  
    14  	"github.com/jingcheng-WU/gonum/blas"
    15  	"github.com/jingcheng-WU/gonum/blas/blas64"
    16  	"github.com/jingcheng-WU/gonum/lapack"
    17  )
    18  
    19  type Dgtsver interface {
    20  	Dgtsv(n, nrhs int, dl, d, du []float64, b []float64, ldb int) (ok bool)
    21  }
    22  
    23  func DgtsvTest(t *testing.T, impl Dgtsver) {
    24  	rnd := rand.New(rand.NewSource(1))
    25  	for _, n := range []int{0, 1, 2, 3, 4, 5, 10, 25, 50} {
    26  		for _, nrhs := range []int{0, 1, 2, 3, 4, 10} {
    27  			for _, ldb := range []int{max(1, nrhs), nrhs + 3} {
    28  				dgtsvTest(t, impl, rnd, n, nrhs, ldb)
    29  			}
    30  		}
    31  	}
    32  }
    33  
    34  func dgtsvTest(t *testing.T, impl Dgtsver, rnd *rand.Rand, n, nrhs, ldb int) {
    35  	const (
    36  		tol   = 1e-14
    37  		extra = 10
    38  	)
    39  
    40  	name := fmt.Sprintf("Case n=%d,nrhs=%d,ldb=%d", n, nrhs, ldb)
    41  
    42  	if n == 0 {
    43  		ok := impl.Dgtsv(n, nrhs, nil, nil, nil, nil, ldb)
    44  		if !ok {
    45  			t.Errorf("%v: unexpected failure for zero size matrix", name)
    46  		}
    47  		return
    48  	}
    49  
    50  	// Generate three random diagonals.
    51  	var (
    52  		d, dCopy   []float64
    53  		dl, dlCopy []float64
    54  		du, duCopy []float64
    55  	)
    56  	d = randomSlice(n+1+extra, rnd)
    57  	dCopy = make([]float64, len(d))
    58  	copy(dCopy, d)
    59  	if n > 1 {
    60  		dl = randomSlice(n+extra, rnd)
    61  		dlCopy = make([]float64, len(dl))
    62  		copy(dlCopy, dl)
    63  
    64  		du = randomSlice(n+extra, rnd)
    65  		duCopy = make([]float64, len(du))
    66  		copy(duCopy, du)
    67  	}
    68  
    69  	b := randomGeneral(n, nrhs, ldb, rnd)
    70  	got := cloneGeneral(b)
    71  
    72  	ok := impl.Dgtsv(n, nrhs, dl, d, du, got.Data, got.Stride)
    73  	if !ok {
    74  		t.Fatalf("%v: unexpected failure in Dgtsv", name)
    75  		return
    76  	}
    77  
    78  	// Compute A*X - B.
    79  	dlagtm(blas.NoTrans, n, nrhs, 1, dlCopy, dCopy, duCopy, got.Data, got.Stride, -1, b.Data, b.Stride)
    80  
    81  	anorm := dlangt(lapack.MaxColumnSum, n, dlCopy, dCopy, duCopy)
    82  	bi := blas64.Implementation()
    83  	var resid float64
    84  	for j := 0; j < nrhs; j++ {
    85  		bnorm := bi.Dasum(n, b.Data[j:], b.Stride)
    86  		xnorm := bi.Dasum(n, got.Data[j:], got.Stride)
    87  		resid = math.Max(resid, bnorm/anorm/xnorm)
    88  	}
    89  	if resid > tol {
    90  		t.Errorf("%v: unexpected result; resid=%v,want<=%v", name, resid, tol)
    91  	}
    92  }