github.com/gopherd/gonum@v0.0.4/lapack/testlapack/dlasv2.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  	"testing"
     9  
    10  	"math/rand"
    11  
    12  	"github.com/gopherd/gonum/floats"
    13  )
    14  
    15  type Dlasv2er interface {
    16  	Dlasv2(f, g, h float64) (ssmin, ssmax, snr, csr, snl, csl float64)
    17  }
    18  
    19  func Dlasv2Test(t *testing.T, impl Dlasv2er) {
    20  	rnd := rand.New(rand.NewSource(1))
    21  	for i := 0; i < 100; i++ {
    22  		f := rnd.NormFloat64()
    23  		g := rnd.NormFloat64()
    24  		h := rnd.NormFloat64()
    25  
    26  		ssmin, ssmax, snr, csr, snl, csl := impl.Dlasv2(f, g, h)
    27  
    28  		// tmp =
    29  		// [ csl snl] [f g]
    30  		// [-snl csl] [0 h]
    31  		tmp11 := csl * f
    32  		tmp12 := csl*g + snl*h
    33  		tmp21 := -snl * f
    34  		tmp22 := -snl*g + csl*h
    35  		// lhs =
    36  		// [tmp11 tmp12] [csr -snr]
    37  		// [tmp21 tmp22] [snr  csr]
    38  		ans11 := tmp11*csr + tmp12*snr
    39  		ans12 := tmp11*-snr + tmp12*csr
    40  		ans21 := tmp21*csr + tmp22*snr
    41  		ans22 := tmp21*-snr + tmp22*csr
    42  
    43  		lhs := []float64{ans11, ans12, ans21, ans22}
    44  		rhs := []float64{ssmax, 0, 0, ssmin}
    45  		if !floats.EqualApprox(rhs, lhs, 1e-12) {
    46  			t.Errorf("SVD mismatch. f = %v, g = %v, h = %v.\nLHS: %v\nRHS: %v", f, g, h, lhs, rhs)
    47  		}
    48  	}
    49  }