github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/blas/testblas/zhemm.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  	"golang.org/x/exp/rand"
    13  
    14  	"github.com/jingcheng-WU/gonum/blas"
    15  )
    16  
    17  type Zhemmer interface {
    18  	Zhemm(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 ZhemmTest(t *testing.T, impl Zhemmer) {
    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  						zhemmTest(t, impl, side, uplo, m, n)
    29  					}
    30  				}
    31  			})
    32  		}
    33  	}
    34  }
    35  
    36  func zhemmTest(t *testing.T, impl Zhemmer, 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  							// Zhemm 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 hermitian matrix for computing
    69  							// the expected result using zmm.
    70  							aHem := make([]complex128, len(a))
    71  							copy(aHem, a)
    72  							if uplo == blas.Upper {
    73  								for i := 0; i < nA; i++ {
    74  									aHem[i*lda+i] = complex(real(aHem[i*lda+i]), 0)
    75  									for j := i + 1; j < nA; j++ {
    76  										aHem[j*lda+i] = cmplx.Conj(aHem[i*lda+j])
    77  									}
    78  								}
    79  							} else {
    80  								for i := 0; i < nA; i++ {
    81  									for j := 0; j < i; j++ {
    82  										aHem[j*lda+i] = cmplx.Conj(aHem[i*lda+j])
    83  									}
    84  									aHem[i*lda+i] = complex(real(aHem[i*lda+i]), 0)
    85  								}
    86  							}
    87  
    88  							// Allocate the matrix B and fill it with random numbers.
    89  							b := make([]complex128, m*ldb)
    90  							for i := range b {
    91  								b[i] = rndComplex128(rnd)
    92  							}
    93  							// Create a copy of B for checking that
    94  							// Zhemm does not modify B.
    95  							bCopy := make([]complex128, len(b))
    96  							copy(bCopy, b)
    97  
    98  							// Allocate the matrix C and fill it with random numbers.
    99  							c := make([]complex128, m*ldc)
   100  							for i := range c {
   101  								c[i] = rndComplex128(rnd)
   102  							}
   103  							if nanC {
   104  								for i := 0; i < n; i++ {
   105  									for j := 0; j < m; j++ {
   106  										c[i+j*ldc] = cmplx.NaN()
   107  									}
   108  								}
   109  							}
   110  
   111  							// Compute the expected result using an internal Zgemm implementation.
   112  							var want []complex128
   113  							if side == blas.Left {
   114  								want = zmm(blas.NoTrans, blas.NoTrans, m, n, m, alpha, aHem, lda, b, ldb, beta, c, ldc)
   115  							} else {
   116  								want = zmm(blas.NoTrans, blas.NoTrans, m, n, n, alpha, b, ldb, aHem, lda, beta, c, ldc)
   117  							}
   118  
   119  							// Compute the result using Zhemm.
   120  							impl.Zhemm(side, uplo, m, n, alpha, a, lda, b, ldb, beta, c, ldc)
   121  
   122  							prefix := fmt.Sprintf("m=%v,n=%v,lda=%v,ldb=%v,ldc=%v,alpha=%v,beta=%v", m, n, lda, ldb, ldc, alpha, beta)
   123  
   124  							if !zsame(a, aCopy) {
   125  								t.Errorf("%v: unexpected modification of A", prefix)
   126  								continue
   127  							}
   128  							if !zsame(b, bCopy) {
   129  								t.Errorf("%v: unexpected modification of B", prefix)
   130  								continue
   131  							}
   132  
   133  							if !zEqualApprox(c, want, tol) {
   134  								t.Errorf("%v: unexpected result", prefix)
   135  							}
   136  						}
   137  					}
   138  				}
   139  			}
   140  		}
   141  	}
   142  }