LAPACK 3.3.1 Linear Algebra PACKage

sbdsdc.f

Go to the documentation of this file.
```00001       SUBROUTINE SBDSDC( UPLO, COMPQ, N, D, E, U, LDU, VT, LDVT, Q, IQ,
00002      \$                   WORK, IWORK, INFO )
00003 *
00004 *  -- LAPACK routine (version 3.2.2) --
00005 *  -- LAPACK is a software package provided by Univ. of Tennessee,    --
00006 *  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
00007 *     June 2010
00008 *
00009 *     .. Scalar Arguments ..
00010       CHARACTER          COMPQ, UPLO
00011       INTEGER            INFO, LDU, LDVT, N
00012 *     ..
00013 *     .. Array Arguments ..
00014       INTEGER            IQ( * ), IWORK( * )
00015       REAL               D( * ), E( * ), Q( * ), U( LDU, * ),
00016      \$                   VT( LDVT, * ), WORK( * )
00017 *     ..
00018 *
00019 *  Purpose
00020 *  =======
00021 *
00022 *  SBDSDC computes the singular value decomposition (SVD) of a real
00023 *  N-by-N (upper or lower) bidiagonal matrix B:  B = U * S * VT,
00024 *  using a divide and conquer method, where S is a diagonal matrix
00025 *  with non-negative diagonal elements (the singular values of B), and
00026 *  U and VT are orthogonal matrices of left and right singular vectors,
00027 *  respectively. SBDSDC can be used to compute all singular values,
00028 *  and optionally, singular vectors or singular vectors in compact form.
00029 *
00030 *  This code makes very mild assumptions about floating point
00031 *  arithmetic. It will work on machines with a guard digit in
00032 *  add/subtract, or on those binary machines without guard digits
00033 *  which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2.
00034 *  It could conceivably fail on hexadecimal or decimal machines
00035 *  without guard digits, but we know of none.  See SLASD3 for details.
00036 *
00037 *  The code currently calls SLASDQ if singular values only are desired.
00038 *  However, it can be slightly modified to compute singular values
00039 *  using the divide and conquer method.
00040 *
00041 *  Arguments
00042 *  =========
00043 *
00044 *  UPLO    (input) CHARACTER*1
00045 *          = 'U':  B is upper bidiagonal.
00046 *          = 'L':  B is lower bidiagonal.
00047 *
00048 *  COMPQ   (input) CHARACTER*1
00049 *          Specifies whether singular vectors are to be computed
00050 *          as follows:
00051 *          = 'N':  Compute singular values only;
00052 *          = 'P':  Compute singular values and compute singular
00053 *                  vectors in compact form;
00054 *          = 'I':  Compute singular values and singular vectors.
00055 *
00056 *  N       (input) INTEGER
00057 *          The order of the matrix B.  N >= 0.
00058 *
00059 *  D       (input/output) REAL array, dimension (N)
00060 *          On entry, the n diagonal elements of the bidiagonal matrix B.
00061 *          On exit, if INFO=0, the singular values of B.
00062 *
00063 *  E       (input/output) REAL array, dimension (N-1)
00064 *          On entry, the elements of E contain the offdiagonal
00065 *          elements of the bidiagonal matrix whose SVD is desired.
00066 *          On exit, E has been destroyed.
00067 *
00068 *  U       (output) REAL array, dimension (LDU,N)
00069 *          If  COMPQ = 'I', then:
00070 *             On exit, if INFO = 0, U contains the left singular vectors
00071 *             of the bidiagonal matrix.
00072 *          For other values of COMPQ, U is not referenced.
00073 *
00074 *  LDU     (input) INTEGER
00075 *          The leading dimension of the array U.  LDU >= 1.
00076 *          If singular vectors are desired, then LDU >= max( 1, N ).
00077 *
00078 *  VT      (output) REAL array, dimension (LDVT,N)
00079 *          If  COMPQ = 'I', then:
00080 *             On exit, if INFO = 0, VT**T contains the right singular
00081 *             vectors of the bidiagonal matrix.
00082 *          For other values of COMPQ, VT is not referenced.
00083 *
00084 *  LDVT    (input) INTEGER
00085 *          The leading dimension of the array VT.  LDVT >= 1.
00086 *          If singular vectors are desired, then LDVT >= max( 1, N ).
00087 *
00088 *  Q       (output) REAL array, dimension (LDQ)
00089 *          If  COMPQ = 'P', then:
00090 *             On exit, if INFO = 0, Q and IQ contain the left
00091 *             and right singular vectors in a compact form,
00092 *             requiring O(N log N) space instead of 2*N**2.
00093 *             In particular, Q contains all the REAL data in
00094 *             LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1))))
00095 *             words of memory, where SMLSIZ is returned by ILAENV and
00096 *             is equal to the maximum size of the subproblems at the
00097 *             bottom of the computation tree (usually about 25).
00098 *          For other values of COMPQ, Q is not referenced.
00099 *
00100 *  IQ      (output) INTEGER array, dimension (LDIQ)
00101 *          If  COMPQ = 'P', then:
00102 *             On exit, if INFO = 0, Q and IQ contain the left
00103 *             and right singular vectors in a compact form,
00104 *             requiring O(N log N) space instead of 2*N**2.
00105 *             In particular, IQ contains all INTEGER data in
00106 *             LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1))))
00107 *             words of memory, where SMLSIZ is returned by ILAENV and
00108 *             is equal to the maximum size of the subproblems at the
00109 *             bottom of the computation tree (usually about 25).
00110 *          For other values of COMPQ, IQ is not referenced.
00111 *
00112 *  WORK    (workspace) REAL array, dimension (MAX(1,LWORK))
00113 *          If COMPQ = 'N' then LWORK >= (4 * N).
00114 *          If COMPQ = 'P' then LWORK >= (6 * N).
00115 *          If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N).
00116 *
00117 *  IWORK   (workspace) INTEGER array, dimension (8*N)
00118 *
00119 *  INFO    (output) INTEGER
00120 *          = 0:  successful exit.
00121 *          < 0:  if INFO = -i, the i-th argument had an illegal value.
00122 *          > 0:  The algorithm failed to compute a singular value.
00123 *                The update process of divide and conquer failed.
00124 *
00125 *  Further Details
00126 *  ===============
00127 *
00128 *  Based on contributions by
00129 *     Ming Gu and Huan Ren, Computer Science Division, University of
00130 *     California at Berkeley, USA
00131 *  =====================================================================
00132 *  Changed dimension statement in comment describing E from (N) to
00133 *  (N-1).  Sven, 17 Feb 05.
00134 *  =====================================================================
00135 *
00136 *     .. Parameters ..
00137       REAL               ZERO, ONE, TWO
00138       PARAMETER          ( ZERO = 0.0E+0, ONE = 1.0E+0, TWO = 2.0E+0 )
00139 *     ..
00140 *     .. Local Scalars ..
00141       INTEGER            DIFL, DIFR, GIVCOL, GIVNUM, GIVPTR, I, IC,
00142      \$                   ICOMPQ, IERR, II, IS, IU, IUPLO, IVT, J, K, KK,
00143      \$                   MLVL, NM1, NSIZE, PERM, POLES, QSTART, SMLSIZ,
00144      \$                   SMLSZP, SQRE, START, WSTART, Z
00145       REAL               CS, EPS, ORGNRM, P, R, SN
00146 *     ..
00147 *     .. External Functions ..
00148       LOGICAL            LSAME
00149       INTEGER            ILAENV
00150       REAL               SLAMCH, SLANST
00151       EXTERNAL           SLAMCH, SLANST, ILAENV, LSAME
00152 *     ..
00153 *     .. External Subroutines ..
00154       EXTERNAL           SCOPY, SLARTG, SLASCL, SLASD0, SLASDA, SLASDQ,
00155      \$                   SLASET, SLASR, SSWAP, XERBLA
00156 *     ..
00157 *     .. Intrinsic Functions ..
00158       INTRINSIC          REAL, ABS, INT, LOG, SIGN
00159 *     ..
00160 *     .. Executable Statements ..
00161 *
00162 *     Test the input parameters.
00163 *
00164       INFO = 0
00165 *
00166       IUPLO = 0
00167       IF( LSAME( UPLO, 'U' ) )
00168      \$   IUPLO = 1
00169       IF( LSAME( UPLO, 'L' ) )
00170      \$   IUPLO = 2
00171       IF( LSAME( COMPQ, 'N' ) ) THEN
00172          ICOMPQ = 0
00173       ELSE IF( LSAME( COMPQ, 'P' ) ) THEN
00174          ICOMPQ = 1
00175       ELSE IF( LSAME( COMPQ, 'I' ) ) THEN
00176          ICOMPQ = 2
00177       ELSE
00178          ICOMPQ = -1
00179       END IF
00180       IF( IUPLO.EQ.0 ) THEN
00181          INFO = -1
00182       ELSE IF( ICOMPQ.LT.0 ) THEN
00183          INFO = -2
00184       ELSE IF( N.LT.0 ) THEN
00185          INFO = -3
00186       ELSE IF( ( LDU.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDU.LT.
00187      \$         N ) ) ) THEN
00188          INFO = -7
00189       ELSE IF( ( LDVT.LT.1 ) .OR. ( ( ICOMPQ.EQ.2 ) .AND. ( LDVT.LT.
00190      \$         N ) ) ) THEN
00191          INFO = -9
00192       END IF
00193       IF( INFO.NE.0 ) THEN
00194          CALL XERBLA( 'SBDSDC', -INFO )
00195          RETURN
00196       END IF
00197 *
00198 *     Quick return if possible
00199 *
00200       IF( N.EQ.0 )
00201      \$   RETURN
00202       SMLSIZ = ILAENV( 9, 'SBDSDC', ' ', 0, 0, 0, 0 )
00203       IF( N.EQ.1 ) THEN
00204          IF( ICOMPQ.EQ.1 ) THEN
00205             Q( 1 ) = SIGN( ONE, D( 1 ) )
00206             Q( 1+SMLSIZ*N ) = ONE
00207          ELSE IF( ICOMPQ.EQ.2 ) THEN
00208             U( 1, 1 ) = SIGN( ONE, D( 1 ) )
00209             VT( 1, 1 ) = ONE
00210          END IF
00211          D( 1 ) = ABS( D( 1 ) )
00212          RETURN
00213       END IF
00214       NM1 = N - 1
00215 *
00216 *     If matrix lower bidiagonal, rotate to be upper bidiagonal
00217 *     by applying Givens rotations on the left
00218 *
00219       WSTART = 1
00220       QSTART = 3
00221       IF( ICOMPQ.EQ.1 ) THEN
00222          CALL SCOPY( N, D, 1, Q( 1 ), 1 )
00223          CALL SCOPY( N-1, E, 1, Q( N+1 ), 1 )
00224       END IF
00225       IF( IUPLO.EQ.2 ) THEN
00226          QSTART = 5
00227          WSTART = 2*N - 1
00228          DO 10 I = 1, N - 1
00229             CALL SLARTG( D( I ), E( I ), CS, SN, R )
00230             D( I ) = R
00231             E( I ) = SN*D( I+1 )
00232             D( I+1 ) = CS*D( I+1 )
00233             IF( ICOMPQ.EQ.1 ) THEN
00234                Q( I+2*N ) = CS
00235                Q( I+3*N ) = SN
00236             ELSE IF( ICOMPQ.EQ.2 ) THEN
00237                WORK( I ) = CS
00238                WORK( NM1+I ) = -SN
00239             END IF
00240    10    CONTINUE
00241       END IF
00242 *
00243 *     If ICOMPQ = 0, use SLASDQ to compute the singular values.
00244 *
00245       IF( ICOMPQ.EQ.0 ) THEN
00246          CALL SLASDQ( 'U', 0, N, 0, 0, 0, D, E, VT, LDVT, U, LDU, U,
00247      \$                LDU, WORK( WSTART ), INFO )
00248          GO TO 40
00249       END IF
00250 *
00251 *     If N is smaller than the minimum divide size SMLSIZ, then solve
00252 *     the problem with another solver.
00253 *
00254       IF( N.LE.SMLSIZ ) THEN
00255          IF( ICOMPQ.EQ.2 ) THEN
00256             CALL SLASET( 'A', N, N, ZERO, ONE, U, LDU )
00257             CALL SLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
00258             CALL SLASDQ( 'U', 0, N, N, N, 0, D, E, VT, LDVT, U, LDU, U,
00259      \$                   LDU, WORK( WSTART ), INFO )
00260          ELSE IF( ICOMPQ.EQ.1 ) THEN
00261             IU = 1
00262             IVT = IU + N
00263             CALL SLASET( 'A', N, N, ZERO, ONE, Q( IU+( QSTART-1 )*N ),
00264      \$                   N )
00265             CALL SLASET( 'A', N, N, ZERO, ONE, Q( IVT+( QSTART-1 )*N ),
00266      \$                   N )
00267             CALL SLASDQ( 'U', 0, N, N, N, 0, D, E,
00268      \$                   Q( IVT+( QSTART-1 )*N ), N,
00269      \$                   Q( IU+( QSTART-1 )*N ), N,
00270      \$                   Q( IU+( QSTART-1 )*N ), N, WORK( WSTART ),
00271      \$                   INFO )
00272          END IF
00273          GO TO 40
00274       END IF
00275 *
00276       IF( ICOMPQ.EQ.2 ) THEN
00277          CALL SLASET( 'A', N, N, ZERO, ONE, U, LDU )
00278          CALL SLASET( 'A', N, N, ZERO, ONE, VT, LDVT )
00279       END IF
00280 *
00281 *     Scale.
00282 *
00283       ORGNRM = SLANST( 'M', N, D, E )
00284       IF( ORGNRM.EQ.ZERO )
00285      \$   RETURN
00286       CALL SLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, IERR )
00287       CALL SLASCL( 'G', 0, 0, ORGNRM, ONE, NM1, 1, E, NM1, IERR )
00288 *
00289       EPS = SLAMCH( 'Epsilon' )
00290 *
00291       MLVL = INT( LOG( REAL( N ) / REAL( SMLSIZ+1 ) ) / LOG( TWO ) ) + 1
00292       SMLSZP = SMLSIZ + 1
00293 *
00294       IF( ICOMPQ.EQ.1 ) THEN
00295          IU = 1
00296          IVT = 1 + SMLSIZ
00297          DIFL = IVT + SMLSZP
00298          DIFR = DIFL + MLVL
00299          Z = DIFR + MLVL*2
00300          IC = Z + MLVL
00301          IS = IC + 1
00302          POLES = IS + 1
00303          GIVNUM = POLES + 2*MLVL
00304 *
00305          K = 1
00306          GIVPTR = 2
00307          PERM = 3
00308          GIVCOL = PERM + MLVL
00309       END IF
00310 *
00311       DO 20 I = 1, N
00312          IF( ABS( D( I ) ).LT.EPS ) THEN
00313             D( I ) = SIGN( EPS, D( I ) )
00314          END IF
00315    20 CONTINUE
00316 *
00317       START = 1
00318       SQRE = 0
00319 *
00320       DO 30 I = 1, NM1
00321          IF( ( ABS( E( I ) ).LT.EPS ) .OR. ( I.EQ.NM1 ) ) THEN
00322 *
00323 *        Subproblem found. First determine its size and then
00324 *        apply divide and conquer on it.
00325 *
00326             IF( I.LT.NM1 ) THEN
00327 *
00328 *        A subproblem with E(I) small for I < NM1.
00329 *
00330                NSIZE = I - START + 1
00331             ELSE IF( ABS( E( I ) ).GE.EPS ) THEN
00332 *
00333 *        A subproblem with E(NM1) not too small but I = NM1.
00334 *
00335                NSIZE = N - START + 1
00336             ELSE
00337 *
00338 *        A subproblem with E(NM1) small. This implies an
00339 *        1-by-1 subproblem at D(N). Solve this 1-by-1 problem
00340 *        first.
00341 *
00342                NSIZE = I - START + 1
00343                IF( ICOMPQ.EQ.2 ) THEN
00344                   U( N, N ) = SIGN( ONE, D( N ) )
00345                   VT( N, N ) = ONE
00346                ELSE IF( ICOMPQ.EQ.1 ) THEN
00347                   Q( N+( QSTART-1 )*N ) = SIGN( ONE, D( N ) )
00348                   Q( N+( SMLSIZ+QSTART-1 )*N ) = ONE
00349                END IF
00350                D( N ) = ABS( D( N ) )
00351             END IF
00352             IF( ICOMPQ.EQ.2 ) THEN
00353                CALL SLASD0( NSIZE, SQRE, D( START ), E( START ),
00354      \$                      U( START, START ), LDU, VT( START, START ),
00355      \$                      LDVT, SMLSIZ, IWORK, WORK( WSTART ), INFO )
00356             ELSE
00357                CALL SLASDA( ICOMPQ, SMLSIZ, NSIZE, SQRE, D( START ),
00358      \$                      E( START ), Q( START+( IU+QSTART-2 )*N ), N,
00359      \$                      Q( START+( IVT+QSTART-2 )*N ),
00360      \$                      IQ( START+K*N ), Q( START+( DIFL+QSTART-2 )*
00361      \$                      N ), Q( START+( DIFR+QSTART-2 )*N ),
00362      \$                      Q( START+( Z+QSTART-2 )*N ),
00363      \$                      Q( START+( POLES+QSTART-2 )*N ),
00364      \$                      IQ( START+GIVPTR*N ), IQ( START+GIVCOL*N ),
00365      \$                      N, IQ( START+PERM*N ),
00366      \$                      Q( START+( GIVNUM+QSTART-2 )*N ),
00367      \$                      Q( START+( IC+QSTART-2 )*N ),
00368      \$                      Q( START+( IS+QSTART-2 )*N ),
00369      \$                      WORK( WSTART ), IWORK, INFO )
00370             END IF
00371             IF( INFO.NE.0 ) THEN
00372                RETURN
00373             END IF
00374             START = I + 1
00375          END IF
00376    30 CONTINUE
00377 *
00378 *     Unscale
00379 *
00380       CALL SLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, IERR )
00381    40 CONTINUE
00382 *
00383 *     Use Selection Sort to minimize swaps of singular vectors
00384 *
00385       DO 60 II = 2, N
00386          I = II - 1
00387          KK = I
00388          P = D( I )
00389          DO 50 J = II, N
00390             IF( D( J ).GT.P ) THEN
00391                KK = J
00392                P = D( J )
00393             END IF
00394    50    CONTINUE
00395          IF( KK.NE.I ) THEN
00396             D( KK ) = D( I )
00397             D( I ) = P
00398             IF( ICOMPQ.EQ.1 ) THEN
00399                IQ( I ) = KK
00400             ELSE IF( ICOMPQ.EQ.2 ) THEN
00401                CALL SSWAP( N, U( 1, I ), 1, U( 1, KK ), 1 )
00402                CALL SSWAP( N, VT( I, 1 ), LDVT, VT( KK, 1 ), LDVT )
00403             END IF
00404          ELSE IF( ICOMPQ.EQ.1 ) THEN
00405             IQ( I ) = I
00406          END IF
00407    60 CONTINUE
00408 *
00409 *     If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO
00410 *
00411       IF( ICOMPQ.EQ.1 ) THEN
00412          IF( IUPLO.EQ.1 ) THEN
00413             IQ( N ) = 1
00414          ELSE
00415             IQ( N ) = 0
00416          END IF
00417       END IF
00418 *
00419 *     If B is lower bidiagonal, update U by those Givens rotations
00420 *     which rotated B to be upper bidiagonal
00421 *
00422       IF( ( IUPLO.EQ.2 ) .AND. ( ICOMPQ.EQ.2 ) )
00423      \$   CALL SLASR( 'L', 'V', 'B', N, N, WORK( 1 ), WORK( N ), U, LDU )
00424 *
00425       RETURN
00426 *
00427 *     End of SBDSDC
00428 *
00429       END
```