File: dlasq1.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (100 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright ©2015 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gonum

import (
	"math"

	"gonum.org/v1/gonum/blas/blas64"
	"gonum.org/v1/gonum/lapack"
)

// Dlasq1 computes the singular values of an n×n bidiagonal matrix with diagonal
// d and off-diagonal e. On exit, d contains the singular values in decreasing
// order, and e is overwritten. d must have length at least n, e must have
// length at least n-1, and the input work must have length at least 4*n. Dlasq1
// will panic if these conditions are not met.
//
// Dlasq1 is an internal routine. It is exported for testing purposes.
func (impl Implementation) Dlasq1(n int, d, e, work []float64) (info int) {
	if n < 0 {
		panic(nLT0)
	}

	if n == 0 {
		return info
	}

	switch {
	case len(d) < n:
		panic(shortD)
	case len(e) < n-1:
		panic(shortE)
	case len(work) < 4*n:
		panic(shortWork)
	}

	if n == 1 {
		d[0] = math.Abs(d[0])
		return info
	}

	if n == 2 {
		d[1], d[0] = impl.Dlas2(d[0], e[0], d[1])
		return info
	}

	// Estimate the largest singular value.
	var sigmx float64
	for i := 0; i < n-1; i++ {
		d[i] = math.Abs(d[i])
		sigmx = math.Max(sigmx, math.Abs(e[i]))
	}
	d[n-1] = math.Abs(d[n-1])
	// Early return if sigmx is zero (matrix is already diagonal).
	if sigmx == 0 {
		impl.Dlasrt(lapack.SortDecreasing, n, d)
		return info
	}

	for i := 0; i < n; i++ {
		sigmx = math.Max(sigmx, d[i])
	}

	// Copy D and E into WORK (in the Z format) and scale (squaring the
	// input data makes scaling by a power of the radix pointless).

	eps := dlamchP
	safmin := dlamchS
	scale := math.Sqrt(eps / safmin)
	bi := blas64.Implementation()
	bi.Dcopy(n, d, 1, work, 2)
	bi.Dcopy(n-1, e, 1, work[1:], 2)
	impl.Dlascl(lapack.General, 0, 0, sigmx, scale, 2*n-1, 1, work, 1)

	// Compute the q's and e's.
	for i := 0; i < 2*n-1; i++ {
		work[i] *= work[i]
	}
	work[2*n-1] = 0

	info = impl.Dlasq2(n, work)
	if info == 0 {
		for i := 0; i < n; i++ {
			d[i] = math.Sqrt(work[i])
		}
		impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, d, 1)
	} else if info == 2 {
		// Maximum number of iterations exceeded. Move data from work
		// into D and E so the calling subroutine can try to finish.
		for i := 0; i < n; i++ {
			d[i] = math.Sqrt(work[2*i])
			e[i] = math.Sqrt(work[2*i+1])
		}
		impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, d, 1)
		impl.Dlascl(lapack.General, 0, 0, scale, sigmx, n, 1, e, 1)
	}
	return info
}