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

     1  // Copyright ©2014 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  	"github.com/gopherd/gonum/blas"
    11  	"github.com/gopherd/gonum/floats"
    12  )
    13  
    14  type Dspmver interface {
    15  	Dspmv(ul blas.Uplo, n int, alpha float64, ap []float64, x []float64, incX int, beta float64, y []float64, incY int)
    16  }
    17  
    18  func DspmvTest(t *testing.T, blasser Dspmver) {
    19  	for i, test := range []struct {
    20  		ul    blas.Uplo
    21  		n     int
    22  		a     [][]float64
    23  		x     []float64
    24  		y     []float64
    25  		alpha float64
    26  		beta  float64
    27  		ans   []float64
    28  	}{
    29  		{
    30  			ul: blas.Upper,
    31  			n:  3,
    32  			a: [][]float64{
    33  				{5, 6, 7},
    34  				{0, 8, 10},
    35  				{0, 0, 13},
    36  			},
    37  			x:     []float64{3, 4, 5},
    38  			y:     []float64{6, 7, 8},
    39  			alpha: 2.1,
    40  			beta:  -3,
    41  			ans:   []float64{137.4, 189, 240.6},
    42  		},
    43  		{
    44  			ul: blas.Lower,
    45  			n:  3,
    46  			a: [][]float64{
    47  				{5, 0, 0},
    48  				{6, 8, 0},
    49  				{7, 10, 13},
    50  			},
    51  			x:     []float64{3, 4, 5},
    52  			y:     []float64{6, 7, 8},
    53  			alpha: 2.1,
    54  			beta:  -3,
    55  			ans:   []float64{137.4, 189, 240.6},
    56  		},
    57  	} {
    58  		incTest := func(incX, incY, extra int) {
    59  			x := makeIncremented(test.x, incX, extra)
    60  			y := makeIncremented(test.y, incY, extra)
    61  			aFlat := flattenTriangular(test.a, test.ul)
    62  			ans := makeIncremented(test.ans, incY, extra)
    63  
    64  			blasser.Dspmv(test.ul, test.n, test.alpha, aFlat, x, incX, test.beta, y, incY)
    65  			if !floats.EqualApprox(ans, y, 1e-14) {
    66  				t.Errorf("Case %v, incX=%v, incY=%v: Want %v, got %v.", i, incX, incY, ans, y)
    67  			}
    68  		}
    69  		incTest(1, 1, 0)
    70  		incTest(2, 3, 0)
    71  		incTest(3, 2, 0)
    72  		incTest(-3, 2, 0)
    73  		incTest(-2, 4, 0)
    74  		incTest(2, -1, 0)
    75  		incTest(-3, -4, 3)
    76  	}
    77  }