Skip to content
This repository was archived by the owner on Nov 24, 2018. It is now read-only.

Commit 63d82fd

Browse files
committed
Add dlae2 and test
1 parent 4b15612 commit 63d82fd

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

native/dlae2.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright ©2016 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 native
6+
7+
import "math"
8+
9+
// Dlae2 computes the eigenvalues of a 2×2 symmetric matrix
10+
// [a b]
11+
// [b c]
12+
// and returns the eigenvalue with the larger absolute value as rt1 and the
13+
// smaller as rt2.
14+
func (impl Implementation) Dlae2(a, b, c float64) (rt1, rt2 float64) {
15+
sm := a + c
16+
df := a - c
17+
adf := math.Abs(df)
18+
tb := b + b
19+
ab := math.Abs(tb)
20+
acmx := c
21+
acmn := a
22+
if math.Abs(a) > math.Abs(c) {
23+
acmx = a
24+
acmn = c
25+
}
26+
var rt float64
27+
if adf > ab {
28+
rt = adf * math.Sqrt(1+(ab/adf)*(ab/adf))
29+
} else if adf < ab {
30+
rt = ab * math.Sqrt(1+(adf/ab)*(adf/ab))
31+
} else {
32+
rt = ab * math.Sqrt2
33+
}
34+
if sm < 0 {
35+
rt1 = 0.5 * (sm - rt)
36+
rt2 = (acmx/rt1)*acmn - (b/rt1)*b
37+
return rt1, rt2
38+
}
39+
if sm > 0 {
40+
rt1 = 0.5 * (sm + rt)
41+
rt2 = (acmx/rt1)*acmn - (b/rt1)*b
42+
return rt1, rt2
43+
}
44+
rt1 = 0.5 * rt
45+
rt2 = -0.5 * rt
46+
return rt1, rt2
47+
}

native/lapack_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func TestDlacpy(t *testing.T) {
7272
testlapack.DlacpyTest(t, impl)
7373
}
7474

75+
func TestDlae2(t *testing.T) {
76+
testlapack.Dlae2Test(t, impl)
77+
}
78+
7579
func TestDlaev2(t *testing.T) {
7680
testlapack.Dlaev2Test(t, impl)
7781
}

testlapack/dlae2.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright ©2016 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+
"fmt"
9+
"math"
10+
"testing"
11+
)
12+
13+
type Dlae2er interface {
14+
Dlae2(a, b, c float64) (rt1, rt2 float64)
15+
}
16+
17+
func Dlae2Test(t *testing.T, impl Dlae2er) {
18+
for _, test := range []struct {
19+
a, b, c float64
20+
}{
21+
{-10, 5, 3},
22+
{3, 5, -10},
23+
{0, 3, 0},
24+
{1, 3, 1},
25+
{1, -3, 1},
26+
{5, 0, 3},
27+
{3, 0, -5},
28+
{1, 3, 1.02},
29+
{1.02, 3, 1},
30+
{1, -3, -9},
31+
} {
32+
a := test.a
33+
b := test.b
34+
c := test.c
35+
rt1, rt2 := impl.Dlae2(a, b, c)
36+
37+
errStr := fmt.Sprintf("a = %v, b = %v, c = %v", a, b, c)
38+
// Check if rt1 and rt2 are eigenvalues by checking if det(a - λI) = 0
39+
a1 := a - rt1
40+
c1 := c - rt1
41+
det := a1*c1 - b*b
42+
if math.Abs(det) > 1e-10 {
43+
t.Errorf("First eigenvalue mismatch. %s. Det = %v", errStr, det)
44+
}
45+
46+
a2 := a - rt2
47+
c2 := c - rt2
48+
det = a2*c2 - b*b
49+
if math.Abs(det) > 1e-10 {
50+
t.Errorf("Second eigenvalue mismatch. %s. Det = %v", errStr, det)
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)