forked from gonum/gonum
/
dlantb.go
213 lines (206 loc) · 4.41 KB
/
dlantb.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright ©2020 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"
"github.com/ArkaGPL/gonum/blas"
"github.com/ArkaGPL/gonum/lapack"
)
// Dlantb returns the value of the given norm of an n×n triangular band matrix A
// with k+1 diagonals.
//
// When norm is lapack.MaxColumnSum, the length of work must be at least n.
func (impl Implementation) Dlantb(norm lapack.MatrixNorm, uplo blas.Uplo, diag blas.Diag, n, k int, a []float64, lda int, work []float64) float64 {
switch {
case norm != lapack.MaxAbs && norm != lapack.MaxRowSum && norm != lapack.MaxColumnSum && norm != lapack.Frobenius:
panic(badNorm)
case uplo != blas.Upper && uplo != blas.Lower:
panic(badUplo)
case n < 0:
panic(nLT0)
case k < 0:
panic(kdLT0)
case lda < k+1:
panic(badLdA)
}
// Quick return if possible.
if n == 0 {
return 0
}
switch {
case len(a) < (n-1)*lda+k+1:
panic(shortAB)
case len(work) < n && norm == lapack.MaxColumnSum:
panic(shortWork)
}
var value float64
switch norm {
case lapack.MaxAbs:
if uplo == blas.Upper {
var jfirst int
if diag == blas.Unit {
value = 1
jfirst = 1
}
for i := 0; i < n; i++ {
for _, aij := range a[i*lda+jfirst : i*lda+min(n-i, k+1)] {
if math.IsNaN(aij) {
return aij
}
aij = math.Abs(aij)
if aij > value {
value = aij
}
}
}
} else {
jlast := k + 1
if diag == blas.Unit {
value = 1
jlast = k
}
for i := 0; i < n; i++ {
for _, aij := range a[i*lda+max(0, k-i) : i*lda+jlast] {
if math.IsNaN(aij) {
return math.NaN()
}
aij = math.Abs(aij)
if aij > value {
value = aij
}
}
}
}
case lapack.MaxRowSum:
var sum float64
if uplo == blas.Upper {
var jfirst int
if diag == blas.Unit {
jfirst = 1
}
for i := 0; i < n; i++ {
sum = 0
if diag == blas.Unit {
sum = 1
}
for _, aij := range a[i*lda+jfirst : i*lda+min(n-i, k+1)] {
sum += math.Abs(aij)
}
if math.IsNaN(sum) {
return math.NaN()
}
if sum > value {
value = sum
}
}
} else {
jlast := k + 1
if diag == blas.Unit {
jlast = k
}
for i := 0; i < n; i++ {
sum = 0
if diag == blas.Unit {
sum = 1
}
for _, aij := range a[i*lda+max(0, k-i) : i*lda+jlast] {
sum += math.Abs(aij)
}
if math.IsNaN(sum) {
return math.NaN()
}
if sum > value {
value = sum
}
}
}
case lapack.MaxColumnSum:
work = work[:n]
if diag == blas.Unit {
for i := range work {
work[i] = 1
}
} else {
for i := range work {
work[i] = 0
}
}
if uplo == blas.Upper {
var jfirst int
if diag == blas.Unit {
jfirst = 1
}
for i := 0; i < n; i++ {
for j, aij := range a[i*lda+jfirst : i*lda+min(n-i, k+1)] {
work[i+jfirst+j] += math.Abs(aij)
}
}
} else {
jlast := k + 1
if diag == blas.Unit {
jlast = k
}
for i := 0; i < n; i++ {
off := max(0, k-i)
for j, aij := range a[i*lda+off : i*lda+jlast] {
work[i+j+off-k] += math.Abs(aij)
}
}
}
for _, wi := range work {
if math.IsNaN(wi) {
return math.NaN()
}
if wi > value {
value = wi
}
}
case lapack.Frobenius:
var scale, ssq float64
switch uplo {
case blas.Upper:
if diag == blas.Unit {
scale = 1
ssq = float64(n)
if k > 0 {
for i := 0; i < n-1; i++ {
ilen := min(n-i-1, k)
rowscale, rowssq := impl.Dlassq(ilen, a[i*lda+1:], 1, 0, 1)
scale, ssq = impl.Dcombssq(scale, ssq, rowscale, rowssq)
}
}
} else {
scale = 0
ssq = 1
for i := 0; i < n; i++ {
ilen := min(n-i, k+1)
rowscale, rowssq := impl.Dlassq(ilen, a[i*lda:], 1, 0, 1)
scale, ssq = impl.Dcombssq(scale, ssq, rowscale, rowssq)
}
}
case blas.Lower:
if diag == blas.Unit {
scale = 1
ssq = float64(n)
if k > 0 {
for i := 1; i < n; i++ {
ilen := min(i, k)
rowscale, rowssq := impl.Dlassq(ilen, a[i*lda+k-ilen:], 1, 0, 1)
scale, ssq = impl.Dcombssq(scale, ssq, rowscale, rowssq)
}
}
} else {
scale = 0
ssq = 1
for i := 0; i < n; i++ {
ilen := min(i, k) + 1
rowscale, rowssq := impl.Dlassq(ilen, a[i*lda+k+1-ilen:], 1, 0, 1)
scale, ssq = impl.Dcombssq(scale, ssq, rowscale, rowssq)
}
}
}
value = scale * math.Sqrt(ssq)
}
return value
}