233
234
235
236
237
238
239 CHARACTER UPLO
240 INTEGER INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU
241
242
243 REAL D( * ), E( * ), RWORK( * )
244 COMPLEX C( LDC, * ), U( LDU, * ), VT( LDVT, * )
245
246
247
248
249
250 REAL ZERO
251 parameter( zero = 0.0e0 )
252 REAL ONE
253 parameter( one = 1.0e0 )
254 REAL NEGONE
255 parameter( negone = -1.0e0 )
256 REAL HNDRTH
257 parameter( hndrth = 0.01e0 )
258 REAL TEN
259 parameter( ten = 10.0e0 )
260 REAL HNDRD
261 parameter( hndrd = 100.0e0 )
262 REAL MEIGTH
263 parameter( meigth = -0.125e0 )
264 INTEGER MAXITR
265 parameter( maxitr = 6 )
266
267
268 LOGICAL LOWER, ROTATE
269 INTEGER I, IDIR, ISUB, ITER, ITERDIVN, J, LL, LLL, M,
270 $ MAXITDIVN, NM1, NM12, NM13, OLDLL, OLDM
271 REAL ABSE, ABSS, COSL, COSR, CS, EPS, F, G, H, MU,
272 $ OLDCS, OLDSN, R, SHIFT, SIGMN, SIGMX, SINL,
273 $ SINR, SLL, SMAX, SMIN, SMINOA,
274 $ SN, THRESH, TOL, TOLMUL, UNFL
275
276
277 LOGICAL LSAME
278 REAL SLAMCH
280
281
285
286
287 INTRINSIC abs, max, min, real, sign, sqrt
288
289
290
291
292
293 info = 0
294 lower =
lsame( uplo,
'L' )
295 IF( .NOT.
lsame( uplo,
'U' ) .AND. .NOT.lower )
THEN
296 info = -1
297 ELSE IF( n.LT.0 ) THEN
298 info = -2
299 ELSE IF( ncvt.LT.0 ) THEN
300 info = -3
301 ELSE IF( nru.LT.0 ) THEN
302 info = -4
303 ELSE IF( ncc.LT.0 ) THEN
304 info = -5
305 ELSE IF( ( ncvt.EQ.0 .AND. ldvt.LT.1 ) .OR.
306 $ ( ncvt.GT.0 .AND. ldvt.LT.max( 1, n ) ) ) THEN
307 info = -9
308 ELSE IF( ldu.LT.max( 1, nru ) ) THEN
309 info = -11
310 ELSE IF( ( ncc.EQ.0 .AND. ldc.LT.1 ) .OR.
311 $ ( ncc.GT.0 .AND. ldc.LT.max( 1, n ) ) ) THEN
312 info = -13
313 END IF
314 IF( info.NE.0 ) THEN
315 CALL xerbla(
'CBDSQR', -info )
316 RETURN
317 END IF
318 IF( n.EQ.0 )
319 $ RETURN
320 IF( n.EQ.1 )
321 $ GO TO 160
322
323
324
325 rotate = ( ncvt.GT.0 ) .OR. ( nru.GT.0 ) .OR. ( ncc.GT.0 )
326
327
328
329 IF( .NOT.rotate ) THEN
330 CALL slasq1( n, d, e, rwork, info )
331
332
333
334 IF( info .NE. 2 ) RETURN
335 info = 0
336 END IF
337
338 nm1 = n - 1
339 nm12 = nm1 + nm1
340 nm13 = nm12 + nm1
341 idir = 0
342
343
344
346 unfl =
slamch(
'Safe minimum' )
347
348
349
350
351 IF( lower ) THEN
352 DO 10 i = 1, n - 1
353 CALL slartg( d( i ), e( i ), cs, sn, r )
354 d( i ) = r
355 e( i ) = sn*d( i+1 )
356 d( i+1 ) = cs*d( i+1 )
357 rwork( i ) = cs
358 rwork( nm1+i ) = sn
359 10 CONTINUE
360
361
362
363 IF( nru.GT.0 )
364 $
CALL clasr(
'R',
'V',
'F', nru, n, rwork( 1 ),
365 $ rwork( n ),
366 $ u, ldu )
367 IF( ncc.GT.0 )
368 $
CALL clasr(
'L',
'V',
'F', n, ncc, rwork( 1 ),
369 $ rwork( n ),
370 $ c, ldc )
371 END IF
372
373
374
375
376
377 tolmul = max( ten, min( hndrd, eps**meigth ) )
378 tol = tolmul*eps
379
380
381
382 smax = zero
383 DO 20 i = 1, n
384 smax = max( smax, abs( d( i ) ) )
385 20 CONTINUE
386 DO 30 i = 1, n - 1
387 smax = max( smax, abs( e( i ) ) )
388 30 CONTINUE
389 smin = zero
390 IF( tol.GE.zero ) THEN
391
392
393
394 sminoa = abs( d( 1 ) )
395 IF( sminoa.EQ.zero )
396 $ GO TO 50
397 mu = sminoa
398 DO 40 i = 2, n
399 mu = abs( d( i ) )*( mu / ( mu+abs( e( i-1 ) ) ) )
400 sminoa = min( sminoa, mu )
401 IF( sminoa.EQ.zero )
402 $ GO TO 50
403 40 CONTINUE
404 50 CONTINUE
405 sminoa = sminoa / sqrt( real( n ) )
406 thresh = max( tol*sminoa,
407 $ real(maxitr)*(real(n)*(real(n)*unfl)) )
408 ELSE
409
410
411
412 thresh = max( abs( tol )*smax,
413 $ real(maxitr)*(real(n)*(real(n)*unfl)) )
414 END IF
415
416
417
418
419
420 maxitdivn = maxitr*n
421 iterdivn = 0
422 iter = -1
423 oldll = -1
424 oldm = -1
425
426
427
428 m = n
429
430
431
432 60 CONTINUE
433
434
435
436 IF( m.LE.1 )
437 $ GO TO 160
438 IF( iter.GE.n ) THEN
439 iter = iter - n
440 iterdivn = iterdivn + 1
441 IF( iterdivn.GE.maxitdivn )
442 $ GO TO 200
443 END IF
444
445
446
447 IF( tol.LT.zero .AND. abs( d( m ) ).LE.thresh )
448 $ d( m ) = zero
449 smax = abs( d( m ) )
450 DO 70 lll = 1, m - 1
451 ll = m - lll
452 abss = abs( d( ll ) )
453 abse = abs( e( ll ) )
454 IF( tol.LT.zero .AND. abss.LE.thresh )
455 $ d( ll ) = zero
456 IF( abse.LE.thresh )
457 $ GO TO 80
458 smax = max( smax, abss, abse )
459 70 CONTINUE
460 ll = 0
461 GO TO 90
462 80 CONTINUE
463 e( ll ) = zero
464
465
466
467 IF( ll.EQ.m-1 ) THEN
468
469
470
471 m = m - 1
472 GO TO 60
473 END IF
474 90 CONTINUE
475 ll = ll + 1
476
477
478
479 IF( ll.EQ.m-1 ) THEN
480
481
482
483 CALL slasv2( d( m-1 ), e( m-1 ), d( m ), sigmn, sigmx, sinr,
484 $ cosr, sinl, cosl )
485 d( m-1 ) = sigmx
486 e( m-1 ) = zero
487 d( m ) = sigmn
488
489
490
491 IF( ncvt.GT.0 )
492 $
CALL csrot( ncvt, vt( m-1, 1 ), ldvt, vt( m, 1 ), ldvt,
493 $ cosr, sinr )
494 IF( nru.GT.0 )
495 $
CALL csrot( nru, u( 1, m-1 ), 1, u( 1, m ), 1, cosl,
496 $ sinl )
497 IF( ncc.GT.0 )
498 $
CALL csrot( ncc, c( m-1, 1 ), ldc, c( m, 1 ), ldc, cosl,
499 $ sinl )
500 m = m - 2
501 GO TO 60
502 END IF
503
504
505
506
507 IF( ll.GT.oldm .OR. m.LT.oldll ) THEN
508 IF( abs( d( ll ) ).GE.abs( d( m ) ) ) THEN
509
510
511
512 idir = 1
513 ELSE
514
515
516
517 idir = 2
518 END IF
519 END IF
520
521
522
523 IF( idir.EQ.1 ) THEN
524
525
526
527
528 IF( abs( e( m-1 ) ).LE.abs( tol )*abs( d( m ) ) .OR.
529 $ ( tol.LT.zero .AND. abs( e( m-1 ) ).LE.thresh ) ) THEN
530 e( m-1 ) = zero
531 GO TO 60
532 END IF
533
534 IF( tol.GE.zero ) THEN
535
536
537
538
539 mu = abs( d( ll ) )
540 smin = mu
541 DO 100 lll = ll, m - 1
542 IF( abs( e( lll ) ).LE.tol*mu ) THEN
543 e( lll ) = zero
544 GO TO 60
545 END IF
546 mu = abs( d( lll+1 ) )*( mu / ( mu+abs( e( lll ) ) ) )
547 smin = min( smin, mu )
548 100 CONTINUE
549 END IF
550
551 ELSE
552
553
554
555
556 IF( abs( e( ll ) ).LE.abs( tol )*abs( d( ll ) ) .OR.
557 $ ( tol.LT.zero .AND. abs( e( ll ) ).LE.thresh ) ) THEN
558 e( ll ) = zero
559 GO TO 60
560 END IF
561
562 IF( tol.GE.zero ) THEN
563
564
565
566
567 mu = abs( d( m ) )
568 smin = mu
569 DO 110 lll = m - 1, ll, -1
570 IF( abs( e( lll ) ).LE.tol*mu ) THEN
571 e( lll ) = zero
572 GO TO 60
573 END IF
574 mu = abs( d( lll ) )*( mu / ( mu+abs( e( lll ) ) ) )
575 smin = min( smin, mu )
576 110 CONTINUE
577 END IF
578 END IF
579 oldll = ll
580 oldm = m
581
582
583
584
585 IF( tol.GE.zero .AND. real( n )*tol*( smin / smax ).LE.
586 $ max( eps, hndrth*tol ) ) THEN
587
588
589
590 shift = zero
591 ELSE
592
593
594
595 IF( idir.EQ.1 ) THEN
596 sll = abs( d( ll ) )
597 CALL slas2( d( m-1 ), e( m-1 ), d( m ), shift, r )
598 ELSE
599 sll = abs( d( m ) )
600 CALL slas2( d( ll ), e( ll ), d( ll+1 ), shift, r )
601 END IF
602
603
604
605 IF( sll.GT.zero ) THEN
606 IF( ( shift / sll )**2.LT.eps )
607 $ shift = zero
608 END IF
609 END IF
610
611
612
613 iter = iter + m - ll
614
615
616
617 IF( shift.EQ.zero ) THEN
618 IF( idir.EQ.1 ) THEN
619
620
621
622
623 cs = one
624 oldcs = one
625 DO 120 i = ll, m - 1
626 CALL slartg( d( i )*cs, e( i ), cs, sn, r )
627 IF( i.GT.ll )
628 $ e( i-1 ) = oldsn*r
629 CALL slartg( oldcs*r, d( i+1 )*sn, oldcs, oldsn,
630 $ d( i ) )
631 rwork( i-ll+1 ) = cs
632 rwork( i-ll+1+nm1 ) = sn
633 rwork( i-ll+1+nm12 ) = oldcs
634 rwork( i-ll+1+nm13 ) = oldsn
635 120 CONTINUE
636 h = d( m )*cs
637 d( m ) = h*oldcs
638 e( m-1 ) = h*oldsn
639
640
641
642 IF( ncvt.GT.0 )
643 $
CALL clasr(
'L',
'V',
'F', m-ll+1, ncvt, rwork( 1 ),
644 $ rwork( n ), vt( ll, 1 ), ldvt )
645 IF( nru.GT.0 )
646 $
CALL clasr(
'R',
'V',
'F', nru, m-ll+1,
647 $ rwork( nm12+1 ),
648 $ rwork( nm13+1 ), u( 1, ll ), ldu )
649 IF( ncc.GT.0 )
650 $
CALL clasr(
'L',
'V',
'F', m-ll+1, ncc,
651 $ rwork( nm12+1 ),
652 $ rwork( nm13+1 ), c( ll, 1 ), ldc )
653
654
655
656 IF( abs( e( m-1 ) ).LE.thresh )
657 $ e( m-1 ) = zero
658
659 ELSE
660
661
662
663
664 cs = one
665 oldcs = one
666 DO 130 i = m, ll + 1, -1
667 CALL slartg( d( i )*cs, e( i-1 ), cs, sn, r )
668 IF( i.LT.m )
669 $ e( i ) = oldsn*r
670 CALL slartg( oldcs*r, d( i-1 )*sn, oldcs, oldsn,
671 $ d( i ) )
672 rwork( i-ll ) = cs
673 rwork( i-ll+nm1 ) = -sn
674 rwork( i-ll+nm12 ) = oldcs
675 rwork( i-ll+nm13 ) = -oldsn
676 130 CONTINUE
677 h = d( ll )*cs
678 d( ll ) = h*oldcs
679 e( ll ) = h*oldsn
680
681
682
683 IF( ncvt.GT.0 )
684 $
CALL clasr(
'L',
'V',
'B', m-ll+1, ncvt,
685 $ rwork( nm12+1 ),
686 $ rwork( nm13+1 ), vt( ll, 1 ), ldvt )
687 IF( nru.GT.0 )
688 $
CALL clasr(
'R',
'V',
'B', nru, m-ll+1, rwork( 1 ),
689 $ rwork( n ), u( 1, ll ), ldu )
690 IF( ncc.GT.0 )
691 $
CALL clasr(
'L',
'V',
'B', m-ll+1, ncc, rwork( 1 ),
692 $ rwork( n ), c( ll, 1 ), ldc )
693
694
695
696 IF( abs( e( ll ) ).LE.thresh )
697 $ e( ll ) = zero
698 END IF
699 ELSE
700
701
702
703 IF( idir.EQ.1 ) THEN
704
705
706
707
708 f = ( abs( d( ll ) )-shift )*
709 $ ( sign( one, d( ll ) )+shift / d( ll ) )
710 g = e( ll )
711 DO 140 i = ll, m - 1
712 CALL slartg( f, g, cosr, sinr, r )
713 IF( i.GT.ll )
714 $ e( i-1 ) = r
715 f = cosr*d( i ) + sinr*e( i )
716 e( i ) = cosr*e( i ) - sinr*d( i )
717 g = sinr*d( i+1 )
718 d( i+1 ) = cosr*d( i+1 )
719 CALL slartg( f, g, cosl, sinl, r )
720 d( i ) = r
721 f = cosl*e( i ) + sinl*d( i+1 )
722 d( i+1 ) = cosl*d( i+1 ) - sinl*e( i )
723 IF( i.LT.m-1 ) THEN
724 g = sinl*e( i+1 )
725 e( i+1 ) = cosl*e( i+1 )
726 END IF
727 rwork( i-ll+1 ) = cosr
728 rwork( i-ll+1+nm1 ) = sinr
729 rwork( i-ll+1+nm12 ) = cosl
730 rwork( i-ll+1+nm13 ) = sinl
731 140 CONTINUE
732 e( m-1 ) = f
733
734
735
736 IF( ncvt.GT.0 )
737 $
CALL clasr(
'L',
'V',
'F', m-ll+1, ncvt, rwork( 1 ),
738 $ rwork( n ), vt( ll, 1 ), ldvt )
739 IF( nru.GT.0 )
740 $
CALL clasr(
'R',
'V',
'F', nru, m-ll+1,
741 $ rwork( nm12+1 ),
742 $ rwork( nm13+1 ), u( 1, ll ), ldu )
743 IF( ncc.GT.0 )
744 $
CALL clasr(
'L',
'V',
'F', m-ll+1, ncc,
745 $ rwork( nm12+1 ),
746 $ rwork( nm13+1 ), c( ll, 1 ), ldc )
747
748
749
750 IF( abs( e( m-1 ) ).LE.thresh )
751 $ e( m-1 ) = zero
752
753 ELSE
754
755
756
757
758 f = ( abs( d( m ) )-shift )*( sign( one, d( m ) )+shift /
759 $ d( m ) )
760 g = e( m-1 )
761 DO 150 i = m, ll + 1, -1
762 CALL slartg( f, g, cosr, sinr, r )
763 IF( i.LT.m )
764 $ e( i ) = r
765 f = cosr*d( i ) + sinr*e( i-1 )
766 e( i-1 ) = cosr*e( i-1 ) - sinr*d( i )
767 g = sinr*d( i-1 )
768 d( i-1 ) = cosr*d( i-1 )
769 CALL slartg( f, g, cosl, sinl, r )
770 d( i ) = r
771 f = cosl*e( i-1 ) + sinl*d( i-1 )
772 d( i-1 ) = cosl*d( i-1 ) - sinl*e( i-1 )
773 IF( i.GT.ll+1 ) THEN
774 g = sinl*e( i-2 )
775 e( i-2 ) = cosl*e( i-2 )
776 END IF
777 rwork( i-ll ) = cosr
778 rwork( i-ll+nm1 ) = -sinr
779 rwork( i-ll+nm12 ) = cosl
780 rwork( i-ll+nm13 ) = -sinl
781 150 CONTINUE
782 e( ll ) = f
783
784
785
786 IF( abs( e( ll ) ).LE.thresh )
787 $ e( ll ) = zero
788
789
790
791 IF( ncvt.GT.0 )
792 $
CALL clasr(
'L',
'V',
'B', m-ll+1, ncvt,
793 $ rwork( nm12+1 ),
794 $ rwork( nm13+1 ), vt( ll, 1 ), ldvt )
795 IF( nru.GT.0 )
796 $
CALL clasr(
'R',
'V',
'B', nru, m-ll+1, rwork( 1 ),
797 $ rwork( n ), u( 1, ll ), ldu )
798 IF( ncc.GT.0 )
799 $
CALL clasr(
'L',
'V',
'B', m-ll+1, ncc, rwork( 1 ),
800 $ rwork( n ), c( ll, 1 ), ldc )
801 END IF
802 END IF
803
804
805
806 GO TO 60
807
808
809
810 160 CONTINUE
811 DO 170 i = 1, n
812 IF( d( i ).EQ.zero ) THEN
813
814
815
816 d( i ) = zero
817 END IF
818 IF( d( i ).LT.zero ) THEN
819 d( i ) = -d( i )
820
821
822
823 IF( ncvt.GT.0 )
824 $
CALL csscal( ncvt, negone, vt( i, 1 ), ldvt )
825 END IF
826 170 CONTINUE
827
828
829
830
831 DO 190 i = 1, n - 1
832
833
834
835 isub = 1
836 smin = d( 1 )
837 DO 180 j = 2, n + 1 - i
838 IF( d( j ).LE.smin ) THEN
839 isub = j
840 smin = d( j )
841 END IF
842 180 CONTINUE
843 IF( isub.NE.n+1-i ) THEN
844
845
846
847 d( isub ) = d( n+1-i )
848 d( n+1-i ) = smin
849 IF( ncvt.GT.0 )
850 $
CALL cswap( ncvt, vt( isub, 1 ), ldvt, vt( n+1-i, 1 ),
851 $ ldvt )
852 IF( nru.GT.0 )
853 $
CALL cswap( nru, u( 1, isub ), 1, u( 1, n+1-i ), 1 )
854 IF( ncc.GT.0 )
855 $
CALL cswap( ncc, c( isub, 1 ), ldc, c( n+1-i, 1 ),
856 $ ldc )
857 END IF
858 190 CONTINUE
859 GO TO 220
860
861
862
863 200 CONTINUE
864 info = 0
865 DO 210 i = 1, n - 1
866 IF( e( i ).NE.zero )
867 $ info = info + 1
868 210 CONTINUE
869 220 CONTINUE
870 RETURN
871
872
873
subroutine xerbla(srname, info)
real function slamch(cmach)
SLAMCH
subroutine slartg(f, g, c, s, r)
SLARTG generates a plane rotation with real cosine and real sine.
subroutine slas2(f, g, h, ssmin, ssmax)
SLAS2 computes singular values of a 2-by-2 triangular matrix.
subroutine slasq1(n, d, e, work, info)
SLASQ1 computes the singular values of a real square bidiagonal matrix. Used by sbdsqr.
subroutine clasr(side, pivot, direct, m, n, c, s, a, lda)
CLASR applies a sequence of plane rotations to a general rectangular matrix.
subroutine slasv2(f, g, h, ssmin, ssmax, snr, csr, snl, csl)
SLASV2 computes the singular value decomposition of a 2-by-2 triangular matrix.
logical function lsame(ca, cb)
LSAME
subroutine csrot(n, cx, incx, cy, incy, c, s)
CSROT
subroutine csscal(n, sa, cx, incx)
CSSCAL
subroutine cswap(n, cx, incx, cy, incy)
CSWAP