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"
14 type Dlaev2er interface {
15 Dlaev2(a, b, c float64) (rt1, rt2, cs1, sn1 float64)
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()
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])
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])
34 if math.Abs(ans[1][1]-rt2) > 1e-14 {
35 t.Errorf("Smallest eigenvalue mismatch. Returned %v, mul %v", rt2, ans[1][1])
40 func mul2by2(a, b [2][2]float64) [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]