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.
10 "gonum.org/v1/gonum/blas/blas64"
11 "gonum.org/v1/gonum/lapack"
14 // Dlasq1 computes the singular values of an n×n bidiagonal matrix with diagonal
15 // d and off-diagonal e. On exit, d contains the singular values in decreasing
16 // order, and e is overwritten. d must have length at least n, e must have
17 // length at least n-1, and the input work must have length at least 4*n. Dlasq1
18 // will panic if these conditions are not met.
20 // Dlasq1 is an internal routine. It is exported for testing purposes.
21 func (impl Implementation) Dlasq1(n int, d, e, work []float64) (info int) {
22 // TODO(btracey): replace info with an error.
30 panic("lapack: length of d less than n")
33 panic("lapack: length of e less than n-1")
43 d[1], d[0] = impl.Dlas2(d[0], e[0], d[1])
46 // Estimate the largest singular value.
48 for i := 0; i < n-1; i++ {
50 sigmx = math.Max(sigmx, math.Abs(e[i]))
52 d[n-1] = math.Abs(d[n-1])
53 // Early return if sigmx is zero (matrix is already diagonal).
55 impl.Dlasrt(lapack.SortDecreasing, n, d)
59 for i := 0; i < n; i++ {
60 sigmx = math.Max(sigmx, d[i])
63 // Copy D and E into WORK (in the Z format) and scale (squaring the
64 // input data makes scaling by a power of the radix pointless).
68 scale := math.Sqrt(eps / safmin)
69 bi := blas64.Implementation()
70 bi.Dcopy(n, d, 1, work, 2)
71 bi.Dcopy(n-1, e, 1, work[1:], 2)
72 impl.Dlascl(lapack.General, 0, 0, sigmx, scale, 2*n-1, 1, work, 1)
74 // Compute the q's and e's.
75 for i := 0; i < 2*n-1; i++ {
80 info = impl.Dlasq2(n, work)
82 for i := 0; i < n; i++ {
83 d[i] = math.Sqrt(work[i])
85 impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, d, 1)
87 // Maximum number of iterations exceeded. Move data from work
88 // into D and E so the calling subroutine can try to finish.
89 for i := 0; i < n; i++ {
90 d[i] = math.Sqrt(work[2*i])
91 e[i] = math.Sqrt(work[2*i+1])
93 impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, d, 1)
94 impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, e, 1)