gonum.org/v1/gonum@v0.14.0/lapack/testlapack/dlacpy.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  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/exp/rand"
    12  
    13  	"gonum.org/v1/gonum/blas"
    14  )
    15  
    16  type Dlacpyer interface {
    17  	Dlacpy(uplo blas.Uplo, m, n int, a []float64, lda int, b []float64, ldb int)
    18  }
    19  
    20  func DlacpyTest(t *testing.T, impl Dlacpyer) {
    21  	rnd := rand.New(rand.NewSource(1))
    22  	for _, uplo := range []blas.Uplo{blas.Upper, blas.Lower, blas.All} {
    23  		for _, test := range []struct {
    24  			m, n, lda, ldb int
    25  		}{
    26  			{3, 5, 0, 0},
    27  			{5, 5, 0, 0},
    28  			{7, 5, 0, 0},
    29  
    30  			{3, 5, 10, 12},
    31  			{5, 5, 10, 12},
    32  			{7, 5, 10, 12},
    33  		} {
    34  			m := test.m
    35  			n := test.n
    36  			lda := test.lda
    37  			if lda == 0 {
    38  				lda = n
    39  			}
    40  			ldb := test.ldb
    41  			if ldb == 0 {
    42  				ldb = n
    43  			}
    44  			a := make([]float64, m*lda)
    45  			for i := range a {
    46  				a[i] = rnd.Float64()
    47  			}
    48  			b := make([]float64, m*ldb)
    49  			for i := range b {
    50  				b[i] = rnd.Float64()
    51  			}
    52  			impl.Dlacpy(uplo, m, n, a, lda, b, ldb)
    53  			equal := true
    54  			switch uplo {
    55  			case blas.Upper:
    56  				for i := 0; i < m; i++ {
    57  					for j := i; j < n; j++ {
    58  						if b[i*ldb+j] != a[i*lda+j] {
    59  							equal = false
    60  							goto DoneCheck
    61  						}
    62  					}
    63  				}
    64  			case blas.Lower:
    65  				for i := 0; i < m; i++ {
    66  					for j := 0; j < min(i, n); j++ {
    67  						if b[i*ldb+j] != a[i*lda+j] {
    68  							equal = false
    69  							goto DoneCheck
    70  						}
    71  					}
    72  				}
    73  			case blas.All:
    74  				for i := 0; i < m; i++ {
    75  					for j := 0; j < n; j++ {
    76  						if b[i*ldb+j] != a[i*lda+j] {
    77  							equal = false
    78  							goto DoneCheck
    79  						}
    80  					}
    81  				}
    82  			}
    83  		DoneCheck:
    84  			if !equal {
    85  				fmt.Println(blas.Lower)
    86  				t.Errorf("Matrices not equal after copy. Uplo = %d, m = %d, n = %d", uplo, m, n)
    87  			}
    88  		}
    89  	}
    90  }