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