Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/mathlibs/src/lapack/cgeevx.f
5213 views
1
SUBROUTINE CGEEVX( BALANC, JOBVL, JOBVR, SENSE, N, A, LDA, W, VL,
2
$ LDVL, VR, LDVR, ILO, IHI, SCALE, ABNRM, RCONDE,
3
$ RCONDV, WORK, LWORK, RWORK, INFO )
4
*
5
* -- LAPACK driver routine (version 3.0) --
6
* Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
7
* Courant Institute, Argonne National Lab, and Rice University
8
* June 30, 1999
9
*
10
* .. Scalar Arguments ..
11
CHARACTER BALANC, JOBVL, JOBVR, SENSE
12
INTEGER IHI, ILO, INFO, LDA, LDVL, LDVR, LWORK, N
13
REAL ABNRM
14
* ..
15
* .. Array Arguments ..
16
REAL RCONDE( * ), RCONDV( * ), RWORK( * ),
17
$ SCALE( * )
18
COMPLEX A( LDA, * ), VL( LDVL, * ), VR( LDVR, * ),
19
$ W( * ), WORK( * )
20
* ..
21
*
22
* Purpose
23
* =======
24
*
25
* CGEEVX computes for an N-by-N complex nonsymmetric matrix A, the
26
* eigenvalues and, optionally, the left and/or right eigenvectors.
27
*
28
* Optionally also, it computes a balancing transformation to improve
29
* the conditioning of the eigenvalues and eigenvectors (ILO, IHI,
30
* SCALE, and ABNRM), reciprocal condition numbers for the eigenvalues
31
* (RCONDE), and reciprocal condition numbers for the right
32
* eigenvectors (RCONDV).
33
*
34
* The right eigenvector v(j) of A satisfies
35
* A * v(j) = lambda(j) * v(j)
36
* where lambda(j) is its eigenvalue.
37
* The left eigenvector u(j) of A satisfies
38
* u(j)**H * A = lambda(j) * u(j)**H
39
* where u(j)**H denotes the conjugate transpose of u(j).
40
*
41
* The computed eigenvectors are normalized to have Euclidean norm
42
* equal to 1 and largest component real.
43
*
44
* Balancing a matrix means permuting the rows and columns to make it
45
* more nearly upper triangular, and applying a diagonal similarity
46
* transformation D * A * D**(-1), where D is a diagonal matrix, to
47
* make its rows and columns closer in norm and the condition numbers
48
* of its eigenvalues and eigenvectors smaller. The computed
49
* reciprocal condition numbers correspond to the balanced matrix.
50
* Permuting rows and columns will not change the condition numbers
51
* (in exact arithmetic) but diagonal scaling will. For further
52
* explanation of balancing, see section 4.10.2 of the LAPACK
53
* Users' Guide.
54
*
55
* Arguments
56
* =========
57
*
58
* BALANC (input) CHARACTER*1
59
* Indicates how the input matrix should be diagonally scaled
60
* and/or permuted to improve the conditioning of its
61
* eigenvalues.
62
* = 'N': Do not diagonally scale or permute;
63
* = 'P': Perform permutations to make the matrix more nearly
64
* upper triangular. Do not diagonally scale;
65
* = 'S': Diagonally scale the matrix, ie. replace A by
66
* D*A*D**(-1), where D is a diagonal matrix chosen
67
* to make the rows and columns of A more equal in
68
* norm. Do not permute;
69
* = 'B': Both diagonally scale and permute A.
70
*
71
* Computed reciprocal condition numbers will be for the matrix
72
* after balancing and/or permuting. Permuting does not change
73
* condition numbers (in exact arithmetic), but balancing does.
74
*
75
* JOBVL (input) CHARACTER*1
76
* = 'N': left eigenvectors of A are not computed;
77
* = 'V': left eigenvectors of A are computed.
78
* If SENSE = 'E' or 'B', JOBVL must = 'V'.
79
*
80
* JOBVR (input) CHARACTER*1
81
* = 'N': right eigenvectors of A are not computed;
82
* = 'V': right eigenvectors of A are computed.
83
* If SENSE = 'E' or 'B', JOBVR must = 'V'.
84
*
85
* SENSE (input) CHARACTER*1
86
* Determines which reciprocal condition numbers are computed.
87
* = 'N': None are computed;
88
* = 'E': Computed for eigenvalues only;
89
* = 'V': Computed for right eigenvectors only;
90
* = 'B': Computed for eigenvalues and right eigenvectors.
91
*
92
* If SENSE = 'E' or 'B', both left and right eigenvectors
93
* must also be computed (JOBVL = 'V' and JOBVR = 'V').
94
*
95
* N (input) INTEGER
96
* The order of the matrix A. N >= 0.
97
*
98
* A (input/output) COMPLEX array, dimension (LDA,N)
99
* On entry, the N-by-N matrix A.
100
* On exit, A has been overwritten. If JOBVL = 'V' or
101
* JOBVR = 'V', A contains the Schur form of the balanced
102
* version of the matrix A.
103
*
104
* LDA (input) INTEGER
105
* The leading dimension of the array A. LDA >= max(1,N).
106
*
107
* W (output) COMPLEX array, dimension (N)
108
* W contains the computed eigenvalues.
109
*
110
* VL (output) COMPLEX array, dimension (LDVL,N)
111
* If JOBVL = 'V', the left eigenvectors u(j) are stored one
112
* after another in the columns of VL, in the same order
113
* as their eigenvalues.
114
* If JOBVL = 'N', VL is not referenced.
115
* u(j) = VL(:,j), the j-th column of VL.
116
*
117
* LDVL (input) INTEGER
118
* The leading dimension of the array VL. LDVL >= 1; if
119
* JOBVL = 'V', LDVL >= N.
120
*
121
* VR (output) COMPLEX array, dimension (LDVR,N)
122
* If JOBVR = 'V', the right eigenvectors v(j) are stored one
123
* after another in the columns of VR, in the same order
124
* as their eigenvalues.
125
* If JOBVR = 'N', VR is not referenced.
126
* v(j) = VR(:,j), the j-th column of VR.
127
*
128
* LDVR (input) INTEGER
129
* The leading dimension of the array VR. LDVR >= 1; if
130
* JOBVR = 'V', LDVR >= N.
131
*
132
* ILO,IHI (output) INTEGER
133
* ILO and IHI are integer values determined when A was
134
* balanced. The balanced A(i,j) = 0 if I > J and
135
* J = 1,...,ILO-1 or I = IHI+1,...,N.
136
*
137
* SCALE (output) REAL array, dimension (N)
138
* Details of the permutations and scaling factors applied
139
* when balancing A. If P(j) is the index of the row and column
140
* interchanged with row and column j, and D(j) is the scaling
141
* factor applied to row and column j, then
142
* SCALE(J) = P(J), for J = 1,...,ILO-1
143
* = D(J), for J = ILO,...,IHI
144
* = P(J) for J = IHI+1,...,N.
145
* The order in which the interchanges are made is N to IHI+1,
146
* then 1 to ILO-1.
147
*
148
* ABNRM (output) REAL
149
* The one-norm of the balanced matrix (the maximum
150
* of the sum of absolute values of elements of any column).
151
*
152
* RCONDE (output) REAL array, dimension (N)
153
* RCONDE(j) is the reciprocal condition number of the j-th
154
* eigenvalue.
155
*
156
* RCONDV (output) REAL array, dimension (N)
157
* RCONDV(j) is the reciprocal condition number of the j-th
158
* right eigenvector.
159
*
160
* WORK (workspace/output) COMPLEX array, dimension (LWORK)
161
* On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
162
*
163
* LWORK (input) INTEGER
164
* The dimension of the array WORK. If SENSE = 'N' or 'E',
165
* LWORK >= max(1,2*N), and if SENSE = 'V' or 'B',
166
* LWORK >= N*N+2*N.
167
* For good performance, LWORK must generally be larger.
168
*
169
* If LWORK = -1, then a workspace query is assumed; the routine
170
* only calculates the optimal size of the WORK array, returns
171
* this value as the first entry of the WORK array, and no error
172
* message related to LWORK is issued by XERBLA.
173
*
174
* RWORK (workspace) REAL array, dimension (2*N)
175
*
176
* INFO (output) INTEGER
177
* = 0: successful exit
178
* < 0: if INFO = -i, the i-th argument had an illegal value.
179
* > 0: if INFO = i, the QR algorithm failed to compute all the
180
* eigenvalues, and no eigenvectors or condition numbers
181
* have been computed; elements 1:ILO-1 and i+1:N of W
182
* contain eigenvalues which have converged.
183
*
184
* =====================================================================
185
*
186
* .. Parameters ..
187
REAL ZERO, ONE
188
PARAMETER ( ZERO = 0.0E0, ONE = 1.0E0 )
189
* ..
190
* .. Local Scalars ..
191
LOGICAL LQUERY, SCALEA, WANTVL, WANTVR, WNTSNB, WNTSNE,
192
$ WNTSNN, WNTSNV
193
CHARACTER JOB, SIDE
194
INTEGER HSWORK, I, ICOND, IERR, ITAU, IWRK, K, MAXB,
195
$ MAXWRK, MINWRK, NOUT
196
REAL ANRM, BIGNUM, CSCALE, EPS, SCL, SMLNUM
197
COMPLEX TMP
198
* ..
199
* .. Local Arrays ..
200
LOGICAL SELECT( 1 )
201
REAL DUM( 1 )
202
* ..
203
* .. External Subroutines ..
204
EXTERNAL CGEBAK, CGEBAL, CGEHRD, CHSEQR, CLACPY, CLASCL,
205
$ CSCAL, CSSCAL, CTREVC, CTRSNA, CUNGHR, SLABAD,
206
$ SLASCL, XERBLA
207
* ..
208
* .. External Functions ..
209
LOGICAL LSAME
210
INTEGER ILAENV, ISAMAX
211
REAL CLANGE, SCNRM2, SLAMCH
212
EXTERNAL LSAME, ILAENV, ISAMAX, CLANGE, SCNRM2, SLAMCH
213
* ..
214
* .. Intrinsic Functions ..
215
INTRINSIC AIMAG, CMPLX, CONJG, MAX, MIN, REAL, SQRT
216
* ..
217
* .. Executable Statements ..
218
*
219
* Test the input arguments
220
*
221
INFO = 0
222
LQUERY = ( LWORK.EQ.-1 )
223
WANTVL = LSAME( JOBVL, 'V' )
224
WANTVR = LSAME( JOBVR, 'V' )
225
WNTSNN = LSAME( SENSE, 'N' )
226
WNTSNE = LSAME( SENSE, 'E' )
227
WNTSNV = LSAME( SENSE, 'V' )
228
WNTSNB = LSAME( SENSE, 'B' )
229
IF( .NOT.( LSAME( BALANC, 'N' ) .OR. LSAME( BALANC, 'S' ) .OR.
230
$ LSAME( BALANC, 'P' ) .OR. LSAME( BALANC, 'B' ) ) ) THEN
231
INFO = -1
232
ELSE IF( ( .NOT.WANTVL ) .AND. ( .NOT.LSAME( JOBVL, 'N' ) ) ) THEN
233
INFO = -2
234
ELSE IF( ( .NOT.WANTVR ) .AND. ( .NOT.LSAME( JOBVR, 'N' ) ) ) THEN
235
INFO = -3
236
ELSE IF( .NOT.( WNTSNN .OR. WNTSNE .OR. WNTSNB .OR. WNTSNV ) .OR.
237
$ ( ( WNTSNE .OR. WNTSNB ) .AND. .NOT.( WANTVL .AND.
238
$ WANTVR ) ) ) THEN
239
INFO = -4
240
ELSE IF( N.LT.0 ) THEN
241
INFO = -5
242
ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
243
INFO = -7
244
ELSE IF( LDVL.LT.1 .OR. ( WANTVL .AND. LDVL.LT.N ) ) THEN
245
INFO = -10
246
ELSE IF( LDVR.LT.1 .OR. ( WANTVR .AND. LDVR.LT.N ) ) THEN
247
INFO = -12
248
END IF
249
*
250
* Compute workspace
251
* (Note: Comments in the code beginning "Workspace:" describe the
252
* minimal amount of workspace needed at that point in the code,
253
* as well as the preferred amount for good performance.
254
* CWorkspace refers to complex workspace, and RWorkspace to real
255
* workspace. NB refers to the optimal block size for the
256
* immediately following subroutine, as returned by ILAENV.
257
* HSWORK refers to the workspace preferred by CHSEQR, as
258
* calculated below. HSWORK is computed assuming ILO=1 and IHI=N,
259
* the worst case.)
260
*
261
MINWRK = 1
262
IF( INFO.EQ.0 .AND. ( LWORK.GE.1 .OR. LQUERY ) ) THEN
263
MAXWRK = N + N*ILAENV( 1, 'CGEHRD', ' ', N, 1, N, 0 )
264
IF( ( .NOT.WANTVL ) .AND. ( .NOT.WANTVR ) ) THEN
265
MINWRK = MAX( 1, 2*N )
266
IF( .NOT.( WNTSNN .OR. WNTSNE ) )
267
$ MINWRK = MAX( MINWRK, N*N+2*N )
268
MAXB = MAX( ILAENV( 8, 'CHSEQR', 'SN', N, 1, N, -1 ), 2 )
269
IF( WNTSNN ) THEN
270
K = MIN( MAXB, N, MAX( 2, ILAENV( 4, 'CHSEQR', 'EN', N,
271
$ 1, N, -1 ) ) )
272
ELSE
273
K = MIN( MAXB, N, MAX( 2, ILAENV( 4, 'CHSEQR', 'SN', N,
274
$ 1, N, -1 ) ) )
275
END IF
276
HSWORK = MAX( K*( K+2 ), 2*N )
277
MAXWRK = MAX( MAXWRK, 1, HSWORK )
278
IF( .NOT.( WNTSNN .OR. WNTSNE ) )
279
$ MAXWRK = MAX( MAXWRK, N*N+2*N )
280
ELSE
281
MINWRK = MAX( 1, 2*N )
282
IF( .NOT.( WNTSNN .OR. WNTSNE ) )
283
$ MINWRK = MAX( MINWRK, N*N+2*N )
284
MAXB = MAX( ILAENV( 8, 'CHSEQR', 'SN', N, 1, N, -1 ), 2 )
285
K = MIN( MAXB, N, MAX( 2, ILAENV( 4, 'CHSEQR', 'EN', N, 1,
286
$ N, -1 ) ) )
287
HSWORK = MAX( K*( K+2 ), 2*N )
288
MAXWRK = MAX( MAXWRK, 1, HSWORK )
289
MAXWRK = MAX( MAXWRK, N+( N-1 )*
290
$ ILAENV( 1, 'CUNGHR', ' ', N, 1, N, -1 ) )
291
IF( .NOT.( WNTSNN .OR. WNTSNE ) )
292
$ MAXWRK = MAX( MAXWRK, N*N+2*N )
293
MAXWRK = MAX( MAXWRK, 2*N, 1 )
294
END IF
295
WORK( 1 ) = MAXWRK
296
END IF
297
IF( LWORK.LT.MINWRK .AND. .NOT.LQUERY ) THEN
298
INFO = -20
299
END IF
300
IF( INFO.NE.0 ) THEN
301
CALL XERBLA( 'CGEEVX', -INFO )
302
RETURN
303
ELSE IF( LQUERY ) THEN
304
RETURN
305
END IF
306
*
307
* Quick return if possible
308
*
309
IF( N.EQ.0 )
310
$ RETURN
311
*
312
* Get machine constants
313
*
314
EPS = SLAMCH( 'P' )
315
SMLNUM = SLAMCH( 'S' )
316
BIGNUM = ONE / SMLNUM
317
CALL SLABAD( SMLNUM, BIGNUM )
318
SMLNUM = SQRT( SMLNUM ) / EPS
319
BIGNUM = ONE / SMLNUM
320
*
321
* Scale A if max element outside range [SMLNUM,BIGNUM]
322
*
323
ICOND = 0
324
ANRM = CLANGE( 'M', N, N, A, LDA, DUM )
325
SCALEA = .FALSE.
326
IF( ANRM.GT.ZERO .AND. ANRM.LT.SMLNUM ) THEN
327
SCALEA = .TRUE.
328
CSCALE = SMLNUM
329
ELSE IF( ANRM.GT.BIGNUM ) THEN
330
SCALEA = .TRUE.
331
CSCALE = BIGNUM
332
END IF
333
IF( SCALEA )
334
$ CALL CLASCL( 'G', 0, 0, ANRM, CSCALE, N, N, A, LDA, IERR )
335
*
336
* Balance the matrix and compute ABNRM
337
*
338
CALL CGEBAL( BALANC, N, A, LDA, ILO, IHI, SCALE, IERR )
339
ABNRM = CLANGE( '1', N, N, A, LDA, DUM )
340
IF( SCALEA ) THEN
341
DUM( 1 ) = ABNRM
342
CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, 1, 1, DUM, 1, IERR )
343
ABNRM = DUM( 1 )
344
END IF
345
*
346
* Reduce to upper Hessenberg form
347
* (CWorkspace: need 2*N, prefer N+N*NB)
348
* (RWorkspace: none)
349
*
350
ITAU = 1
351
IWRK = ITAU + N
352
CALL CGEHRD( N, ILO, IHI, A, LDA, WORK( ITAU ), WORK( IWRK ),
353
$ LWORK-IWRK+1, IERR )
354
*
355
IF( WANTVL ) THEN
356
*
357
* Want left eigenvectors
358
* Copy Householder vectors to VL
359
*
360
SIDE = 'L'
361
CALL CLACPY( 'L', N, N, A, LDA, VL, LDVL )
362
*
363
* Generate unitary matrix in VL
364
* (CWorkspace: need 2*N-1, prefer N+(N-1)*NB)
365
* (RWorkspace: none)
366
*
367
CALL CUNGHR( N, ILO, IHI, VL, LDVL, WORK( ITAU ), WORK( IWRK ),
368
$ LWORK-IWRK+1, IERR )
369
*
370
* Perform QR iteration, accumulating Schur vectors in VL
371
* (CWorkspace: need 1, prefer HSWORK (see comments) )
372
* (RWorkspace: none)
373
*
374
IWRK = ITAU
375
CALL CHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, W, VL, LDVL,
376
$ WORK( IWRK ), LWORK-IWRK+1, INFO )
377
*
378
IF( WANTVR ) THEN
379
*
380
* Want left and right eigenvectors
381
* Copy Schur vectors to VR
382
*
383
SIDE = 'B'
384
CALL CLACPY( 'F', N, N, VL, LDVL, VR, LDVR )
385
END IF
386
*
387
ELSE IF( WANTVR ) THEN
388
*
389
* Want right eigenvectors
390
* Copy Householder vectors to VR
391
*
392
SIDE = 'R'
393
CALL CLACPY( 'L', N, N, A, LDA, VR, LDVR )
394
*
395
* Generate unitary matrix in VR
396
* (CWorkspace: need 2*N-1, prefer N+(N-1)*NB)
397
* (RWorkspace: none)
398
*
399
CALL CUNGHR( N, ILO, IHI, VR, LDVR, WORK( ITAU ), WORK( IWRK ),
400
$ LWORK-IWRK+1, IERR )
401
*
402
* Perform QR iteration, accumulating Schur vectors in VR
403
* (CWorkspace: need 1, prefer HSWORK (see comments) )
404
* (RWorkspace: none)
405
*
406
IWRK = ITAU
407
CALL CHSEQR( 'S', 'V', N, ILO, IHI, A, LDA, W, VR, LDVR,
408
$ WORK( IWRK ), LWORK-IWRK+1, INFO )
409
*
410
ELSE
411
*
412
* Compute eigenvalues only
413
* If condition numbers desired, compute Schur form
414
*
415
IF( WNTSNN ) THEN
416
JOB = 'E'
417
ELSE
418
JOB = 'S'
419
END IF
420
*
421
* (CWorkspace: need 1, prefer HSWORK (see comments) )
422
* (RWorkspace: none)
423
*
424
IWRK = ITAU
425
CALL CHSEQR( JOB, 'N', N, ILO, IHI, A, LDA, W, VR, LDVR,
426
$ WORK( IWRK ), LWORK-IWRK+1, INFO )
427
END IF
428
*
429
* If INFO > 0 from CHSEQR, then quit
430
*
431
IF( INFO.GT.0 )
432
$ GO TO 50
433
*
434
IF( WANTVL .OR. WANTVR ) THEN
435
*
436
* Compute left and/or right eigenvectors
437
* (CWorkspace: need 2*N)
438
* (RWorkspace: need N)
439
*
440
CALL CTREVC( SIDE, 'B', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
441
$ N, NOUT, WORK( IWRK ), RWORK, IERR )
442
END IF
443
*
444
* Compute condition numbers if desired
445
* (CWorkspace: need N*N+2*N unless SENSE = 'E')
446
* (RWorkspace: need 2*N unless SENSE = 'E')
447
*
448
IF( .NOT.WNTSNN ) THEN
449
CALL CTRSNA( SENSE, 'A', SELECT, N, A, LDA, VL, LDVL, VR, LDVR,
450
$ RCONDE, RCONDV, N, NOUT, WORK( IWRK ), N, RWORK,
451
$ ICOND )
452
END IF
453
*
454
IF( WANTVL ) THEN
455
*
456
* Undo balancing of left eigenvectors
457
*
458
CALL CGEBAK( BALANC, 'L', N, ILO, IHI, SCALE, N, VL, LDVL,
459
$ IERR )
460
*
461
* Normalize left eigenvectors and make largest component real
462
*
463
DO 20 I = 1, N
464
SCL = ONE / SCNRM2( N, VL( 1, I ), 1 )
465
CALL CSSCAL( N, SCL, VL( 1, I ), 1 )
466
DO 10 K = 1, N
467
RWORK( K ) = REAL( VL( K, I ) )**2 +
468
$ AIMAG( VL( K, I ) )**2
469
10 CONTINUE
470
K = ISAMAX( N, RWORK, 1 )
471
TMP = CONJG( VL( K, I ) ) / SQRT( RWORK( K ) )
472
CALL CSCAL( N, TMP, VL( 1, I ), 1 )
473
VL( K, I ) = CMPLX( REAL( VL( K, I ) ), ZERO )
474
20 CONTINUE
475
END IF
476
*
477
IF( WANTVR ) THEN
478
*
479
* Undo balancing of right eigenvectors
480
*
481
CALL CGEBAK( BALANC, 'R', N, ILO, IHI, SCALE, N, VR, LDVR,
482
$ IERR )
483
*
484
* Normalize right eigenvectors and make largest component real
485
*
486
DO 40 I = 1, N
487
SCL = ONE / SCNRM2( N, VR( 1, I ), 1 )
488
CALL CSSCAL( N, SCL, VR( 1, I ), 1 )
489
DO 30 K = 1, N
490
RWORK( K ) = REAL( VR( K, I ) )**2 +
491
$ AIMAG( VR( K, I ) )**2
492
30 CONTINUE
493
K = ISAMAX( N, RWORK, 1 )
494
TMP = CONJG( VR( K, I ) ) / SQRT( RWORK( K ) )
495
CALL CSCAL( N, TMP, VR( 1, I ), 1 )
496
VR( K, I ) = CMPLX( REAL( VR( K, I ) ), ZERO )
497
40 CONTINUE
498
END IF
499
*
500
* Undo scaling if necessary
501
*
502
50 CONTINUE
503
IF( SCALEA ) THEN
504
CALL CLASCL( 'G', 0, 0, CSCALE, ANRM, N-INFO, 1, W( INFO+1 ),
505
$ MAX( N-INFO, 1 ), IERR )
506
IF( INFO.EQ.0 ) THEN
507
IF( ( WNTSNV .OR. WNTSNB ) .AND. ICOND.EQ.0 )
508
$ CALL SLASCL( 'G', 0, 0, CSCALE, ANRM, N, 1, RCONDV, N,
509
$ IERR )
510
ELSE
511
CALL CLASCL( 'G', 0, 0, CSCALE, ANRM, ILO-1, 1, W, N, IERR )
512
END IF
513
END IF
514
*
515
WORK( 1 ) = MAXWRK
516
RETURN
517
*
518
* End of CGEEVX
519
*
520
END
521
522