gonum.org/v1/gonum@v0.14.0/blas/testblas/zgemv.go (about)

     1  // Copyright ©2017 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  	"testing"
     9  
    10  	"gonum.org/v1/gonum/blas"
    11  )
    12  
    13  type Zgemver interface {
    14  	Zgemv(trans blas.Transpose, m, n int, alpha complex128, a []complex128, lda int, x []complex128, incX int, beta complex128, y []complex128, incY int)
    15  }
    16  
    17  func ZgemvTest(t *testing.T, impl Zgemver) {
    18  	for tc, test := range []struct {
    19  		trans blas.Transpose
    20  		alpha complex128
    21  		a     []complex128
    22  		x     []complex128
    23  		beta  complex128
    24  		y     []complex128
    25  
    26  		want      []complex128
    27  		wantXNeg  []complex128
    28  		wantYNeg  []complex128
    29  		wantXYNeg []complex128
    30  	}{
    31  		{
    32  			trans: blas.NoTrans,
    33  			alpha: 1 + 2i,
    34  			beta:  3 + 4i,
    35  		},
    36  		{
    37  			trans: blas.NoTrans,
    38  			alpha: 1 + 2i,
    39  			a: []complex128{
    40  				9 + 5i, -2 + 6i, 5 + 1i, 9 + 2i, 10 + 4i,
    41  				0 - 7i, 9 - 9i, 5 + 3i, -8 - 1i, 7 - 7i,
    42  				10 - 7i, -1 + 3i, 2 + 2i, 7 + 6i, 9 + 1i,
    43  				10 + 0i, 8 - 6i, 4 - 6i, -2 - 10i, -5 + 0i,
    44  			},
    45  			x: []complex128{
    46  				4 - 9i,
    47  				8 + 5i,
    48  				-2 - 10i,
    49  				2 - 4i,
    50  				-6 + 6i,
    51  			},
    52  			beta: 3 + 4i,
    53  			y: []complex128{
    54  				-2 + 3i,
    55  				10 + 5i,
    56  				-8 - 5i,
    57  				-8 + 7i,
    58  			},
    59  			want: []complex128{
    60  				101 - 116i,
    61  				58 + 166i,
    62  				126 - 242i,
    63  				336 - 75i,
    64  			},
    65  			wantXNeg: []complex128{
    66  				98 + 128i,
    67  				374 - 252i,
    68  				-113 + 205i,
    69  				-60 - 312i,
    70  			},
    71  			wantYNeg: []complex128{
    72  				370 - 63i,
    73  				140 - 140i,
    74  				44 + 64i,
    75  				67 - 128i,
    76  			},
    77  			wantXYNeg: []complex128{
    78  				-26 - 300i,
    79  				-99 + 307i,
    80  				360 - 354i,
    81  				64 + 116i,
    82  			},
    83  		},
    84  		{
    85  			trans: blas.Trans,
    86  			alpha: 1 + 2i,
    87  			a: []complex128{
    88  				9 + 5i, -2 + 6i, 5 + 1i, 9 + 2i, 10 + 4i,
    89  				0 - 7i, 9 - 9i, 5 + 3i, -8 - 1i, 7 - 7i,
    90  				10 - 7i, -1 + 3i, 2 + 2i, 7 + 6i, 9 + 1i,
    91  				10 + 0i, 8 - 6i, 4 - 6i, -2 - 10i, -5 + 0i,
    92  			},
    93  			x: []complex128{
    94  				4 - 9i,
    95  				8 + 5i,
    96  				-2 - 10i,
    97  				2 - 4i,
    98  			},
    99  			beta: 3 + 4i,
   100  			y: []complex128{
   101  				8 - 6i,
   102  				-8 - 2i,
   103  				9 + 5i,
   104  				4 - 1i,
   105  				6 - 4i,
   106  			},
   107  			want: []complex128{
   108  				580 - 137i,
   109  				221 + 311i,
   110  				149 + 115i,
   111  				443 - 208i,
   112  				517 + 143i,
   113  			},
   114  			wantXNeg: []complex128{
   115  				387 + 152i,
   116  				109 - 433i,
   117  				225 - 53i,
   118  				-246 + 44i,
   119  				13 + 20i,
   120  			},
   121  			wantYNeg: []complex128{
   122  				531 + 145i,
   123  				411 - 259i,
   124  				149 + 115i,
   125  				253 + 362i,
   126  				566 - 139i,
   127  			},
   128  			wantXYNeg: []complex128{
   129  				27 + 22i,
   130  				-278 - 7i,
   131  				225 - 53i,
   132  				141 - 382i,
   133  				373 + 150i,
   134  			},
   135  		},
   136  		{
   137  			trans: blas.ConjTrans,
   138  			alpha: 1 + 2i,
   139  			a: []complex128{
   140  				9 + 5i, -2 + 6i, 5 + 1i, 9 + 2i, 10 + 4i,
   141  				0 - 7i, 9 - 9i, 5 + 3i, -8 - 1i, 7 - 7i,
   142  				10 - 7i, -1 + 3i, 2 + 2i, 7 + 6i, 9 + 1i,
   143  				10 + 0i, 8 - 6i, 4 - 6i, -2 - 10i, -5 + 0i,
   144  			},
   145  			x: []complex128{
   146  				4 - 9i,
   147  				8 + 5i,
   148  				-2 - 10i,
   149  				2 - 4i,
   150  			},
   151  			beta: 3 + 4i,
   152  			y: []complex128{
   153  				8 - 6i,
   154  				-8 - 2i,
   155  				9 + 5i,
   156  				4 - 1i,
   157  				6 - 4i,
   158  			},
   159  			want: []complex128{
   160  				472 - 133i,
   161  				-253 + 23i,
   162  				217 + 131i,
   163  				229 - 316i,
   164  				187 - 97i,
   165  			},
   166  			wantXNeg: []complex128{
   167  				289 + 276i,
   168  				499 + 47i,
   169  				237 + 91i,
   170  				54 + 504i,
   171  				251 + 196i,
   172  			},
   173  			wantYNeg: []complex128{
   174  				201 - 95i,
   175  				197 - 367i,
   176  				217 + 131i,
   177  				-221 + 74i,
   178  				458 - 135i,
   179  			},
   180  			wantXYNeg: []complex128{
   181  				265 + 198i,
   182  				22 + 453i,
   183  				237 + 91i,
   184  				531 + 98i,
   185  				275 + 274i,
   186  			},
   187  		},
   188  		{
   189  			trans: blas.ConjTrans,
   190  			alpha: 1 + 2i,
   191  			a: []complex128{
   192  				9 + 5i, -2 + 6i, 5 + 1i, 9 + 2i, 10 + 4i,
   193  				0 - 7i, 9 - 9i, 5 + 3i, -8 - 1i, 7 - 7i,
   194  				10 - 7i, -1 + 3i, 2 + 2i, 7 + 6i, 9 + 1i,
   195  				10 + 0i, 8 - 6i, 4 - 6i, -2 - 10i, -5 + 0i,
   196  			},
   197  			x: []complex128{
   198  				4 - 9i,
   199  				8 + 5i,
   200  				-2 - 10i,
   201  				2 - 4i,
   202  			},
   203  			beta: 0,
   204  			y: []complex128{
   205  				8 - 6i,
   206  				-8 - 2i,
   207  				9 + 5i,
   208  				4 - 1i,
   209  				6 - 4i,
   210  			},
   211  			want: []complex128{
   212  				424 - 147i,
   213  				-237 + 61i,
   214  				210 + 80i,
   215  				213 - 329i,
   216  				153 - 109i,
   217  			},
   218  			wantXNeg: []complex128{
   219  				241 + 262i,
   220  				515 + 85i,
   221  				230 + 40i,
   222  				38 + 491i,
   223  				217 + 184i,
   224  			},
   225  			wantYNeg: []complex128{
   226  				153 - 109i,
   227  				213 - 329i,
   228  				210 + 80i,
   229  				-237 + 61i,
   230  				424 - 147i,
   231  			},
   232  			wantXYNeg: []complex128{
   233  				217 + 184i,
   234  				38 + 491i,
   235  				230 + 40i,
   236  				515 + 85i,
   237  				241 + 262i,
   238  			},
   239  		},
   240  		{
   241  			trans: blas.ConjTrans,
   242  			alpha: 0,
   243  			a: []complex128{
   244  				9 + 5i, -2 + 6i, 5 + 1i, 9 + 2i, 10 + 4i,
   245  				0 - 7i, 9 - 9i, 5 + 3i, -8 - 1i, 7 - 7i,
   246  				10 - 7i, -1 + 3i, 2 + 2i, 7 + 6i, 9 + 1i,
   247  				10 + 0i, 8 - 6i, 4 - 6i, -2 - 10i, -5 + 0i,
   248  			},
   249  			x: []complex128{
   250  				4 - 9i,
   251  				8 + 5i,
   252  				-2 - 10i,
   253  				2 - 4i,
   254  			},
   255  			beta: 3 + 4i,
   256  			y: []complex128{
   257  				8 - 6i,
   258  				-8 - 2i,
   259  				9 + 5i,
   260  				4 - 1i,
   261  				6 - 4i,
   262  			},
   263  			want: []complex128{
   264  				48 + 14i,
   265  				-16 - 38i,
   266  				7 + 51i,
   267  				16 + 13i,
   268  				34 + 12i,
   269  			},
   270  			wantXNeg: []complex128{
   271  				48 + 14i,
   272  				-16 - 38i,
   273  				7 + 51i,
   274  				16 + 13i,
   275  				34 + 12i,
   276  			},
   277  			wantYNeg: []complex128{
   278  				48 + 14i,
   279  				-16 - 38i,
   280  				7 + 51i,
   281  				16 + 13i,
   282  				34 + 12i,
   283  			},
   284  			wantXYNeg: []complex128{
   285  				48 + 14i,
   286  				-16 - 38i,
   287  				7 + 51i,
   288  				16 + 13i,
   289  				34 + 12i,
   290  			},
   291  		},
   292  	} {
   293  		var m, n int
   294  		switch test.trans {
   295  		case blas.NoTrans:
   296  			m = len(test.y)
   297  			n = len(test.x)
   298  		case blas.Trans, blas.ConjTrans:
   299  			m = len(test.x)
   300  			n = len(test.y)
   301  		}
   302  		for _, incX := range []int{-11, -2, -1, 1, 2, 7} {
   303  			for _, incY := range []int{-11, -2, -1, 1, 2, 7} {
   304  				for _, lda := range []int{max(1, n), n + 11} {
   305  					alpha := test.alpha
   306  
   307  					a := makeZGeneral(test.a, m, n, lda)
   308  					aCopy := make([]complex128, len(a))
   309  					copy(aCopy, a)
   310  
   311  					x := makeZVector(test.x, incX)
   312  					xCopy := make([]complex128, len(x))
   313  					copy(xCopy, x)
   314  
   315  					y := makeZVector(test.y, incY)
   316  
   317  					impl.Zgemv(test.trans, m, n, alpha, a, lda, x, incX, test.beta, y, incY)
   318  
   319  					if !zsame(x, xCopy) {
   320  						t.Errorf("Case %v (incX=%v,incY=%v,lda=%v): unexpected modification of x", tc, incX, incY, lda)
   321  					}
   322  					if !zsame(a, aCopy) {
   323  						t.Errorf("Case %v (incX=%v,incY=%v,lda=%v): unexpected modification of A", tc, incX, incY, lda)
   324  					}
   325  
   326  					var want []complex128
   327  					switch {
   328  					case incX > 0 && incY > 0:
   329  						want = makeZVector(test.want, incY)
   330  					case incX < 0 && incY > 0:
   331  						want = makeZVector(test.wantXNeg, incY)
   332  					case incX > 0 && incY < 0:
   333  						want = makeZVector(test.wantYNeg, incY)
   334  					default:
   335  						want = makeZVector(test.wantXYNeg, incY)
   336  					}
   337  					if !zsame(y, want) {
   338  						t.Errorf("Case %v (incX=%v,incY=%v,lda=%v): unexpected result\nwant %v\ngot  %v", tc, incX, incY, lda, want, y)
   339  					}
   340  				}
   341  			}
   342  		}
   343  	}
   344  }