gonum.org/v1/gonum@v0.14.0/lapack/gonum/dlae2.go (about)

     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 gonum
     6  
     7  import "math"
     8  
     9  // Dlae2 computes the eigenvalues of a 2×2 symmetric matrix
    10  //
    11  //	[a b]
    12  //	[b c]
    13  //
    14  // and returns the eigenvalue with the larger absolute value as rt1 and the
    15  // smaller as rt2.
    16  //
    17  // Dlae2 is an internal routine. It is exported for testing purposes.
    18  func (impl Implementation) Dlae2(a, b, c float64) (rt1, rt2 float64) {
    19  	sm := a + c
    20  	df := a - c
    21  	adf := math.Abs(df)
    22  	tb := b + b
    23  	ab := math.Abs(tb)
    24  	acmx := c
    25  	acmn := a
    26  	if math.Abs(a) > math.Abs(c) {
    27  		acmx = a
    28  		acmn = c
    29  	}
    30  	var rt float64
    31  	if adf > ab {
    32  		rt = adf * math.Sqrt(1+(ab/adf)*(ab/adf))
    33  	} else if adf < ab {
    34  		rt = ab * math.Sqrt(1+(adf/ab)*(adf/ab))
    35  	} else {
    36  		rt = ab * math.Sqrt2
    37  	}
    38  	if sm < 0 {
    39  		rt1 = 0.5 * (sm - rt)
    40  		rt2 = (acmx/rt1)*acmn - (b/rt1)*b
    41  		return rt1, rt2
    42  	}
    43  	if sm > 0 {
    44  		rt1 = 0.5 * (sm + rt)
    45  		rt2 = (acmx/rt1)*acmn - (b/rt1)*b
    46  		return rt1, rt2
    47  	}
    48  	rt1 = 0.5 * rt
    49  	rt2 = -0.5 * rt
    50  	return rt1, rt2
    51  }