-
Notifications
You must be signed in to change notification settings - Fork 2
/
schurQR.f
2572 lines (2521 loc) · 72.9 KB
/
schurQR.f
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
christen this file schurQR.f
cut here >>>>>>>>>>>>>>>>>>>
c Copyright (C) 2011 Roger Fletcher
c Current version dated 17 January 2012
c THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC
c LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
c CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
c***************** sparse matrix routines for manipulating L *******************
c **********************************************************
c Basis matrix routines for LCP solvers with sparse matrices
c **********************************************************
c These routines form and update L-Implicit-U factors LPB=U of a matrix B
c whose columns are the normal vectors of the active constraints. In this
c method only the unit lower triangular matrix L and the diagonal of U (in
c addition to the row permutation P) is stored. B is represented in block form
c | I A_2 | where the last m1 columns (A_2 and A_1) come from the
c | 0 A_1 | general constraint normals (columns of the matrix A in bqpd)
c and the remaining unit columns come from simple bounds. The matrix A must be
c specified in sparse format and the user is referred to the file sparseA.f.
c The data structure used for L is that of a profile or skyline scheme, in
c which the nontrivial rows of L are stored as dense row spikes. The use of
c a Tarjan+spk1 ordering algorithm to control the length of these spikes has
c proved quite effective.
c In schurQR.f, the factors are updated by the Schur complement method with
c QR factors. This is based on the block factorization
c
c | B V | = | L 0 | | U V |
c | E 0 | | S I | | 0 C |
c
c where V are columns of the constraint normals [I A] that have been added to
c the active set, and E has unit rows in which the unit element marks
c columns of B that have been removed from B. C=-E.inv(B).V is the
c Schur complement matrix, which is independent of how L and U are defined,
c and its QR factors are stored. The current dimension of C is stored in the
c parameter ms of common/refactorc/mc,mxmc and the user must set a
c maximum permitted value of mc in mxmc (mxmc <= n). The current basis matrix
c is refactored if mc would exceed mxmc, or if issues of numerical stability
c arise. Typically mxmc=25 is suitable.
c Workspace
c *********
c The user needs to supply storage for the row spikes in the LIU data
c structure of L, Also storage for matrices in the Schur complement scheme
c is required. The amount of storage required is unknown a-priori.
c Storage for schurQR.f is situated at the end of the workspace arrays ws
c and lws in bqpd. Allow as much space for ws as you can afford: the routine
c will report if there is not enough. So far 10^6 locations has proved
c adequate for problems of up to 5000 variables.
c The user is also allowed to reserve storage in ws and lws, for use in the
c user-supplied routine gdotx. This storage is situated at the start of the
c arrays ws and lws. The user specifies the amount required by
c setting the parameters kk and ll in the common block
c common/wsc/kk,ll,kkk,lll,mxws,mxlws
c Storage required by the LCP solver is also required: the amount is set by
c the LCP solver in kkk and lll. The user MUST set mxws and mxlws to be
c the total amount of real and integer workspace for the arrays ws and lws.
c Other information
c *****************
c The methodology behind the L-Implicit-U factors and the row spike storage
c scheme for L is described in the references
c Fletcher R., Dense Factors of Sparse Matrices, in "Approximation Theory
c and Optimization. Tributes to M.J.D. Powell", (M.D. Buhmann and A. Iserles,
c eds), Cambridge University Press (1997), pp. 145-166.
c and
c Fletcher R., Block Triangular Orderings and Factors for Sparse Matrices
c in LP, in "Numerical analysis 1997" (D.F. Griffiths, D.J. Higham and
c G.A. Watson, eds.), Pitman Research Notes in Mathematics 380, (1998),
c Longman, Harlow, pp. 91-110.
c The file contains routines for solving systems with B or its transpose
c which might be of use in association with bqpd. These routines are
c documented below.
c Steepest edge coefficients e(i) are also updated in these routines
subroutine start_up(n,nm,nmi,a,la,nk,e,ls,aa,ll,mode,ifail)
implicit double precision (a-h,r-z), integer (i-q)
dimension a(*),la(0:*),e(*),ls(*),aa(*),ll(*)
common/noutc/nout
common/wsc/kk,ll_,kkk,lll,mxws,mxlws
common/epsc/eps,tol,emin
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv,lv1,le,le1
common/factorc/m1,m2,mp,mq,lastr,irow
common/refactorc/mc,mxmc
mxmc=min(n,mxmc)
c set storage map for sparse factors
ns=n
ns1=ns+1
nt=ns+n
nt1=nt+1
nu=nt+n
nu1=nu+1
nx=nu+n
nx1=nx+1
nq=nx+n
nq1=nq+1
nr=nq+mxmc**2
nr1=nr+1
ny=nr+mxmc*(mxmc+1)/2
ny1=ny+1
nz=ny+mxmc+1
nz1=nz+1
np=nz+mxmc+1
np1=np+1
nprof=mxws-kk-kkk-np
c print *,'nprof =',nprof
if(nprof.le.0)then
write(nout,*)'not enough real workspace in ws'
write(nout,*)'you give mxws as',mxws
write(nout,*)'mxws must be much greater than',mxws-nprof
ifail=7
return
endif
lc=n
lc1=lc+1
li=lc+n
li1=li+1
lm=li+nmi
lm1=lm+1
lp=lm+n
lp1=lp+1
lq=lp+n
lq1=lq+1
lr=lq+n
lr1=lr+1
ls_=lr+n
ls1=ls_+1
lt=ls_+n
lt1=lt+1
lv=lt+n
lv1=lv+1
le=lv+mxmc+1
le1=le+1
lleft=mxlws-ll_-lll-le-mxmc-1
if(lleft.lt.0)then
write(nout,*)'not enough integer workspace in lws'
write(nout,*)'you give mxlws as',mxlws
write(nout,*)'minimum value for mxlws is',mxlws-lleft
ifail=7
return
endif
m=nm-n
mp=-1
mq=-1
c write(nout,*)'ls',(ls(ij),ij=1,nk)
if(mode.eq.3)then
if(nk.lt.n)then
c reset ls from e
do j=1,nk
i=-ls(j)
if(i.gt.0)e(i)=-e(i)
enddo
j=0
nk=nmi
do i=1,nmi
if(e(i).ne.0.D0)then
j=j+1
if(e(i).gt.0.D0)then
ls(j)=i
else
ls(j)=-i
e(i)=-e(i)
endif
else
ls(nk)=i
nk=nk-1
endif
enddo
if(j.ne.n)then
write(nout,*)'malfunction in reset sequence in start_up'
stop
endif
endif
c reset lr, lc, li, m1 and m2 from ls
do i=li+n+1,li+nm
ll(i)=0
enddo
m1=n
m2=0
do j=1,n
i=abs(ls(j))
if(i.gt.n)then
ll(lc+m1)=i
ll(li+i)=m1
m1=m1-1
else
m2=m2+1
lii=ll(li+i)
lrm2=ll(m2)
call iexch(ll(lii),ll(m2))
call iexch(ll(li+i),ll(li+lrm2))
endif
enddo
m1=n-m1
call re_order(n,nm,a,la(1),la(la(0)),ll,ll(lc1),ll(li1),
* ll(lm1),ll(lp1),ll(lq1),ll(lr1),ll(ls1),ll(lt1),aa(np1),
* nprof,ifail)
if(ifail.ge.1)then
c write(nout,*)'failure in re_order (1)'
if(ifail.eq.7)return
mode=2
goto1
endif
call re_factor(n,nm,a,la,ll,ll(lc1),ll(li1),
* ll(lm1),ll(lp1),ll(lq1),ll(lr1),ll(ls1),ll(lt1),aa(np1),
* nprof,aa,ifail)
if(ifail.eq.7)return
call check_L(n,aa,ll(lp1),ifail)
if(ifail.eq.1)then
mode=2
goto1
endif
call EBspace(n,ll(lp1),ll(lq1),ll(ls1),ll,aa(np1),
* neb,nprof,ifail)
if(ifail.gt.0)return
neb=np+neb
neb1=neb+1
mc=0
do i=1,m2
ll(lm+i)=ll(i)
enddo
do i=m2+1,n
ll(lm+i)=ll(lc+i)
enddo
return
endif
1 continue
if(emin.eq.0.D0)then
c set a lower bound on e(i): setting emin=0.D0 will force emin to be recalculated: do this only if mode<3
emin=1.D0
do i=1,nmi-n
emin=max(emin,ailen(n,a,la,i))
enddo
emin=1.D0/emin
endif
do i=1,n
ll(i)=i
ll(li+i)=i
e(i)=1.D0
enddo
do i=n+1,nm
ll(li+i)=0
e(i)=0.D0
enddo
nu_=0
if(mode.ne.0)then
c shift designated bounds to end and order the resulting rows and columns
do j=1,nk
i=abs(ls(j))
if(i.le.n)then
nn=n-nu_
nu_=nu_+1
call iexch(ls(nu_),ls(j))
ii=ll(li+i)
ll(ii)=ll(nn)
ll(li+ll(ii))=ii
ll(nn)=i
ll(li+i)=nn
endif
enddo
call order(n,nu_,nk,la,ll,ls,ll(li1),ll(lp1),ll(lq1),ll(lr1),
* aa(np1),nprof,ifail)
if(ifail.gt.0)return
endif
call factor(n,nm,nu_,nk,a,la,e,ls,aa(ns1),aa(nt1),aa(nu1),
* aa(nx1),ll,ll(lc1),ll(li1),ll(lm1),ll(lp1),ll(lq1),ll(lr1),
* ll(ls1),aa(np1),nprof,aa,ifail)
call EBspace(n,ll(lp1),ll(lq1),ll(ls1),ll,aa(np1),
* neb,nprof,ifail)
if(ifail.gt.0)return
neb=np+neb
neb1=neb+1
mc=0
do i=1,m2
ll(lm+i)=ll(i)
enddo
do i=m2+1,n
ll(lm+i)=ll(lc+i)
enddo
if(ifail.gt.0)return
3 format(A/(15I5))
4 format(A/(5E15.7))
c write(nout,*)'steepest edge coefficients',(e(ij),ij=1,nm)
c emax=0.D0
c do i=1,nm
c if(e(i).gt.0.D0)then
c call eptsol(n,a,la,i,a,aa(nq1),aa(nr1),aa(neb1),aa(ny1),
c * aa(ns1),aa(nu1),aa(nx1),aa,aa(np1),
c * ll,ll(lc1),ll(li1),ll(lv1),ll(le1),ll(lp1),ll(lq1),ei)
c emax=max(emax,abs(ei-e(i)))
c endif
c enddo
c if(emax.ge.tol)
c * write(nout,*)'error in steepest edge coefficients =',emax
return
end
subroutine refactor(n,nm,a,la,aa,ll,ifail)
implicit double precision (a-h,o-z)
dimension a(*),la(0:*),aa(*),ll(*)
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv,lv1,le,le1
common/factorc/m1,m2,mp,mq,lastr,irow
common/noutc/nout
c write(nout,*)'refactor'
ifail=1
return
end
subroutine pivot(p,q,n,nm,a,la,e,aa,ll,ifail,npv)
implicit double precision (a-h,r-z), integer (i-q)
dimension a(*),la(0:*),e(*),aa(*),ll(*)
common/noutc/nout
common/iprintc/iprint
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv,lv1,le,le1
common/factorc/m1,m2,mp,mq,lastr,irow
common/mxm1c/mxm1
common/refactorc/mc,mxmc
common/epsc/eps,tol,emin
common/pqc/pc,qr,lmp
c write(nout,*)'pivot: p,q =',p,q
call updateSE(p,q,n,nm,a,la,e,aa(nq1),aa(nr1),aa(neb1),
* aa(ny1),aa(nz1),aa(ns1),aa(nt1),aa(nu1),aa(nx1),aa,aa(np1),ll,
* ll(lc1),ll(li1),ll(lp1),ll(lq1),ll(lm1),ll(lv1),ll(le1),ifail)
if(ifail.eq.1)return
if(mc.eq.mxmc.and.pc.eq.0.and.qr.eq.0)then
c reset permutations and refactorize L
mc1=mc+1
ll(le+mc1)=p
ll(lv+mc1)=q
c print 3,'le =',(ll(le+i),i=1,mc1)
c print 3,'lv =',(ll(lv+i),i=1,mc1)
do i=1,mc1
p=ll(le+i)
q=ll(lv+i)
ip=ll(li+p)
if(p.gt.n)then
m2=m2+1
qq=ll(lc+m2)
ll(lc+ip)=qq
ll(li+qq)=ip
ll(li+p)=0
else
ll(ip)=ll(m2)
ll(li+ll(ip))=ip
ll(m2)=p
ll(li+p)=m2
endif
if(q.gt.n)then
ll(lc+m2)=q
ll(li+q)=m2
m2=m2-1
else
iq=ll(li+q)
ll(iq)=ll(m2)
ll(li+ll(iq))=iq
ll(m2)=q
ll(li+q)=m2
endif
enddo
c print 3,'lr =',(ll(i),i=1,n)
c print 3,'lc =',(ll(lc+i),i=m2+1,n)
c print 3,'li =',(ll(li+i),i=1,nm)
m1=n-m2
c call checkperms(n,ll,ll(lc1),ll(li1))
c mp=-1
c mq=-1
call re_order(n,nm,a,la(1),la(la(0)),ll,ll(lc1),ll(li1),
* ll(lm1),ll(lp1),ll(lq1),ll(lr1),ll(ls1),ll(lt1),aa(np1),
* nprof,ifail)
if(ifail.ge.1)then
c print *,'no traversal in re_order (3)'
ifail=11
return
stop
endif
call re_factor(n,nm,a,la,ll,ll(lc1),ll(li1),
* ll(lm1),ll(lp1),ll(lq1),ll(lr1),ll(ls1),ll(lt1),aa(np1),
* nprof,aa,ifail)
if(ifail.eq.7)return
call EBspace(n,ll(lp1),ll(lq1),ll(ls1),ll,aa(np1),
* neb,nprof,ifail)
if(ifail.gt.0)return
neb=np+neb
neb1=neb+1
mc=0
do i=1,m2
ll(lm+i)=ll(i)
enddo
do i=m2+1,n
ll(lm+i)=ll(lc+i)
enddo
else
call updateQR(p,q,n,a,la,aa(nq1),aa(nr1),aa(neb1),aa(nx1),
* aa(ny1),aa(nz1),ll,ll(lc1),ll(li1),ll(lv1),ll(le1),ll(lm1),
* ifail)
if(ifail.gt.0)return
endif
npv=npv+1
mp=-1
mq=-1
c call check_L(n,aa,ll(lp1),ifail)
c print 4,'e =',(e(i),i=1,nm)
c print 3,'lm =',(ll(lm+i),i=1,n)
return
c check Steepest Edge coefficients
emax=0.D0
do j=1,n
i=ll(lm+j)
call eptsol(n,a,la,i,a,aa(nq1),aa(nr1),aa(neb1),aa(ny1),
* aa(ns1),aa(nu1),aa(nx1),aa,aa(np1),
* ll,ll(lc1),ll(li1),ll(lv1),ll(le1),ll(lp1),ll(lq1),ei)
emax=max(emax,abs(ei-e(i)))
c if(abs(ei-e(i)).gt.tol)then
c print *,'error in steepest edge coefficient =',i,ei,e(i)
c print 4,'s =',(aa(ns+i),i=1,n)
c if(abs(ei-e(i)).gt.1.D-6)stop
c endif
enddo
if(emax.gt.tol)then
print 2,'max error in steepest edge coefficients =',emax
c if(emax.gt.1.D-2)stop
endif
return
2 format(A,5E15.7)
3 format(A/(15I5))
4 format(A/(5E15.7))
5 format((5E15.7))
end
subroutine fbsub(n,jmin,jmax,a,la,q,b,x,ls,aa,ll,save)
implicit double precision (a-h,r-z), integer (i-q)
logical save
9 format(A/(15I5))
dimension a(*),la(*),b(*),x(*),ls(*),aa(*),ll(*)
c solves a system B.x=b
c Parameter list
c **************
c n number of variables (as for bqpd)
c jmin,jmax now redundant
c a,la specification of QP problem data (as for bqpd)
c q an integer which, if in the range 1:n+m, specifies that the rhs vector
c b is to be column q of the matrix A of general constraint normals.
c In this case the parameter b is not referenced by fbsub.
c If q=0 then b is taken as the vector given in the parameter b.
c b(n) must be set to the r.h.s. vector b in natural order (but only if q=0)
c x(n+m) contains the solution x, set according to the index number of that
c component (in the range 1:n for a simple bound and n+1:n+m
c for a general constraint)
c ls(*) now redundant. Previously ls was an index vector, listing the
c components of x that are required. Now all the solution x is provided,
c set as described above.
c aa(*) real storage used by the basis matrix code (supply the vector
c ws(lu1) with ws as in the call of bqpd and lu1 as in common/bqpdc/...)
c ll(*) integer storage used by the basis matrix code (supply the vector
c lws(ll1) with lws as in the call of bqpd and ll1 as in common/bqpdc/...)
c save now redundant
common/noutc/nout
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv,lv1,le,le1
common/factorc/m1,m2,mp,mq,lastr,irow
c write(nout,*)'fbsub q =',q
if(q.eq.0)then
do i=1,n
aa(nt+ll(li+i))=b(i)
enddo
endif
call aqsol(n,a,la,q,aa(nq1),aa(nr1),aa(neb1),aa(nz1),aa(nt1),
* aa(nu1),aa(nx1),aa,aa(np1),ll,ll(lc1),ll(li1),ll(lv1),
* ll(le1),ll(lp1),ll(lq1))
do j=1,n
x(ll(lm+j))=aa(nt+j)
enddo
c print *,'x =',(x(i),i=1,18)
return
end
subroutine ztg(n,k,rg,lv,aa,ll)
implicit double precision (a-h,r-z), integer (i-q)
dimension rg(*),lv(*),aa(*),ll(*)
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv_,lv1,le,le1
c print *,'aa =',(aa(nu+i),i=1,18)
do j=1,k
rg(j)=aa(nu+ll(li+lv(j)))
enddo
return
end
subroutine tfbsub(n,a,la,p,b,x,aa,ll,ep,save)
implicit double precision (a-h,r-z), integer (i-q)
logical save
dimension a(*),la(*),b(*),x(*),aa(*),ll(*)
c solves a system Bt.x=b
c Parameter list
c **************
c n number of variables (as for bqpd)
c a,la specification of QP problem data (as for bqpd)
c p an integer which, if in the range 1:n+m, specifies that the rhs vector
c b is a unit vector appropriate to the position of p in the current
c ordering. In this case b is not referenced by tfbsub.
c b(n+m) If p=0, this must be set to the r.h.s. vector b. Only the components
c of b need be set, according to the index number of each component (in
c the range 1:n for a simple bound and n+1:n+m for a general constraint)
c x(n) contains the solution x (in natural ordering)
c aa(*) real storage used by the basis matrix code (supply the vector
c ws(lu1) with ws as in the call of bqpd and lu1 as in common/bqpdc/...)
c ll(*) integer storage used by the basis matrix code (supply the vector
c lws(ll1) with lws as in the call of bqpd and ll1 as in common/bqpdc/...)
c ep if p>0, ep contains the L2 norm of the solution
c save now redundant
common/noutc/nout
common/schurc/ns,ns1,nt,nt1,nu,nu1,nx,nx1,np,np1,neb,neb1,nprof,
* lc,lc1,li,li1,lm,lm1,lp,lp1,lq,lq1,lr,lr1,ls_,ls1,lt,lt1,
* nq,nq1,nr,nr1,ny,ny1,nz,nz1,lv,lv1,le,le1
common/factorc/m1,m2,mp,mq,lastr,irow
c write(nout,*)'tfbsub p =',p
call eptsol(n,a,la,p,b,aa(nq1),aa(nr1),aa(neb1),aa(ny1),
* aa(ns1),aa(nu1),aa(nx1),aa,aa(np1),ll,ll(lc1),ll(li1),
* ll(lv1),ll(le1),ll(lp1),ll(lq1),ep)
do i=1,n
x(ll(i))=aa(ns+i)
enddo
c print 4,'x =',(x(i),i=1,n)
4 format(A/(5E15.7))
return
end
subroutine newg
common/factorc/m1,m2,mp,mq,lastr,irow
mq=-1
return
end
c******** The following routines are internal to schurQR.f **************
subroutine check_L(n,d,p,ifail)
implicit double precision (a-h,r-z), integer (i-q)
dimension d(*),p(*)
common/noutc/nout
common/factorc/m1,nu,mp,mq,lastr,irow
common/epsc/eps,tol,emin
write(nout,*)'check_L'
ifail=1
dmin=1.D37
do k=nu+1,n
dmin=min(dmin,abs(d(k)))
c if(abs(d(k)).le.tol)return
enddo
write(nout,*)'dmin =',dmin
c len=0
c do i=1,n
c len=len+p(i)
c enddo
c write(nout,*)m1*(m1+1)/2,len+m1
c write(nout,*)'m1 =',m1,' file length =',len,' total =',len+m1
ifail=0
return
end
subroutine aqsol(n,a,la,q,Q_,R,EB,z,t,u,x,d,ws,
* lr,lc,li,lv,le,pp,qq)
implicit double precision (a-h,r-z), integer (i-q)
double precision Q_
dimension a(*),la(*),Q_(*),R(*),EB(*),z(*),t(*),u(*),x(*),
* d(*),ws(*),lr(*),lc(*),li(*),lv(*),le(*),pp(*),qq(*)
common/factorc/m1,m2,mp,mq,lastr,irow
common/refactorc/mc,mxmc
common/pqc/pc,qr,lmp
c print *,'aqsol q =',q
if(q.gt.0)then
c print *,'q,n,li(q),m2',q,n,li(q),m2
if(q.le.n.and.li(q).le.m2.or.q.gt.n.and.li(q).gt.0)then
c q is in the starting active set (and hence in row qr of E)
do qr=1,mc
if(q.eq.le(qr))goto10
enddo
print *,'malfunction: q not in E'
stop
10 continue
else
c q is a new column
qr=0
endif
c form t=Bk^{-1}.aq, else form t=Bk^{-1}.t
c scatter a_q into t
liq=li(q)
do i=1,n
t(i)=0.D0
enddo
if(q.le.n)then
t(liq)=1.D0
else
call iscatter(a,la,q-n,li,t,n)
endif
endif
c print 4,'t=',(t(i),i=1,n)
if(mc.gt.0)then
c form u=E.B^{-1}.t and possibly z=-u
if(q.eq.0.or.q.gt.n.and.qr.eq.0)then
i1=1
do i=1,mc
c print 4,'EB =',(EB(j),j=i1,i1+m1-1)
u(i)=scpr(0.D0,EB(i1),t(m2+1),m1)
if(le(i).le.n)u(i)=u(i)+t(li(le(i)))
z(i)=-u(i)
i1=i1+m1
enddo
elseif(qr.gt.0)then
do i=1,mc
u(i)=0.D0
enddo
u(qr)=1.D0
else
c print 4,'EB =',(EB(j),j=1,m1*mc)
liq=liq-m2
do i=1,mc
u(i)=EB(liq)
z(i)=-u(i)
liq=liq+m1
enddo
endif
c print 4,'u=',(u(i),i=1,mc)
c form x=C^{-1}.u
call Qtprod(mc,mxmc,Q_,u,x)
mm=mc*(3-mc)/2+(mc-1)*mxmc
call rsol(mc,mm,mxmc,R,x)
c print 4,'x=',(x(i),i=1,mc)
c accumulate t=t+V.x
do i=1,mc
lvi=lv(i)
if(lvi.le.n)then
t(li(lvi))=t(li(lvi))+x(i)
else
call isaipy(x(i),a,la,lvi-n,t,n,lr,li)
endif
enddo
endif
c print 4,'t in natural order =',(t(li(i)),i=1,n)
c finally t:=B^{-1}.t-E'.x
call aqsol0(n,a,la,0,t,u,d,ws,lr,lc,li,pp,qq)
do i=1,mc
lei=li(le(i))
t(lei)=t(lei)-x(i)
enddo
c print 4,'t in column order',(t(i),i=1,n)
mq=q
return
3 format(A/(15I5))
4 format(A/(5E15.7))
end
subroutine aqsol0(n,a,la,q,tn,xn,d,ws,lr,lc,li,pp,qq)
implicit double precision (a-h,r-z), integer (i-q)
dimension a(*),la(*),tn(*),xn(*),d(*),ws(*),
* lr(*),lc(*),li(*),pp(*),qq(*)
common/factorc/m1,m2,mp,mq,lastr,irow
if(q.gt.0)then
do i=1,n
tn(i)=0.D0
enddo
if(q.le.n)then
tn(li(q))=1.D0
else
call iscatter(a,la,q-n,li,tn,n)
endif
endif
c print *,'tn =',(tn(i),i=1,n)
do i=n,m2+1,-1
ir=lr(i)
pri=pp(ir)
if(pri.eq.0)then
xn(i)=tn(i)/d(i)
else
xn(i)=(scpr(tn(i),ws(qq(ir)+1),tn(i-pri),pri))/d(i)
endif
call isaipy(-xn(i),a,la,lc(i)-n,tn,n,lr,li)
enddo
do i=m2+1,n
tn(i)=xn(i)
enddo
c print *,'tn =',(tn(i),i=1,n)
return
end
subroutine eptsol(n,a,la,p,b,Q_,R,EB,y,s,u,x,d,ws,
* lr,lc,li,lv,le,pp,qq,ep)
implicit double precision (a-h,r-z), integer (i-q)
double precision Q_
dimension a(*),la(*),b(*),Q_(*),R(*),EB(*),y(*),s(*),u(*),x(*),
* d(*),ws(*),lr(*),lc(*),li(*),lv(*),le(*),pp(*),qq(*)
common/epsc/eps,tol,emin
common/factorc/m1,m2,mp,mq,lastr,irow
common/refactorc/mc,mxmc
common/pqc/pc,qr,lmp
c print *,'eptsol p =',p
c column ordering is that defined by Bk = B + (V-B.E').E
c row order is same as for B
if(p.eq.0)then
c print 3,'lr =',(lr(i),i=1,m2)
c print 3,'lc =',(lc(i),i=m2+1,n)
c print 3,'le =',(le(i),i=1,mc)
c print 3,'lv =',(lv(i),i=1,mc)
do i=1,mc
x(i)=b(le(i))
b(le(i))=0.D0
enddo
call eptsol0(n,a,la,0,b,s,d,ws,lr,lc,li,pp,qq)
do i=1,mc
b(le(i))=x(i)
if(lv(i).le.n)then
x(i)=s(li(lv(i)))-b(lv(i))
else
x(i)=aiscpri(n,a,la,lv(i)-n,s,-b(lv(i)),lr,li)
endif
enddo
else
if(p.le.n.and.li(p).le.m2.or.p.gt.n.and.li(p).gt.0)then
c p is in the starting active set (set pc=0)
pc=0
lmp=li(p)
else
c p is in V (pc indicates where p is in V)
do pc=1,mc
if(p.eq.lv(pc))goto10
enddo
print 1,'p,pc,li(p),m1,m2 =',p,pc,li(p),m1,m2
print 3,'le =',(le(i),i=1,mc)
print 3,'lv =',(lv(i),i=1,mc)
print *,'malfunction: p not in V'
stop
10 continue
lmp=li(le(pc))
endif
c get s=Bk^{-T}.ep
if(pc.eq.0)then
call eptsol0(n,a,la,p,a,s,d,ws,lr,lc,li,pp,qq)
c print 4,'s0 ordered by lr',(s(i),i=1,n)
c print 4,'s0 in natural order',(s(li(i)),i=1,n)
m1mc=m1*mc
do i=1,m1
EB(m1mc+i)=s(m2+i)
enddo
c print 1,'eptsol: p =',p
c print 4,'EB is',(s(i),i=m2+1,n)
c print 4,'EB is',(EB(i),i=1,m1mc+m1)
c form s=B^{-T}.ep and then y=-V'.s
do i=1,mc
if(lv(i).le.n)then
x(i)=s(li(lv(i)))
else
x(i)=aiscpri(n,a,la,lv(i)-n,s,0.D0,lr,li)
endif
y(i)=-x(i)
enddo
else
c this is pc>0: set s=0 and x=-e_pc
do i=1,n
s(i)=0.D0
enddo
do i=1,mc
x(i)=0.D0
enddo
x(pc)=-1.D0
endif
endif
c print 4,'x =',(x(i),i=1,mc)
if(mc.gt.0)then
c form u=C^{-T}.x and accumulate EB'.u into s
call rtsol(mc,mm,mxmc,R,x)
call Qprod(mc,mxmc,Q_,x,u)
i1=1
do i=1,mc
call mysaxpy(u(i),EB(i1),s(m2+1),m1)
if(le(i).le.n)s(li(le(i)))=u(i)
i1=i1+m1
enddo
endif
c print 4,'sk in natural order=',(s(li(i)),i=1,n)
c print 4,'s =',(s(i),i=1,n)
mp=p
if(p.gt.0)ep=xlen(0.D0,s,n)
return
1 format(A,15I5)
2 format(A,6E15.7)
3 format(A/(15I5))
4 format(A/(5E15.7))
end
subroutine eptsol0(n,a,la,p,b,sn,d,ws,lr,lc,li,pp,qq)
implicit double precision (a-h,r-z), integer (i-q)
dimension a(*),la(*),b(*),sn(*),d(*),ws(*),
* lr(*),lc(*),li(*),pp(*),qq(*)
common/epsc/eps,tol,emin
common/factorc/m1,m2,mp,mq,lastr,irow
if(p.eq.0)then
do i=1,m2
sn(i)=b(lr(i))
enddo
do i=m2+1,n
sn(i)=0.D0
enddo
do i=m2+1,n
j=lc(i)
sn(i)=-aiscpri(n,a,la,j-n,sn,-b(j),lr,li)/d(i)
ir=lr(i)
pri=pp(ir)
if(pri.gt.0)call mysaxpy(sn(i),ws(qq(ir)+1),sn(i-pri),pri)
enddo
else
do i=1,n
sn(i)=0.D0
enddo
pr=li(p)
if(p.le.n)then
if(pr.gt.m2)goto1
sn(pr)=1.D0
do i=m2+1,n
sn(i)=-aiscpri(n,a,la,lc(i)-n,sn,0.D0,lr,li)/d(i)
ir=lr(i)
pri=pp(ir)
if(pri.gt.0)call mysaxpy(sn(i),ws(qq(ir)+1),sn(i-pri),pri)
enddo
else
if(pr.le.m2)goto1
do i=m2+1,n
bi=0.D0
if(i.eq.pr)bi=-1.D0
sn(i)=-aiscpri(n,a,la,lc(i)-n,sn,bi,lr,li)/d(i)
ir=lr(i)
pri=pp(ir)
if(pri.gt.0)call mysaxpy(sn(i),ws(qq(ir)+1),sn(i-pri),pri)
enddo
endif
endif
c print *,'sn =',(sn(i),i=1,n)
return
1 continue
print *,'malfunction detected in eptsol0: p =',p
stop
end
subroutine order(n,nu,nc,la,lr,ls,li,p,q,r,ws,mxws,ifail)
implicit integer (c-t)
double precision ws
dimension la(0:*),lr(*),ls(*),li(*),p(*),q(*),r(*),ws(*)
common/noutc/nout
c character star(1000,80)
c write(nout,*)'order'
c spk1 ordering on full matrix
ifail=0
if(nu.eq.n)return
c set row and column counts and row-wise data structure
nn=n-nu
ii=mxws/nn
do j=1,nn
rowj=lr(j)
p(rowj)=(j-1)*ii
r(rowj)=0
enddo
do j=nn+1,n
r(lr(j))=0
enddo
1 continue
do i=nu+1,nc
coli=abs(ls(i))
li(coli)=0
jp=la(0)+coli-n
do j=la(jp),la(jp+1)-1
rowj=la(j)
if(li(rowj).le.nn)then
li(coli)=li(coli)+1
r(rowj)=r(rowj)+1
ij=p(rowj)+r(rowj)
if(ij.gt.mxws)then
ij=mxws
ifail=1
endif
ws(ij)=dble(coli)
endif
enddo
enddo
c check for no overlaps
qrj=0
do j=1,nn
rowj=lr(j)
if(p(rowj).lt.qrj)ifail=1
qrj=p(rowj)+r(rowj)
q(rowj)=qrj
p(rowj)=p(rowj)+1
enddo
if(ifail.eq.1.or.qrj.gt.mxws)then
qrj=0
do j=1,nn
rowj=lr(j)
p(rowj)=qrj
qrj=qrj+r(rowj)
r(rowj)=0
enddo
if(qrj.gt.mxws)then
write(nout,*)'not enough space for ws in order: mxws =',mxws
ifail=7
return
endif
ifail=0
goto1
endif
ifirstc=nu+1
ifirstr=1
2 continue
c move zero-column-count columns to lhs and find minimum column count
mcc=n
do i=ifirstc,nc
coli=abs(ls(i))
if(li(coli).eq.0)then
call iexch(ls(i),ls(ifirstc))
li(coli)=ifirstr-1
ifirstc=ifirstc+1
else
mcc=min(mcc,li(coli))
endif
enddo
c write(nout,*)'ifirstc,ifirstr,mcc',ifirstc,ifirstr,mcc
c write(nout,*)'lr =',(lr(j),j=1,n)
c write(nout,*)'ls =',(ls(i),i=nu+1,nc)
c write(nout,*)'row counts =',(r(lr(j)),j=1,n)
c write(nout,*)'column counts =',(li(abs(ls(i))),i=nu+1,nc)
if(ifirstc.gt.nc)goto4
c apply tie-break rule
tie=0
do i=ifirstc,nc
coli=abs(ls(i))
if(li(coli).eq.mcc)then
ti=0
jp=la(0)+coli-n
do j=la(jp),la(jp+1)-1
rowj=la(j)
if(li(rowj).ge.ifirstr)ti=ti+r(rowj)
enddo
if(ti.gt.tie)then
tie=ti
mccc=coli
endif
endif
enddo
c write(nout,*)'tie,mccc',tie,mccc
c permute rows of m-c-c column to top and update column counts
jp=la(0)+mccc-n
do j=la(jp),la(jp+1)-1
rowj=la(j)
jr=li(rowj)
if(jr.lt.ifirstr)goto3
if(jr.gt.nn)goto3
lr(jr)=lr(ifirstr)
li(lr(jr))=jr
lr(ifirstr)=rowj
li(rowj)=ifirstr
ifirstr=ifirstr+1
do i=p(rowj),q(rowj)
coli=int(ws(i))
li(coli)=li(coli)-1
enddo
3 continue
enddo
goto2
4 continue
c print star diagram
c if(nc-nu.gt.80.or.n.gt.1000)stop
c write(nout,*)'spk1 ordering'
c ij=li(abs(ls(nc)))
c do i=1,ij
c do j=1,nc-nu
c star(i,j)=' '