001:       SUBROUTINE DLASD1( NL, NR, SQRE, D, ALPHA, BETA, U, LDU, VT, LDVT,
002:      $                   IDXQ, IWORK, WORK, INFO )
003: *
004: *  -- LAPACK auxiliary routine (version 3.2) --
005: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
006: *     November 2006
007: *
008: *     .. Scalar Arguments ..
009:       INTEGER            INFO, LDU, LDVT, NL, NR, SQRE
010:       DOUBLE PRECISION   ALPHA, BETA
011: *     ..
012: *     .. Array Arguments ..
013:       INTEGER            IDXQ( * ), IWORK( * )
014:       DOUBLE PRECISION   D( * ), U( LDU, * ), VT( LDVT, * ), WORK( * )
015: *     ..
016: *
017: *  Purpose
018: *  =======
019: *
020: *  DLASD1 computes the SVD of an upper bidiagonal N-by-M matrix B,
021: *  where N = NL + NR + 1 and M = N + SQRE. DLASD1 is called from DLASD0.
022: *
023: *  A related subroutine DLASD7 handles the case in which the singular
024: *  values (and the singular vectors in factored form) are desired.
025: *
026: *  DLASD1 computes the SVD as follows:
027: *
028: *                ( D1(in)  0    0     0 )
029: *    B = U(in) * (   Z1'   a   Z2'    b ) * VT(in)
030: *                (   0     0   D2(in) 0 )
031: *
032: *      = U(out) * ( D(out) 0) * VT(out)
033: *
034: *  where Z' = (Z1' a Z2' b) = u' VT', and u is a vector of dimension M
035: *  with ALPHA and BETA in the NL+1 and NL+2 th entries and zeros
036: *  elsewhere; and the entry b is empty if SQRE = 0.
037: *
038: *  The left singular vectors of the original matrix are stored in U, and
039: *  the transpose of the right singular vectors are stored in VT, and the
040: *  singular values are in D.  The algorithm consists of three stages:
041: *
042: *     The first stage consists of deflating the size of the problem
043: *     when there are multiple singular values or when there are zeros in
044: *     the Z vector.  For each such occurence the dimension of the
045: *     secular equation problem is reduced by one.  This stage is
046: *     performed by the routine DLASD2.
047: *
048: *     The second stage consists of calculating the updated
049: *     singular values. This is done by finding the square roots of the
050: *     roots of the secular equation via the routine DLASD4 (as called
051: *     by DLASD3). This routine also calculates the singular vectors of
052: *     the current problem.
053: *
054: *     The final stage consists of computing the updated singular vectors
055: *     directly using the updated singular values.  The singular vectors
056: *     for the current problem are multiplied with the singular vectors
057: *     from the overall problem.
058: *
059: *  Arguments
060: *  =========
061: *
062: *  NL     (input) INTEGER
063: *         The row dimension of the upper block.  NL >= 1.
064: *
065: *  NR     (input) INTEGER
066: *         The row dimension of the lower block.  NR >= 1.
067: *
068: *  SQRE   (input) INTEGER
069: *         = 0: the lower block is an NR-by-NR square matrix.
070: *         = 1: the lower block is an NR-by-(NR+1) rectangular matrix.
071: *
072: *         The bidiagonal matrix has row dimension N = NL + NR + 1,
073: *         and column dimension M = N + SQRE.
074: *
075: *  D      (input/output) DOUBLE PRECISION array,
076: *                        dimension (N = NL+NR+1).
077: *         On entry D(1:NL,1:NL) contains the singular values of the
078: *         upper block; and D(NL+2:N) contains the singular values of
079: *         the lower block. On exit D(1:N) contains the singular values
080: *         of the modified matrix.
081: *
082: *  ALPHA  (input/output) DOUBLE PRECISION
083: *         Contains the diagonal element associated with the added row.
084: *
085: *  BETA   (input/output) DOUBLE PRECISION
086: *         Contains the off-diagonal element associated with the added
087: *         row.
088: *
089: *  U      (input/output) DOUBLE PRECISION array, dimension(LDU,N)
090: *         On entry U(1:NL, 1:NL) contains the left singular vectors of
091: *         the upper block; U(NL+2:N, NL+2:N) contains the left singular
092: *         vectors of the lower block. On exit U contains the left
093: *         singular vectors of the bidiagonal matrix.
094: *
095: *  LDU    (input) INTEGER
096: *         The leading dimension of the array U.  LDU >= max( 1, N ).
097: *
098: *  VT     (input/output) DOUBLE PRECISION array, dimension(LDVT,M)
099: *         where M = N + SQRE.
100: *         On entry VT(1:NL+1, 1:NL+1)' contains the right singular
101: *         vectors of the upper block; VT(NL+2:M, NL+2:M)' contains
102: *         the right singular vectors of the lower block. On exit
103: *         VT' contains the right singular vectors of the
104: *         bidiagonal matrix.
105: *
106: *  LDVT   (input) INTEGER
107: *         The leading dimension of the array VT.  LDVT >= max( 1, M ).
108: *
109: *  IDXQ  (output) INTEGER array, dimension(N)
110: *         This contains the permutation which will reintegrate the
111: *         subproblem just solved back into sorted order, i.e.
112: *         D( IDXQ( I = 1, N ) ) will be in ascending order.
113: *
114: *  IWORK  (workspace) INTEGER array, dimension( 4 * N )
115: *
116: *  WORK   (workspace) DOUBLE PRECISION array, dimension( 3*M**2 + 2*M )
117: *
118: *  INFO   (output) INTEGER
119: *          = 0:  successful exit.
120: *          < 0:  if INFO = -i, the i-th argument had an illegal value.
121: *          > 0:  if INFO = 1, an singular value did not converge
122: *
123: *  Further Details
124: *  ===============
125: *
126: *  Based on contributions by
127: *     Ming Gu and Huan Ren, Computer Science Division, University of
128: *     California at Berkeley, USA
129: *
130: *  =====================================================================
131: *
132: *     .. Parameters ..
133: *
134:       DOUBLE PRECISION   ONE, ZERO
135:       PARAMETER          ( ONE = 1.0D+0, ZERO = 0.0D+0 )
136: *     ..
137: *     .. Local Scalars ..
138:       INTEGER            COLTYP, I, IDX, IDXC, IDXP, IQ, ISIGMA, IU2,
139:      $                   IVT2, IZ, K, LDQ, LDU2, LDVT2, M, N, N1, N2
140:       DOUBLE PRECISION   ORGNRM
141: *     ..
142: *     .. External Subroutines ..
143:       EXTERNAL           DLAMRG, DLASCL, DLASD2, DLASD3, XERBLA
144: *     ..
145: *     .. Intrinsic Functions ..
146:       INTRINSIC          ABS, MAX
147: *     ..
148: *     .. Executable Statements ..
149: *
150: *     Test the input parameters.
151: *
152:       INFO = 0
153: *
154:       IF( NL.LT.1 ) THEN
155:          INFO = -1
156:       ELSE IF( NR.LT.1 ) THEN
157:          INFO = -2
158:       ELSE IF( ( SQRE.LT.0 ) .OR. ( SQRE.GT.1 ) ) THEN
159:          INFO = -3
160:       END IF
161:       IF( INFO.NE.0 ) THEN
162:          CALL XERBLA( 'DLASD1', -INFO )
163:          RETURN
164:       END IF
165: *
166:       N = NL + NR + 1
167:       M = N + SQRE
168: *
169: *     The following values are for bookkeeping purposes only.  They are
170: *     integer pointers which indicate the portion of the workspace
171: *     used by a particular array in DLASD2 and DLASD3.
172: *
173:       LDU2 = N
174:       LDVT2 = M
175: *
176:       IZ = 1
177:       ISIGMA = IZ + M
178:       IU2 = ISIGMA + N
179:       IVT2 = IU2 + LDU2*N
180:       IQ = IVT2 + LDVT2*M
181: *
182:       IDX = 1
183:       IDXC = IDX + N
184:       COLTYP = IDXC + N
185:       IDXP = COLTYP + N
186: *
187: *     Scale.
188: *
189:       ORGNRM = MAX( ABS( ALPHA ), ABS( BETA ) )
190:       D( NL+1 ) = ZERO
191:       DO 10 I = 1, N
192:          IF( ABS( D( I ) ).GT.ORGNRM ) THEN
193:             ORGNRM = ABS( D( I ) )
194:          END IF
195:    10 CONTINUE
196:       CALL DLASCL( 'G', 0, 0, ORGNRM, ONE, N, 1, D, N, INFO )
197:       ALPHA = ALPHA / ORGNRM
198:       BETA = BETA / ORGNRM
199: *
200: *     Deflate singular values.
201: *
202:       CALL DLASD2( NL, NR, SQRE, K, D, WORK( IZ ), ALPHA, BETA, U, LDU,
203:      $             VT, LDVT, WORK( ISIGMA ), WORK( IU2 ), LDU2,
204:      $             WORK( IVT2 ), LDVT2, IWORK( IDXP ), IWORK( IDX ),
205:      $             IWORK( IDXC ), IDXQ, IWORK( COLTYP ), INFO )
206: *
207: *     Solve Secular Equation and update singular vectors.
208: *
209:       LDQ = K
210:       CALL DLASD3( NL, NR, SQRE, K, D, WORK( IQ ), LDQ, WORK( ISIGMA ),
211:      $             U, LDU, WORK( IU2 ), LDU2, VT, LDVT, WORK( IVT2 ),
212:      $             LDVT2, IWORK( IDXC ), IWORK( COLTYP ), WORK( IZ ),
213:      $             INFO )
214:       IF( INFO.NE.0 ) THEN
215:          RETURN
216:       END IF
217: *
218: *     Unscale.
219: *
220:       CALL DLASCL( 'G', 0, 0, ONE, ORGNRM, N, 1, D, N, INFO )
221: *
222: *     Prepare the IDXQ sorting permutation.
223: *
224:       N1 = K
225:       N2 = N - K
226:       CALL DLAMRG( N1, N2, D, 1, -1, IDXQ )
227: *
228:       RETURN
229: *
230: *     End of DLASD1
231: *
232:       END
233: