-
Notifications
You must be signed in to change notification settings - Fork 1
/
lmbmprogram.f95
executable file
·3069 lines (2460 loc) · 77.9 KB
/
lmbmprogram.f95
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
MODULE r_precision ! Precision for reals.
IMPLICIT NONE
INTEGER, PARAMETER, PUBLIC :: prec = SELECTED_REAL_KIND(12)
! INTEGER, PARAMETER, PUBLIC :: prec = SELECTED_REAL_KIND(6) ! do not work very well
END MODULE r_precision
MODULE param ! Parameters
USE r_precision, ONLY : prec ! Precision for reals.
IMPLICIT NONE
!Parameters
INTEGER, PARAMETER, PUBLIC :: maxeps = 20, maxnrs = 2000
REAL(KIND=prec), PARAMETER, PUBLIC :: &
zero = 0.0_prec, & !
half = 0.5_prec, & !
one = 1.0_prec, & !
large = 3.40282347*10.**38, & !
small = 1.17549435*10.**(-38) !
END MODULE param
MODULE initializat ! Initialization of parameters.
USE r_precision, ONLY : prec ! Precision for reals.
USE param, ONLY : large,small
IMPLICIT NONE
! Parameters
INTEGER, PARAMETER :: &
na = 2, & ! Size of the bundle na >= 2.
mcu = 15, & ! Upper limit for maximum number of stored corrections, mcu >= 3.
mcinit = 7 ! Initial maximum number of stored corrections, mcu >= mcinit >= 3. If mcinit <= 0, the default value mcinit = 3 will be used. However, the value mcinit = 7 is recommented.
! Real parameters (if parameter value <= 0.0 the default value of the parameter will be used).
REAL(KIND=prec), SAVE :: &
tolf = 1.0E-4_prec, & ! Tolerance for change of function values (default = 1.0E-8), pie 1.0E-7 reg, OLLUT 1.0e-4
tolf2 = -1.0_prec, & ! Second tolerance for change of function values. If tolf2 < 0 the the parameter and the corresponding termination criterion will be ignored. If tolf2 = 0 the default value 1.0E+4 will be used.
tolb = -large + small, & ! Tolerance for the function value (default = -large).
tolg = 1.0E-4_prec, & ! Tolerance for the first termination criterion (default = 1.0E-6)
tolg2 = 1.0E-4_prec, & ! Tolerance for the second termination criterion (default = tolg)
eta = 0.5_prec, & ! Distance measure parameter, eta >= 0. If eta < 0 the default value 0.5 will be used.
epsl = 0.24_prec, & ! Line search parameter, 0 < epsl < 0.25 (default = 1.0E-4).
xmax = 1000_prec ! Maximum stepsize, 1 < XMAX (default = 1.5).
! Integer parameters (if parameter value <= 0.0 the default value of the parameter will be used).
INTEGER, SAVE :: &
mit = 10000, & ! Maximun number of iterations (default = 10000).
mfe = 500000000, & ! Maximun number of function evaluations (default = n*mit).
mtesf = 10, & ! Maximum number of iterations with changes of function values smaller than tolf (default = 10).
iscale = 0 ! Selection of the scaling: 0 - Scaling at every iteration with STU/UTU (default). 1 - Scaling at every iteration with STS/STU. 2 - Interval scaling with STU/UTU. 3 - Interval scaling with STS/STU. 4 - Preliminary scaling with STU/UTU. 5 - Preliminary scaling with STS/STU. 6 - No scaling.
END MODULE initializat
MODULE lmbm_sub ! Subprograms for LMBM
USE r_precision, ONLY : prec ! Precision for reals.
IMPLICIT NONE
CONTAINS
FUNCTION vdot(n,x,y) RESULT(xty) ! Dot product of two vectors.
USE param, ONLY : zero
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vectors.
! Scalar Arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
REAL(KIND=prec) xty
INTEGER :: i
xty = zero
DO i = 1,n
xty = xty + x(i-1)*y(i-1)
END DO
END FUNCTION vdot
SUBROUTINE vneg(n,x,y) ! Change the signs of vector elements.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y ! Output vector y:= -x.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
!PM O(n)=n
DO i = 1,n
y(i-1) = -x(i-1)
END DO
END SUBROUTINE vneg
SUBROUTINE scalex(n,a,x,y) ! Scaling a vector y:= a*x.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y ! Output vector y:= a*x.
! Scalar arguments
REAL(KIND=prec), INTENT(IN) :: &
a ! Scaling parameter.
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
y(i-1) = a*x(i-1)
END DO
END SUBROUTINE scalex
SUBROUTINE xdiffy(n,x,y,z) ! Difference of two vectors z:= x - y.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
z ! Output vector z:= x - y.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
z(i-1) = x(i-1) - y(i-1)
END DO
END SUBROUTINE xdiffy
SUBROUTINE xsumy(n,x,y,z) ! Sum of two vectors z:= x + y.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vectors.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
z ! Output vector z:= x + y.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
z(i-1) = x(i-1) + y(i-1)
END DO
END SUBROUTINE xsumy
SUBROUTINE scdiff(n,a,x,y,z) ! Difference of the scaled vector and a vector z:= a*x - y.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
z ! Output vector z:= a*x - y.
! Scalar arguments
REAL(KIND=prec), INTENT(IN) :: &
a ! Scaling factor.
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
z(i-1) = a*x(i-1) - y(i-1)
END DO
END SUBROUTINE scdiff
SUBROUTINE scsum(n,a,x,y,z) ! Sum of a vector and the scaled vector z:= y + a*x.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vectors.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
z ! Output vector z:= a*x + y.
! Scalar arguments
REAL(KIND=prec), INTENT(IN) :: &
a ! Scaling factor.
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
z(i-1) = a*x(i-1) + y(i-1)
END DO
END SUBROUTINE scsum
SUBROUTINE copy(n,x,y) ! Copying a vector y:= x.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y ! Output vector.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
y(i-1) = x(i-1)
END DO
END SUBROUTINE copy
SUBROUTINE copy2(n,x,y,z,v) ! Copying of two vectors: y:=x, v:=z.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,z ! Input vector.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y,v ! Output vectors.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
y(i-1) = x(i-1)
v(i-1) = z(i-1)
END DO
END SUBROUTINE copy2
SUBROUTINE vxdiag(n,d,x,y) ! Vector is multiplied by a diagonal matrix y:=d*x.
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x, & ! Input vector.
d ! Diagonal matrix stored as a vector with n elements.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y ! Output vector y:= d*x.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n ! Vectors dimension.
! Local scalars
INTEGER :: i
DO i = 1,n
y(i-1) = x(i-1)*d(i-1)
END DO
END SUBROUTINE vxdiag
SUBROUTINE symax(n,m,iold,a,x,y) ! Multiplication of a dense symmetric matrix A by a vector x.
USE param, ONLY : zero
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(IN) :: &
x ! Input vector stored in a circular order.
REAL(KIND=prec), DIMENSION(0:(n*(n+1)/2)-1), INTENT(IN) :: &
a ! Dense symmetric matrix stored in the packed form: a(n*(n+1)/2).
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(OUT) :: &
y ! Output vector y:= a*x. Vector y has the same circular order than x.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Order of matrix A.
m, & ! Length of vector x, m >= n, note that only n components from vector x are used.
iold ! Index, which controlls the circular order of the vector x.
! Local scalars
INTEGER :: i,j,k,l
DO j=1,n
l=j+iold-1
IF (l > m) l=l-m
y(l-1) = zero
k=l
DO i=j,n
y(l-1) = a((i-1)*i/2+j-1)*x(k-1)+y(l-1)
k=k+1
IF (k > m) k=k-m
END DO
END DO
DO j=2,n
l=j+iold-1
IF (l > m) l=l-m
k=iold
DO i=1,j-1
IF (k > m) k=k-m
y(l-1) = a((j-1)*j/2+i-1)*x(k-1)+y(l-1)
k=k+1
END DO
END DO
END SUBROUTINE symax
SUBROUTINE cwmaxv(n,m,a,x,y) ! Multiplication of a columnwise stored dense rectangular matrix A by a vector x.
USE param, ONLY : zero
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n*m-1), INTENT(IN) :: &
a ! Rectangular matrix stored columnwise in the one-dimensional array (dimension n*m).
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(IN) :: &
x ! Input vector (dimension m).
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
y ! Output vector equal to s*a*x. If m = 0 y is a zero vector.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Number of rows of the matrix A.
m ! Number of columns of the matrix A.
! Local scalars
INTEGER :: i,j,k
DO i = 1,n
y(i-1) = zero
END DO
k = 1
DO j = 1,m
CALL scsum(n,x(j-1),a(k-1:),y,y)
k = k + n
END DO
END SUBROUTINE cwmaxv
SUBROUTINE rwaxv2(n,m,a,b,x,y,v,w) ! Multiplication of two rowwise stored dense rectangular matrices A and B by vectors x and y.
USE param, ONLY : zero
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x,y ! Input vectors (dimension n).
REAL(KIND=prec), DIMENSION(0:n*m-1), INTENT(IN) :: &
a,b ! Rectangular matrices stored rowwise in the one-dimensional array (dimension n*m).
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(OUT) :: &
v,w ! Output vectors v=a*x and w=b*y.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Number of columns of the matrices A and B.
m ! Number of rows of the matrices A and B.
! Local scalars
REAL(KIND=prec) :: tmp1,tmp2
INTEGER :: i,j,k
k = 0
DO i = 1,m
tmp1 = zero
tmp2 = zero
DO j = 1,n
tmp1 = tmp1 + a(k+j-1)*x(j-1)
tmp2 = tmp2 + b(k+j-1)*y(j-1)
END DO
v(i-1) = tmp1
w(i-1) = tmp2
k = k + n
END DO
END SUBROUTINE rwaxv2
SUBROUTINE trlieq(n,m,iold,u,x,y,job,ierr) ! Solving x from linear equation u*x=y or u'*x=y, where u is an upper triangular matrix.
USE param, ONLY : small
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(IN) :: &
y ! Input vector stored in a circular order.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
u ! Triangular matrix.
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(OUT) :: &
x ! Output vector y:= a*x. Vector y has the same circular order than x. Note that x may be equal to y in calling sequence.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Order of matrix U.
m, & ! Length of vectors x and y, m >= n, note that only n components from vectors are used.
iold, & ! Index, which controlls the circular order of the vectors x and y.
job ! Option: 0 - x:=(u')**(-1)*y, u upper triangular. 1 - x:=u**(-1)*y, u upper triangular.
INTEGER, INTENT(OUT) :: &
ierr ! Error indicador: 0 - Everything is ok. -3 - Error; 0 at diagonal.
! Local scalars
INTEGER :: i,ii,ij,j,k,l,ji
! Intrinsic functions
INTRINSIC ABS
ierr = -3
DO i=1,m
x(i-1)=y(i-1)
END DO
IF (job == 0) THEN
! x=u'**(-1)*y, u' = [u1 ] is lower triangular.
! [u2 u3 ]
! [u4 u5 u6 ]
! [. . . . ]
ii = 0
DO i = 1,n
ii=ii+i
l=i+iold-1
IF (l > m) l=l-m
IF (ABS(u(ii-1)) <= small) RETURN
x(l-1) = x(l-1)/u(ii-1)
DO j = i+1,n
ji = (j-1)*j/2+i
k=j+iold-1
IF (k > m) k=k-m
x(k-1) = x(k-1) - u(ji-1)*x(l-1)
END DO
END DO
ELSE IF (job == 1) THEN
! x=u**(-1)*y, u = [u1 u2 u4 . ] is upper triangular.
! [ u3 u5 . ]
! [ u6 . ]
! [ . ]
ii = n* (n+1)/2
DO i = n,1,-1
l=i+iold-1
IF (l > m) l=l-m
IF (ABS(u(ii-1)) <= small) RETURN
ij = ii
DO j = i + 1,n
k=j+iold-1
IF (k > m) k=k-m
ij = ij + j - 1
x(l-1) = x(l-1) - u(ij-1)*x(k-1)
END DO
x(l-1)=x(l-1)/u(ii-1)
ii = ii - i
END DO
ELSE
RETURN
END IF
ierr = 0
END SUBROUTINE trlieq
SUBROUTINE lineq(n,m,iold,a,x,y,ierr) ! Solving x from linear equation A*x=y. Positive definite matrix A+E is given using the factorization A+E=L*D*L' obtained by the subroutine mxdpgf.
USE param, ONLY : small
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(IN) :: &
y ! Input vector stored in a circular order (dimension m).
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
a ! Factorization a+e=l*d*l' obtained by the subroutine mxdpgf.
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(OUT) :: &
x ! Output vector y:= a*x. Vector x has the same circular order than y. Note that x may be equal to y in calling sequence.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Order of matrix a.
m, & ! Length of vectors x and y, m >= n, note that only n components from vectors are used.
iold ! Index, which controlls the circular order of the vectors x and y.
INTEGER, INTENT(OUT) :: &
ierr ! Error indicador: 0 - Everything is ok. -2 - Error; indefinite matrix.
! Local scalars
INTEGER :: i,ii,ij,j,k,l
ierr = -2
! Phase 1: x=l**(-1)*x
ij = 0
DO i = 1,n
l=i+iold-1
IF (l > m) l=l-m
x(l-1) = y(l-1)
DO j = 1,i - 1
ij = ij + 1
k=j+iold-1
IF (k > m) k=k-m
x(l-1) = x(l-1) - a(ij-1)*x(k-1)
END DO
ij = ij + 1
END DO
! Phase 2: x:=d**(-1)*x
ii = 0
DO i = 1,n
ii = ii + i
IF (a(ii-1) <= small) RETURN
l=i+iold-1
IF (l > m) l=l-m
x(l-1) = x(l-1)/a(ii-1)
END DO
! Phase 3: x:=trans(l)**(-1)*x
ii = n* (n-1)/2
DO i = n - 1,1,-1
ij = ii
l=i+iold-1
IF (l > m) l=l-m
DO j = i + 1,n
k=j+iold-1
IF (k > m) k=k-m
ij = ij + j - 1
x(l-1) = x(l-1) - a(ij-1)*x(k-1)
END DO
ii = ii - i
END DO
ierr = 0
END SUBROUTINE lineq
SUBROUTINE mxdpgf(n,a,inf,alf,tau) ! Factorization A+E=L*D*trans(L) of a dense symmetric positive definite matrix A+E, where D and E are diagonal positive definite matrices and L is a lower triangular matrix. If A is sufficiently positive definite then E=0.
USE param, ONLY : zero,one
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(INOUT) :: &
a ! On input: Dense symmetric matrix stored in the packed form. On output: factorization A+E=L*D*trans(L).
! Scalar arguments
REAL(KIND=prec), INTENT(INOUT) :: &
alf ! On input a desired tolerance for positive definiteness. On output the most negative diagonal element used in the factorization process (if inf>0).
REAL(KIND=prec), INTENT(OUT) :: &
tau ! Maximum diagonal element of matrix E.
INTEGER, INTENT(IN) :: &
n ! Order of matrix a.
INTEGER, INTENT(OUT) :: &
inf ! An information obtained in the factorization process: inf=0 - A is sufficiently positive definite and E=0. inf<0 - A is not sufficiently positive definite and E>0. inf>0 - A is indefinite and inf is an index of the most negative diagonal element used in the factorization process.
! Local scalars
REAL(KIND=prec) :: bet,del,gam,rho,sig,tol
INTEGER :: i,ij,ik,j,k,kj,kk,l
! Intrinsic functions
INTRINSIC ABS,MAX
l = 0
inf = 0
tol = alf
! Estimation of the matrix norm
alf = zero
bet = zero
gam = zero
tau = zero
kk = 0
DO k = 1,n
kk = kk + k
bet = MAX(bet,ABS(a(kk-1)))
kj = kk
DO j = k + 1,n
kj = kj + j - 1
gam = MAX(gam,ABS(a(kj-1)))
END DO
END DO
bet = MAX(tol,bet,gam/n)
del = tol*MAX(bet,one)
kk = 0
DO k = 1,n
kk = kk + k
! Determination of a diagonal correction
sig = a(kk-1)
IF (alf > sig) THEN
alf = sig
l = k
END IF
gam = zero
kj = kk
DO j = k + 1,n
kj = kj + j - 1
gam = MAX(gam,ABS(a(kj-1)))
END DO
gam = gam*gam
rho = MAX(ABS(sig),gam/bet,del)
IF (tau < rho-sig) THEN
tau = rho - sig
inf = -1
END IF
! Gaussian elimination
a(kk-1) = rho
kj = kk
DO j = k + 1,n
kj = kj + j - 1
gam = a(kj-1)
a(kj-1) = gam/rho
ik = kk
ij = kj
DO i = k + 1,j
ik = ik + i - 1
ij = ij + 1
a(ij-1) = a(ij-1) - a(ik-1)*gam
END DO
END DO
END DO
IF (l > 0 .AND. ABS(alf) > del) inf = l
END SUBROUTINE mxdpgf
SUBROUTINE calq(n,m,iold,a,x,y) ! Solving x from linear equation A*x=y.
USE param, ONLY : small
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(IN) :: &
y ! Input vector stored in a circular order (dimension m).
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(INOUT) :: &
a ! On input: Dense symmetric matrix stored in the packed form. On output: factorization A+E=L*D*trans(L).
REAL(KIND=prec), DIMENSION(0:m-1), INTENT(OUT) :: &
x ! Output vector y:= a*x. Vector x has the same circular order than y. Note that x may be equal to y in calling sequence.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Order of matrix a.
m, & ! Length of vectors x and y, m >= n, note that only n components from vectors are used.
iold ! Index, which controlls the circular order of the vectors x and y.
! INTEGER, INTENT(OUT) :: &
! ierr ! Error indicador: 0 - Everything is ok. -2 - Error; indefinite matrix.
! Local scalars
REAL(KIND=prec) :: eta,bet
INTEGER :: inf,ierr
eta = small+small
CALL mxdpgf(n,a,inf,eta,bet)
CALL lineq(n,m,iold,a,x,y,ierr)
END SUBROUTINE calq
END MODULE lmbm_sub
MODULE lmbm_mod ! Limited memory bundle method
USE r_precision, ONLY : prec ! Precision for reals.
IMPLICIT NONE
CONTAINS
!***********************************************************************
!* *
!* * SUBROUTINE init_lmbm * *
!* *
!* Initialization for limited memory bundle subroutine for *
!* large-scale unconstrained nonsmooth optimization. *
!* *
!***********************************************************************
SUBROUTINE init_lmbm(n,mc,iterm)
USE param, ONLY : zero,half,small,large
USE initializat, ONLY: na,mcu,iscale,tolf,tolf2,tolb,tolg,tolg2,xmax,eta,epsl,mtesf,mit,mfe
IMPLICIT NONE
! Scalar arguments
INTEGER, INTENT(IN) :: n ! Number of the variables.
INTEGER, INTENT(INOUT) :: mc ! Initial maximum number of stored corrections.
INTEGER, INTENT(OUT) :: iterm ! Cause of termination: 0 - Everything is ok. -5 - Invalid input parameters.
! Initialization and error checking
! PM muokkaus
iterm = 0
IF (n <= 0) THEN
iterm = -5
RETURN
END IF
IF (na < 2) THEN
iterm = -5
RETURN
END IF
IF (epsl >= 0.25_prec) THEN
iterm = -5
RETURN
END IF
IF (mcu <= 3) THEN
iterm = -5
RETURN
END IF
IF (mc > mcu) THEN
mc = mcu
END IF
! Default values
IF (mc <= 0) mc = 3 ! Initial maximum number of corrections.
IF (mit <= 0) mit = 10000 ! Maximum number of iterations.
IF (mfe <= 0) mfe = n*mit ! Maximum number of function evalutions.
IF (tolf <= zero) tolf = 1.0E-08_prec ! Tolerance for change of function values.
IF (tolf2 == zero) tolf2 = 1.0E+04_prec ! Second tolerance for change of function values.
IF (tolb == zero) tolb = -large + small ! Tolerance for the function value.
IF (tolg <= zero) tolg = 1.0E-06_prec ! Tolerance for the first termination criterion.
IF (tolg2 <= zero) tolg2 = tolg ! Tolerance for the second termination criterion.
IF (xmax <= zero) xmax = 1.5_prec ! Maximum stepsize.
IF (eta < zero) eta = half ! Distance measure parameter.
IF (epsl <= zero) epsl = 1.0E-04_prec ! Line search parameter.
IF (mtesf <= 0) mtesf = 10 ! Maximum number of iterations with changes of function values smaller than tolf.
IF (iscale > 6 .OR. iscale < 0) iscale = 0 ! Selection of the scaling.
END SUBROUTINE init_lmbm
SUBROUTINE restar(n,mc,mcc,mcinit,inew,ibun,ibfgs,iters,gp,g,nnk,alfv,alfn,gamma,d,ic,icn,mal,ncres,iflag) ! Initialization and reinitialization.
USE param, ONLY : zero,one
USE lmbm_sub, ONLY : copy,vneg
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
gp ! Basic subgradient of the objective function.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(INOUT) :: &
g ! Current (auxiliary) subgradient of the objective function.
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(OUT) :: &
d ! Search direction.
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Number of variables.
mcinit ! Initial maximum number of stored corrections.
INTEGER, INTENT(OUT) :: &
mc, & ! Current maximum number of stored corrections.
mcc, & ! Current number of stored corrections.
inew, & ! Index for the circular arrays.
ibun, & ! Index for the circular arrays in bundle updating.
ibfgs, & ! Index of the type of BFGS update.
nnk, & ! Consecutive null steps counter.
ic, & ! Correction indicator.
icn, & ! Correction indicator for null steps.
mal, & ! Current size of the bundle.
iflag ! Index for adaptive version.
INTEGER, INTENT(INOUT) :: &
iters, & ! Null step indicator. 0 - Null step. 1 - Serious step.
ncres ! Number of restarts.
REAL(KIND=prec), INTENT(OUT) :: &
alfn, & ! Locality measure.
alfv, & ! Aggregate locality measure.
gamma ! Scaling parameter.
! Restart
mc = mcinit
mcc = 0
inew = 1
ibun = 1
ibfgs = 0
ic = 0
icn = 0
mal = 0
ncres = ncres + 1
iflag = 0
IF (iters == 0) THEN
CALL copy(n,gp,g)
iters = 1
nnk = 0
alfv=zero
alfn=zero
END IF
gamma = one
CALL vneg(n,g,d)
END SUBROUTINE restar
SUBROUTINE dobun(n,ma,mal,x,g,fu,ay,ag,af,iters,ibun) ! Bundle construction.
USE lmbm_sub, ONLY : copy2
IMPLICIT NONE
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
g, & ! Subgradient of the objective function.
x ! Vector of variables
REAL(KIND=prec), DIMENSION(0:(n*ma)-1), INTENT(INOUT) :: &
ay, & ! Matrix whose columns are bundle points (stored in one-dimensional n*ma -array).
ag ! Matrix whose columns are bundle subgradients (stored in one-dimensional n*ma -array).
REAL(KIND=prec), DIMENSION(0:ma-1), INTENT(INOUT) :: &
af ! Vector of values of bundle functions (stored in one-dimensional ma -array).
! Scalar arguments
INTEGER, INTENT(IN) :: &
n, & ! Number of variables.
iters, & ! Null step indicator. 0 - Null step. 1 - Serious step.
ma ! Maximum size of the bundle.
INTEGER, INTENT(INOUT) :: &
ibun, & ! Index for the circular arrays in bundle updating.
mal ! Current size of the bundle.
REAL(KIND=prec), INTENT(IN) :: &
fu ! Value of the objective function.
! Local scalars
INTEGER :: i,j
IF (iters == 1) THEN
! Serious step
af(ibun-1) = fu
i = (ibun-1)*n+1
CALL copy2(n,g,ag(i-1:),x,ay(i-1:))
ELSE
! Null step
IF (mal < ma) THEN
af(ibun-1) = af(mal-1)
af(mal-1) = fu
i = mal*n + 1
CALL copy2(n,ag(i-n-1:),ag(i-1:),ay(i-n-1:),ay(i-1:))
CALL copy2(n,g,ag(i-n-1:),x,ay(i-n-1:))
ELSE
i = ibun-1
IF (i < 1) i = mal
af(ibun-1) = af(i-1)
af(i-1) = fu
i = (ibun-2)*n + 1
IF (i < 1) i = (mal-1)*n + 1
j = (ibun-1)*n + 1
CALL copy2(n,ag(i-1:),ag(j-1:),ay(i-1:),ay(j-1:))
CALL copy2(n,g,ag(i-1:),x,ay(i-1:))
END IF
END IF
mal = mal + 1
IF (mal > ma) mal = ma
ibun = ibun + 1
IF (ibun > ma) ibun = 1
END SUBROUTINE dobun
SUBROUTINE destep(n,ma,mal,x,af,ag,ay,ibun,d,fu,df,t,eta,iterm) ! Stepsize selection.
USE param, ONLY : zero,half,one,large
IMPLICIT NONE
! Scalar arguments
REAL(KIND=prec), INTENT(INOUT) :: &
t ! Initial stepsize
REAL(KIND=prec), INTENT(IN) :: &
df, & ! Directional derivative.
fu, & ! Value of the objective function.
eta ! Distance measure parameter.
INTEGER, INTENT(IN) :: &
n, & ! Number of variables
ma, & ! Maximum size of the bundle.
mal, & ! Current size of the bundle.
ibun ! Index for the circular arrays in bundle updating.
INTEGER, INTENT(OUT) :: &
iterm ! Cause of termination: 0 - Everything is ok. -6 - Error.
! Array arguments
REAL(KIND=prec), DIMENSION(0:n-1), INTENT(IN) :: &
x, & ! Vector of variables (n array).
d ! Direction vector (n array).
REAL(KIND=prec), DIMENSION(0:(n*ma)-1), INTENT(IN) :: &
ay, & ! Matrix whose columns are bundle points (stored in one-dimensional n*ma -array).
ag ! Matrix whose columns are bundle subgradients (stored in one-dimensional n*ma -array).
REAL(KIND=prec), DIMENSION(0:ma-1), INTENT(IN) :: &
af ! Vector of values of bundle functions (stored in one-dimensional ma -array).
! Local arrays
REAL(KIND=prec), DIMENSION(0:(2*ma)-1) :: &
tmparray ! Auxiliary array.
! Local scalars
REAL(KIND=prec) :: alf,alfl,alfr,bet,betl,betr,dx,q,r,w
INTEGER :: i,j,jn,k,l,lq,ib
! Intrinsic functions
INTRINSIC ABS,REAL,MAX,MIN,SQRT
iterm = 0
alfl = zero
betl = zero
w = df*t* (one - t*half)
! Initial choice of possibly active lines
k = 0
l = -1
jn = (ibun-1)*n
betr = - large
DO j=1,mal-1
ib = ibun - 1 + j
IF (ib > mal) ib = ib - mal
IF (jn >= mal*n) jn = jn - mal*n
r = zero
bet = zero
alfl = af(ib-1) - fu
DO i=1,n
dx = x(i-1) - ay(jn+i-1)
q = ag(jn+i-1)
r = r + dx*dx