File: utils.f

package info (click to toggle)
qrupdate 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 596 kB
  • ctags: 289
  • sloc: fortran: 5,031; makefile: 102; sh: 18; perl: 17
file content (1098 lines) | stat: -rw-r--r-- 29,371 bytes parent folder | download | duplicates (5)
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
c Copyright (C) 2008, 2009  VZLU Prague, a.s., Czech Republic
c
c Author: Jaroslav Hajek <highegg@gmail.com>
c
c This file is part of qrupdate.
c
c qrupdate is free software; you can redistribute it and/or modify
c it under the terms of the GNU General Public License as published by
c the Free Software Foundation; either version 3 of the License, or
c (at your option) any later version.
c
c This program is distributed in the hope that it will be useful,
c but WITHOUT ANY WARRANTY; without even the implied warranty of
c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c GNU General Public License for more details.
c
c You should have received a copy of the GNU General Public License
c along with this software; see the file COPYING.  If not, see
c <http://www.gnu.org/licenses/>.
c
      subroutine srandg(m,n,x,ldx)
      integer m,n,ldx
      real x(ldx,*)
      external slaruv
      integer seed(4),j,k
      common /xrand/ seed
      do j = 1,n
        do k = 1,m,128
          call slaruv(seed,min(m-k+1,128),x(k,j))
        end do
      end do
      do j = 1,n
        do k = 1,m
          if (.not. (x(k,j) > 0e0 .and. x(k,j) < 1e0)) then
            stop 'slaruv produced invalid number'
          end if
        end do
      end do
      end subroutine

      subroutine drandg(m,n,x,ldx)
      integer m,n,ldx
      double precision x(ldx,*)
      external dlaruv
      integer seed(4),j,k
      common /xrand/ seed
      do j = 1,n
        do k = 1,m,128
          call dlaruv(seed,min(m-k+1,128),x(k,j))
        end do
      end do
      do j = 1,n
        do k = 1,m
          if (.not. (x(k,j) > 0d0 .and. x(k,j) < 1d0)) then
            stop 'dlaruv produced invalid number'
          end if
        end do
      end do
      end subroutine

      subroutine crandg(m,n,x,ldx)
      integer m,n,ldx
      complex x(ldx,*)
      external srandg
      call srandg(2*m,n,x,2*ldx)
      end subroutine

      subroutine zrandg(m,n,x,ldx)
      integer m,n,ldx
      double complex x(ldx,*)
      external srandg
      call drandg(2*m,n,x,2*ldx)
      end subroutine

      block data xrandi
      integer seed(4)
      common /xrand/ seed
      data seed /4*1/
      end block data

      subroutine sqrgen(m,n,A,lda,Q,ldq,R,ldr)
      integer m,n,lda,ldq,ldr
      real A(lda,n),Q(ldq,m),R(ldr,n)
      real work(max(m,n)),tau(min(m,n))
      integer info,i,j
      external slacpy,sgeqrf,sorgqr
      if (m == 0) return
      call slacpy('0',m,n,A,lda,R,ldr)
      call sgeqrf(m,n,R,ldr,tau,work,max(m,n),info)
      do i = 1,n
        do j = i+1,m
          Q(j,i) = R(j,i)
          R(j,i) = 0e0
        end do
      end do
      call sorgqr(m,m,min(m,n),Q,ldq,tau,work,max(m,n),info)
      end subroutine

      subroutine dqrgen(m,n,A,lda,Q,ldq,R,ldr)
      integer m,n,lda,ldq,ldr
      double precision A(lda,n),Q(ldq,m),R(ldr,n)
      double precision work(max(m,n)),tau(min(m,n))
      integer info,i,j
      external dlacpy,dgeqrf,dorgqr
      if (m == 0) return
      call dlacpy('0',m,n,A,lda,R,ldr)
      call dgeqrf(m,n,R,ldr,tau,work,max(m,n),info)
      do i = 1,n
        do j = i+1,m
          Q(j,i) = R(j,i)
          R(j,i) = 0d0
        end do
      end do
      call dorgqr(m,m,min(m,n),Q,ldq,tau,work,max(m,n),info)
      end subroutine

      subroutine cqrgen(m,n,A,lda,Q,ldq,R,ldr)
      integer m,n,lda,ldq,ldr
      complex A(lda,n),Q(ldq,m),R(ldr,n)
      complex work(max(m,n)),tau(min(m,n))
      integer info,i,j
      external clacpy,cgeqrf,cungqr
      if (m == 0) return
      call clacpy('0',m,n,A,lda,R,ldr)
      call cgeqrf(m,n,R,ldr,tau,work,max(m,n),info)
      do i = 1,n
        do j = i+1,m
          Q(j,i) = R(j,i)
          R(j,i) = 0e0
        end do
      end do
      call cungqr(m,m,min(m,n),Q,ldq,tau,work,max(m,n),info)
      end subroutine

      subroutine zqrgen(m,n,A,lda,Q,ldq,R,ldr)
      integer m,n,lda,ldq,ldr
      double complex A(lda,n),Q(ldq,m),R(ldr,n)
      double complex work(max(m,n)),tau(min(m,n))
      integer info,i,j
      external zlacpy,zgeqrf,zungqr
      if (m == 0) return
      call zlacpy('0',m,n,A,lda,R,ldr)
      call zgeqrf(m,n,R,ldr,tau,work,max(m,n),info)
      do i = 1,n
        do j = i+1,m
          Q(j,i) = R(j,i)
          R(j,i) = 0d0
        end do
      end do
      call zungqr(m,m,min(m,n),Q,ldq,tau,work,max(m,n),info)
      end subroutine

      subroutine smdump(name,m,n,A,lda)
      character(*) name
      integer m,n,lda
      real A(lda,n)
      integer i,j
      write (*,1001) name
      do i = 1,m
        do j = 1,n
          write(*,1002) A(i,j)
        end do
        write(*,*)
      end do

 1001 format (A,' = ')
 1002 format (1x,F6.3,$)
      end subroutine

      subroutine dmdump(name,m,n,A,lda)
      character(*) name
      integer m,n,lda
      double precision A(lda,n)
      integer i,j
      write (*,1001) name
      do i = 1,m
        do j = 1,n
          write(*,1002) A(i,j)
        end do
        write(*,*)
      end do

 1001 format (A,' = ')
 1002 format (1x,F6.3,$)
      end subroutine

      subroutine cmdump(name,m,n,A,lda)
      character(*) name
      integer m,n,lda
      complex A(lda,n)
      integer i,j
      write (*,1001) name
      do i = 1,m
        do j = 1,n
          write(*,1002) A(i,j)
        end do
        write(*,*)
      end do

 1001 format (A,' = ')
 1002 format (1x,F6.3,SP,F6.3,'i',$)
      end subroutine

      subroutine zmdump(name,m,n,A,lda)
      character(*) name
      integer m,n,lda
      double complex A(lda,n)
      integer i,j
      write (*,1001) name
      do i = 1,m
        do j = 1,n
          write(*,1002) A(i,j)
        end do
        write(*,*)
      end do

 1001 format (A,' = ')
 1002 format (1x,F6.3,SP,F6.3,'i',$)
      end subroutine

      character*4 function spftol(rnrm)
      real rnrm,slamch
      external slamch
      common /stats/ passed,failed
      integer passed,failed
      if (rnrm < 2e2*slamch('p')) then
        spftol = 'PASS'
        passed = passed + 1
      else
        spftol = 'FAIL'
        failed = failed + 1
      end if
      end function

      character*4 function dpftol(rnrm)
      double precision rnrm,dlamch
      external dlamch
      common /stats/ passed,failed
      integer passed,failed
      if (rnrm < 2d2*dlamch('p')) then
        dpftol = 'PASS'
        passed = passed + 1
      else
        dpftol = 'FAIL'
        failed = failed + 1
      end if
      end function

      subroutine pstats
      common /stats/ passed,failed
      integer passed,failed

      write(*,1001) 
      write(*,1002) passed,failed
      write(*,*)
 1001 format(70('-'))
 1002 format(1x,'total:',5x,'PASSED',1x,I3,5x,'FAILED',1x,I3)
      end subroutine

      subroutine sqrchk(m,n,k,A,lda,Q,ldq,R,ldr)
      integer m,n,k,lda,ldq,ldr
      real A(lda,max(n,k)),Q(ldq,k),R(ldr,n)
      real rnrm,slange,slansy
      external sgemm,ssyrk,slange,slansy,spftol
      character*4 spftol
      real wrk(m)
      integer i

c get residual
      call sgemm('N','N',m,n,k,-1e0,Q,ldq,R,ldr,1e0,A,lda)
c get frobenius norm
      rnrm = slange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
c form Q'*Q - I
      call ssyrk('U','T',k,m,1e0,Q,ldq,0e0,A,lda)
      do i = 1,k
        A(i,i) = A(i,i) - 1e0
      end do
c get frobenius norm
      rnrm = slansy('M','U',k,A,lda,wrk)
      write(*,1002) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
 1002 format('orth. residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine dqrchk(m,n,k,A,lda,Q,ldq,R,ldr)
      integer m,n,k,lda,ldq,ldr
      double precision A(lda,max(n,k)),Q(ldq,k),R(ldr,n)
      double precision rnrm,dlange,dlansy
      external dgemm,dsyrk,dlange,dlansy,dpftol
      character*4 dpftol
      double precision wrk(m)
      integer i

c get residual
      call dgemm('N','N',m,n,k,-1d0,Q,ldq,R,ldr,1d0,A,lda)
c get frobenius norm
      rnrm = dlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
c form Q'*Q - I
      call dsyrk('U','T',k,m,1d0,Q,ldq,0d0,A,lda)
      do i = 1,k
        A(i,i) = A(i,i) - 1d0
      end do
c get frobenius norm
      rnrm = dlansy('M','U',k,A,lda,wrk)
      write(*,1002) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
 1002 format('orth. residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine cqrchk(m,n,k,A,lda,Q,ldq,R,ldr)
      integer m,n,k,lda,ldq,ldr
      complex A(lda,max(n,k)),Q(ldq,k),R(ldr,n)
      real rnrm,clange,clanhe
      external cgemm,cherk,clange,clanhe,spftol
      character*4 spftol
      real wrk(m)
      integer i

c get residual
      call cgemm('N','N',m,n,k,-(1e0,0e0),Q,ldq,R,ldr,(1e0,0e0),A,lda)
c get frobenius norm
      rnrm = clange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
c form Q'*Q - I
      call cherk('U','C',k,m,1e0,Q,ldq,0e0,A,lda)
      do i = 1,k
        A(i,i) = A(i,i) - 1e0
      end do
c get frobenius norm
      rnrm = clanhe('M','U',k,A,lda,wrk)
      write(*,1002) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
 1002 format('orth. residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine zqrchk(m,n,k,A,lda,Q,ldq,R,ldr)
      integer m,n,k,lda,ldq,ldr
      double complex A(lda,max(n,k)),Q(ldq,k),R(ldr,n)
      double precision rnrm,zlange,zlanhe
      external zgemm,zherk,zlange,zlanhe,dpftol
      character*4 dpftol
      double precision wrk(m)
      integer i

c get residual
      call zgemm('N','N',m,n,k,-(1d0,0d0),Q,ldq,R,ldr,(1d0,0d0),A,lda)
c get frobenius norm
      rnrm = zlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
c form Q'*Q - I
      call zherk('U','C',k,m,1d0,Q,ldq,0d0,A,lda)
      do i = 1,k
        A(i,i) = A(i,i) - 1d0
      end do
c get frobenius norm
      rnrm = zlanhe('M','U',k,A,lda,wrk)
      write(*,1002) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
 1002 format('orth. residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine schgen(n,A,lda,R,ldr)
      integer n,lda,ldr
      real A(lda,n),R(ldr,n)
      external ssyrk,slacpy,spotrf
      integer i,j,info
      call ssyrk('U','T',n,n,1e0,A,lda,0e0,R,ldr)
c augment to ensure strict positivity, zero below diag
      do i = 1,n
        R(i,i) = R(i,i) + 1e-3
c zero below diagonal
        do j = i+1,n
          R(j,i) = 0e0
        end do
      end do
      call slacpy('U',n,n,R,ldr,A,lda)
c symmetrize A
      do i = 1,n-1
        do j = i+1,n
          A(j,i) = A(i,j)
        end do
      end do
      call spotrf('U',n,R,ldr,info)
      if (info /= 0) stop 'fatal:error generating positive matrix'
      end subroutine

      subroutine dchgen(n,A,lda,R,ldr)
      integer n,lda,ldr
      double precision A(lda,n),R(ldr,n)
      external dsyrk,dlacpy,dpotrf
      integer i,j,info
      call dsyrk('U','T',n,n,1d0,A,lda,0d0,R,ldr)
c augment to ensure strict positivity
      do i = 1,n
        R(i,i) = R(i,i) + 1d-3
c zero below diagonal
        do j = i+1,n
          R(j,i) = 0d0
        end do
      end do
      call dlacpy('U',n,n,R,ldr,A,lda)
c symmetrize A
      do i = 1,n-1
        do j = i+1,n
          A(j,i) = A(i,j)
        end do
      end do
      call dpotrf('U',n,R,ldr,info)
      if (info /= 0) stop 'fatal:error generating positive matrix'
      end subroutine

      subroutine cchgen(n,A,lda,R,ldr)
      integer n,lda,ldr
      complex A(lda,n),R(ldr,n)
      external cherk,clacpy,cpotrf,cdotc
      complex cdotc,Rii
      integer i,j,info
      call cherk('U','C',n,n,1e0,A,lda,0e0,R,ldr)
c augment to ensure strict positivity
      do i = 1,n
c CHERK is often buggy. We'll recompute the diagonal elements and
c possibly warn about the bug.
        Rii = cdotc (n, A(1,i), 1, A(1,i), 1)
        if (.not. abs (Rii - R(i,i)) < 1e-5 * abs(Rii)) then
          write (*,1001)
          write (*,1002) R(i,i), Rii
        endif
        R(i,i) = Rii + 1e-3
c zero below diagonal
        do j = i+1,n
          R(j,i) = 0e0
        end do
      end do
      call clacpy('U',n,n,R,ldr,A,lda)
c symmetrize A
      do i = 1,n-1
        do j = i+1,n
          A(j,i) = conjg(A(i,j))
        end do
      end do
      call cpotrf('U',n,R,ldr,info)
      if (info /= 0) stop 'fatal:error generating positive matrix'
 1001 format ('WARNING: Possible bug in BLAS CHERK:')
 1002 format ('WARNING: CHERK computed diagonal element (',
     +F6.3,F6.3,'), direct computation gives (',F6.3,F6.3,').')
      end subroutine

      subroutine zchgen(n,A,lda,R,ldr)
      integer n,lda,ldr
      double complex A(lda,n),R(ldr,n)
      external zherk,zlacpy,zpotrf,zdotc
      double complex zdotc,Rii
      integer i,j,info
      call zherk('U','C',n,n,1d0,A,lda,0d0,R,ldr)
c augment to ensure strict positivity
      do i = 1,n
c ZHERK is often buggy. We'll recompute the diagonal elements and
c possibly warn about the bug.
        Rii = zdotc (n, A(1,i), 1, A(1,i), 1)
        if (.not. abs (Rii - R(i,i)) < 1d-10 * abs(Rii)) then
          write (*,1001)
          write (*,1002) R(i,i), Rii 
        endif
        R(i,i) = Rii + 1d-3
c zero below diagonal
        do j = i+1,n
          R(j,i) = 0d0
        end do
      end do
      call zlacpy('U',n,n,R,ldr,A,lda)
c symmetrize A
      do i = 1,n-1
        do j = i+1,n
          A(j,i) = conjg(A(i,j))
        end do
      end do
      call zpotrf('U',n,R,ldr,info)
      if (info /= 0) stop 'fatal:error generating positive matrix'
 1001 format ('WARNING: Possible bug in BLAS CHERK:')
 1002 format ('WARNING: ZHERK computed diagonal element (',
     +F6.3,F6.3,'), direct computation gives (',F6.3,F6.3,').')
      end subroutine

      subroutine schchk(n,A,lda,R,ldr)
      integer n,lda,ldr
      real A(lda,n),R(ldr,n)
      real rnrm,slansy
      external ssyrk,slansy,spftol
      character*4 spftol
      real wrk(n)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,n
          R(i,j) = 0e0
        end do
      end do
c form A - R'*R
      call ssyrk('U','T',n,n,1e0,R,ldr,-1e0,A,lda)
c get frobenius norm
      rnrm = slansy('M','U',n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine dchchk(n,A,lda,R,ldr)
      integer n,lda,ldr
      double precision A(lda,n),R(ldr,n)
      double precision rnrm,dlansy
      external dsyrk,dlansy,dpftol
      character*4 dpftol
      double precision wrk(n)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,n
          R(i,j) = 0d0
        end do
      end do
c form A - R'*R
      call dsyrk('U','T',n,n,1d0,R,ldr,-1d0,A,lda)
c get frobenius norm
      rnrm = dlansy('M','U',n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine cchchk(n,A,lda,R,ldr)
      integer n,lda,ldr
      complex A(lda,n),R(ldr,n)
      real rnrm,clanhe
      external cherk,clanhe,spftol
      character*4 spftol
      real wrk(n)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,n
          R(i,j) = 0e0
        end do
      end do
c form A - R'*R
      call cherk('U','C',n,n,1e0,R,ldr,-1e0,A,lda)
c get frobenius norm
      rnrm = clanhe('M','U',n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine zchchk(n,A,lda,R,ldr)
      integer n,lda,ldr
      double complex A(lda,n),R(ldr,n)
      double precision rnrm,zlanhe
      external zherk,zlanhe,dpftol
      character*4 dpftol
      double precision wrk(n)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,n
          R(i,j) = 0d0
        end do
      end do
c form A - R'*R
      call zherk('U','C',n,n,1d0,R,ldr,-1d0,A,lda)
c get frobenius norm
      rnrm = zlanhe('M','U',n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine slugen(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      real A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j
      external sswap,slacpy,sgetrf
      if (m >= n) then
        call slacpy('0',m,n,A,lda,L,ldl)
        call sgetrf(m,n,L,ldl,ipiv,info)
        call slacpy('U',m,n,L,ldl,R,ldr)
      else
        call slacpy('0',m,n,A,lda,R,ldr)
        call sgetrf(m,n,R,ldr,ipiv,info)
        call slacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0e0
        end do
        L(i,i) = 1e0
      end do
c permute the orig matrix      
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          call sswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      end subroutine

      subroutine dlugen(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      double precision A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j
      external dswap,dlacpy,dgetrf
      if (m >= n) then
        call dlacpy('0',m,n,A,lda,L,ldl)
        call dgetrf(m,n,L,ldl,ipiv,info)
        call dlacpy('U',m,n,L,ldl,R,ldr)
      else
        call dlacpy('0',m,n,A,lda,R,ldr)
        call dgetrf(m,n,R,ldr,ipiv,info)
        call dlacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0d0
        end do
        L(i,i) = 1d0
      end do
c permute the orig matrix      
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          call dswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      end subroutine

      subroutine clugen(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j
      external cswap,clacpy,cgetrf
      if (m >= n) then
        call clacpy('0',m,n,A,lda,L,ldl)
        call cgetrf(m,n,L,ldl,ipiv,info)
        call clacpy('U',m,n,L,ldl,R,ldr)
      else
        call clacpy('0',m,n,A,lda,R,ldr)
        call cgetrf(m,n,R,ldr,ipiv,info)
        call clacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0d0
        end do
        L(i,i) = 1e0
      end do
c permute the orig matrix      
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          call cswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      end subroutine

      subroutine zlugen(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      double complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j
      external zswap,zlacpy,zgetrf
      if (m >= n) then
        call zlacpy('0',m,n,A,lda,L,ldl)
        call zgetrf(m,n,L,ldl,ipiv,info)
        call zlacpy('U',m,n,L,ldl,R,ldr)
      else
        call zlacpy('0',m,n,A,lda,R,ldr)
        call zgetrf(m,n,R,ldr,ipiv,info)
        call zlacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0d0
        end do
        L(i,i) = 1d0
      end do
c permute the orig matrix      
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          call zswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      end subroutine

      subroutine sluchk(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      real A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      real rnrm,slange
      external sgemm,slange,spftol
      character*4 spftol
      real wrk(1)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
c form A - L*R
      call sgemm('N','N',m,n,min(m,n),1e0,L,ldl,R,ldr,-1e0,A,lda)
c get frobenius norm
      rnrm = slange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine dluchk(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      double precision A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      double precision rnrm,dlange
      external dgemm,dlange,dpftol
      character*4 dpftol
      double precision wrk(1)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
c form A - L*R
      call dgemm('N','N',m,n,min(m,n),1d0,L,ldl,R,ldr,-1d0,A,lda)
c get frobenius norm
      rnrm = dlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine cluchk(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      real rnrm,clange
      external cgemm,clange,spftol
      character*4 spftol
      real wrk(1)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
c form A - L*R
      call cgemm('N','N',m,n,min(m,n),(1e0,0e0),L,ldl,R,ldr,(-1e0,0e0),
     +A,lda)
c get frobenius norm
      rnrm = clange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine zluchk(m,n,A,lda,L,ldl,R,ldr)
      integer m,n,lda,ldl,ldr
      double complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      double precision rnrm,zlange
      external zgemm,zlange,dpftol
      character*4 dpftol
      double precision wrk(1)
      integer i,j

c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
c form A - L*R
      call zgemm('N','N',m,n,min(m,n),(1d0,0d0),L,ldl,R,ldr,(-1d0,0d0),
     +A,lda)
c get frobenius norm
      rnrm = zlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine slupgen(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      real A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j,tmp
      external sswap,slacpy,sgetrf
      if (m >= n) then
        call slacpy('0',m,n,A,lda,L,ldl)
        call sgetrf(m,n,L,ldl,ipiv,info)
        call slacpy('U',m,n,L,ldl,R,ldr)
      else
        call slacpy('0',m,n,A,lda,R,ldr)
        call sgetrf(m,n,R,ldr,ipiv,info)
        call slacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0e0
        end do
        L(i,i) = 1e0
      end do
c generate permutation
      do i = 1,m
        p(i) = i
      end do
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          tmp = p(i)
          p(i) = p(j)
          p(j) = tmp
        end if
      end do
c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
      end subroutine

      subroutine dlupgen(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      double precision A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j,tmp
      external dswap,dlacpy,dgetrf
      if (m >= n) then
        call dlacpy('0',m,n,A,lda,L,ldl)
        call dgetrf(m,n,L,ldl,ipiv,info)
        call dlacpy('U',m,n,L,ldl,R,ldr)
      else
        call dlacpy('0',m,n,A,lda,R,ldr)
        call dgetrf(m,n,R,ldr,ipiv,info)
        call dlacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0d0
        end do
        L(i,i) = 1d0
      end do
c generate permutation
      do i = 1,m
        p(i) = i
      end do
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          tmp = p(i)
          p(i) = p(j)
          p(j) = tmp
        end if
      end do
c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0d0
        end do
      end do
      end subroutine

      subroutine clupgen(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j,tmp
      external cswap,clacpy,cgetrf
      if (m >= n) then
        call clacpy('0',m,n,A,lda,L,ldl)
        call cgetrf(m,n,L,ldl,ipiv,info)
        call clacpy('U',m,n,L,ldl,R,ldr)
      else
        call clacpy('0',m,n,A,lda,R,ldr)
        call cgetrf(m,n,R,ldr,ipiv,info)
        call clacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0e0
        end do
        L(i,i) = 1e0
      end do
c generate permutation
      do i = 1,m
        p(i) = i
      end do
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          tmp = p(i)
          p(i) = p(j)
          p(j) = tmp
        end if
      end do
c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0e0
        end do
      end do
      end subroutine

      subroutine zlupgen(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      double complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      integer ipiv(min(m,n)),info,i,j,tmp
      external zswap,zlacpy,zgetrf
      if (m >= n) then
        call zlacpy('0',m,n,A,lda,L,ldl)
        call zgetrf(m,n,L,ldl,ipiv,info)
        call zlacpy('U',m,n,L,ldl,R,ldr)
      else
        call zlacpy('0',m,n,A,lda,R,ldr)
        call zgetrf(m,n,R,ldr,ipiv,info)
        call zlacpy('L',m,n,R,ldr,L,ldl)
      end if
      do i = 1,min(m,n)
        do j = 1,i-1
          L(j,i) = 0d0
        end do
        L(i,i) = 1d0
      end do
c generate permutation
      do i = 1,m
        p(i) = i
      end do
      do i = 1,min(m,n)
        j = ipiv(i)
        if (i /= j) then
          tmp = p(i)
          p(i) = p(j)
          p(j) = tmp
        end if
      end do
c zero lower triangle of R
      do j = 1,n-1
        do i = j+1,min(m,n)
          R(i,j) = 0d0
        end do
      end do
      end subroutine

c converts a linear permutation into LAPACK ipiv-style form
      subroutine p2ipiv(n,p)
      integer n,p(n)
      integer q(n),i,j,k
      do i = 1,n
        q(p(i)) = i
      end do
      do i = 1,n
        j = p(i)
        k = q(i)
        if (j /= i) then
          p(k) = j
          q(j) = k
          p(i) = j
        end if
      end do
      end subroutine

      subroutine slupchk(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      real A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      real rnrm,slange
      external sswap,sgemm,slange,spftol
      character*4 spftol
      real wrk(1)
      integer i,j

c convert p into successive swaps      
      call p2ipiv(m,p)
c form A - L*R
      do i = 1,m
        j = p(i)
        if (i /= j) then
          call sswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      call sgemm('N','N',m,n,min(m,n),1e0,L,ldl,R,ldr,-1e0,A,lda)
c get frobenius norm
      rnrm = slange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine dlupchk(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      double precision A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      double precision rnrm,dlange
      external dswap,dgemm,dlange,dpftol
      character*4 dpftol
      double precision wrk(1)
      integer i,j

c convert p into successive swaps      
      call p2ipiv(m,p)
c form A - L*R
      do i = 1,m
        j = p(i)
        if (i /= j) then
          call dswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      call dgemm('N','N',m,n,min(m,n),1d0,L,ldl,R,ldr,-1d0,A,lda)
c get frobenius norm
      rnrm = dlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine clupchk(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      real rnrm,clange
      external cswap,cgemm,clange,spftol
      character*4 spftol
      real wrk(1)
      integer i,j

c convert p into successive swaps      
      call p2ipiv(m,p)
c form A - L*R
      do i = 1,m
        j = p(i)
        if (i /= j) then
          call cswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      call cgemm('N','N',m,n,min(m,n),(1e0,0e0),L,ldl,R,ldr,(-1e0,0e0),
     +A,lda)
c get frobenius norm
      rnrm = clange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,spftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine

      subroutine zlupchk(m,n,A,lda,L,ldl,R,ldr,p)
      integer m,n,lda,ldl,ldr,p(m)
      double complex A(lda,n),L(ldl,min(m,n)),R(ldr,n)
      double precision rnrm,zlange
      external zswap,zgemm,zlange,dpftol
      character*4 dpftol
      double precision wrk(1)
      integer i,j

c convert p into successive swaps      
      call p2ipiv(m,p)
c form A - L*R
      do i = 1,m
        j = p(i)
        if (i /= j) then
          call zswap(n,A(i,1),lda,A(j,1),lda)
        end if
      end do
      call zgemm('N','N',m,n,min(m,n),(1d0,0d0),L,ldl,R,ldr,(-1d0,0d0),
     +A,lda)
c get frobenius norm
      rnrm = zlange('M',m,n,A,lda,wrk)
      write(*,1001) rnrm,dpftol(rnrm)
      return

 1001 format(6x,'residual error = ',10x,E21.12,5x,A6)
      end subroutine