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