1       COMPLEX FUNCTION CLATM2( M, N, I, J, KL, KU, IDIST, ISEED, D,
  2      $                         IGRADE, DL, DR, IPVTNG, IWORK, SPARSE )
  3 *
  4 *  -- LAPACK auxiliary test routine (version 3.1) --
  5 *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
  6 *     June 2010
  7 *
  8 *     .. Scalar Arguments ..
  9 *
 10       INTEGER            I, IDIST, IGRADE, IPVTNG, J, KL, KU, M, N
 11       REAL               SPARSE
 12 *     ..
 13 *
 14 *     .. Array Arguments ..
 15 *
 16       INTEGER            ISEED( 4 ), IWORK( * )
 17       COMPLEX            D( * ), DL( * ), DR( * )
 18 *     ..
 19 *
 20 *  Purpose
 21 *  =======
 22 *
 23 *     CLATM2 returns the (I,J) entry of a random matrix of dimension
 24 *     (M, N) described by the other paramters. It is called by the
 25 *     CLATMR routine in order to build random test matrices. No error
 26 *     checking on parameters is done, because this routine is called in
 27 *     a tight loop by CLATMR which has already checked the parameters.
 28 *
 29 *     Use of CLATM2 differs from CLATM3 in the order in which the random
 30 *     number generator is called to fill in random matrix entries.
 31 *     With CLATM2, the generator is called to fill in the pivoted matrix
 32 *     columnwise. With CLATM3, the generator is called to fill in the
 33 *     matrix columnwise, after which it is pivoted. Thus, CLATM3 can
 34 *     be used to construct random matrices which differ only in their
 35 *     order of rows and/or columns. CLATM2 is used to construct band
 36 *     matrices while avoiding calling the random number generator for
 37 *     entries outside the band (and therefore generating random numbers
 38 *
 39 *     The matrix whose (I,J) entry is returned is constructed as
 40 *     follows (this routine only computes one entry):
 41 *
 42 *       If I is outside (1..M) or J is outside (1..N), return zero
 43 *          (this is convenient for generating matrices in band format).
 44 *
 45 *       Generate a matrix A with random entries of distribution IDIST.
 46 *
 47 *       Set the diagonal to D.
 48 *
 49 *       Grade the matrix, if desired, from the left (by DL) and/or
 50 *          from the right (by DR or DL) as specified by IGRADE.
 51 *
 52 *       Permute, if desired, the rows and/or columns as specified by
 53 *          IPVTNG and IWORK.
 54 *
 55 *       Band the matrix to have lower bandwidth KL and upper
 56 *          bandwidth KU.
 57 *
 58 *       Set random entries to zero as specified by SPARSE.
 59 *
 60 *  Arguments
 61 *  =========
 62 *
 63 *  M        (input) INTEGER
 64 *           Number of rows of matrix. Not modified.
 65 *
 66 *  N        (input) INTEGER
 67 *           Number of columns of matrix. Not modified.
 68 *
 69 *  I        (input) INTEGER
 70 *           Row of entry to be returned. Not modified.
 71 *
 72 *  J        (input) INTEGER
 73 *           Column of entry to be returned. Not modified.
 74 *
 75 *  KL       (input) INTEGER
 76 *           Lower bandwidth. Not modified.
 77 *
 78 *  KU       (input) INTEGER
 79 *           Upper bandwidth. Not modified.
 80 *
 81 *  IDIST    (input) INTEGER
 82 *           On entry, IDIST specifies the type of distribution to be
 83 *           used to generate a random matrix .
 84 *           1 => real and imaginary parts each UNIFORM( 0, 1 )
 85 *           2 => real and imaginary parts each UNIFORM( -1, 1 )
 86 *           3 => real and imaginary parts each NORMAL( 0, 1 )
 87 *           4 => complex number uniform in DISK( 0 , 1 )
 88 *           Not modified.
 89 *
 90 *  ISEED    (input/output) INTEGER array of dimension ( 4 )
 91 *           Seed for random number generator.
 92 *           Changed on exit.
 93 *
 94 *  D        (input) COMPLEX array of dimension ( MIN( I , J ) )
 95 *           Diagonal entries of matrix. Not modified.
 96 *
 97 *  IGRADE   (input) INTEGER
 98 *           Specifies grading of matrix as follows:
 99 *           0  => no grading
100 *           1  => matrix premultiplied by diag( DL )
101 *           2  => matrix postmultiplied by diag( DR )
102 *           3  => matrix premultiplied by diag( DL ) and
103 *                         postmultiplied by diag( DR )
104 *           4  => matrix premultiplied by diag( DL ) and
105 *                         postmultiplied by inv( diag( DL ) )
106 *           5  => matrix premultiplied by diag( DL ) and
107 *                         postmultiplied by diag( CONJG(DL) )
108 *           6  => matrix premultiplied by diag( DL ) and
109 *                         postmultiplied by diag( DL )
110 *           Not modified.
111 *
112 *  DL       (input) COMPLEX array ( I or J, as appropriate )
113 *           Left scale factors for grading matrix.  Not modified.
114 *
115 *  DR       (input) COMPLEX array ( I or J, as appropriate )
116 *           Right scale factors for grading matrix.  Not modified.
117 *
118 *  IPVTNG   (input) INTEGER
119 *           On entry specifies pivoting permutations as follows:
120 *           0 => none.
121 *           1 => row pivoting.
122 *           2 => column pivoting.
123 *           3 => full pivoting, i.e., on both sides.
124 *           Not modified.
125 *
126 *  IWORK    (workspace) INTEGER array ( I or J, as appropriate )
127 *           This array specifies the permutation used. The
128 *           row (or column) in position K was originally in
129 *           position IWORK( K ).
130 *           This differs from IWORK for CLATM3. Not modified.
131 *
132 *  SPARSE   (input) REAL 
133 *           Value between 0. and 1.
134 *           On entry specifies the sparsity of the matrix
135 *           if sparse matix is to be generated.
136 *           SPARSE should lie between 0 and 1.
137 *           A uniform ( 0, 1 ) random number x is generated and
138 *           compared to SPARSE; if x is larger the matrix entry
139 *           is unchanged and if x is smaller the entry is set
140 *           to zero. Thus on the average a fraction SPARSE of the
141 *           entries will be set to zero.
142 *           Not modified.
143 *
144 *  =====================================================================
145 *
146 *     .. Parameters ..
147 *
148       COMPLEX            CZERO
149       PARAMETER          ( CZERO = ( 0.0E00.0E0 ) )
150       REAL               ZERO
151       PARAMETER          ( ZERO = 0.0E0 )
152 *     ..
153 *
154 *     .. Local Scalars ..
155 *
156       INTEGER            ISUB, JSUB
157       COMPLEX            CTEMP
158 *     ..
159 *
160 *     .. External Functions ..
161 *
162       REAL               SLARAN
163       COMPLEX            CLARND
164       EXTERNAL           SLARAN, CLARND
165 *     ..
166 *
167 *     .. Intrinsic Functions ..
168 *
169       INTRINSIC          CONJG
170 *     ..
171 *
172 *-----------------------------------------------------------------------
173 *
174 *     .. Executable Statements ..
175 *
176 *
177 *     Check for I and J in range
178 *
179       IF( I.LT.1 .OR. I.GT..OR. J.LT.1 .OR. J.GT.N ) THEN
180          CLATM2 = CZERO
181          RETURN
182       END IF
183 *
184 *     Check for banding
185 *
186       IF( J.GT.I+KU .OR. J.LT.I-KL ) THEN
187          CLATM2 = CZERO
188          RETURN
189       END IF
190 *
191 *     Check for sparsity
192 *
193       IF( SPARSE.GT.ZERO ) THEN
194          IF( SLARAN( ISEED ).LT.SPARSE ) THEN
195             CLATM2 = CZERO
196             RETURN
197          END IF
198       END IF
199 *
200 *     Compute subscripts depending on IPVTNG
201 *
202       IF( IPVTNG.EQ.0 ) THEN
203          ISUB = I
204          JSUB = J
205       ELSE IF( IPVTNG.EQ.1 ) THEN
206          ISUB = IWORK( I )
207          JSUB = J
208       ELSE IF( IPVTNG.EQ.2 ) THEN
209          ISUB = I
210          JSUB = IWORK( J )
211       ELSE IF( IPVTNG.EQ.3 ) THEN
212          ISUB = IWORK( I )
213          JSUB = IWORK( J )
214       END IF
215 *
216 *     Compute entry and grade it according to IGRADE
217 *
218       IF( ISUB.EQ.JSUB ) THEN
219          CTEMP = D( ISUB )
220       ELSE
221          CTEMP = CLARND( IDIST, ISEED )
222       END IF
223       IF( IGRADE.EQ.1 ) THEN
224          CTEMP = CTEMP*DL( ISUB )
225       ELSE IF( IGRADE.EQ.2 ) THEN
226          CTEMP = CTEMP*DR( JSUB )
227       ELSE IF( IGRADE.EQ.3 ) THEN
228          CTEMP = CTEMP*DL( ISUB )*DR( JSUB )
229       ELSE IF( IGRADE.EQ.4 .AND. ISUB.NE.JSUB ) THEN
230          CTEMP = CTEMP*DL( ISUB ) / DL( JSUB )
231       ELSE IF( IGRADE.EQ.5 ) THEN
232          CTEMP = CTEMP*DL( ISUB )*CONJG( DL( JSUB ) )
233       ELSE IF( IGRADE.EQ.6 ) THEN
234          CTEMP = CTEMP*DL( ISUB )*DL( JSUB )
235       END IF
236       CLATM2 = CTEMP
237       RETURN
238 *
239 *     End of CLATM2
240 *
241       END