File: dgesv.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (1249 lines) | stat: -rw-r--r-- 34,476 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
/*
   DGESV.C
   LAPACK routines to solve a matrix by LU decomposition.

   $Id$
 */

#include "dg.h"



/*-----prototypes of functions defined here-----*/
extern long ilaenv( long ispec, char *name, char *opts,
		   long n1, long n2, long n3,long n4 );
extern void dgetf2( long m, long n, double a[], long lda, long ipiv[],
		   long *info );
extern void dlaswp( long n, double a[], long lda, long k1, long k2,
		   long ipiv[], long incx );
extern void dgetrf( long m, long n, double a[], long lda, long ipiv[],
		   long *info );
extern void dgetrs( char trans, long n, long nrhs, double a[], long lda,
		   long ipiv[], double b[], long ldb, long *info );
extern void dgesv( long n, long nrhs, double a[], long lda, long ipiv[],
		  double b[], long ldb, long *info );
/*-----end of prototypes-----*/



/*---blas routines---*/
extern long  yidamax(long n,double dx[],long incx);
extern void  ydswap (long n,double dx[],long incx,double dy[],long incy);
extern void  ydscal(long n,double da,double dx[],long incx);
extern void ydger  ( long m, long n, double alpha, double x[], long incx,
		   double y[], long incy, double a[], long lda );
extern void ydgemm ( char transa, char transb, long m, long n, long k,
		   double alpha, double a[], long lda, double b[], long ldb,
		   double beta, double c[], long ldc );
extern void ydtrsm ( char side, char uplo, char transa, char diag,
		   long m, long n, double alpha, double a[], long lda,
		   double b[], long ldb );



/*---converted nutty string switches to single characters (lower case)---*/
#define lsame(x,y) ((x)==(y))

extern void xerbla(char *,long);

extern int strcmp(const char *, const char *);



/*-----Fortran intrinsics converted-----*/
#define abs(x) ((x)>=0?(x):-(x))
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
/*-----end of Fortran intrinsics-----*/



void dgesv( long n, long nrhs, double a[], long lda, long ipiv[],
	   double b[], long ldb, long *info )
{
  /**
   *  -- LAPACK driver routine (version 1.1) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     March 31, 1993
   *
   *     .. Scalar Arguments ..*/
  /**     ..
   *     .. Array Arguments ..*/
#undef ipiv_1
#define ipiv_1(a1) ipiv[a1-1]
#undef b_2
#define b_2(a1,a2) b[a1-1+ldb*(a2-1)]
#undef a_2
#define a_2(a1,a2) a[a1-1+lda*(a2-1)]
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  DGESV computes the solution to a real system of linear equations
   *     A * X = B,
   *  where A is an N-by-N matrix and X and B are N-by-NRHS matrices.
   *
   *  The LU decomposition with partial pivoting and row interchanges is
   *  used to factor A as
   *     A = P * L * U,
   *  where P is a permutation matrix, L is unit lower triangular, and U is
   *  upper triangular.  The factored form of A is then used to solve the
   *  system of equations A * X = B.
   *
   *  Arguments
   *  =========
   *
   *  N       (input) INTEGER
   *          The number of linear equations, i.e., the order of the
   *          matrix A.  N >= 0.
   *
   *  NRHS    (input) INTEGER
   *          The number of right hand sides, i.e., the number of columns
   *          of the matrix B.  NRHS >= 0.
   *
   *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
   *          On entry, the N-by-N coefficient matrix A.
   *          On exit, the factors L and U from the factorization
   *          A = P*L*U; the unit diagonal elements of L are not stored.
   *
   *  LDA     (input) INTEGER
   *          The leading dimension of the array A.  LDA >= max(1,N).
   *
   *  IPIV    (output) INTEGER array, dimension (N)
   *          The pivot indices that define the permutation matrix P;
   *          row i of the matrix was interchanged with row IPIV(i).
   *
   *  B       (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS)
   *          On entry, the N-by-NRHS matrix of right hand side matrix B.
   *          On exit, if INFO = 0, the N-by-NRHS solution matrix X.
   *
   *  LDB     (input) INTEGER
   *          The leading dimension of the array B.  LDB >= max(1,N).
   *
   *  INFO    (output) INTEGER
   *          = 0:  successful exit
   *          < 0:  if INFO = -i, the i-th argument had an illegal value
   *          > 0:  if INFO = i, U(i,i) is exactly zero.  The factorization
   *                has been completed, but the factor U is exactly
   *                singular, so the solution could not be computed.
   *
   *  =====================================================================
   **/
  /**     ..
   *     .. Intrinsic Functions ..*/
  /*      intrinsic          max;*/
  /**     ..
   *     .. Executable Statements ..
   *
   *     Test the input parameters.
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  *info = 0;
  if( n<0 ) {
    *info = -1;
  } else if( nrhs<0 ) {
    *info = -2;
  } else if( lda<max( 1, n ) ) {
    *info = -4;
  } else if( ldb<max( 1, n ) ) {
    *info = -7;
  }
  if( *info!=0 ) {
    xerbla( "dgesv ", -*info );
    return;
  }
  /**
   *     Compute the LU factorization of A.
   **/
  dgetrf( n, n, a, lda, ipiv, info );
  if( *info==0 ) {
    /**
     *        Solve the system A*X = B, overwriting B with X.
     **/
    dgetrs( 'n'/*o transpose*/, n, nrhs, a, lda, ipiv, b, ldb,
	   info );
  }
  return;
  /**
   *     End of DGESV
   **/
}



void dgetrs( char trans, long n, long nrhs, double a[], long lda,
	    long ipiv[], double b[], long ldb, long *info )
{
  /**
   *  -- LAPACK routine (version 1.1) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     March 31, 1993
   *
   *     .. Scalar Arguments ..*/
  /**     ..
   *     .. Array Arguments ..*/
#undef ipiv_1
#define ipiv_1(a1) ipiv[a1-1]
#undef b_2
#define b_2(a1,a2) b[a1-1+ldb*(a2-1)]
#undef a_2
#define a_2(a1,a2) a[a1-1+lda*(a2-1)]
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  DGETRS solves a system of linear equations
   *     A * X = B  or  A' * X = B
   *  with a general N-by-N matrix A using the LU factorization computed
   *  by DGETRF.
   *
   *  Arguments
   *  =========
   *
   *  TRANS   (input) CHARACTER*1
   *          Specifies the form of the system of equations:
   *          = 'N':  A * X = B  (No transpose)
   *          = 'T':  A'* X = B  (Transpose)
   *          = 'C':  A'* X = B  (Conjugate transpose = Transpose)
   *
   *  N       (input) INTEGER
   *          The order of the matrix A.  N >= 0.
   *
   *  NRHS    (input) INTEGER
   *          The number of right hand sides, i.e., the number of columns
   *          of the matrix B.  NRHS >= 0.
   *
   *  A       (input) DOUBLE PRECISION array, dimension (LDA,N)
   *          The factors L and U from the factorization A = P*L*U
   *          as computed by DGETRF.
   *
   *  LDA     (input) INTEGER
   *          The leading dimension of the array A.  LDA >= max(1,N).
   *
   *  IPIV    (input) INTEGER array, dimension (N)
   *          The pivot indices from DGETRF; for 1<=i<=N, row i of the
   *          matrix was interchanged with row IPIV(i).
   *
   *  B       (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS)
   *          On entry, the right hand side matrix B.
   *          On exit, the solution matrix X.
   *
   *  LDB     (input) INTEGER
   *          The leading dimension of the array B.  LDB >= max(1,N).
   *
   *  INFO    (output) INTEGER
   *          = 0:  successful exit
   *          < 0:  if INFO = -i, the i-th argument had an illegal value
   *
   *  =====================================================================
   *
   *     .. Parameters ..*/
#undef one
#define one 1.0e+0
  /**     ..
   *     .. Local Scalars ..*/
  int            notran;
  /**     ..
   *     .. Intrinsic Functions ..*/
  /*      intrinsic          max;*/
  /**     ..
   *     .. Executable Statements ..
   *
   *     Test the input parameters.
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  *info = 0;
  notran = lsame( trans, 'n' );
  if( !notran && !lsame( trans, 't' ) && !
     lsame( trans, 'c' ) ) {
    *info = -1;
  } else if( n<0 ) {
    *info = -2;
  } else if( nrhs<0 ) {
    *info = -3;
  } else if( lda<max( 1, n ) ) {
    *info = -5;
  } else if( ldb<max( 1, n ) ) {
    *info = -8;
  }
  if( *info!=0 ) {
    xerbla( "dgetrs", -*info );
    return;
  }
  /**
   *     Quick return if possible
   **/
  if( n==0 || nrhs==0 )
    return;

  if( notran ) {
    /**
     *        Solve A * X = B.
     *
     *        Apply row interchanges to the right hand sides.
     **/
    dlaswp( nrhs, b, ldb, 1, n, ipiv, 1 );
    /**
     *        Solve L*X = B, overwriting B with X.
     **/
    ydtrsm( 'l'/*eft*/, 'l'/*ower*/, 'n'/*o transpose*/, 'u'/*nit*/, n, nrhs,
	  one, a, lda, b, ldb );
    /**
     *        Solve U*X = B, overwriting B with X.
     **/
    ydtrsm( 'l'/*eft*/, 'u'/*pper*/, 'n'/*o transpose*/, 'n'/*on-unit*/, n,
	  nrhs, one, a, lda, b, ldb );
  } else {
    /**
     *        Solve A' * X = B.
     *
     *        Solve U'*X = B, overwriting B with X.
     **/
    ydtrsm( 'l'/*eft*/, 'u'/*pper*/, 't'/*ranspose*/, 'n'/*on-unit*/, n, nrhs,
	  one, a, lda, b, ldb );
    /**
     *        Solve L'*X = B, overwriting B with X.
     **/
    ydtrsm( 'l'/*eft*/, 'l'/*ower*/, 't'/*ranspose*/, 'u'/*nit*/, n, nrhs, one,
	  a, lda, b, ldb );
    /**
     *        Apply row interchanges to the solution vectors.
     **/
    dlaswp( nrhs, b, ldb, 1, n, ipiv, -1 );
  }

  return;
  /**
   *     End of DGETRS
   **/
}



void dgetrf( long m, long n, double a[], long lda, long ipiv[], long *info )
{
  /**
   *  -- LAPACK routine (version 1.1) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     March 31, 1993
   *
   *     .. Scalar Arguments ..*/
  /**     ..
   *     .. Array Arguments ..*/
#undef ipiv_1
#define ipiv_1(a1) ipiv[a1-1]
#undef a_2
#define a_2(a1,a2) a[a1-1+lda*(a2-1)]
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  DGETRF computes an LU factorization of a general M-by-N matrix A
   *  using partial pivoting with row interchanges.
   *
   *  The factorization has the form
   *     A = P * L * U
   *  where P is a permutation matrix, L is lower triangular with unit
   *  diagonal elements (lower trapezoidal if m > n), and U is upper
   *  triangular (upper trapezoidal if m < n).
   *
   *  This is the right-looking Level 3 BLAS version of the algorithm.
   *
   *  Arguments
   *  =========
   *
   *  M       (input) INTEGER
   *          The number of rows of the matrix A.  M >= 0.
   *
   *  N       (input) INTEGER
   *          The number of columns of the matrix A.  N >= 0.
   *
   *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
   *          On entry, the M-by-N matrix to be factored.
   *          On exit, the factors L and U from the factorization
   *          A = P*L*U; the unit diagonal elements of L are not stored.
   *
   *  LDA     (input) INTEGER
   *          The leading dimension of the array A.  LDA >= max(1,M).
   *
   *  IPIV    (output) INTEGER array, dimension (min(M,N))
   *          The pivot indices; for 1 <= i <= min(M,N), row i of the
   *          matrix was interchanged with row IPIV(i).
   *
   *  INFO    (output) INTEGER
   *          = 0:  successful exit
   *          < 0:  if INFO = -i, the i-th argument had an illegal value
   *          > 0:  if INFO = i, U(i,i) is exactly zero. The factorization
   *                has been completed, but the factor U is exactly
   *                singular, and division by zero will occur if it is used
   *                to solve a system of equations.
   *
   *  =====================================================================
   *
   *     .. Parameters ..*/
#undef one
#define one 1.0e+0
  /**     ..
   *     .. Local Scalars ..*/
  long            i, iinfo, j, jb, nb;
  /**     ..
   *     .. Intrinsic Functions ..*/
  /*      intrinsic          max, min;*/
  /**     ..
   *     .. Executable Statements ..
   *
   *     Test the input parameters.
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  *info = 0;
  if( m<0 ) {
    *info = -1;
  } else if( n<0 ) {
    *info = -2;
  } else if( lda<max( 1, m ) ) {
    *info = -4;
  }
  if( *info!=0 ) {
    xerbla( "dgetrf", -*info );
    return;
  }
  /**
   *     Quick return if possible
   **/
  if( m==0 || n==0 )
    return;
  /**
   *     Determine the block size for this environment.
   **/
  nb = ilaenv( 1, "dgetrf", " ", m, n, -1, -1 );
  if( nb<=1 || nb>=min( m, n ) ) {
    /**
     *        Use unblocked code.
     **/
    dgetf2( m, n, a, lda, ipiv, info );
  } else {
    /**
     *        Use blocked code.
     **/
    for (j=1 ; nb>0?j<=min( m, n ):j>=min( m, n ) ; j+=nb) {
      jb = min( min( m, n )-j+1, nb );
      /**
       *           Factor diagonal and subdiagonal blocks and test for exact
       *           singularity.
       **/
      dgetf2( m-j+1, jb, &a_2( j, j ), lda, &ipiv_1( j ), &iinfo );
      /**
       *           Adjust INFO and the pivot indices.
       **/
      if( *info==0 && iinfo>0 )
	*info = iinfo + j - 1;
      for (i=j ; i<=min( m, j+jb-1 ) ; i+=1) {
	ipiv_1( i ) = j - 1 + ipiv_1( i );
      }
      /**
       *           Apply interchanges to columns 1:J-1.
       **/
      dlaswp( j-1, a, lda, j, j+jb-1, ipiv, 1 );

      if( j+jb<=n ) {
	/**
	 *              Apply interchanges to columns J+JB:N.
	 **/
	dlaswp( n-j-jb+1, &a_2( 1, j+jb ), lda, j, j+jb-1,
	       ipiv, 1 );
	/**
	 *              Compute block row of U.
	 **/
	ydtrsm( 'l'/*eft*/, 'l'/*ower*/, 'n'/*o transpose*/, 'u'/*nit*/, jb,
	      n-j-jb+1, one, &a_2( j, j ), lda, &a_2( j, j+jb ),
	      lda );
	if( j+jb<=m ) {
	  /**
	   *                 Update trailing submatrix.
	   **/
	  ydgemm( 'n'/*o transpose*/, 'n'/*o transpose*/, m-j-jb+1,
		n-j-jb+1, jb, -one, &a_2( j+jb, j ), lda,
		&a_2( j, j+jb ), lda, one, &a_2( j+jb, j+jb ),
		lda );
	}
      }
    }
  }
  return;
  /**
   *     End of DGETRF
   **/
}



void dlaswp( long n, double a[], long lda, long k1, long k2,
	    long ipiv[], long incx )
{
  /**
   *  -- LAPACK auxiliary routine (version 1.1) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     October 31, 1992
   *
   *     .. Scalar Arguments ..*/
  /**     ..
   *     .. Array Arguments ..*/
#undef ipiv_1
#define ipiv_1(a1) ipiv[a1-1]
#undef a_2
#define a_2(a1,a2) a[a1-1+lda*(a2-1)]
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  DLASWP performs a series of row interchanges on the matrix A.
   *  One row interchange is initiated for each of rows K1 through K2 of A.
   *
   *  Arguments
   *  =========
   *
   *  N       (input) INTEGER
   *          The number of columns of the matrix A.
   *
   *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
   *          On entry, the matrix of column dimension N to which the row
   *          interchanges will be applied.
   *          On exit, the permuted matrix.
   *
   *  LDA     (input) INTEGER
   *          The leading dimension of the array A.
   *
   *  K1      (input) INTEGER
   *          The first element of IPIV for which a row interchange will
   *          be done.
   *
   *  K2      (input) INTEGER
   *          The last element of IPIV for which a row interchange will
   *          be done.
   *
   *  IPIV    (input) INTEGER array, dimension (M*abs(INCX))
   *          The vector of pivot indices.  Only the elements in positions
   *          K1 through K2 of IPIV are accessed.
   *          IPIV(K) = L implies rows K and L are to be interchanged.
   *
   *  INCX    (input) INTEGER
   *          The increment between successive values of IPIV.  If IPIV
   *          is negative, the pivots are applied in reverse order.
   *
   * =====================================================================
   *
   *     .. Local Scalars ..*/
  long            i, ip, ix;
  /**     ..
   *     .. External Subroutines ..*/
  extern void ydswap();
  /**     ..
   *     .. Executable Statements ..
   *
   *     Interchange row I with row IPIV(I) for each of rows K1 through K2.
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  if( incx==0 )
    return;
  if( incx>0 ) {
    ix = k1;
  } else {
    ix = 1 + ( 1-k2 )*incx;
  }
  if( incx==1 ) {
    for (i=k1 ; i<=k2 ; i+=1) {
      ip = ipiv_1( i );
      if( ip!=i )
	ydswap( n, &a_2( i, 1 ), lda, &a_2( ip, 1 ), lda );
    }
  } else if( incx>1 ) {
    for (i=k1 ; i<=k2 ; i+=1) {
      ip = ipiv_1( ix );
      if( ip!=i )
	ydswap( n, &a_2( i, 1 ), lda, &a_2( ip, 1 ), lda );
      ix = ix + incx;
    }
  } else if( incx<0 ) {
    for (i=k2 ; i>=k1 ; i+=-1) {
      ip = ipiv_1( ix );
      if( ip!=i )
	ydswap( n, &a_2( i, 1 ), lda, &a_2( ip, 1 ), lda );
      ix = ix + incx;
    }
  }

  return;
  /**
   *     End of DLASWP
   **/
}



void dgetf2( long m, long n, double a[], long lda, long ipiv[], long *info )
{
  /**
   *  -- LAPACK routine (version 1.1) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     June 30, 1992
   *
   *     .. Scalar Arguments ..*/
  /**     ..
   *     .. Array Arguments ..*/
#undef ipiv_1
#define ipiv_1(a1) ipiv[a1-1]
#undef a_2
#define a_2(a1,a2) a[a1-1+lda*(a2-1)]
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  DGETF2 computes an LU factorization of a general m-by-n matrix A
   *  using partial pivoting with row interchanges.
   *
   *  The factorization has the form
   *     A = P * L * U
   *  where P is a permutation matrix, L is lower triangular with unit
   *  diagonal elements (lower trapezoidal if m > n), and U is upper
   *  triangular (upper trapezoidal if m < n).
   *
   *  This is the right-looking Level 2 BLAS version of the algorithm.
   *
   *  Arguments
   *  =========
   *
   *  M       (input) INTEGER
   *          The number of rows of the matrix A.  M >= 0.
   *
   *  N       (input) INTEGER
   *          The number of columns of the matrix A.  N >= 0.
   *
   *  A       (input/output) DOUBLE PRECISION array, dimension (LDA,N)
   *          On entry, the m by n matrix to be factored.
   *          On exit, the factors L and U from the factorization
   *          A = P*L*U; the unit diagonal elements of L are not stored.
   *
   *  LDA     (input) INTEGER
   *          The leading dimension of the array A.  LDA >= max(1,M).
   *
   *  IPIV    (output) INTEGER array, dimension (min(M,N))
   *          The pivot indices; for 1 <= i <= min(M,N), row i of the
   *          matrix was interchanged with row IPIV(i).
   *
   *  INFO    (output) INTEGER
   *          = 0: successful exit
   *          < 0: if INFO = -k, the k-th argument had an illegal value
   *          > 0: if INFO = k, U(k,k) is exactly zero. The factorization
   *               has been completed, but the factor U is exactly
   *               singular, and division by zero will occur if it is used
   *               to solve a system of equations.
   *
   *  =====================================================================
   *
   *     .. Parameters ..*/
#undef one
#define one 1.0e+0
#undef zero
#define zero 0.0e+0
  /**     ..
   *     .. Local Scalars ..*/
  long            j, jp;
  /**     ..
   *     .. Intrinsic Functions ..*/
  /*      intrinsic          max, min;*/
  /**     ..
   *     .. Executable Statements ..
   *
   *     Test the input parameters.
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  *info = 0;
  if( m<0 ) {
    *info = -1;
  } else if( n<0 ) {
    *info = -2;
  } else if( lda<max( 1, m ) ) {
    *info = -4;
  }
  if( *info!=0 ) {
    xerbla( "dgetf2", -*info );
    return;
  }
  /**
   *     Quick return if possible
   **/
  if( m==0 || n==0 )
    return;

  for (j=1 ; j<=min( m, n ) ; j+=1) {
    /**
     *        Find pivot and test for singularity.
     **/
    jp = j - 1 + yidamax( m-j+1, &a_2( j, j ), 1 );
    ipiv_1( j ) = jp;
    if( a_2( jp, j )!=zero ) {
      /**
       *           Apply the interchange to columns 1:N.
       **/
      if( jp!=j )
	ydswap( n, &a_2( j, 1 ), lda, &a_2( jp, 1 ), lda );
      /**
       *           Compute elements J+1:M of J-th column.
       **/
      if( j<m )
	ydscal( m-j, one / a_2( j, j ), &a_2( j+1, j ), 1 );

    } else if( *info==0 ) {

      *info = j;
    }

    if( j<min( m, n ) ) {
      /**
       *           Update trailing submatrix.
       **/
      ydger( m-j, n-j, -one, &a_2( j+1, j ), 1, &a_2( j, j+1 ), lda,
	   &a_2( j+1, j+1 ), lda );
    }
  }
  return;
  /**
   *     End of DGETF2
   **/
}



long ilaenv( long ispec, char *name, char *opts,
	    long n1, long n2, long n3,long n4 )
{
  /**
   *  -- LAPACK auxiliary routine (preliminary version) --
   *     Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
   *     Courant Institute, Argonne National Lab, and Rice University
   *     February 20, 1992
   *
   *     .. Scalar Arguments ..*/
  long ilaenv_R;
  /**     ..
   *
   *  Purpose
   *  =======
   *
   *  ILAENV is called from the LAPACK routines to choose problem-dependent
   *  parameters for the local environment.  See ISPEC for a description of
   *  the parameters.
   *
   *  This version provides a set of parameters which should give good,
   *  but not optimal, performance on many of the currently available
   *  computers.  Users are encouraged to modify this subroutine to set
   *  the tuning parameters for their particular machine using the option
   *  and problem size information in the arguments.
   *
   *  This routine will not function correctly if it is converted to all
   *  lower case.  Converting it to all upper case is allowed.
   *
   *  Arguments
   *  =========
   *
   *  ISPEC   (input) INTEGER
   *          Specifies the parameter to be returned as the value of
   *          ILAENV.
   *          = 1: the optimal blocksize; if this value is 1, an unblocked
   *               algorithm will give the best performance.
   *          = 2: the minimum block size for which the block routine
   *               should be used; if the usable block size is less than
   *               this value, an unblocked routine should be used.
   *          = 3: the crossover point (in a block routine, for N less
   *               than this value, an unblocked routine should be used)
   *          = 4: the number of shifts, used in the nonsymmetric
   *               eigenvalue routines
   *          = 5: the minimum column dimension for blocking to be used;
   *               rectangular blocks must have dimension at least k by m,
   *               where k is given by ILAENV(2,...) and m by ILAENV(5,...)
   *          = 6: the crossover point for the SVD (when reducing an m by n
   *               matrix to bidiagonal form, if max(m,n)/min(m,n) exceeds
   *               this value, a QR factorization is used first to reduce
   *               the matrix to a triangular form.)
   *          = 7: the number of processors
   *          = 8: the crossover point for the multishift QR and QZ methods
   *               for nonsymmetric eigenvalue problems.
   *
   *  NAME    (input) CHARACTER*(*)
   *          The name of the calling subroutine, in either upper case or
   *          lower case.
   *
   *  OPTS    (input) CHARACTER*(*)
   *          The character options to the subroutine NAME, concatenated
   *          into a single character string.  For example, UPLO = 'U',
   *          TRANS = 'T', and DIAG = 'N' for a triangular routine would
   *          be specified as OPTS = 'UTN'.
   *
   *  N1      (input) INTEGER
   *  N2      (input) INTEGER
   *  N3      (input) INTEGER
   *  N4      (input) INTEGER
   *          Problem dimensions for the subroutine NAME; these may not all
   *          be required.
   *
   * (ILAENV) (output) INTEGER
   *          >= 0: the value of the parameter specified by ISPEC
   *          < 0:  if ILAENV = -k, the k-th argument had an illegal value.
   *
   *  Further Details
   *  ===============
   *
   *  The following conventions have been used when calling ILAENV from the
   *  LAPACK routines:
   *  1)  OPTS is a concatenation of all of the character options to
   *      subroutine NAME, in the same order that they appear in the
   *      argument list for NAME, even if they are not used in determining
   *      the value of the parameter specified by ISPEC.
   *  2)  The problem dimensions N1, N2, N3, N4 are specified in the order
   *      that they appear in the argument list for NAME.  N1 is used
   *      first, N2 second, and so on, and unused problem dimensions are
   *      passed a value of -1.
   *  3)  The parameter value returned by ILAENV is checked for validity in
   *      the calling subroutine.  For example, ILAENV is used to retrieve
   *      the optimal blocksize for STRTRI as follows:
   *
   *      NB = ILAENV( 1, 'STRTRI', UPLO // DIAG, N, -1, -1, -1 )
   *      IF( NB.LE.1 ) NB = MAX( 1, N )
   *
   *  =====================================================================
   *
   *     .. Local Scalars ..*/
  int            cname, sname;
  char        c1;
  char        c2[3], c4[3];
  char        c3[4];
  char        subnam[6];
  long            i, ic, iz, nb, nbmin, nx;
  /**     ..
   *     .. Intrinsic Functions ..*/
  /*      intrinsic          char, ichar, int, min, float;*/
  /**     ..
   *     .. Executable Statements ..
   **/
  /*-----implicit-declarations-----*/
  /*-----end-of-declarations-----*/
  switch (ispec) {
  case 1: goto L_100;
  case 2: goto L_100;
  case 3: goto L_100;
  case 4: goto L_400;
  case 5: goto L_500;
  case 6: goto L_600;
  case 7: goto L_700;
  case 8: goto L_800;
  }
  /**
   *     Invalid value for ISPEC
   **/
  ilaenv_R = -1;
  return ilaenv_R;

 L_100:
  /**
   *     Convert NAME to upper case if the first character is lower case.
   **/
  ilaenv_R = 1;
  for (iz=0 ; iz<6 && name[iz] ; iz++) subnam[iz]= name[iz];
  ic = subnam[0];
  iz = 'z';
  if( iz==90 || iz==122 ) {
    /**
     *        ASCII character set
     **/
    if( ic>=97 && ic<=122 ) {
      subnam[0] = ic-32;
      for (i=2 ; i<=6 ; i+=1) {
	ic = subnam[i-1];
	if( ic>=97 && ic<=122 ) subnam[i-1] = ic-32;
      }
    }

  } else if( iz==233 || iz==169 ) {
    /**
     *        EBCDIC character set
     **/
    if( ( ic>=129 && ic<=137 ) ||
       ( ic>=145 && ic<=153 ) ||
       ( ic>=162 && ic<=169 ) ) {
      subnam[0] = ic+64;
      for (i=2 ; i<=6 ; i+=1) {
	ic = subnam[i-1];
	if( ( ic>=129 && ic<=137 ) ||
	   ( ic>=145 && ic<=153 ) ||
	   ( ic>=162 && ic<=169 ) ) subnam[i-1] = ic+64;
      }
    }

  } else if( iz==218 || iz==250 ) {
    /**
     *        Prime machines:  ASCII+128
     **/
    if( ic>=225 && ic<=250 ) {
      subnam[0] = ic-32;
      for (i=2 ; i<=6 ; i+=1) {
	ic = subnam[i-1];
	if( ic>=225 && ic<=250 ) subnam[i-1] = ic-32;
      }
    }
  }

  c1 = subnam[0];
  sname = c1=='S' || c1=='D';
  cname = c1=='C' || c1=='Z';
  if( !( cname || sname ) )
    return ilaenv_R;
  c2[0]= subnam[1];  c2[1]= subnam[2];  c2[2]= '\0';
  c3[0]= subnam[3];  c3[1]= subnam[4];  c3[2]= subnam[5];  c3[3]= '\0';
  c4[0]= c3[1];  c4[1]= c3[2];  c4[2]= '\0';

  switch (ispec) {
  case 1: goto L_110;
  case 2: goto L_200;
  case 3: goto L_300;
  }

 L_110:
  /**
   *     ISPEC = 1:  block size
   *
   *     In these examples, separate code is provided for setting NB for
   *     real and complex.  We assume that NB will take the same value in
   *     single or double precision.
   **/
  nb = 1;

  if( !strcmp(c2,"GE") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    } else if( !strcmp(c3,"QRF") || !strcmp(c3,"RQF") || !strcmp(c3,"LQF") ||
	      !strcmp(c3,"QLF") ) {
      if( sname ) {
	nb = 32;
      } else {
	nb = 32;
      }
    } else if( !strcmp(c3,"HRD") ) {
      if( sname ) {
	nb = 32;
      } else {
	nb = 32;
      }
    } else if( !strcmp(c3,"BRD") ) {
      if( sname ) {
	nb = 32;
      } else {
	nb = 32;
      }
    } else if( !strcmp(c3,"TRI") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    }
  } else if( !strcmp(c2,"PO") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    }
  } else if( !strcmp(c2,"SY") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    } else if( sname && !strcmp(c3,"TRD") ) {
      nb = 1;
    } else if( sname && !strcmp(c3,"GST") ) {
      nb = 64;
    }
  } else if( cname && !strcmp(c2,"HE") ) {
    if( !strcmp(c3,"TRF") ) {
      nb = 64;
    } else if( !strcmp(c3,"TRD") ) {
      nb = 1;
    } else if( !strcmp(c3,"GST") ) {
      nb = 64;
    }
  } else if( sname && !strcmp(c2,"OR") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nb = 32;
      }
    } else if( c3[0]=='M' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nb = 32;
      }
    }
  } else if( cname && !strcmp(c2,"UN") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nb = 32;
      }
    } else if( c3[0]=='M' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nb = 32;
      }
    }
  } else if( !strcmp(c2,"GB") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	if( n4<=64 ) {
	  nb = 1;
	} else {
	  nb = 32;
	}
      } else {
	if( n4<=64 ) {
	  nb = 1;
	} else {
	  nb = 32;
	}
      }
    }
  } else if( !strcmp(c2,"PB") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	if( n2<=64 ) {
	  nb = 1;
	} else {
	  nb = 32;
	}
      } else {
	if( n2<=64 ) {
	  nb = 1;
	} else {
	  nb = 32;
	}
      }
    }
  } else if( !strcmp(c2,"TR") ) {
    if( !strcmp(c3,"TRI") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    }
  } else if( !strcmp(c2,"LA") ) {
    if( !strcmp(c3,"UUM") ) {
      if( sname ) {
	nb = 64;
      } else {
	nb = 64;
      }
    }
  } else if( sname && !strcmp(c2,"ST") ) {
    if( !strcmp(c3,"EBZ") ) {
      nb = 1;
    }
  }
  ilaenv_R = nb;
  return ilaenv_R;

 L_200:
  /**
   *     ISPEC = 2:  minimum block size
   **/
  nbmin = 2;
  if( !strcmp(c2,"GE") ) {
    if( !strcmp(c3,"QRF") || !strcmp(c3,"RQF") || !strcmp(c3,"LQF") ||
       !strcmp(c3,"QLF") ) {
      if( sname ) {
	nbmin = 2;
      } else {
	nbmin = 2;
      }
    } else if( !strcmp(c3,"HRD") ) {
      if( sname ) {
	nbmin = 2;
      } else {
	nbmin = 2;
      }
    } else if( !strcmp(c3,"BRD") ) {
      if( sname ) {
	nbmin = 2;
      } else {
	nbmin = 2;
      }
    } else if( !strcmp(c3,"TRI") ) {
      if( sname ) {
	nbmin = 2;
      } else {
	nbmin = 2;
      }
    }
  } else if( !strcmp(c2,"SY") ) {
    if( !strcmp(c3,"TRF") ) {
      if( sname ) {
	nbmin = 2;
      } else {
	nbmin = 2;
      }
    } else if( sname && !strcmp(c3,"TRD") ) {
      nbmin = 2;
    }
  } else if( cname && !strcmp(c2,"HE") ) {
    if( !strcmp(c3,"TRD") ) {
      nbmin = 2;
    }
  } else if( sname && !strcmp(c2,"OR") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nbmin = 2;
      }
    } else if( c3[0]=='M' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nbmin = 2;
      }
    }
  } else if( cname && !strcmp(c2,"UN") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nbmin = 2;
      }
    } else if( c3[0]=='M' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nbmin = 2;
      }
    }
  }
  ilaenv_R = nbmin;
  return ilaenv_R;

 L_300:
  /**
   *     ISPEC = 3:  crossover point
   **/
  nx = 0;
  if( !strcmp(c2,"GE") ) {
    if( !strcmp(c3,"QRF") || !strcmp(c3,"RQF") || !strcmp(c3,"LQF") ||
       !strcmp(c3,"QLF") ) {
      if( sname ) {
	nx = 128;
      } else {
	nx = 128;
      }
    } else if( !strcmp(c3,"HRD") ) {
      if( sname ) {
	nx = 128;
      } else {
	nx = 128;
      }
    } else if( !strcmp(c3,"BRD") ) {
      if( sname ) {
	nx = 128;
      } else {
	nx = 128;
      }
    }
  } else if( !strcmp(c2,"SY") ) {
    if( sname && !strcmp(c3,"TRD") ) {
      nx = 1;
    }
  } else if( cname && !strcmp(c2,"HE") ) {
    if( !strcmp(c3,"TRD") ) {
      nx = 1;
    }
  } else if( sname && !strcmp(c2,"OR") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nx = 128;
      }
    }
  } else if( cname && !strcmp(c2,"UN") ) {
    if( c3[0]=='G' ) {
      if( !strcmp(c4,"QR") || !strcmp(c4,"RQ") || !strcmp(c4,"LQ") ||
	 !strcmp(c4,"QL") || !strcmp(c4,"HR") || !strcmp(c4,"TR") ||
	 !strcmp(c4,"BR") ) {
	nx = 128;
      }
    }
  }
  ilaenv_R = nx;
  return ilaenv_R;

 L_400:
  /**
   *     ISPEC = 4:  number of shifts (used by xHSEQR)
   **/
  ilaenv_R = 6;
  return ilaenv_R;

 L_500:
  /**
   *     ISPEC = 5:  minimum column dimension (not used)
   **/
  ilaenv_R = 2;
  return ilaenv_R;

 L_600:
  /**
   *     ISPEC = 6:  crossover point for SVD (used by xGELSS and xGESVD)
   **/
  ilaenv_R = (long)(min( n1, n2 ) * 1.6e0);
  return ilaenv_R;

 L_700:
  /**
   *     ISPEC = 7:  number of processors (not used)
   **/
  ilaenv_R = 1;
  return ilaenv_R;

 L_800:
  /**
   *     ISPEC = 8:  crossover point for multishift (used by xHSEQR)
   **/
  ilaenv_R = 50;
  return ilaenv_R;
  /**
   *     End of ILAENV
   **/
}