-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lapack.hpp
27089 lines (26971 loc) · 806 KB
/
Lapack.hpp
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
#ifndef LAPACK_HEADER
#define LAPACK_HEADER
#include <limits>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include "Blas.hpp"
/*!\class Lapack
* \brief A template class containing LAPACK routines.
* Lapack contains the LAPACK routines as static members.
* Any routine can be called using Lapack<type>::routine(...).
* The template type is meant to be double, but can be any floating point type
*
* \author Simon De Ridder */
template<class real>
class Lapack
{
private:
// constants
static constexpr real NEGONE= real(-1.0); //!< A constant negative one (-1.0) value
static constexpr real MEIGTH= real(-0.125);//!< A constant negative one eigth (-0.125)value
static constexpr real ZERO = real(0.0); //!< A constant zero (0.0) value
static constexpr real QURTR = real(0.25); //!< A constant one quarter (0.25) value
static constexpr real HALF = real(0.5); //!< A constant one half (0.5) value
static constexpr real ONE = real(1.0); //!< A constant one (1.0) value
static constexpr real TWO = real(2.0); //!< A constant two (2.0) value
static constexpr real THREE = real(3.0); //!< A constant three (3.0) value
static constexpr real FOUR = real(4.0); //!< A constant four (4.0) value
static constexpr real EIGHT = real(8.0); //!< A constant eight (8.0) value
static constexpr real TEN = real(10.0); //!< A constant ten (10.0) value
static constexpr real HNDRD = real(100.0); //!< A constant one hundred (100.0) value
public:
// LAPACK INSTALL (alphabetically)
/*! §dlamc3
*
* §dlamc3 is intended to force §A and §B to be stored prior to doing the addition of §A and
* §B, for use in situations where optimizers might hold one of these in a register.
* \param[in] A
* \param[in] B The values §A and §B.
* \return The sum of §A and §B
* \authors
* LAPACK is a software package provided by Univ. of Tennessee,
* Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd.
* \date December 2016 */
static real dlamc3(real const A, real const B) /*const*/
{
return A + B;
}
/*! §dlamch
*
* §dlamch determines double precision machine parameters.
* \param[in] cmach §cmach[0] Specifies the value to be returned by §dlamch: \n
* 'E' or 'e': eps: Relative machine precision.\n
* 'S' or 's': sfmin: Safe minimum, such that 1/sfmin does not overflow.\n
* 'B' or 'b': base: Base of the machine (radix).\n
* 'P' or 'p': prec: $\{eps}\cdot\{base}$.\n
* 'N' or 'n': t: Number of(base) digits in the mantissa.\n
* 'R' or 'r': rnd: 1.0 when rounding occurs in addition, 0.0 otherwise.\n
* 'M' or 'm': emin: Minimum exponent before(gradual) underflow.\n
* 'U' or 'u': rmin: Underflow threshold: $\{base}^{\{emin}-1}$.\n
* 'L' or 'l': emax: Largest exponent before overflow.\n
* 'O' or 'o': rmax: Overflow threshold: $(\{base}^\{emax})(1-\{eps})$.
* \return The value specified by §cmach, or zero when §cmach is not recognized.
* \authors Univ. of Tennessee
* \authors Univ. of California Berkeley
* \authors Univ. of Colorado Denver
* \authors NAG Ltd.
* \date December 2016 */
static real dlamch(char const* const cmach) /*const*/
{
// Assume rounding, not chopping.Always.
real rnd = ONE;
real eps;
if (ONE==rnd)
{
eps = std::numeric_limits<real>::epsilon() * HALF;
}
else
{
eps = std::numeric_limits<real>::epsilon();
}
real sfmin, small;
switch (std::toupper(cmach[0]))
{
case 'E':
return eps;
case 'S':
sfmin = std::numeric_limits<real>::min();
small = ONE / std::numeric_limits<real>::max();
if (small>=sfmin)
{
//Use SMALL plus a bit, to avoid the possibility of rounding
// causing overflow when computing 1/sfmin.
sfmin = small * (ONE+eps);
}
return sfmin;
case 'B':
return std::numeric_limits<real>::radix;
case 'P':
return eps * std::numeric_limits<real>::radix;
case 'N':
return std::numeric_limits<real>::digits;
case 'R':
return rnd;
case 'M':
return std::numeric_limits<real>::min_exponent;
case 'U':
return std::numeric_limits<real>::min();
case 'L':
return std::numeric_limits<real>::max_exponent;
case 'O':
return std::numeric_limits<real>::max();
default:
return ZERO;
}
}
/* §dsecnd replaced inline by std::time */
/*! §ilaver returns the LAPACK version.
*
* This subroutine returns the LAPACK version.
* \param[out] vers_major return the lapack major version
* \param[out] vers_minor return the lapack minor version from the major version.
* \param[out] vers_patch return the lapack patch version from the minor version
* \authors Univ. of Tennessee
* \authors Univ. of California Berkeley
* \authors Univ. of Colorado Denver
* \authors NAG Ltd.
* \date June 2017 */
static void ilaver(int& vers_major, int& vers_minor, int& vers_patch) /*const*/
{
vers_major = 3;
vers_minor = 8;
vers_patch = 0;
}
// LAPACK SRC (alphabetically)
/*! §dbdsdc
*
* §dbdsdc computes the singular value decomposition (SVD) of a real §n by §n (upper or lower)
* bidiagonal matrix $B$: $B = U S V_T$, using a divide and conquer method, where $S$ is a
* diagonal matrix with non-negative diagonal elements (the singular values of $B$), and $U$
* and $V_T$ are orthogonal matrices of left and right singular vectors, respectively. §dbdsdc
* can be used to compute all singular values, and optionally, singular vectors or singular
* vectors in compact form.\n
* This code makes very mild assumptions about floating point arithmetic. It will work on
* machines with a guard digit in add/subtract, or on those binary machines without guard
* digits which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. It could
* conceivably fail on hexadecimal or decimal machines without guard digits, but we know of
* none. See §dlasd3 for details.\n
* The code currently calls §dlasdq if singular values only are desired. However, it can be
* slightly modified to compute singular values using the divide and conquer method.
* \param[in] uplo
* = 'U': $B$ is upper bidiagonal.\n
* = 'L': $B$ is lower bidiagonal.
*
* \param[in] compq
* Specifies whether singular vectors are to be computed as follows:\n
* = 'N': Compute singular values only;\n
* = 'P': Compute singular values and compute singular vectors in compact form;\n
* = 'I': Compute singular values and singular vectors.
*
* \param[in] n The order of the matrix $B$. $\{n}\ge 0$.
* \param[in,out] d
* an array, dimension (§n)\n
* On entry, the §n diagonal elements of the bidiagonal matrix $B$.\n
* On exit, if §info = 0, the singular values of $B$.
*
* \param[in,out] e
* an array, dimension ($\{n}-1$)\n
* On entry, the elements of §e contain the offdiagonal elements of the bidiagonal matrix
* whose SVD is desired.\n
* On exit, §e has been destroyed.
*
* \param[out] U
* an array, dimension (§ldu,§n)\n.
* If §compq = 'I', then:\n On exit, if §info = 0, §U contains the left singular vectors of
* the bidiagonal matrix.\n For other values of §compq, §U is not referenced.
*
* \param[in] ldu
* The leading dimension of the array §U. $\{ldu}\ge 1$.\n
* If singular vectors are desired, then $\{ldu}\ge\max(1,\{n})$.
*
* \param[out] Vt
* an array, dimension (§ldvt,§n)\n
* If §compq = 'I', then:\n On exit, if §info = 0, $\{Vt}^T$ contains the right singular
* vectors of the bidiagonal matrix.\n For other values of §compq, §Vt is not referenced.
*
* \param[in] ldvt
* The leading dimension of the array §Vt. $\{ldvt}\ge 1$.\n
* If singular vectors are desired, then $\{ldvt}\ge\max(1,\{n})$.
*
* \param[out] q
* an array, dimension (§ldq)\n
* If §compq = 'P', then:\n
* On exit, if §info = 0, §q and §iq contain the left and right singular vectors in a
* compact form, requiring $\mathcal{O}(\{n}\log\{n})$ space instead of $2\{n}^2$.
* In particular, §q contains all the real data in
* $\{ldq}\ge\{n}*(11+2\{smlsiz}+8\lfloor\log_2(\{n}/(\{smlsiz}+1))\rfloor)$ words of
* memory, where §smlsiz is returned by §ilaenv and is equal to the maximum size of the
* subproblems at the bottom of the computation tree (usually about 25).\n
* For other values of §compq, §q is not referenced.
*
* \param[out] iq
* an integer array, dimension (§LDIQ)\n
* If §compq = 'P', then:\n
* On exit, if §info = 0, §q and §iq contain the left and right singular vectors in a
* compact form, requiring $\mathcal{O}(\{n}\log\{n})$ space instead of $2\{n}^2$.
* In particular, §iq contains all integer data in
* $\{LDIQ}\ge\{n}(3+3\lfloor\log_2(\{n}/(\{smlsiz}+1))\rfloor)$ words of memory, where
* §smlsiz is returned by §ilaenv and is equal to the maximum size of the subproblems
* at the bottom of the computation tree (usually about 25).\n
* For other values of §compq, §iq is not referenced.
*
* \param[out] work
* an array, dimension ($\max(1,\{lwork})$)\n
* If §compq = 'N' then $\{lwork}\ge(4\{n})$.\n
* If §compq = 'P' then $\{lwork}\ge(6\{n})$.\n
* If §compq = 'I' then $\{lwork}\ge(3\{n}^2+4\{n})$.
*
* \param[out] iwork an integer array, dimension ($8\{n}$)
* \param[out] info
* = 0: successful exit.\n
* < 0: if §info = $-i$, the $i$-th argument had an illegal value.\n
* > 0: The algorithm failed to compute a singular value.
* The update process of divide and conquer failed.
* \authors Univ. of Tennessee
* \authors Univ. of California Berkeley
* \authors Univ. of Colorado Denver
* \authors NAG Ltd.
* \date June 2016
* \remark
* Contributors:\n
* Ming Gu and Huan Ren, Computer Science Division,
* University of California at Berkeley, USA */
static void dbdsdc(char const* const uplo, char const* const compq, int const n, real* const d,
real* const e, real* const U, int const ldu, real* const Vt, int const ldvt,
real* const q, int* const iq, real* const work, int* const iwork, int& info)
/*const*/
{
// Test the input parameters.
info = 0;
int icompq, iuplo = 0;
if (std::toupper(uplo[0])=='U')
{
iuplo = 1;
}
if (std::toupper(uplo[0])=='L')
{
iuplo = 2;
}
if (std::toupper(compq[0])=='N')
{
icompq = 0;
}
else if (std::toupper(compq[0])=='P')
{
icompq = 1;
}
else if (std::toupper(compq[0])=='I')
{
icompq = 2;
}
else
{
icompq = -1;
}
if (iuplo==0)
{
info = -1;
}
else if (icompq<0)
{
info = -2;
}
else if (n<0)
{
info = -3;
}
else if (ldu<1 || (icompq==2 && ldu<n))
{
info = -7;
}
else if (ldvt<1 || (icompq==2 && ldvt<n))
{
info = -9;
}
if (info!=0)
{
xerbla("DBDSDC", -info);
return;
}
// Quick return if possible
if (n==0)
{
return;
}
int smlsiz = ilaenv(9, "DBDSDC", " ", 0, 0, 0, 0);
if (n==1)
{
if (icompq==1)
{
q[0] = std::copysign(ONE, d[0]);
q[smlsiz*n] = ONE;
}
else if (icompq==2)
{
U[0] = std::copysign(ONE, d[0]);
Vt[0] = ONE;
}
d[0] = std::fabs(d[0]);
return;
}
int nm1 = n - 1;
int nm2 = n - 2;
// If matrix lower bidiagonal,
// rotate to be upper bidiagonal by applying Givens rotations on the left
int wstart = 0;
int qstart = 2;
if (icompq==1)
{
Blas<real>::dcopy(n, d, 1, &q[0], 1);
Blas<real>::dcopy(nm1, e, 1, &q[n], 1);
}
int i;
real r;
if (iuplo==2)
{
qstart = 4;
if (icompq == 2)
{
wstart = 2 * nm1;
}
real cs, sn;
for (i=0; i<nm1; i++)
{
dlartg(d[i], e[i], cs, sn, r);
d[i] = r;
e[i] = sn * d[i+1];
d[i+1] = cs * d[i+1];
if (icompq==1)
{
q[i+2*n] = cs;
q[i+3*n] = sn;
}
else if (icompq==2)
{
work[i] = cs;
work[nm1+i] = -sn;
}
}
}
// If icompq = 0, use dlasdq to compute the singular values.
if (icompq==0)
{
/* Ignore wstart, instead using work, since the two vectors for cs and -sn above are
* added only if icompq == 2, and adding them exceeds documented work size of 4*n. */
dlasdq("U", 0, n, 0, 0, 0, d, e, Vt, ldvt, U, ldu, U, ldu, work, info);
}
else
{
// If n is smaller than the minimum divide size smlsiz,
// then solve the problem with another solver.
int iu, ivt=0;
if (n<=smlsiz)
{
if (icompq==2)
{
dlaset("A", n, n, ZERO, ONE, U, ldu);
dlaset("A", n, n, ZERO, ONE, Vt, ldvt);
dlasdq("U", 0, n, n, n, 0, d, e, Vt, ldvt, U, ldu, U, ldu, &work[wstart],
info);
}
else if (icompq==1)
{
iu = 0;
ivt = iu + n;
dlaset("A", n, n, ZERO, ONE, &q[iu+qstart*n], n);
dlaset("A", n, n, ZERO, ONE, &q[ivt+qstart*n], n);
dlasdq("U", 0, n, n, n, 0, d, e, &q[ivt+qstart*n], n, &q[iu+qstart*n], n,
&q[iu+qstart*n], n, &work[wstart], info);
}
}
else
{
if (icompq==2)
{
dlaset("A", n, n, ZERO, ONE, U, ldu);
dlaset("A", n, n, ZERO, ONE, Vt, ldvt);
}
// Scale.
real orgnrm = dlanst("M", n, d, e);
if (orgnrm==ZERO)
{
return;
}
int ierr;
dlascl("G", 0, 0, orgnrm, ONE, n, 1, d, n, ierr);
dlascl("G", 0, 0, orgnrm, ONE, nm1, 1, e, nm1, ierr);
real eps = real(0.9) * dlamch("Epsilon");
int difl=0, difr=0, givcol=0, givnum=0, givptr, ic=0, is=0, k, perm, poles=0, z=0;
if (icompq==1)
{
int mlvl = int(std::log(real(n)/real(smlsiz+1)) / std::log(TWO)) + 1;
iu = 0;
ivt = smlsiz;
difl = ivt + smlsiz + 1;
difr = difl + mlvl;
z = difr + mlvl*2;
ic = z + mlvl;
is = ic + 1;
poles = is + 1;
givnum = poles + 2*mlvl;
k = 1;
givptr = 2;
perm = 3;
givcol = perm + mlvl;
}
for (i=0; i<n; i++)
{
if (std::fabs(d[i])<eps)
{
d[i] = std::copysign(eps, d[i]);
}
}
int start = 0;
int sqre = 0;
int nsize;
for (i=0; i<nm1; i++)
{
if (std::fabs(e[i])<eps || i==nm2)
{
// Subproblem found.
// First determine its size and then apply divide and conquer on it.
if (i<nm2)
{
// A subproblem with e[i] small for i < n - 2.
nsize = i - start + 1;
}
else if (std::fabs(e[i])>=eps)
{
// A subproblem with e[n-2] not too small but i = n - 2.
nsize = n - start;
}
else
{
// A subproblem with e[n-2] small. This implies an 1-by-1 subproblem at
// d[n-1]. Solve this 1-by-1 problem first.
nsize = i - start + 1;
if (icompq==2)
{
U[nm1+ldu*nm1] = std::copysign(ONE, d[nm1]);
Vt[nm1+ldvt*nm1] = ONE;
}else if (icompq==1)
{
q[nm1+qstart*n] = std::copysign(ONE, d[nm1]);
q[nm1+(smlsiz+qstart)*n] = ONE;
}
d[nm1] = std::fabs(d[nm1]);
}
if (icompq==2)
{
dlasd0(nsize, sqre, &d[start], &e[start], &U[start+ldu*start], ldu,
&Vt[start+ldvt*start], ldvt, smlsiz, iwork, &work[wstart],
info);
}
else
{
dlasda(icompq, smlsiz, nsize, sqre, &d[start], &e[start],
&q[start+(iu+qstart)*n], n, &q[start+(ivt+qstart)*n],
&iq[start+k*n], &q[start+(difl+qstart)*n],
&q[start+(difr+qstart)*n], &q[start+(z+qstart)*n],
&q[start+(poles+qstart)*n], &iq[start+givptr*n],
&iq[start+givcol*n], n, &iq[start+perm*n],
&q[start+(givnum+qstart)*n], &q[start+(ic+qstart)*n],
&q[start+(is+qstart)*n], &work[wstart], iwork, info);
}
if (info!=0)
{
return;
}
start = i + 1;
}
}
// Unscale
dlascl("G", 0, 0, ONE, orgnrm, n, 1, d, n, ierr);
}
}
// Use Selection Sort to minimize swaps of singular vectors
int ii, j, kk;
real p;
for (ii=1; ii<n; ii++)
{
i = ii - 1;
kk = i;
p = d[i];
for (j=ii; j<n; j++)
{
if (d[j]>p)
{
kk = j;
p = d[j];
}
}
if (kk!=i)
{
d[kk] = d[i];
d[i] = p;
if (icompq==1)
{
iq[i] = kk+1;
}
else if (icompq==2)
{
Blas<real>::dswap(n, &U[ldu*i], 1, &U[ldu*kk], 1);
Blas<real>::dswap(n, &Vt[i], ldvt, &Vt[kk], ldvt);
}
}
else if (icompq==1)
{
iq[i] = i+1;
}
}
// If icompq = 1, use iq[n-1] as the indicator for uplo
if (icompq==1)
{
if (iuplo==1)
{
iq[nm1] = 1;
}
else
{
iq[nm1] = 0;
}
}
// If B is lower bidiagonal,
// update U by those Givens rotations which rotated B to be upper bidiagonal
if (iuplo==2 && icompq==2)
{
dlasr("L", "V", "B", n, n, work, &work[nm1], U, ldu);
}
}
/*! \fn dbdsqr
*
* \brief §dbdsqr
*
* \details §dbdsqr computes the singular values and, optionally, the right and/or left
* singular vectors from the singular value decomposition (SVD) of a real §n by §n (upper or
* lower) bidiagonal matrix $B$ using the implicit zero-shift QR algorithm. The SVD of $B$ has
* the form\n
* $B = Q S P^T$\n
* where $S$ is the diagonal matrix of singular values, $Q$ is an orthogonal matrix of left
* singular vectors, and $P$ is an orthogonal matrix of right singular vectors. If left
* singular vectors are requested, this subroutine actually returns $UQ$ instead of $Q$, and,
* if right singular vectors are requested, this subroutine returns $P^TV_T$ instead of $P^T$,
* for given real input matrices $U$ and $V_T$. When $U$ and $V_T$ are the orthogonal matrices
* that reduce a general matrix $A$ to bidiagonal form: $A = U B V_T$, as computed by §dgebrd,
* then\n
* $A = (UQ) S (P^TV_T)$\n
* is the SVD of $A$. Optionally, the subroutine may also compute $Q^TC$ for a given real input
* matrix $C$.n
* See "Computing Small Singular Values of Bidiagonal Matrices With Guaranteed High Relative
* Accuracy," by J. Demmel and W. Kahan, LAPACK Working Note #3 (or SIAM J. Sci. Statist.
* Comput. vol. 11, no. 5, pp. 873-912, Sept 1990)\n
* and "Accurate singular values and differential qd algorithms," by B. Parlett and
* V. Fernando, Technical Report CPAM-554, Mathematics Department, University of California
* at Berkeley, July 1992\n
* for a detailed description of the algorithm.
* \param[in] uplo
* 'U': $B$ is upper bidiagonal.\n
* 'L': $B$ is lower bidiagonal.
*
* \param[in] n The order of the matrix $B$. $\{n} \ge 0$.
* \param[in] ncvt The number of columns of the matrix $V_T$. $\{ncvt} \ge 0$.
* \param[in] nru The number of rows of the matrix $U$. $\{nru} \ge 0$.
* \param[in] ncc The number of columns of the matrix $C$. $\{ncc} \ge 0$.
* \param[in,out] d
* an array, dimension (§n)\n
* On entry, the §n diagonal elements of the bidiagonal matrix $B$.\n
* On exit, if §info = 0, the singular values of $B$ in decreasing order.
*
* \param[in,out] e
* an array, dimension (§n-1)\n
* On entry, the §n-1 offdiagonal elements of the bidiagonal matrix $B$.\n
* On exit, if §info = 0, §e is destroyed; if §info > 0, §d and §e will contain the
* diagonal and superdiagonal elements of a bidiagonal matrix orthogonally equivalent to
* the one given as input.
*
* \param[in,out] Vt
* an array, dimension (§ldvt, §ncvt)\n
* On entry, an §n by §ncvt matrix $V_T$.\n
* On exit, §Vt is overwritten by $P^T V_T$.\n
* Not referenced if §ncvt = 0.
*
* \param[in] ldvt
* The leading dimension of the array §Vt.\n
* $\{ldvt}\ge\max(1,\{n})$ if §ncvt > 0;\n
* $\{ldvt}\ge 1$ if §ncvt = 0.
*
* \param[in,out] U
* an array, dimension (§ldu, §n)\n
* On entry, an §nru by §n matrix $U$.\n
* On exit, §U is overwritten by $U Q$.\n
* Not referenced if §nru = 0.
*
* \param[in] ldu The leading dimension of the array §U. $\{ldu} \ge \max(1,\{nru})$.
* \param[in,out] C
* an array, dimension (§ldc, §ncc)\n
* On entry, an §n by §ncc matrix $C$.\n
* On exit, §C is overwritten by $Q^T C$.\n
* Not referenced if §ncc = 0.
*
* \param[in] ldc
* The leading dimension of the array §C.\n
* $\{ldc}\ge\max(1,\{n})$ if §ncc > 0;\n
* $\{ldc}\ge 1$ if §ncc = 0.
*
* \param[out] work an array, dimension (4§n)
* \param[out] info
* =0: Successful exit.\n
* <0: If §info = -§i, the §i-th argument had an illegal value.\n
* >0:\n
* if §ncvt = §nru = §ncc = 0,
* \li = 1, a split was marked by a positive value in §e.
* \li = 2, current block of Z not diagonalized after 30*§n iterations
* (in inner while loop)
* \li = 3, termination criterion of outer while loop not met
* (program created more than n unreduced blocks)
*
* else\n the algorithm did not converge; §d and §e contain the elements of a bidiagonal
* matrix which is orthogonally similar to the input matrix $B$;\n
* if §info = §i, §i elements of §e have not converged to zero.
* \remark
* Bug report from Cezary Dendek.\n
* On March 23rd 2017, the integer variable §maxit = $\{MAXITR}\cdot\{n}^2$ is removed
* since it can overflow pretty easily (for §n larger or equal than 18,919). We instead use
* §maxitdivn = $\{MAXITR}\cdot\{n}$.
* \authors Univ.of Tennessee
* \authors Univ.of California Berkeley
* \authors Univ.of Colorado Denver
* \authors NAG Ltd.
* \date June 2017 */
static void dbdsqr(char const* const uplo, int const n, int const ncvt, int const nru,
int const ncc, real* const d, real* const e, real* const Vt, int const ldvt,
real* const U, int const ldu, real* const C, int const ldc,
real* const work, int& info) /*const*/
{
const real HNDRTH = real(0.01);
/* tolmul
*
* default = max(10, min(100, eps^(-1/8)))\n
* §tolmul controls the convergence criterion of the QR loop.\n
* If it is positive, §tolmul * eps is the desired relative precision in the computed
* singular values.\n
* If it is negative, $|\{tolmul}\ \{eps}\ \{sigma}_\{max}|$ is the desired absolute
* accuracy in the computed singular values (corresponds to relative accuracy
* $|\{tolmul}\ \{eps}|$ in the largest singular value.)\n
* $|\{tolmul}|$ should be between 1 and 1/eps, and preferably between 10
* (for fast convergence) and 0.1/eps (for there to be some accuracy in the results).\n
* Default is to lose at either one eighth or 2 of the available decimal digits in each
* computed singular value (whichever is smaller). */
real tolmul;
/* MAXITR
*
* default = 6\n
* §MAXITR controls the maximum number of passes of the algorithm through its inner loop.
* The algorithms stops (and so fails to converge) if the number of passes through the
* inner loop exceeds $\{MAXITR}\{n}^2$. */
const int MAXITR = 6;
// Test the input parameters.
info = 0;
bool lower = (std::toupper(uplo[0])=='L');
if (!(std::toupper(uplo[0])=='U') && !lower)
{
info = -1;
}
else if (n<0)
{
info = -2;
}
else if (ncvt<0)
{
info = -3;
}
else if (nru<0)
{
info = -4;
}
else if (ncc<0)
{
info = -5;
}
else if ((ncvt==0 && ldvt<1) || (ncvt>0 && ldvt<std::max(1, n)))
{
info = -9;
}
else if (ldu<std::max(1, nru))
{
info = -11;
}
else if ((ncc==0 && ldc<1) || (ncc>0 && ldc<std::max(1, n)))
{
info = -13;
}
if (info!=0)
{
xerbla("DBDSQR", -info);
return;
}
if (n==0)
{
return;
}
int i;
real smin;
int nm1 = n - 1;
if (n!=1)
{
// If no singular vectors desired, use qd algorithm
if (!(ncvt>0 || nru>0 || ncc>0))
{
dlasq1(n, d, e, work, info);
// If info equals 2, dqds didn't finish, try to finish
if (info!=2)
{
return;
}
info = 0;
}
int nm12 = nm1 + nm1;
int nm13 = nm12 + nm1;
int idir = 0;
// Get machine constants
real eps = dlamch("Epsilon");
real unfl = dlamch("Safe minimum");
// If matrix lower bidiagonal, rotate to be upper bidiagonal by applying Givens
// rotations on the left
real cs, sn, r;
if (lower)
{
for (i=0; i<nm1; i++)
{
dlartg(d[i], e[i], cs, sn, r);
d[i] = r;
e[i] = sn * d[i+1];
d[i+1] = cs * d[i+1];
work[i] = cs;
work[nm1+i] = sn;
}
// Update singular vectors if desired
if (nru>0)
{
dlasr("R", "V", "F", nru, n, &work[0], &work[nm1], U, ldu);
}
if (ncc>0)
{
dlasr("L", "V", "F", n, ncc, &work[0], &work[nm1], C, ldc);
}
}
// Compute singular values to relative accuracy tol (By setting tol to be negative,
// algorithm will compute singular values to absolute accuracy |tol|*||input matrix||
tolmul = std::max(TEN, std::min(HNDRD, std::pow(eps, MEIGTH)));
real tol = tolmul * eps;
// Compute approximate maximum, minimum singular values
real smax = ZERO;
for (i=0; i<n; i++)
{
smax = std::max(smax, std::fabs(d[i]));
}
for (i=0; i<nm1; i++)
{
smax = std::max(smax, std::fabs(e[i]));
}
real sminl = ZERO, sminoa, mu, thresh;
if (tol>=ZERO)
{
// Relative accuracy desired
sminoa = std::fabs(d[0]);
if (sminoa!=ZERO)
{
mu = sminoa;
for (i=1; i<n; i++)
{
mu = std::fabs(d[i]) * (mu/(mu+std::fabs(e[i-1])));
if (mu<sminoa)
{
sminoa = mu;
}
if (sminoa==ZERO)
{
break;
}
}
}
sminoa /= std::sqrt(real(n));
thresh = std::max(tol*sminoa, MAXITR*(n*(n*unfl)));
}
else
{
// Absolute accuracy desired
thresh = std::max(std::fabs(tol)*smax, MAXITR*(n*(n*unfl)));
}
// Prepare for main iteration loop for the singular values (MAXIT is the maximum number
// of passes through the inner loop permitted before nonconvergence signalled.)
int maxitdivn = MAXITR * n;
int iterdivn = 0;
int iter = -1;
int oldll = -2;
int oldm = -2;
// m points to last element of unconverged part of matrix
int m = nm1;
// Begin main iteration loop
int ll, lll;
real abse, abss, cosl, cosr, f, g, h, oldcs, oldsn, shift, sigmn, sigmx, sinl, sinr,
sll, temp;
bool breakloop;
while (m>0)
{
// Check for exceeding iteration count
if (iter>=n)
{
iter -= n;
iterdivn++;
if (iterdivn>=maxitdivn)
{
// Maximum number of iterations exceeded, failure to converge
info = 0;
for (i=0; i<nm1; i++)
{
if (e[i]!=ZERO)
{
info++;
}
}
return;
}
}
// Find diagonal block of matrix to work on
if (tol<ZERO && std::fabs(d[m])<=thresh)
{
d[m] = ZERO;
}
smax = std::fabs(d[m]);
smin = smax;
breakloop = false;
for (lll=0; lll<m; lll++)
{
ll = m - lll - 1;
abss = std::fabs(d[ll]);
abse = std::fabs(e[ll]);
if (tol<ZERO && abss<=thresh)
{
d[ll] = ZERO;
}
if (abse<=thresh)
{
breakloop = true;
break;
}
if (abss<smin)
{
smin = abss;
}
temp = std::max(abss, abse);
if (temp>smax)
{
smax = temp;
}
}
if (breakloop)
{
e[ll] = ZERO;
// Matrix splits since e[ll] = 0
if (ll==m-1)
{
// Convergence of bottom singular value, return to top of loop
m--;
continue;
}
}
else
{
ll = -1;
}
ll++;
// e[ll] through e[m-1] are nonzero, e[ll-1] is zero
if (ll==m-1)
{
// 2 by 2 block, handle separately
dlasv2(d[m-1], e[m-1], d[m], sigmn, sigmx, sinr, cosr, sinl, cosl);
d[m-1] = sigmx;
e[m-1] = ZERO;
d[m] = sigmn;
// Compute singular vectors, if desired
if (ncvt>0)
{
Blas<real>::drot(ncvt, &Vt[m-1], ldvt, &Vt[m], ldvt, cosr, sinr);
}
if (nru>0)
{
Blas<real>::drot(nru, &U[ldu*(m-1)], 1, &U[ldu*m], 1, cosl, sinl);
}
if (ncc>0)
{
Blas<real>::drot(ncc, &C[m-1], ldc, &C[m], ldc, cosl, sinl);
}
m -= 2;
continue;
}
// If working on new submatrix, choose shift direction
// (from larger end diagonal element towards smaller)
if (ll>oldm || m<oldll)
{
if (std::fabs(d[ll]) >= std::fabs(d[m]))
{
// Chase bulge from top (big end) to bottom (small end)
idir = 1;
}
else
{
// Chase bulge from bottom (big end) to top (small end)
idir = 2;
}
}
// Apply convergence tests
if (idir==1)
{
// Run convergence test in forward direction
// First apply standard test to bottom of matrix
if (std::fabs(e[m-1])<=std::fabs(tol)*std::fabs(d[m])
|| (tol<ZERO && std::fabs(e[m-1])<=thresh))
{
e[m-1] = ZERO;
continue;
}
if (tol>=ZERO)
{
// If relative accuracy desired, apply convergence criterion forward
mu = std::fabs(d[ll]);
sminl = mu;
breakloop = false;
for (lll=ll; lll<m; lll++)
{
if (std::fabs(e[lll])<=tol*mu)
{
e[lll] = ZERO;
breakloop = true;
break;
}
mu = std::fabs(d[lll+1]) * (mu/(mu+std::fabs(e[lll])));
if (mu<sminl)
{
sminl = mu;
}
}
if (breakloop)
{
continue;
}
}
}
else
{
// Run convergence test in backward direction
// First apply standard test to top of matrix
if (std::fabs(e[ll])<=std::fabs(tol)*std::fabs(d[ll])
|| (tol<ZERO && std::fabs(e[ll])<=thresh))
{
e[ll] = ZERO;
continue;
}
if (tol>=ZERO)
{
// If relative accuracy desired, apply convergence criterion backward
mu = std::fabs(d[m]);
sminl = mu;
breakloop = false;
for (lll=m-1; lll>=ll; lll--)
{
if (std::fabs(e[lll])<=tol*mu)
{
e[lll] = ZERO;
breakloop = true;