-
Notifications
You must be signed in to change notification settings - Fork 526
/
general.go
1524 lines (1421 loc) · 35.8 KB
/
general.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 testlapack
import (
"fmt"
"math"
"math/cmplx"
"testing"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/blas"
"gonum.org/v1/gonum/blas/blas64"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/lapack"
)
const (
// dlamchE is the machine epsilon. For IEEE this is 2^{-53}.
dlamchE = 1.0 / (1 << 53)
dlamchB = 2
dlamchP = dlamchB * dlamchE
// dlamchS is the smallest normal number. For IEEE this is 2^{-1022}.
dlamchS = 1.0 / (1 << 256) / (1 << 256) / (1 << 256) / (1 << 254)
)
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// worklen describes how much workspace a test should use.
type worklen int
const (
minimumWork worklen = iota
mediumWork
optimumWork
)
// nanSlice allocates a new slice of length n filled with NaN.
func nanSlice(n int) []float64 {
s := make([]float64, n)
for i := range s {
s[i] = math.NaN()
}
return s
}
// randomSlice allocates a new slice of length n filled with random values.
func randomSlice(n int, rnd *rand.Rand) []float64 {
s := make([]float64, n)
for i := range s {
s[i] = rnd.NormFloat64()
}
return s
}
// nanGeneral allocates a new r×c general matrix filled with NaN values.
func nanGeneral(r, c, stride int) blas64.General {
if r < 0 || c < 0 {
panic("bad matrix size")
}
if r == 0 || c == 0 {
return blas64.General{Stride: max(1, stride)}
}
if stride < c {
panic("bad stride")
}
return blas64.General{
Rows: r,
Cols: c,
Stride: stride,
Data: nanSlice((r-1)*stride + c),
}
}
// randomGeneral allocates a new r×c general matrix filled with random
// numbers. Out-of-range elements are filled with NaN values.
func randomGeneral(r, c, stride int, rnd *rand.Rand) blas64.General {
ans := nanGeneral(r, c, stride)
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
ans.Data[i*ans.Stride+j] = rnd.NormFloat64()
}
}
return ans
}
// randomHessenberg allocates a new n×n Hessenberg matrix filled with zeros
// under the first subdiagonal and with random numbers elsewhere. Out-of-range
// elements are filled with NaN values.
func randomHessenberg(n, stride int, rnd *rand.Rand) blas64.General {
ans := nanGeneral(n, n, stride)
for i := 0; i < n; i++ {
for j := 0; j < i-1; j++ {
ans.Data[i*ans.Stride+j] = 0
}
for j := max(0, i-1); j < n; j++ {
ans.Data[i*ans.Stride+j] = rnd.NormFloat64()
}
}
return ans
}
// randomSchurCanonical returns a random, general matrix in Schur canonical
// form, that is, block upper triangular with 1×1 and 2×2 diagonal blocks where
// each 2×2 diagonal block has its diagonal elements equal and its off-diagonal
// elements of opposite sign.
func randomSchurCanonical(n, stride int, rnd *rand.Rand) blas64.General {
t := randomGeneral(n, n, stride, rnd)
// Zero out the lower triangle.
for i := 0; i < t.Rows; i++ {
for j := 0; j < i; j++ {
t.Data[i*t.Stride+j] = 0
}
}
// Randomly create 2×2 diagonal blocks.
for i := 0; i < t.Rows; {
if i == t.Rows-1 || rnd.Float64() < 0.5 {
// 1×1 block.
i++
continue
}
// 2×2 block.
// Diagonal elements equal.
t.Data[(i+1)*t.Stride+i+1] = t.Data[i*t.Stride+i]
// Off-diagonal elements of opposite sign.
c := rnd.NormFloat64()
if math.Signbit(c) == math.Signbit(t.Data[i*t.Stride+i+1]) {
c *= -1
}
t.Data[(i+1)*t.Stride+i] = c
i += 2
}
return t
}
// blockedUpperTriGeneral returns a normal random, general matrix in the form
//
// c-k-l k l
// A = k [ 0 A12 A13 ] if r-k-l >= 0;
// l [ 0 0 A23 ]
// r-k-l [ 0 0 0 ]
//
// c-k-l k l
// A = k [ 0 A12 A13 ] if r-k-l < 0;
// r-k [ 0 0 A23 ]
//
// where the k×k matrix A12 and l×l matrix is non-singular
// upper triangular. A23 is l×l upper triangular if r-k-l >= 0,
// otherwise A23 is (r-k)×l upper trapezoidal.
func blockedUpperTriGeneral(r, c, k, l, stride int, kblock bool, rnd *rand.Rand) blas64.General {
t := l
if kblock {
t += k
}
ans := zeros(r, c, stride)
for i := 0; i < min(r, t); i++ {
var v float64
for v == 0 {
v = rnd.NormFloat64()
}
ans.Data[i*ans.Stride+i+(c-t)] = v
}
for i := 0; i < min(r, t); i++ {
for j := i + (c - t) + 1; j < c; j++ {
ans.Data[i*ans.Stride+j] = rnd.NormFloat64()
}
}
return ans
}
// nanTriangular allocates a new r×c triangular matrix filled with NaN values.
func nanTriangular(uplo blas.Uplo, n, stride int) blas64.Triangular {
if n < 0 {
panic("bad matrix size")
}
if n == 0 {
return blas64.Triangular{
Stride: max(1, stride),
Uplo: uplo,
Diag: blas.NonUnit,
}
}
if stride < n {
panic("bad stride")
}
return blas64.Triangular{
N: n,
Stride: stride,
Data: nanSlice((n-1)*stride + n),
Uplo: uplo,
Diag: blas.NonUnit,
}
}
// generalOutsideAllNaN returns whether all out-of-range elements have NaN
// values.
func generalOutsideAllNaN(a blas64.General) bool {
// Check after last column.
for i := 0; i < a.Rows-1; i++ {
for _, v := range a.Data[i*a.Stride+a.Cols : i*a.Stride+a.Stride] {
if !math.IsNaN(v) {
return false
}
}
}
// Check after last element.
last := (a.Rows-1)*a.Stride + a.Cols
if a.Rows == 0 || a.Cols == 0 {
last = 0
}
for _, v := range a.Data[last:] {
if !math.IsNaN(v) {
return false
}
}
return true
}
// triangularOutsideAllNaN returns whether all out-of-triangle elements have NaN
// values.
func triangularOutsideAllNaN(a blas64.Triangular) bool {
if a.Uplo == blas.Upper {
// Check below diagonal.
for i := 0; i < a.N; i++ {
for _, v := range a.Data[i*a.Stride : i*a.Stride+i] {
if !math.IsNaN(v) {
return false
}
}
}
// Check after last column.
for i := 0; i < a.N-1; i++ {
for _, v := range a.Data[i*a.Stride+a.N : i*a.Stride+a.Stride] {
if !math.IsNaN(v) {
return false
}
}
}
} else {
// Check above diagonal.
for i := 0; i < a.N-1; i++ {
for _, v := range a.Data[i*a.Stride+i+1 : i*a.Stride+a.Stride] {
if !math.IsNaN(v) {
return false
}
}
}
}
// Check after last element.
for _, v := range a.Data[max(0, a.N-1)*a.Stride+a.N:] {
if !math.IsNaN(v) {
return false
}
}
return true
}
// transposeGeneral returns a new general matrix that is the transpose of the
// input. Nothing is done with data outside the {rows, cols} limit of the general.
func transposeGeneral(a blas64.General) blas64.General {
ans := blas64.General{
Rows: a.Cols,
Cols: a.Rows,
Stride: a.Rows,
Data: make([]float64, a.Cols*a.Rows),
}
for i := 0; i < a.Rows; i++ {
for j := 0; j < a.Cols; j++ {
ans.Data[j*ans.Stride+i] = a.Data[i*a.Stride+j]
}
}
return ans
}
// columnNorms returns the column norms of a.
func columnNorms(m, n int, a []float64, lda int) []float64 {
bi := blas64.Implementation()
norms := make([]float64, n)
for j := 0; j < n; j++ {
norms[j] = bi.Dnrm2(m, a[j:], lda)
}
return norms
}
// extractVMat collects the single reflectors from a into a matrix.
func extractVMat(m, n int, a []float64, lda int, direct lapack.Direct, store lapack.StoreV) blas64.General {
k := min(m, n)
switch {
default:
panic("not implemented")
case direct == lapack.Forward && store == lapack.ColumnWise:
v := blas64.General{
Rows: m,
Cols: k,
Stride: k,
Data: make([]float64, m*k),
}
for i := 0; i < k; i++ {
for j := 0; j < i; j++ {
v.Data[j*v.Stride+i] = 0
}
v.Data[i*v.Stride+i] = 1
for j := i + 1; j < m; j++ {
v.Data[j*v.Stride+i] = a[j*lda+i]
}
}
return v
case direct == lapack.Forward && store == lapack.RowWise:
v := blas64.General{
Rows: k,
Cols: n,
Stride: n,
Data: make([]float64, k*n),
}
for i := 0; i < k; i++ {
for j := 0; j < i; j++ {
v.Data[i*v.Stride+j] = 0
}
v.Data[i*v.Stride+i] = 1
for j := i + 1; j < n; j++ {
v.Data[i*v.Stride+j] = a[i*lda+j]
}
}
return v
}
}
// constructBidiagonal constructs a bidiagonal matrix with the given diagonal
// and off-diagonal elements.
func constructBidiagonal(uplo blas.Uplo, n int, d, e []float64) blas64.General {
bMat := blas64.General{
Rows: n,
Cols: n,
Stride: n,
Data: make([]float64, n*n),
}
for i := 0; i < n-1; i++ {
bMat.Data[i*bMat.Stride+i] = d[i]
if uplo == blas.Upper {
bMat.Data[i*bMat.Stride+i+1] = e[i]
} else {
bMat.Data[(i+1)*bMat.Stride+i] = e[i]
}
}
bMat.Data[(n-1)*bMat.Stride+n-1] = d[n-1]
return bMat
}
// constructVMat transforms the v matrix based on the storage.
func constructVMat(vMat blas64.General, store lapack.StoreV, direct lapack.Direct) blas64.General {
m := vMat.Rows
k := vMat.Cols
switch {
default:
panic("not implemented")
case store == lapack.ColumnWise && direct == lapack.Forward:
ldv := k
v := make([]float64, m*k)
for i := 0; i < m; i++ {
for j := 0; j < k; j++ {
if j > i {
v[i*ldv+j] = 0
} else if j == i {
v[i*ldv+i] = 1
} else {
v[i*ldv+j] = vMat.Data[i*vMat.Stride+j]
}
}
}
return blas64.General{
Rows: m,
Cols: k,
Stride: k,
Data: v,
}
case store == lapack.RowWise && direct == lapack.Forward:
ldv := m
v := make([]float64, m*k)
for i := 0; i < m; i++ {
for j := 0; j < k; j++ {
if j > i {
v[j*ldv+i] = 0
} else if j == i {
v[j*ldv+i] = 1
} else {
v[j*ldv+i] = vMat.Data[i*vMat.Stride+j]
}
}
}
return blas64.General{
Rows: k,
Cols: m,
Stride: m,
Data: v,
}
case store == lapack.ColumnWise && direct == lapack.Backward:
rowsv := m
ldv := k
v := make([]float64, m*k)
for i := 0; i < m; i++ {
for j := 0; j < k; j++ {
vrow := rowsv - i - 1
vcol := k - j - 1
if j > i {
v[vrow*ldv+vcol] = 0
} else if j == i {
v[vrow*ldv+vcol] = 1
} else {
v[vrow*ldv+vcol] = vMat.Data[i*vMat.Stride+j]
}
}
}
return blas64.General{
Rows: rowsv,
Cols: ldv,
Stride: ldv,
Data: v,
}
case store == lapack.RowWise && direct == lapack.Backward:
rowsv := k
ldv := m
v := make([]float64, m*k)
for i := 0; i < m; i++ {
for j := 0; j < k; j++ {
vcol := ldv - i - 1
vrow := k - j - 1
if j > i {
v[vrow*ldv+vcol] = 0
} else if j == i {
v[vrow*ldv+vcol] = 1
} else {
v[vrow*ldv+vcol] = vMat.Data[i*vMat.Stride+j]
}
}
}
return blas64.General{
Rows: rowsv,
Cols: ldv,
Stride: ldv,
Data: v,
}
}
}
func constructH(tau []float64, v blas64.General, store lapack.StoreV, direct lapack.Direct) blas64.General {
m := v.Rows
k := v.Cols
if store == lapack.RowWise {
m, k = k, m
}
h := blas64.General{
Rows: m,
Cols: m,
Stride: m,
Data: make([]float64, m*m),
}
for i := 0; i < m; i++ {
h.Data[i*m+i] = 1
}
for i := 0; i < k; i++ {
vecData := make([]float64, m)
if store == lapack.ColumnWise {
for j := 0; j < m; j++ {
vecData[j] = v.Data[j*v.Cols+i]
}
} else {
for j := 0; j < m; j++ {
vecData[j] = v.Data[i*v.Cols+j]
}
}
vec := blas64.Vector{
Inc: 1,
Data: vecData,
}
hi := blas64.General{
Rows: m,
Cols: m,
Stride: m,
Data: make([]float64, m*m),
}
for i := 0; i < m; i++ {
hi.Data[i*m+i] = 1
}
// hi = I - tau * v * v^T
blas64.Ger(-tau[i], vec, vec, hi)
hcopy := blas64.General{
Rows: m,
Cols: m,
Stride: m,
Data: make([]float64, m*m),
}
copy(hcopy.Data, h.Data)
if direct == lapack.Forward {
// H = H * H_I in forward mode
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, hcopy, hi, 0, h)
} else {
// H = H_I * H in backward mode
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, hi, hcopy, 0, h)
}
}
return h
}
// constructQ constructs the Q matrix from the result of dgeqrf and dgeqr2.
func constructQ(kind string, m, n int, a []float64, lda int, tau []float64) blas64.General {
k := min(m, n)
return constructQK(kind, m, n, k, a, lda, tau)
}
// constructQK constructs the Q matrix from the result of dgeqrf and dgeqr2 using
// the first k reflectors.
func constructQK(kind string, m, n, k int, a []float64, lda int, tau []float64) blas64.General {
var sz int
switch kind {
case "QR":
sz = m
case "LQ", "RQ":
sz = n
}
q := blas64.General{
Rows: sz,
Cols: sz,
Stride: sz,
Data: make([]float64, sz*sz),
}
for i := 0; i < sz; i++ {
q.Data[i*sz+i] = 1
}
qCopy := blas64.General{
Rows: q.Rows,
Cols: q.Cols,
Stride: q.Stride,
Data: make([]float64, len(q.Data)),
}
for i := 0; i < k; i++ {
h := blas64.General{
Rows: sz,
Cols: sz,
Stride: sz,
Data: make([]float64, sz*sz),
}
for j := 0; j < sz; j++ {
h.Data[j*sz+j] = 1
}
vVec := blas64.Vector{
Inc: 1,
Data: make([]float64, sz),
}
switch kind {
case "QR":
vVec.Data[i] = 1
for j := i + 1; j < sz; j++ {
vVec.Data[j] = a[lda*j+i]
}
case "LQ":
vVec.Data[i] = 1
for j := i + 1; j < sz; j++ {
vVec.Data[j] = a[i*lda+j]
}
case "RQ":
for j := 0; j < n-k+i; j++ {
vVec.Data[j] = a[(m-k+i)*lda+j]
}
vVec.Data[n-k+i] = 1
}
blas64.Ger(-tau[i], vVec, vVec, h)
copy(qCopy.Data, q.Data)
// Multiply q by the new h.
switch kind {
case "QR", "RQ":
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, qCopy, h, 0, q)
case "LQ":
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, h, qCopy, 0, q)
}
}
return q
}
// checkBidiagonal checks the bidiagonal decomposition from dlabrd and dgebd2.
// The input to this function is the answer returned from the routines, stored
// in a, d, e, tauP, and tauQ. The data of original A matrix (before
// decomposition) is input in aCopy.
//
// checkBidiagonal constructs the V and U matrices, and from them constructs Q
// and P. Using these constructions, it checks that Q^T * A * P and checks that
// the result is bidiagonal.
func checkBidiagonal(t *testing.T, m, n, nb int, a []float64, lda int, d, e, tauP, tauQ, aCopy []float64) {
// Check the answer.
// Construct V and U.
qMat := constructQPBidiagonal(lapack.ApplyQ, m, n, nb, a, lda, tauQ)
pMat := constructQPBidiagonal(lapack.ApplyP, m, n, nb, a, lda, tauP)
// Compute Q^T * A * P.
aMat := blas64.General{
Rows: m,
Cols: n,
Stride: lda,
Data: make([]float64, len(aCopy)),
}
copy(aMat.Data, aCopy)
tmp1 := blas64.General{
Rows: m,
Cols: n,
Stride: n,
Data: make([]float64, m*n),
}
blas64.Gemm(blas.Trans, blas.NoTrans, 1, qMat, aMat, 0, tmp1)
tmp2 := blas64.General{
Rows: m,
Cols: n,
Stride: n,
Data: make([]float64, m*n),
}
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, tmp1, pMat, 0, tmp2)
// Check that the first nb rows and cols of tm2 are upper bidiagonal
// if m >= n, and lower bidiagonal otherwise.
correctDiag := true
matchD := true
matchE := true
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if i >= nb && j >= nb {
continue
}
v := tmp2.Data[i*tmp2.Stride+j]
if i == j {
if math.Abs(d[i]-v) > 1e-12 {
matchD = false
}
continue
}
if m >= n && i == j-1 {
if math.Abs(e[j-1]-v) > 1e-12 {
matchE = false
}
continue
}
if m < n && i-1 == j {
if math.Abs(e[i-1]-v) > 1e-12 {
matchE = false
}
continue
}
if math.Abs(v) > 1e-12 {
correctDiag = false
}
}
}
if !correctDiag {
t.Errorf("Updated A not bi-diagonal")
}
if !matchD {
fmt.Println("d = ", d)
t.Errorf("D Mismatch")
}
if !matchE {
t.Errorf("E mismatch")
}
}
// constructQPBidiagonal constructs Q or P from the Bidiagonal decomposition
// computed by dlabrd and bgebd2.
func constructQPBidiagonal(vect lapack.DecompUpdate, m, n, nb int, a []float64, lda int, tau []float64) blas64.General {
sz := n
if vect == lapack.ApplyQ {
sz = m
}
var ldv int
var v blas64.General
if vect == lapack.ApplyQ {
ldv = nb
v = blas64.General{
Rows: m,
Cols: nb,
Stride: ldv,
Data: make([]float64, m*ldv),
}
} else {
ldv = n
v = blas64.General{
Rows: nb,
Cols: n,
Stride: ldv,
Data: make([]float64, m*ldv),
}
}
if vect == lapack.ApplyQ {
if m >= n {
for i := 0; i < m; i++ {
for j := 0; j <= min(nb-1, i); j++ {
if i == j {
v.Data[i*ldv+j] = 1
continue
}
v.Data[i*ldv+j] = a[i*lda+j]
}
}
} else {
for i := 1; i < m; i++ {
for j := 0; j <= min(nb-1, i-1); j++ {
if i-1 == j {
v.Data[i*ldv+j] = 1
continue
}
v.Data[i*ldv+j] = a[i*lda+j]
}
}
}
} else {
if m < n {
for i := 0; i < nb; i++ {
for j := i; j < n; j++ {
if i == j {
v.Data[i*ldv+j] = 1
continue
}
v.Data[i*ldv+j] = a[i*lda+j]
}
}
} else {
for i := 0; i < nb; i++ {
for j := i + 1; j < n; j++ {
if j-1 == i {
v.Data[i*ldv+j] = 1
continue
}
v.Data[i*ldv+j] = a[i*lda+j]
}
}
}
}
// The variable name is a computation of Q, but the algorithm is mostly the
// same for computing P (just with different data).
qMat := blas64.General{
Rows: sz,
Cols: sz,
Stride: sz,
Data: make([]float64, sz*sz),
}
hMat := blas64.General{
Rows: sz,
Cols: sz,
Stride: sz,
Data: make([]float64, sz*sz),
}
// set Q to I
for i := 0; i < sz; i++ {
qMat.Data[i*qMat.Stride+i] = 1
}
for i := 0; i < nb; i++ {
qCopy := blas64.General{Rows: qMat.Rows, Cols: qMat.Cols, Stride: qMat.Stride, Data: make([]float64, len(qMat.Data))}
copy(qCopy.Data, qMat.Data)
// Set g and h to I
for i := 0; i < sz; i++ {
for j := 0; j < sz; j++ {
if i == j {
hMat.Data[i*sz+j] = 1
} else {
hMat.Data[i*sz+j] = 0
}
}
}
var vi blas64.Vector
// H -= tauQ[i] * v[i] * v[i]^t
if vect == lapack.ApplyQ {
vi = blas64.Vector{
Inc: v.Stride,
Data: v.Data[i:],
}
} else {
vi = blas64.Vector{
Inc: 1,
Data: v.Data[i*v.Stride:],
}
}
blas64.Ger(-tau[i], vi, vi, hMat)
// Q = Q * G[1]
blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, qCopy, hMat, 0, qMat)
}
return qMat
}
// printRowise prints the matrix with one row per line. This is useful for debugging.
// If beyond is true, it prints beyond the final column to lda. If false, only
// the columns are printed.
func printRowise(a []float64, m, n, lda int, beyond bool) {
for i := 0; i < m; i++ {
end := n
if beyond {
end = lda
}
fmt.Println(a[i*lda : i*lda+end])
}
}
// isOrthonormal returns whether a square matrix Q is orthogonal.
func isOrthonormal(q blas64.General) bool {
if q.Rows != q.Cols {
panic("matrix not square")
}
n := q.Rows
// A real square matrix is orthogonal if and only if its rows form
// an orthonormal basis of the Euclidean space R^n.
const tol = 1e-10
for i := 0; i < n; i++ {
nrm := blas64.Nrm2(n, blas64.Vector{Data: q.Data[i*q.Stride:], Inc: 1})
if math.IsNaN(nrm) {
return false
}
if math.Abs(nrm-1) > tol {
return false
}
for j := i + 1; j < n; j++ {
dot := blas64.Dot(n,
blas64.Vector{Data: q.Data[i*q.Stride:], Inc: 1},
blas64.Vector{Data: q.Data[j*q.Stride:], Inc: 1},
)
if math.IsNaN(dot) {
return false
}
if math.Abs(dot) > tol {
return false
}
}
}
return true
}
// copyMatrix copies an m×n matrix src of stride n into an m×n matrix dst of stride ld.
func copyMatrix(m, n int, dst []float64, ld int, src []float64) {
for i := 0; i < m; i++ {
copy(dst[i*ld:i*ld+n], src[i*n:i*n+n])
}
}
func copyGeneral(dst, src blas64.General) {
r := min(dst.Rows, src.Rows)
c := min(dst.Cols, src.Cols)
for i := 0; i < r; i++ {
copy(dst.Data[i*dst.Stride:i*dst.Stride+c], src.Data[i*src.Stride:i*src.Stride+c])
}
}
// cloneGeneral allocates and returns an exact copy of the given general matrix.
func cloneGeneral(a blas64.General) blas64.General {
c := a
c.Data = make([]float64, len(a.Data))
copy(c.Data, a.Data)
return c
}
// equalApprox returns whether the matrices A and B of order n are approximately
// equal within given tolerance.
func equalApprox(m, n int, a []float64, lda int, b []float64, tol float64) bool {
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
diff := a[i*lda+j] - b[i*n+j]
if math.IsNaN(diff) || math.Abs(diff) > tol {
return false
}
}
}
return true
}
// equalApproxGeneral returns whether the general matrices a and b are
// approximately equal within given tolerance.
func equalApproxGeneral(a, b blas64.General, tol float64) bool {
if a.Rows != b.Rows || a.Cols != b.Cols {
panic("bad input")
}
for i := 0; i < a.Rows; i++ {
for j := 0; j < a.Cols; j++ {
diff := a.Data[i*a.Stride+j] - b.Data[i*b.Stride+j]
if math.IsNaN(diff) || math.Abs(diff) > tol {
return false
}
}
}
return true
}
// equalApproxTriangular returns whether the triangular matrices A and B of
// order n are approximately equal within given tolerance.
func equalApproxTriangular(upper bool, n int, a []float64, lda int, b []float64, tol float64) bool {
if upper {
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
diff := a[i*lda+j] - b[i*n+j]
if math.IsNaN(diff) || math.Abs(diff) > tol {
return false
}
}
}
return true
}
for i := 0; i < n; i++ {
for j := 0; j <= i; j++ {
diff := a[i*lda+j] - b[i*n+j]
if math.IsNaN(diff) || math.Abs(diff) > tol {
return false
}
}
}
return true
}
func equalApproxSymmetric(a, b blas64.Symmetric, tol float64) bool {
if a.Uplo != b.Uplo {
return false
}
if a.N != b.N {
return false
}
if a.Uplo == blas.Upper {
for i := 0; i < a.N; i++ {
for j := i; j < a.N; j++ {
if !floats.EqualWithinAbsOrRel(a.Data[i*a.Stride+j], b.Data[i*b.Stride+j], tol, tol) {
return false
}
}
}
return true
}
for i := 0; i < a.N; i++ {
for j := 0; j <= i; j++ {
if !floats.EqualWithinAbsOrRel(a.Data[i*a.Stride+j], b.Data[i*b.Stride+j], tol, tol) {
return false
}
}
}
return true
}
// randSymBand creates a random symmetric banded matrix, and returns both the
// random matrix and the equivalent Symmetric matrix for testing. rnder
// specifies the random number
func randSymBand(ul blas.Uplo, n, ldab, kb int, rnd *rand.Rand) (blas64.Symmetric, blas64.SymmetricBand) {
// A matrix is positive definite if and only if it has a Cholesky
// decomposition. Generate a random banded lower triangular matrix
// to construct the random symmetric matrix.
a := make([]float64, n*n)
for i := 0; i < n; i++ {
for j := max(0, i-kb); j <= i; j++ {
a[i*n+j] = rnd.NormFloat64()
}
a[i*n+i] = math.Abs(a[i*n+i])
// Add an extra amound to the diagonal in order to improve the condition number.
a[i*n+i] += 1.5 * rnd.Float64()
}
agen := blas64.General{
Rows: n,
Cols: n,
Stride: n,
Data: a,
}
// Construct the SymDense from a*a^T
c := make([]float64, n*n)
cgen := blas64.General{
Rows: n,
Cols: n,
Stride: n,
Data: c,
}
blas64.Gemm(blas.NoTrans, blas.Trans, 1, agen, agen, 0, cgen)
sym := blas64.Symmetric{
N: n,
Stride: n,
Data: c,
Uplo: ul,
}
b := symToSymBand(ul, c, n, n, kb, ldab)
band := blas64.SymmetricBand{