github.com/gopherd/gonum@v0.0.4/blas/testblas/zsymm.go (about)

     1  // Copyright ©2019 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 testblas
     6  
     7  import (
     8  	"fmt"
     9  	"math/cmplx"
    10  	"testing"
    11  
    12  	"math/rand"
    13  
    14  	"github.com/gopherd/gonum/blas"
    15  )
    16  
    17  type Zsymmer interface {
    18  	Zsymm(side blas.Side, uplo blas.Uplo, m, n int, alpha complex128, a []complex128, lda int, b []complex128, ldb int, beta complex128, c []complex128, ldc int)
    19  }
    20  
    21  func ZsymmTest(t *testing.T, impl Zsymmer) {
    22  	for _, side := range []blas.Side{blas.Left, blas.Right} {
    23  		for _, uplo := range []blas.Uplo{blas.Lower, blas.Upper} {
    24  			name := sideString(side) + "-" + uploString(uplo)
    25  			t.Run(name, func(t *testing.T) {
    26  				for _, m := range []int{0, 1, 2, 3, 4, 5} {
    27  					for _, n := range []int{0, 1, 2, 3, 4, 5} {
    28  						zsymmTest(t, impl, side, uplo, m, n)
    29  					}
    30  				}
    31  			})
    32  		}
    33  	}
    34  }
    35  
    36  func zsymmTest(t *testing.T, impl Zsymmer, side blas.Side, uplo blas.Uplo, m, n int) {
    37  	const tol = 1e-13
    38  
    39  	rnd := rand.New(rand.NewSource(1))
    40  
    41  	nA := m
    42  	if side == blas.Right {
    43  		nA = n
    44  	}
    45  	for _, lda := range []int{max(1, nA), nA + 2} {
    46  		for _, ldb := range []int{max(1, n), n + 3} {
    47  			for _, ldc := range []int{max(1, n), n + 4} {
    48  				for _, alpha := range []complex128{0, 1, complex(0.7, -0.9)} {
    49  					for _, beta := range []complex128{0, 1, complex(1.3, -1.1)} {
    50  						for _, nanC := range []bool{false, true} {
    51  							if nanC && beta != 0 {
    52  								// Skip tests with C containing NaN values
    53  								// unless beta would zero out the NaNs.
    54  								continue
    55  							}
    56  
    57  							// Allocate the matrix A and fill it with random numbers.
    58  							a := make([]complex128, nA*lda)
    59  							for i := range a {
    60  								a[i] = rndComplex128(rnd)
    61  							}
    62  							// Create a copy of A for checking that
    63  							// Zsymm does not modify its triangle
    64  							// opposite to uplo.
    65  							aCopy := make([]complex128, len(a))
    66  							copy(aCopy, a)
    67  							// Create a copy of A expanded into a
    68  							// full symmetric matrix for computing
    69  							// the expected result using zmm.
    70  							aSym := make([]complex128, len(a))
    71  							copy(aSym, a)
    72  							if uplo == blas.Upper {
    73  								for i := 0; i < nA-1; i++ {
    74  									for j := i + 1; j < nA; j++ {
    75  										aSym[j*lda+i] = aSym[i*lda+j]
    76  									}
    77  								}
    78  							} else {
    79  								for i := 1; i < nA; i++ {
    80  									for j := 0; j < i; j++ {
    81  										aSym[j*lda+i] = aSym[i*lda+j]
    82  									}
    83  								}
    84  							}
    85  
    86  							// Allocate the matrix B and fill it with random numbers.
    87  							b := make([]complex128, m*ldb)
    88  							for i := range b {
    89  								b[i] = rndComplex128(rnd)
    90  							}
    91  							// Create a copy of B for checking that
    92  							// Zsymm does not modify B.
    93  							bCopy := make([]complex128, len(b))
    94  							copy(bCopy, b)
    95  
    96  							// Allocate the matrix C and fill it with random numbers.
    97  							c := make([]complex128, m*ldc)
    98  							for i := range c {
    99  								c[i] = rndComplex128(rnd)
   100  							}
   101  							if nanC {
   102  								for i := 0; i < n; i++ {
   103  									for j := 0; j < m; j++ {
   104  										c[i+j*ldc] = cmplx.NaN()
   105  									}
   106  								}
   107  							}
   108  
   109  							// Compute the expected result using an internal Zgemm implementation.
   110  							var want []complex128
   111  							if side == blas.Left {
   112  								want = zmm(blas.NoTrans, blas.NoTrans, m, n, m, alpha, aSym, lda, b, ldb, beta, c, ldc)
   113  							} else {
   114  								want = zmm(blas.NoTrans, blas.NoTrans, m, n, n, alpha, b, ldb, aSym, lda, beta, c, ldc)
   115  							}
   116  
   117  							// Compute the result using Zsymm.
   118  							impl.Zsymm(side, uplo, m, n, alpha, a, lda, b, ldb, beta, c, ldc)
   119  
   120  							prefix := fmt.Sprintf("m=%v,n=%v,lda=%v,ldb=%v,ldc=%v,alpha=%v,beta=%v", m, n, lda, ldb, ldc, alpha, beta)
   121  
   122  							if !zsame(a, aCopy) {
   123  								t.Errorf("%v: unexpected modification of A", prefix)
   124  								continue
   125  							}
   126  							if !zsame(b, bCopy) {
   127  								t.Errorf("%v: unexpected modification of B", prefix)
   128  								continue
   129  							}
   130  
   131  							if !zEqualApprox(c, want, tol) {
   132  								t.Errorf("%v: unexpected result", prefix)
   133  							}
   134  						}
   135  					}
   136  				}
   137  			}
   138  		}
   139  	}
   140  }