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