github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/testlapack/drscl.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  	"math"
     9  	"testing"
    10  
    11  	"github.com/jingcheng-WU/gonum/floats"
    12  )
    13  
    14  type Drscler interface {
    15  	Drscl(n int, a float64, x []float64, incX int)
    16  }
    17  
    18  func DrsclTest(t *testing.T, impl Drscler) {
    19  	for _, test := range []struct {
    20  		x []float64
    21  		a float64
    22  	}{
    23  		{
    24  			x: []float64{1, 2, 3, 4, 5},
    25  			a: 4,
    26  		},
    27  		{
    28  			x: []float64{1, 2, 3, 4, 5},
    29  			a: math.MaxFloat64,
    30  		},
    31  		{
    32  			x: []float64{1, 2, 3, 4, 5},
    33  			a: 1e-307,
    34  		},
    35  	} {
    36  		xcopy := make([]float64, len(test.x))
    37  		copy(xcopy, test.x)
    38  
    39  		// Cannot test the scaling directly because of floating point scaling issues
    40  		// (the purpose of Drscl). Instead, check that scaling and scaling back
    41  		// yeilds approximately x. If overflow or underflow occurs then the scaling
    42  		// won't match.
    43  		impl.Drscl(len(test.x), test.a, xcopy, 1)
    44  		if floats.Equal(xcopy, test.x) {
    45  			t.Errorf("x unchanged during call to drscl. a = %v, x = %v.", test.a, test.x)
    46  		}
    47  		impl.Drscl(len(test.x), 1/test.a, xcopy, 1)
    48  		if !floats.EqualApprox(xcopy, test.x, 1e-14) {
    49  			t.Errorf("x not equal after scaling and unscaling. a = %v, x = %v.", test.a, test.x)
    50  		}
    51  	}
    52  }