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.
11 "golang.org/x/exp/rand"
13 "gonum.org/v1/gonum/blas"
14 "gonum.org/v1/gonum/blas/blas64"
17 type Dlarfger interface {
18 Dlarfg(n int, alpha float64, x []float64, incX int) (beta, tau float64)
21 func DlarfgTest(t *testing.T, impl Dlarfger) {
22 rnd := rand.New(rand.NewSource(1))
23 for i, test := range []struct {
47 x: []float64{4, 5, 6},
52 x: []float64{0, 0, 0},
57 x: []float64{dlamchS, dlamchS, dlamchS},
64 x = make([]float64, n-1)
69 if len(test.x) != n-1 {
72 x = make([]float64, n-1)
75 xcopy := make([]float64, n-1)
78 beta, tau := impl.Dlarfg(n, alpha, x, incX)
80 // Verify the returns and the values in v. Construct h and perform
81 // the explicit multiplication.
82 h := make([]float64, n*n)
83 for i := 0; i < n; i++ {
86 hmat := blas64.General{
92 v := make([]float64, n)
95 vVec := blas64.Vector{
99 blas64.Ger(-tau, vVec, vVec, hmat)
100 eye := blas64.General{
104 Data: make([]float64, n*n),
106 blas64.Gemm(blas.Trans, blas.NoTrans, 1, hmat, hmat, 0, eye)
108 for i := 0; i < n; i++ {
109 for j := 0; j < n; j++ {
111 if math.Abs(eye.Data[i*n+j]-1) > 1e-14 {
115 if math.Abs(eye.Data[i*n+j]) > 1e-14 {
122 t.Errorf("H^T * H is not I %v", eye)
125 xVec := blas64.Vector{
127 Data: make([]float64, n),
129 xVec.Data[0] = test.alpha
130 copy(xVec.Data[1:], xcopy)
132 ans := make([]float64, n)
133 ansVec := blas64.Vector{
137 blas64.Gemv(blas.NoTrans, 1, hmat, xVec, 0, ansVec)
138 if math.Abs(ans[0]-beta) > 1e-14 {
139 t.Errorf("Case %v, beta mismatch. Want %v, got %v", i, ans[0], beta)
141 for i := 1; i < n; i++ {
142 if math.Abs(ans[i]) > 1e-14 {
143 t.Errorf("Case %v, nonzero answer %v", i, ans)