github.com/gopherd/gonum@v0.0.4/lapack/testlapack/dlaev2.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  	"math/rand"
    12  )
    13  
    14  type Dlaev2er interface {
    15  	Dlaev2(a, b, c float64) (rt1, rt2, cs1, sn1 float64)
    16  }
    17  
    18  func Dlaev2Test(t *testing.T, impl Dlaev2er) {
    19  	rnd := rand.New(rand.NewSource(1))
    20  	for trial := 0; trial < 100; trial++ {
    21  		a := rnd.NormFloat64()
    22  		b := rnd.NormFloat64()
    23  		c := rnd.NormFloat64()
    24  
    25  		rt1, rt2, cs1, sn1 := impl.Dlaev2(a, b, c)
    26  		tmp := mul2by2([2][2]float64{{cs1, sn1}, {-sn1, cs1}}, [2][2]float64{{a, b}, {b, c}})
    27  		ans := mul2by2(tmp, [2][2]float64{{cs1, -sn1}, {sn1, cs1}})
    28  		if math.Abs(ans[0][0]-rt1) > 1e-14 {
    29  			t.Errorf("Largest eigenvalue mismatch. Returned %v, mul %v", rt1, ans[0][0])
    30  		}
    31  		if math.Abs(ans[1][0]) > 1e-14 || math.Abs(ans[0][1]) > 1e-14 {
    32  			t.Errorf("Non-zero off diagonal. ans[1][0] = %v, ans[0][1] = %v", ans[1][0], ans[0][1])
    33  		}
    34  		if math.Abs(ans[1][1]-rt2) > 1e-14 {
    35  			t.Errorf("Smallest eigenvalue mismatch. Returned %v, mul %v", rt2, ans[1][1])
    36  		}
    37  	}
    38  }
    39  
    40  func mul2by2(a, b [2][2]float64) [2][2]float64 {
    41  	var c [2][2]float64
    42  	c[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0]
    43  	c[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1]
    44  	c[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0]
    45  	c[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1]
    46  	return c
    47  }