1 *> \brief \b DLATRS solves a triangular system of equations with the scale factor set to prevent overflow.
3 * =========== DOCUMENTATION ===========
5 * Online html documentation available at
6 * http://www.netlib.org/lapack/explore-html/
9 *> Download DLATRS + dependencies
10 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dlatrs.f">
12 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dlatrs.f">
14 *> <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dlatrs.f">
21 * SUBROUTINE DLATRS( UPLO, TRANS, DIAG, NORMIN, N, A, LDA, X, SCALE,
24 * .. Scalar Arguments ..
25 * CHARACTER DIAG, NORMIN, TRANS, UPLO
26 * INTEGER INFO, LDA, N
27 * DOUBLE PRECISION SCALE
29 * .. Array Arguments ..
30 * DOUBLE PRECISION A( LDA, * ), CNORM( * ), X( * )
39 *> DLATRS solves one of the triangular systems
41 *> A *x = s*b or A**T *x = s*b
43 *> with scaling to prevent overflow. Here A is an upper or lower
44 *> triangular matrix, A**T denotes the transpose of A, x and b are
45 *> n-element vectors, and s is a scaling factor, usually less than
46 *> or equal to 1, chosen so that the components of x will be less than
47 *> the overflow threshold. If the unscaled problem will not cause
48 *> overflow, the Level 2 BLAS routine DTRSV is called. If the matrix A
49 *> is singular (A(j,j) = 0 for some j), then s is set to 0 and a
50 *> non-trivial solution to A*x = 0 is returned.
58 *> UPLO is CHARACTER*1
59 *> Specifies whether the matrix A is upper or lower triangular.
60 *> = 'U': Upper triangular
61 *> = 'L': Lower triangular
66 *> TRANS is CHARACTER*1
67 *> Specifies the operation applied to A.
68 *> = 'N': Solve A * x = s*b (No transpose)
69 *> = 'T': Solve A**T* x = s*b (Transpose)
70 *> = 'C': Solve A**T* x = s*b (Conjugate transpose = Transpose)
75 *> DIAG is CHARACTER*1
76 *> Specifies whether or not the matrix A is unit triangular.
77 *> = 'N': Non-unit triangular
78 *> = 'U': Unit triangular
83 *> NORMIN is CHARACTER*1
84 *> Specifies whether CNORM has been set or not.
85 *> = 'Y': CNORM contains the column norms on entry
86 *> = 'N': CNORM is not set on entry. On exit, the norms will
87 *> be computed and stored in CNORM.
93 *> The order of the matrix A. N >= 0.
98 *> A is DOUBLE PRECISION array, dimension (LDA,N)
99 *> The triangular matrix A. If UPLO = 'U', the leading n by n
100 *> upper triangular part of the array A contains the upper
101 *> triangular matrix, and the strictly lower triangular part of
102 *> A is not referenced. If UPLO = 'L', the leading n by n lower
103 *> triangular part of the array A contains the lower triangular
104 *> matrix, and the strictly upper triangular part of A is not
105 *> referenced. If DIAG = 'U', the diagonal elements of A are
106 *> also not referenced and are assumed to be 1.
112 *> The leading dimension of the array A. LDA >= max (1,N).
117 *> X is DOUBLE PRECISION array, dimension (N)
118 *> On entry, the right hand side b of the triangular system.
119 *> On exit, X is overwritten by the solution vector x.
124 *> SCALE is DOUBLE PRECISION
125 *> The scaling factor s for the triangular system
126 *> A * x = s*b or A**T* x = s*b.
127 *> If SCALE = 0, the matrix A is singular or badly scaled, and
128 *> the vector x is an exact or approximate solution to A*x = 0.
131 *> \param[in,out] CNORM
133 *> CNORM is DOUBLE PRECISION array, dimension (N)
135 *> If NORMIN = 'Y', CNORM is an input argument and CNORM(j)
136 *> contains the norm of the off-diagonal part of the j-th column
137 *> of A. If TRANS = 'N', CNORM(j) must be greater than or equal
138 *> to the infinity-norm, and if TRANS = 'T' or 'C', CNORM(j)
139 *> must be greater than or equal to the 1-norm.
141 *> If NORMIN = 'N', CNORM is an output argument and CNORM(j)
142 *> returns the 1-norm of the offdiagonal part of the j-th column
149 *> = 0: successful exit
150 *> < 0: if INFO = -k, the k-th argument had an illegal value
156 *> \author Univ. of Tennessee
157 *> \author Univ. of California Berkeley
158 *> \author Univ. of Colorado Denver
161 *> \date September 2012
163 *> \ingroup doubleOTHERauxiliary
165 *> \par Further Details:
166 * =====================
170 *> A rough bound on x is computed; if that is less than overflow, DTRSV
171 *> is called, otherwise, specific code is used which checks for possible
172 *> overflow or divide-by-zero at every operation.
174 *> A columnwise scheme is used for solving A*x = b. The basic algorithm
175 *> if A is lower triangular is
179 *> x(j) := x(j) / A(j,j)
180 *> x[j+1:n] := x[j+1:n] - x(j) * A[j+1:n,j]
183 *> Define bounds on the components of x after j iterations of the loop:
184 *> M(j) = bound on x[1:j]
185 *> G(j) = bound on x[j+1:n]
186 *> Initially, let M(0) = 0 and G(0) = max{x(i), i=1,...,n}.
188 *> Then for iteration j+1 we have
189 *> M(j+1) <= G(j) / | A(j+1,j+1) |
190 *> G(j+1) <= G(j) + M(j+1) * | A[j+2:n,j+1] |
191 *> <= G(j) ( 1 + CNORM(j+1) / | A(j+1,j+1) | )
193 *> where CNORM(j+1) is greater than or equal to the infinity-norm of
194 *> column j+1 of A, not counting the diagonal. Hence
196 *> G(j) <= G(0) product ( 1 + CNORM(i) / | A(i,i) | )
200 *> |x(j)| <= ( G(0) / |A(j,j)| ) product ( 1 + CNORM(i) / |A(i,i)| )
203 *> Since |x(j)| <= M(j), we use the Level 2 BLAS routine DTRSV if the
204 *> reciprocal of the largest M(j), j=1,..,n, is larger than
205 *> max(underflow, 1/overflow).
207 *> The bound on x(j) is also used to determine when a step in the
208 *> columnwise method can be performed without fear of overflow. If
209 *> the computed bound is greater than a large constant, x is scaled to
210 *> prevent overflow, but if the bound overflows, x is set to 0, x(j) to
211 *> 1, and scale to 0, and a non-trivial solution to A*x = 0 is found.
213 *> Similarly, a row-wise scheme is used to solve A**T*x = b. The basic
214 *> algorithm for A upper triangular is
217 *> x(j) := ( b(j) - A[1:j-1,j]**T * x[1:j-1] ) / A(j,j)
220 *> We simultaneously compute two bounds
221 *> G(j) = bound on ( b(i) - A[1:i-1,i]**T * x[1:i-1] ), 1<=i<=j
222 *> M(j) = bound on x(i), 1<=i<=j
224 *> The initial values are G(0) = 0, M(0) = max{b(i), i=1,..,n}, and we
225 *> add the constraint G(j) >= G(j-1) and M(j) >= M(j-1) for j >= 1.
226 *> Then the bound on x(j) is
228 *> M(j) <= M(j-1) * ( 1 + CNORM(j) ) / | A(j,j) |
230 *> <= M(0) * product ( ( 1 + CNORM(i) ) / |A(i,i)| )
233 *> and we can safely call DTRSV if 1/M(n) and 1/G(n) are both greater
234 *> than max(underflow, 1/overflow).
237 * =====================================================================
238 SUBROUTINE DLATRS( UPLO, TRANS, DIAG, NORMIN, N, A, LDA, X, SCALE,
241 * -- LAPACK auxiliary routine (version 3.4.2) --
242 * -- LAPACK is a software package provided by Univ. of Tennessee, --
243 * -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..--
246 * .. Scalar Arguments ..
247 CHARACTER DIAG, NORMIN, TRANS, UPLO
249 DOUBLE PRECISION SCALE
251 * .. Array Arguments ..
252 DOUBLE PRECISION A( LDA, * ), CNORM( * ), X( * )
255 * =====================================================================
258 DOUBLE PRECISION ZERO, HALF, ONE
259 PARAMETER ( ZERO = 0.0D+0, HALF = 0.5D+0, ONE = 1.0D+0 )
261 * .. Local Scalars ..
262 LOGICAL NOTRAN, NOUNIT, UPPER
263 INTEGER I, IMAX, J, JFIRST, JINC, JLAST
264 DOUBLE PRECISION BIGNUM, GROW, REC, SMLNUM, SUMJ, TJJ, TJJS,
265 $ TMAX, TSCAL, USCAL, XBND, XJ, XMAX
267 * .. External Functions ..
270 DOUBLE PRECISION DASUM, DDOT, DLAMCH
271 EXTERNAL LSAME, IDAMAX, DASUM, DDOT, DLAMCH
273 * .. External Subroutines ..
274 EXTERNAL DAXPY, DSCAL, DTRSV, XERBLA
276 * .. Intrinsic Functions ..
277 INTRINSIC ABS, MAX, MIN
279 * .. Executable Statements ..
282 UPPER = LSAME( UPLO, 'U' )
283 NOTRAN = LSAME( TRANS, 'N' )
284 NOUNIT = LSAME( DIAG, 'N' )
286 * Test the input parameters.
288 IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
290 ELSE IF( .NOT.NOTRAN .AND. .NOT.LSAME( TRANS, 'T' ) .AND. .NOT.
291 $ LSAME( TRANS, 'C' ) ) THEN
293 ELSE IF( .NOT.NOUNIT .AND. .NOT.LSAME( DIAG, 'U' ) ) THEN
295 ELSE IF( .NOT.LSAME( NORMIN, 'Y' ) .AND. .NOT.
296 $ LSAME( NORMIN, 'N' ) ) THEN
298 ELSE IF( N.LT.0 ) THEN
300 ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
304 CALL XERBLA( 'DLATRS', -INFO )
308 * Quick return if possible
313 * Determine machine dependent parameters to control overflow.
315 SMLNUM = DLAMCH( 'Safe minimum' ) / DLAMCH( 'Precision' )
316 BIGNUM = ONE / SMLNUM
319 IF( LSAME( NORMIN, 'N' ) ) THEN
321 * Compute the 1-norm of each column, not including the diagonal.
325 * A is upper triangular.
328 CNORM( J ) = DASUM( J-1, A( 1, J ), 1 )
332 * A is lower triangular.
335 CNORM( J ) = DASUM( N-J, A( J+1, J ), 1 )
341 * Scale the column norms by TSCAL if the maximum element in CNORM is
342 * greater than BIGNUM.
344 IMAX = IDAMAX( N, CNORM, 1 )
346 IF( TMAX.LE.BIGNUM ) THEN
349 TSCAL = ONE / ( SMLNUM*TMAX )
350 CALL DSCAL( N, TSCAL, CNORM, 1 )
353 * Compute a bound on the computed solution vector to see if the
354 * Level 2 BLAS routine DTRSV can be used.
356 J = IDAMAX( N, X, 1 )
361 * Compute the growth in A * x = b.
373 IF( TSCAL.NE.ONE ) THEN
380 * A is non-unit triangular.
382 * Compute GROW = 1/G(j) and XBND = 1/M(j).
383 * Initially, G(0) = max{x(i), i=1,...,n}.
385 GROW = ONE / MAX( XBND, SMLNUM )
387 DO 30 J = JFIRST, JLAST, JINC
389 * Exit the loop if the growth factor is too small.
394 * M(j) = G(j-1) / abs(A(j,j))
396 TJJ = ABS( A( J, J ) )
397 XBND = MIN( XBND, MIN( ONE, TJJ )*GROW )
398 IF( TJJ+CNORM( J ).GE.SMLNUM ) THEN
400 * G(j) = G(j-1)*( 1 + CNORM(j) / abs(A(j,j)) )
402 GROW = GROW*( TJJ / ( TJJ+CNORM( J ) ) )
405 * G(j) could overflow, set GROW to 0.
413 * A is unit triangular.
415 * Compute GROW = 1/G(j), where G(0) = max{x(i), i=1,...,n}.
417 GROW = MIN( ONE, ONE / MAX( XBND, SMLNUM ) )
418 DO 40 J = JFIRST, JLAST, JINC
420 * Exit the loop if the growth factor is too small.
425 * G(j) = G(j-1)*( 1 + CNORM(j) )
427 GROW = GROW*( ONE / ( ONE+CNORM( J ) ) )
434 * Compute the growth in A**T * x = b.
446 IF( TSCAL.NE.ONE ) THEN
453 * A is non-unit triangular.
455 * Compute GROW = 1/G(j) and XBND = 1/M(j).
456 * Initially, M(0) = max{x(i), i=1,...,n}.
458 GROW = ONE / MAX( XBND, SMLNUM )
460 DO 60 J = JFIRST, JLAST, JINC
462 * Exit the loop if the growth factor is too small.
467 * G(j) = max( G(j-1), M(j-1)*( 1 + CNORM(j) ) )
469 XJ = ONE + CNORM( J )
470 GROW = MIN( GROW, XBND / XJ )
472 * M(j) = M(j-1)*( 1 + CNORM(j) ) / abs(A(j,j))
474 TJJ = ABS( A( J, J ) )
476 $ XBND = XBND*( TJJ / XJ )
478 GROW = MIN( GROW, XBND )
481 * A is unit triangular.
483 * Compute GROW = 1/G(j), where G(0) = max{x(i), i=1,...,n}.
485 GROW = MIN( ONE, ONE / MAX( XBND, SMLNUM ) )
486 DO 70 J = JFIRST, JLAST, JINC
488 * Exit the loop if the growth factor is too small.
493 * G(j) = ( 1 + CNORM(j) )*G(j-1)
495 XJ = ONE + CNORM( J )
502 IF( ( GROW*TSCAL ).GT.SMLNUM ) THEN
504 * Use the Level 2 BLAS solve if the reciprocal of the bound on
505 * elements of X is not too small.
507 CALL DTRSV( UPLO, TRANS, DIAG, N, A, LDA, X, 1 )
510 * Use a Level 1 BLAS solve, scaling intermediate results.
512 IF( XMAX.GT.BIGNUM ) THEN
514 * Scale X so that its components are less than or equal to
515 * BIGNUM in absolute value.
517 SCALE = BIGNUM / XMAX
518 CALL DSCAL( N, SCALE, X, 1 )
526 DO 110 J = JFIRST, JLAST, JINC
528 * Compute x(j) = b(j) / A(j,j), scaling x if necessary.
532 TJJS = A( J, J )*TSCAL
539 IF( TJJ.GT.SMLNUM ) THEN
541 * abs(A(j,j)) > SMLNUM:
543 IF( TJJ.LT.ONE ) THEN
544 IF( XJ.GT.TJJ*BIGNUM ) THEN
549 CALL DSCAL( N, REC, X, 1 )
554 X( J ) = X( J ) / TJJS
556 ELSE IF( TJJ.GT.ZERO ) THEN
558 * 0 < abs(A(j,j)) <= SMLNUM:
560 IF( XJ.GT.TJJ*BIGNUM ) THEN
562 * Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM
563 * to avoid overflow when dividing by A(j,j).
565 REC = ( TJJ*BIGNUM ) / XJ
566 IF( CNORM( J ).GT.ONE ) THEN
568 * Scale by 1/CNORM(j) to avoid overflow when
569 * multiplying x(j) times column j.
571 REC = REC / CNORM( J )
573 CALL DSCAL( N, REC, X, 1 )
577 X( J ) = X( J ) / TJJS
581 * A(j,j) = 0: Set x(1:n) = 0, x(j) = 1, and
582 * scale = 0, and compute a solution to A*x = 0.
594 * Scale x if necessary to avoid overflow when adding a
595 * multiple of column j of A.
599 IF( CNORM( J ).GT.( BIGNUM-XMAX )*REC ) THEN
601 * Scale x by 1/(2*abs(x(j))).
604 CALL DSCAL( N, REC, X, 1 )
607 ELSE IF( XJ*CNORM( J ).GT.( BIGNUM-XMAX ) ) THEN
611 CALL DSCAL( N, HALF, X, 1 )
619 * x(1:j-1) := x(1:j-1) - x(j) * A(1:j-1,j)
621 CALL DAXPY( J-1, -X( J )*TSCAL, A( 1, J ), 1, X,
623 I = IDAMAX( J-1, X, 1 )
630 * x(j+1:n) := x(j+1:n) - x(j) * A(j+1:n,j)
632 CALL DAXPY( N-J, -X( J )*TSCAL, A( J+1, J ), 1,
634 I = J + IDAMAX( N-J, X( J+1 ), 1 )
644 DO 160 J = JFIRST, JLAST, JINC
646 * Compute x(j) = b(j) - sum A(k,j)*x(k).
651 REC = ONE / MAX( XMAX, ONE )
652 IF( CNORM( J ).GT.( BIGNUM-XJ )*REC ) THEN
654 * If x(j) could overflow, scale x by 1/(2*XMAX).
658 TJJS = A( J, J )*TSCAL
663 IF( TJJ.GT.ONE ) THEN
665 * Divide by A(j,j) when scaling x if A(j,j) > 1.
667 REC = MIN( ONE, REC*TJJ )
670 IF( REC.LT.ONE ) THEN
671 CALL DSCAL( N, REC, X, 1 )
678 IF( USCAL.EQ.ONE ) THEN
680 * If the scaling needed for A in the dot product is 1,
681 * call DDOT to perform the dot product.
684 SUMJ = DDOT( J-1, A( 1, J ), 1, X, 1 )
685 ELSE IF( J.LT.N ) THEN
686 SUMJ = DDOT( N-J, A( J+1, J ), 1, X( J+1 ), 1 )
690 * Otherwise, use in-line code for the dot product.
694 SUMJ = SUMJ + ( A( I, J )*USCAL )*X( I )
696 ELSE IF( J.LT.N ) THEN
698 SUMJ = SUMJ + ( A( I, J )*USCAL )*X( I )
703 IF( USCAL.EQ.TSCAL ) THEN
705 * Compute x(j) := ( x(j) - sumj ) / A(j,j) if 1/A(j,j)
706 * was not used to scale the dotproduct.
708 X( J ) = X( J ) - SUMJ
711 TJJS = A( J, J )*TSCAL
718 * Compute x(j) = x(j) / A(j,j), scaling if necessary.
721 IF( TJJ.GT.SMLNUM ) THEN
723 * abs(A(j,j)) > SMLNUM:
725 IF( TJJ.LT.ONE ) THEN
726 IF( XJ.GT.TJJ*BIGNUM ) THEN
728 * Scale X by 1/abs(x(j)).
731 CALL DSCAL( N, REC, X, 1 )
736 X( J ) = X( J ) / TJJS
737 ELSE IF( TJJ.GT.ZERO ) THEN
739 * 0 < abs(A(j,j)) <= SMLNUM:
741 IF( XJ.GT.TJJ*BIGNUM ) THEN
743 * Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM.
745 REC = ( TJJ*BIGNUM ) / XJ
746 CALL DSCAL( N, REC, X, 1 )
750 X( J ) = X( J ) / TJJS
753 * A(j,j) = 0: Set x(1:n) = 0, x(j) = 1, and
754 * scale = 0, and compute a solution to A**T*x = 0.
766 * Compute x(j) := x(j) / A(j,j) - sumj if the dot
767 * product has already been divided by 1/A(j,j).
769 X( J ) = X( J ) / TJJS - SUMJ
771 XMAX = MAX( XMAX, ABS( X( J ) ) )
774 SCALE = SCALE / TSCAL
777 * Scale the column norms by 1/TSCAL for return.
779 IF( TSCAL.NE.ONE ) THEN
780 CALL DSCAL( N, ONE / TSCAL, CNORM, 1 )