-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLevenbergMarquart.cls
More file actions
1205 lines (1159 loc) · 36.3 KB
/
Copy pathLevenbergMarquart.cls
File metadata and controls
1205 lines (1159 loc) · 36.3 KB
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
'Minpack Copyright Notice (1999) University of Chicago. All rights reserved
'
'Redistribution and use in source and binary forms, with or
'without modification, are permitted provided that the
'following conditions are met:
'
'1. Redistributions of source code must retain the above
'copyright notice, this list of conditions and the following
'disclaimer.
'
'2. Redistributions in binary form must reproduce the above
'copyright notice, this list of conditions and the following
'disclaimer in the documentation and/or other materials
'provided with the distribution.
'
'3. The end-user documentation included with the
'redistribution, if any, must include the following
'acknowledgment:
'
' "This product includes software developed by the
' University of Chicago, as Operator of Argonne National
' Laboratory.
'
'Alternately, this acknowledgment may appear in the software
'itself, if and wherever such third-party acknowledgments
'normally appear.
'
'4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
'WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
'UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
'THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
'OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
'OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
'USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
'THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
'DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
'UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
'BE CORRECTED.
'
'5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
'HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
'ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
'INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
'ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
'PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
'SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
'(INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
'EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
'POSSIBILITY OF SUCH LOSS OR DAMAGES.
'|--------------------------------------------------------------------------|'
'|--------------------------------------------------------------------------|'
' Documentation for MINPACK subroutine LMDIF
'
' Double precision version
'
' Argonne National Laboratory
'
' Burton S. Garbow, Kenneth E. Hillstrom, Jorge J. More
'
' March 1980
'
'
' 1. Purpose.
'
' The purpose of LMDIF is to minimize the sum of the squares of M
' nonlinear functions in N variables by a modification of the
' Levenberg-Marquardt algorithm. The user must provide a subrou-
' tine which calculates the functions. The Jacobian is then cal-
' culated by a forward-difference approximation.
'
'
' 2. Subroutine and type statements.
'
' SUBROUTINE LMDIF(FCN,M,N,X,FVEC,FTOL,XTOL,GTOL,MAXFEV,EPSFCN,
' * DIAG,MODE,FACTOR,NPRINT,INFO,NFEV,FJAC,LDFJAC,
' * IPVT,QTF,WA1,WA2,WA3,WA4)
' INTEGER M,N,MAXFEV,MODE,NPRINT,INFO,NFEV,LDFJAC
' INTEGER IPVT(N)
' DOUBLE PRECISION FTOL,XTOL,GTOL,EPSFCN,FACTOR
' DOUBLE PRECISION X(N),FVEC(M),DIAG(N),FJAC(LDFJAC,N),QTF(N),
' * WA1(N),WA2(N),WA3(N),WA4(M)
' EXTERNAL fcn
'
'
' 3. Parameters.
'
' Parameters designated as input parameters must be specified on
' entry to LMDIF and are not changed on exit, while parameters
' designated as output parameters need not be specified on entry
' and are set to appropriate values on exit from LMDIF.
'
' FCN is the name of the user-supplied subroutine which calculates
' the functions. FCN must be declared in an EXTERNAL statement
' in the user calling program, and should be written as follows.
'
' SUBROUTINE fcn(m, n, x, fvec, iflag)
' INTEGER M,N,IFLAG
' DOUBLE PRECISION X(N),FVEC(M)
' ----------
' CALCULATE THE FUNCTIONS AT X AND
' RETURN THIS VECTOR IN FVEC.
' ----------
' Return
' End
'
'
' The value of IFLAG should not be changed by FCN unless the
' user wants to terminate execution of LMDIF. In this case set
' IFLAG to a negative integer.
'
' M is a positive integer input variable set to the number of
' functions.
'
' N is a positive integer input variable set to the number of
' variables. N must not exceed M.
'
' X is an array of length N. On input X must contain an initial
' estimate of the solution vector. On output X contains the
' final estimate of the solution vector.
'
' FVEC is an output array of length M which contains the functions
' evaluated at the output X.
'
' FTOL is a nonnegative input variable. Termination occurs when
' both the actual and predicted relative reductions in the sum
' of squares are at most FTOL. Therefore, FTOL measures the
' relative error desired in the sum of squares. Section 4 con-
' tains more details about FTOL.
'
' XTOL is a nonnegative input variable. Termination occurs when
' the relative error between two consecutive iterates is at most
' XTOL. Therefore, XTOL measures the relative error desired in
' the approximate solution. Section 4 contains more details
' about XTOL.
'
' GTOL is a nonnegative input variable. Termination occurs when
' the cosine of the angle between FVEC and any column of the
' Jacobian is at most GTOL in absolute value. Therefore, GTOL
' measures the orthogonality desired between the function vector
' and the columns of the Jacobian. Section 4 contains more
' details about GTOL.
'
' MAXFEV is a positive integer input variable. Termination occurs
' when the number of calls to FCN is at least MAXFEV by the end
' of an iteration.
'
' EPSFCN is an input variable used in determining a suitable step
' for the forward-difference approximation. This approximation
' assumes that the relative errors in the functions are of the
' order of EPSFCN. If EPSFCN is less than the machine preci-
' sion, it is assumed that the relative errors in the functions
' are of the order of the machine precision.
'
' DIAG is an array of length N. If MODE = 1 (see below), DIAG is
' internally set. If MODE = 2, DIAG must contain positive
' entries that serve as multiplicative scale factors for the
' variables.
'
' MODE is an integer input variable. If MODE = 1, the variables
' will be scaled internally. If MODE = 2, the scaling is
'
'
' Page 3
'
' specified by the input DIAG. Other values of MODE are equiva-
' lent to MODE = 1.
'
' FACTOR is a positive input variable used in determining the ini-
' tial step bound. This bound is set to the product of FACTOR
' and the Euclidean norm of DIAG*X if nonzero, or else to FACTOR
' itself. In most cases FACTOR should lie in the interval
' (.1,100.). 100. is a generally recommended value.
'
' NPRINT is an integer input variable that enables controlled
' printing of iterates if it is positive. In this case, FCN is
' called with IFLAG = 0 at the beginning of the first iteration
' and every NPRINT iterations thereafter and immediately prior
' to return, with X and FVEC available for printing. If NPRINT
' is not positive, no special calls of FCN with IFLAG = 0 are
' made.
'
' INFO is an integer output variable. If the user has terminated
' execution, INFO is set to the (negative) value of IFLAG. See
' description of FCN. Otherwise, INFO is set as follows.
'
' INFO = 0 Improper input parameters.
'
' INFO = 1 Both actual and predicted relative reductions in the
' sum of squares are at most FTOL.
'
' INFO = 2 Relative error between two consecutive iterates is
' at most XTOL.
'
' INFO = 3 Conditions for INFO = 1 and INFO = 2 both hold.
'
' INFO = 4 The cosine of the angle between FVEC and any column
' of the Jacobian is at most GTOL in absolute value.
'
' INFO = 5 Number of calls to FCN has reached or exceeded
' MAXFEV.
'
' INFO = 6 FTOL is too small. No further reduction in the sum
' of squares is possible.
'
' INFO = 7 XTOL is too small. No further improvement in the
' approximate solution X is possible.
'
' INFO = 8 GTOL is too small. FVEC is orthogonal to the
' columns of the Jacobian to machine precision.
'
' Sections 4 and 5 contain more details about INFO.
'
' NFEV is an integer output variable set to the number of calls to
' FCN.
'
' FJAC is an output M by N array. The upper N by N submatrix of
' FJAC contains an upper triangular matrix R with diagonal ele-
' ments of nonincreasing magnitude such that
'
'
' Page 4
'
' T T T
' P *(JAC *JAC)*P = R *R,
'
' where P is a permutation matrix and JAC is the final calcu-
' lated j
'|-------------------------------------------------------------------------|
'|-------------------------------------------------------------------------|
Option Explicit
Private FunctionName_ As String
Public fvec As Variant
Private IndependentVariables_ As Variant
Private ObservedVariables_ As Variant
Private iflag_ As Variant
Public jacobian As Variant
Private Sub lmdif(fcn As String, m As Single, n As Single, x As Variant, fvec As Variant, _
ftol As Double, xtol As Double, gtol As Double, maxfev As Single, epsfcn As Double, _
diag As Variant, mode As Single, factor As Double, nprint As Single, _
info As Variant, nfev As Variant, fjac As Variant, ldfjac As Single, _
ipvt As Variant, qtf As Variant, wa1 As Variant, wa2 As Variant, _
wa3 As Variant, wa4 As Variant)
Dim i As Single
Dim iflag As Single
Dim ij As Single
Dim jj As Single
Dim iter As Single
Dim j As Single
Dim l As Single
Dim actred As Double
Dim delta As Double
delta = 0
Dim dirder As Double
Dim fnorm As Double
Dim fnorm1 As Double
Dim gnorm As Double
Dim par As Double
Dim pnorm As Double
Dim prered As Double
Dim ratio As Double
Dim sum As Double
Dim temp As Double
Dim temp1 As Double
Dim temp2 As Double
Dim temp3 As Double
Dim xnorm As Double
xnorm = 0
Dim one As Double
Dim p1 As Double
Dim p5 As Double
Dim p25 As Double
Dim p75 As Double
Dim p0001 As Double
Dim zero As Double
one = 1
p1 = 0.1
p5 = 0.5
p25 = 0.25
p75 = 0.75
p0001 = 0.0001
zero = 0
Dim epsmch As Double
' epsmch is the machine precision.
epsmch = 1E-16
info = 0
iflag = 0
nfev = 0
' check the input parameters for errors.
If ((n <= 0) Or (m < n) Or (ldfjac < m) _
Or (ftol < zero) Or (xtol < zero) Or (gtol < zero) _
Or (maxfev <= 0) Or (factor <= zero)) Then GoTo L300
If (mode <> 2) Then GoTo L20
For j = 1 To n
If (diag(j) <= zero) Then GoTo L300
Next j
L20:
' evaluate the function at the starting point
' and calculate its norm.
iflag = 1
LevenbergCostFunction m, n, x, fvec, iflag
nfev = 1
If (iflag < 0) Then GoTo L300
fnorm = myenorm(m, fvec)
' initialize levenberg-marquardt parameter and iteration counter.
par = zero
iter = 1
' beginning of the outer loop.
'
L30:
' calculate the jacobian matrix.
iflag = 2
fdjac2 m, n, x, fvec, fjac, ldfjac, iflag, epsfcn, wa4
nfev = nfev + n
If (iflag < 0) Then GoTo L300
' if requested, call fcn to enable printing of iterates.
If (nprint <= 0) Then GoTo L40
iflag = 0
If (mymod(iter - 1, nprint) = 0) Then LevenbergCostFunction m, n, x, fvec, iflag
If (iflag < 0) Then GoTo L300
L40:
'
' compute the qr factorization of the jacobian.
'
qrfac m, n, fjac, ldfjac, True, ipvt, n, wa1, wa2, wa3
'
' on the first iteration and if mode is 1, scale according
' to the norms of the columns of the initial jacobian.
'
If (iter <> 1) Then GoTo L80
If (mode = 2) Then GoTo L60
For j = 1 To n
diag(j) = wa2(j)
If (wa2(j) = zero) Then diag(j) = one
Next j
L60:
'
' on the first iteration, calculate the norm of the scaled x
' and initialize the step bound delta.
'
For j = 1 To n
wa3(j) = diag(j) * x(j)
Next j
xnorm = myenorm(n, wa3)
delta = factor * xnorm
If (delta = zero) Then delta = factor
L80:
'
' form (q transpose)*fvec and store the first n components in
' qtf.
'
For i = 1 To m
wa4(i) = fvec(i)
Next i
For j = 1 To n
If (fjac(j, j) = zero) Then GoTo L120
sum = zero
For i = j To m
sum = sum + fjac(i, j) * wa4(i)
Next i
temp = -sum / fjac(j, j)
For i = j To m
wa4(i) = wa4(i) + fjac(i, j) * temp
Next i
L120:
fjac(j, j) = wa1(j)
qtf(j) = wa4(j)
Next j
'
' compute the norm of the scaled gradient.
'
gnorm = zero
If (fnorm = zero) Then GoTo L170
For j = 1 To n
l = ipvt(j)
If (wa2(l) = zero) Then GoTo L150
sum = zero
For i = 1 To j
sum = sum + fjac(i, j) * (qtf(i) / fnorm)
Next i
gnorm = dmax1(gnorm, dabs(sum / wa2(l)))
L150:
Next j
L170:
'
' test for convergence of the gradient norm.
'
If (gnorm <= gtol) Then info = 4
If (info <> 0) Then GoTo L300
'
' rescale if necessary.
'
If (mode = 2) Then GoTo L190
For j = 1 To n
diag(j) = dmax1(CDbl(diag(j)), CDbl(wa2(j)))
Next j
L190:
'
' beginning of the inner loop.
'
L200:
'
' determine the levenberg-marquardt parameter.
'
lmpar n, fjac, ldfjac, ipvt, diag, qtf, delta, par, wa1, wa2, wa3, wa4
'
' store the direction p and x + p. calculate the norm of p.
'
For j = 1 To n
wa1(j) = -wa1(j)
wa2(j) = x(j) + wa1(j)
wa3(j) = diag(j) * wa1(j)
Next j
pnorm = myenorm(n, wa3)
'
' on the first iteration, adjust the initial step bound.
'
If (iter = 1) Then delta = dmin1(delta, pnorm)
'
' evaluate the function at x + p and calculate its norm.
'
iflag = 1
LevenbergCostFunction m, n, wa2, wa4, iflag
nfev = nfev + 1
If (iflag < 0) Then GoTo L300
fnorm1 = myenorm(m, wa4)
'
' compute the scaled actual reduction.
'
actred = -one
If (p1 * fnorm1 < fnorm) Then actred = one - (fnorm1 / fnorm) * (fnorm1 / fnorm)
'
' compute the scaled predicted reduction and
' the scaled directional derivative.
'
For j = 1 To n
wa3(j) = zero
l = ipvt(j)
temp = wa1(l)
For i = 1 To j
wa3(i) = wa3(i) + fjac(i, j) * temp
Next i
Next j
temp1 = myenorm(n, wa3) / fnorm
temp2 = (dsqrt(par) * pnorm) / fnorm
prered = temp1 * temp1 + (temp2 * temp2) / p5
dirder = dirder - (temp1 * temp1 + temp2 * temp2)
'
' compute the ratio of the actual to the predicted
' reduction.
'
ratio = zero
If (prered <> zero) Then ratio = actred / prered
'
' update the step bound.
'
If (ratio > p25) Then GoTo L240
If (actred >= zero) Then temp = p5
If (actred < zero) Then _
temp = p5 * dirder / (dirder + p5 * actred)
If ((p1 * fnorm1 >= fnorm) Or (temp < p1)) Then temp = p1
delta = temp * dmin1(delta, pnorm / p1)
par = par / temp
GoTo L260
L240:
If ((par <> zero) And (ratio < p75)) Then GoTo L250
delta = pnorm / p5
par = p5 * par
L250:
L260:
'
' test for successful iteration.
'
If (ratio < p0001) Then GoTo L290
'
' successful iteration. update x, fvec, and their norms.
'
For j = 1 To n
x(j) = wa2(j)
wa2(j) = diag(j) * x(j)
Next j
For i = 1 To m
fvec(i) = wa4(i)
Next i
xnorm = myenorm(n, wa2)
fnorm = fnorm1
iter = iter + 1
L290:
'
' tests for convergence.
'
If ((dabs(actred) <= ftol) And (prered <= ftol) _
And (p5 * ratio <= one)) Then info = 1
If (delta <= xtol * xnorm) Then info = 2
If ((dabs(actred) <= ftol) And (prered <= ftol) _
And (p5 * ratio <= one) And (info = 2)) Then info = 3
If (info <> 0) Then GoTo L300
'
' tests for termination and stringent tolerances.
'
If (nfev >= maxfev) Then info = 5
If ((dabs(actred) <= epsmch) And (prered <= epsmch) _
And (p5 * ratio <= one)) Then info = 6
If (delta <= epsmch * xnorm) Then info = 7
If (gnorm <= epsmch) Then info = 8
If (info <> 0) Then GoTo L300
'
' end of the inner loop. repeat if iteration unsuccessful.
'
If (ratio < p0001) Then GoTo L200
'
' end of the outer loop.
'
GoTo L30
L300:
'
' termination, either normal or user imposed.
'
If (iflag < 0) Then info = iflag
iflag = 0
If (nprint > 0) Then LevenbergCostFunction m, n, x, fvec, iflag
'Return
'
' last card of subroutine lmdif.
'
End Sub
Private Function dsqrt(x As Double) As Double
dsqrt = x ^ 0.5
End Function
Private Function dmax1(a As Double, b As Double)
If a >= b Then
dmax1 = a
Else
dmax1 = b
End If
End Function
Private Function dmin1(a As Double, b As Double)
If a <= b Then
dmin1 = a
Else
dmin1 = b
End If
End Function
Private Function min1single(a As Single, b As Single)
If a <= b Then
min1single = a
Else
min1single = b
End If
End Function
Private Function dabs(x As Double) As Double
dabs = Abs(x)
End Function
Private Sub testenorm(x As Variant)
End Sub
Private Sub qrfac(m As Single, n As Single, ByRef a As Variant, lda As Single, _
pivot As Boolean, ByRef ipvt As Variant, lipvt As Single, ByRef rdiag As Variant, _
ByRef acnorm As Variant, ByRef wa As Variant)
Dim i As Single
Dim j As Single
Dim k As Single
Dim jp1 As Single
Dim kmax As Single
Dim minmn As Single
Dim ajnorm As Double
Dim epsmch As Double
Dim one As Double
Dim p05 As Double
Dim sum As Double
Dim temp As Double
Dim zero As Double
Dim dpmpar As Double
Dim enorm As Double
one = 1
p05 = 0.05
zero = 0
ReDim tmpaarr(1 To m) As Variant
Dim tmpj As Single
'
' epsmch is the machine precision.
'
epsmch = 1.2E-16
'
' compute the initial column norms and initialize several arrays.
'
For j = 1 To n
For tmpj = 1 To m
tmpaarr(tmpj) = a(tmpj, j)
Next tmpj
'acnorm(j) = enorm(m, a(1, j))
'a3 = enorm(m, a2)
'testenorm tmpaarr
acnorm(j) = myenorm(m, tmpaarr)
rdiag(j) = acnorm(j)
wa(j) = rdiag(j)
If (pivot) Then ipvt(j) = j
Next j
'
' reduce a to r with householder transformations.
'
minmn = min1single(m, n)
For j = 1 To minmn
If (Not pivot) Then GoTo L40
'
' bring the column of largest norm into the pivot position.
'
kmax = j
For k = j To n
If (rdiag(k) > rdiag(kmax)) Then kmax = k
Next k
If (kmax = j) Then GoTo L40
For i = 1 To m
temp = a(i, j)
a(i, j) = a(i, kmax)
a(i, kmax) = temp
Next i
rdiag(kmax) = rdiag(j)
wa(kmax) = wa(j)
k = ipvt(j)
ipvt(j) = ipvt(kmax)
ipvt(kmax) = k
L40:
'
' compute the householder transformation to reduce the
' j-th column of a to a multiple of the j-th unit vector.
'
ReDim tmpaarr(1 To m - j + 1) As Variant
For tmpj = 1 To m - j + 1
tmpaarr(tmpj) = a(tmpj + j - 1, j)
Next tmpj
'ajnorm = myenorm(m - j + 1, a(j, j))
ajnorm = myenorm(m - j + 1, tmpaarr)
If (ajnorm = zero) Then GoTo L100
If (a(j, j) < zero) Then ajnorm = -ajnorm
For i = j To m
a(i, j) = a(i, j) / ajnorm
Next i
a(j, j) = a(j, j) + one
'
' apply the transformation to the remaining columns
' and update the norms.
'
jp1 = j + 1
If (n < jp1) Then GoTo L100
For k = jp1 To n
sum = zero
For i = j To m
sum = sum + a(i, j) * a(i, k)
Next i
temp = sum / a(j, j)
For i = j To m
a(i, k) = a(i, k) - temp * a(i, j)
Next i
If ((Not pivot) Or (rdiag(k) = zero)) Then GoTo L80
temp = a(j, k) / rdiag(k)
rdiag(k) = rdiag(k) * dsqrt(dmax1(zero, one - temp * temp))
If (p05 * (rdiag(k) / wa(k)) ^ 2 > epsmch) Then GoTo L80
rdiag(k) = myenorm(m - j, a(jp1, k))
wa(k) = rdiag(k)
L80:
Next k
L100:
rdiag(j) = -ajnorm
Next j
'Return
'
' last card of subroutine qrfac.
'
End Sub
Private Function mymod(x As Single, y As Single) As Single
mymod = 1
End Function
Private Function myenorm(n As Single, x As Variant) As Double
Dim i As Single
Dim agiant As Double
Dim floatn As Double
Dim one As Double
Dim rdwarf As Double
Dim rgiant As Double
Dim s1 As Double
Dim s2 As Double
Dim s3 As Double
Dim xabs As Double
Dim x1max As Double
Dim x3max As Double
Dim zero As Double
one = 1
zero = 0
rdwarf = 3.834E-20
rgiant = 1.304E+19
s1 = zero
s2 = zero
s3 = zero
x1max = zero
x3max = zero
floatn = n
agiant = rgiant / floatn
For i = 1 To n
xabs = dabs(CDbl(x(i)))
If ((xabs > rdwarf) And (xabs < agiant)) Then GoTo L70
If (xabs <= rdwarf) Then GoTo L30
'
' sum for large components.
'
If (xabs <= x1max) Then GoTo L10
s1 = one + s1 * (x1max / xabs) ^ 2
x1max = xabs
GoTo L20
L10:
s1 = s1 + (xabs / x1max) ^ 2
L20:
GoTo L60
L30:
'
' sum for small components.
'
If (xabs <= x3max) Then GoTo L40
s3 = one + s3 * (x3max / xabs) ^ 2
x3max = xabs
GoTo L50
L40:
If (xabs <> zero) Then s3 = s3 + (xabs / x3max) ^ 2
L50:
L60:
GoTo L80
L70:
'
' sum for intermediate components.
'
s2 = s2 + xabs ^ 2
L80:
Next i
'
' calculation of norm.
'
If (s1 = zero) Then GoTo L100
myenorm = x1max * dsqrt(s1 + (s2 / x1max) / x1max)
GoTo L130
L100:
If (s2 = zero) Then GoTo L110
If (s2 >= x3max) Then _
myenorm = dsqrt(s2 * (one + (x3max / s2) * (x3max * s3)))
If (s2 < x3max) Then _
myenorm = dsqrt(x3max * ((s2 / x3max) + (x3max * s3)))
GoTo L120
L110:
myenorm = x3max * dsqrt(s3)
L120:
L130:
'
' last card of function enorm.
'
End Function
Private Sub lmpar(n As Single, ByRef r As Variant, ldr As Single, ipvt As Variant, _
diag As Variant, qtb As Variant, delta As Double, ByRef par As Double, _
ByRef x As Variant, ByRef sdiag As Variant, ByRef wa1 As Variant, ByRef wa2 As Variant)
Dim i As Single
Dim iter As Single
Dim j As Single
Dim jm1 As Single
Dim jp1 As Single
Dim k As Single
Dim l As Single
Dim nsing As Single
Dim dxnorm As Double
Dim dwarf As Double
Dim fp As Double
Dim gnorm As Double
Dim parc As Double
Dim parl As Double
Dim paru As Double
Dim p1 As Double
Dim p001 As Double
Dim sum As Double
Dim temp As Double
Dim zero As Double
Dim dmppar As Double
Dim enorm As Double
p1 = 0.1
p001 = 0.001
zero = 0
'
' dwarf is the smallest positive magnitude.
'
dwarf = 1E-38
'
' compute and store in x the gauss-newton direction. if the
' jacobian is rank-deficient, obtain a least squares solution.
'
nsing = n
For j = 1 To n
wa1(j) = qtb(j)
If ((r(j, j) = zero) And (nsing = n)) Then nsing = j - 1
If (nsing < n) Then wa1(j) = zero
Next j
If (nsing < 1) Then GoTo L50
For k = 1 To nsing
j = nsing - k + 1
wa1(j) = wa1(j) / r(j, j)
temp = wa1(j)
jm1 = j - 1
If (jm1 < 1) Then GoTo L30
For i = 1 To jm1
wa1(i) = wa1(i) - r(i, j) * temp
Next i
L30:
Next k
L50:
For j = 1 To n
l = ipvt(j)
x(l) = wa1(j)
Next j
'
' initialize the iteration counter.
' evaluate the function at the origin, and test
' for acceptance of the gauss-newton direction.
'
iter = 0
For j = 1 To n
wa2(j) = diag(j) * x(j)
Next j
dxnorm = myenorm(n, wa2)
fp = dxnorm - delta
If (fp <= p1 * delta) Then GoTo L220
'
' if the jacobian is not rank deficient, the newton
' step provides a lower bound, parl, for the zero of
' the function. otherwise set this bound to zero.
'
parl = zero
If (nsing < n) Then GoTo L120
For j = 1 To n
l = ipvt(j)
wa1(j) = diag(l) * (wa2(l) / dxnorm)
Next j
For j = 1 To n
sum = zero
jm1 = j - 1
If (jm1 < 1) Then GoTo L100
For i = 1 To jm1
sum = sum + r(i, j) * wa1(i)
Next i
L100:
wa1(j) = (wa1(j) - sum) / r(j, j)
Next j
temp = myenorm(n, wa1)
parl = ((fp / delta) / temp) / temp
L120:
'
' calculate an upper bound, paru, for the zero of the function.
'
For j = 1 To n
sum = zero
For i = 1 To j
sum = sum + r(i, j) * qtb(i)
Next i
l = ipvt(j)
wa1(j) = sum / diag(l)
Next j
gnorm = myenorm(n, wa1)
paru = gnorm / delta
If (paru = zero) Then paru = dwarf / dmin1(delta, p1)
'
' if the input par lies outside of the interval (parl,paru),
' set par to the closer endpoint.
'
par = dmax1(par, parl)
par = dmin1(par, paru)
If (par = zero) Then par = gnorm / dxnorm
'
' beginning of an iteration.
'
L150:
iter = iter + 1
'
' evaluate the function at the current value of par.
'
If (par = zero) Then par = dmax1(dwarf, p001 * paru)
temp = dsqrt(par)
For j = 1 To n
wa1(j) = temp * diag(j)
Next j
qrsolv n, r, ldr, ipvt, wa1, qtb, x, sdiag, wa2
For j = 1 To n
wa2(j) = diag(j) * x(j)
Next j
dxnorm = myenorm(n, wa2)
temp = fp
fp = dxnorm - delta
'
' if the function is small enough, accept the current value
' of par. also test for the exceptional cases where parl
' is zero or the number of iterations has reached 10.
'
If ((dabs(fp) <= p1 * delta) _
Or ((parl = zero) And (fp <= temp) And (temp < zero)) _
Or (iter = 10)) Then GoTo L220
'
' compute the newton correction.
'
For j = 1 To n
l = ipvt(j)
wa1(j) = diag(l) * (wa2(l) / dxnorm)
Next j
For j = 1 To n
wa1(j) = wa1(j) / sdiag(j)
temp = wa1(j)
jp1 = j + 1
If (n < jp1) Then GoTo L200
For i = jp1 To n
wa1(i) = wa1(i) - r(i, j) * temp
Next i
L200:
Next j
temp = myenorm(n, wa1)
parc = ((fp / delta) / temp) / temp
'
' depending on the sign of the function, update parl or paru.
'
If (fp > zero) Then parl = dmax1(parl, par)
If (fp < zero) Then paru = dmin1(paru, par)
'
' compute an improved estimate for par.
'
par = dmax1(parl, par + parc)
'
' end of an iteration.
'
GoTo L150
L220:
'
' termination.
'
If (iter = 0) Then par = zero
'
' last card of subroutine lmpar.
'
End Sub
Private Sub qrsolv(n As Single, ByRef r As Variant, ldr As Single, ByRef ipvt As Variant, _
ByRef diag As Variant, ByRef qtb As Variant, ByRef x As Variant, ByRef sdiag As Variant, ByRef wa As Variant)
Dim i As Single
Dim j As Single
Dim jp1 As Single
Dim k As Single
Dim kp1 As Single
Dim l As Single
Dim nsing As Single
Dim mycos As Double
Dim mycotan As Double
Dim p5 As Double
Dim p25 As Double
Dim qtbpj As Double
Dim mysin As Double
Dim sum As Double
Dim mytan As Double
Dim temp As Double
Dim zero As Double
p5 = 0.5
p25 = 0.25
zero = 0
'
' copy r and (q transpose)*b to preserve input and initialize s.
' in particular, save the diagonal elements of r in x.
'
For j = 1 To n
For i = j To n
r(i, j) = r(j, i)
Next i
x(j) = r(j, j)