github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dlaqr1.go (about)

     1  // Copyright ©2016 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  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/gonum/floats"
    13  )
    14  
    15  type Dlaqr1er interface {
    16  	Dlaqr1(n int, h []float64, ldh int, sr1, si1, sr2, si2 float64, v []float64)
    17  }
    18  
    19  func Dlaqr1Test(t *testing.T, impl Dlaqr1er) {
    20  	rnd := rand.New(rand.NewSource(1))
    21  
    22  	for _, n := range []int{2, 3} {
    23  		for _, ldh := range []int{n, n + 1, n + 10} {
    24  			for _, cas := range []int{1, 2} {
    25  				for k := 0; k < 100; k++ {
    26  					v := make([]float64, n)
    27  					for i := range v {
    28  						v[i] = math.NaN()
    29  					}
    30  					h := make([]float64, n*(n-1)*ldh)
    31  					for i := range h {
    32  						h[i] = math.NaN()
    33  					}
    34  					for i := 0; i < n; i++ {
    35  						for j := 0; j < n; j++ {
    36  							h[i*ldh+j] = rnd.NormFloat64()
    37  						}
    38  					}
    39  					var sr1, sr2, si1, si2 float64
    40  					if cas == 1 {
    41  						sr1 = rnd.NormFloat64()
    42  						sr2 = sr1
    43  						si1 = rnd.NormFloat64()
    44  						si2 = -si1
    45  					} else {
    46  						sr1 = rnd.NormFloat64()
    47  						sr2 = rnd.NormFloat64()
    48  						si1 = 0
    49  						si2 = 0
    50  					}
    51  					impl.Dlaqr1(n, h, ldh, sr1, si1, sr2, si2, v)
    52  
    53  					// Matrix H - s1*I.
    54  					h1 := make([]complex128, n*n)
    55  					for i := 0; i < n; i++ {
    56  						for j := 0; j < n; j++ {
    57  							h1[i*n+j] = complex(h[i*ldh+j], 0)
    58  							if i == j {
    59  								h1[i*n+j] -= complex(sr1, si1)
    60  							}
    61  						}
    62  					}
    63  					// First column of H - s2*I.
    64  					h2 := make([]complex128, n)
    65  					for i := 0; i < n; i++ {
    66  						h2[i] = complex(h[i*ldh], 0)
    67  					}
    68  					h2[0] -= complex(sr2, si2)
    69  
    70  					wantv := make([]float64, n)
    71  					// Multiply (H-s1*I)*(H-s2*I) to get a tentative
    72  					// wantv.
    73  					for i := 0; i < n; i++ {
    74  						for j := 0; j < n; j++ {
    75  							wantv[i] += real(h1[i*n+j] * h2[j])
    76  						}
    77  					}
    78  					// Get the unknown scale.
    79  					scale := v[0] / wantv[0]
    80  					// Compute the actual wantv.
    81  					floats.Scale(scale, wantv)
    82  
    83  					// The scale must be the same for all elements.
    84  					if floats.Distance(wantv, v, math.Inf(1)) > 1e-13 {
    85  						t.Errorf("n = %v, ldh = %v, case = %v: Unexpected value of v: got %v, want %v", n, ldh, cas, v, wantv)
    86  					}
    87  				}
    88  			}
    89  		}
    90  	}
    91  }