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

Namespace MathEx.LM

    Public Class levenbergmarquardt

        '************************************************************************
        '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.
        '************************************************************************

        '
        '    This members must be defined by you:
        '    static void funcvecjac(ref double[] x,
        '        ref double[] fvec,
        '        ref double[,] fjac,
        '        ref int iflag)
        '    
        Public fv As funcvecjacdelegate
        Delegate Sub funcvecjacdelegate(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

        Sub New()

        End Sub

        Sub DefineFuncGradDelegate(ByVal fvj As funcvecjacdelegate)
            Me.fv = fvj
        End Sub

        Public Sub funcvecjac(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)
            fv.Invoke(x, fvec, fjac, iflag)
        End Sub

        '************************************************************************
        '    The subroutine minimizes the sum of squares  of  M nonlinear finctions  of
        '    N  arguments  with  Levenberg-Marquardt  algorithm  using  Jacobian    and
        '    information about function values.
        '
        '    Programmer  should  redefine  FuncVecJac  subroutine  which  takes array X
        '    (argument)  whose  index  ranges  from  1 to N as an input and if variable
        '    IFlag is equal to:
        '        * 1, returns vector of function values in array FVec (in elements from
        '          1 to M), not changing FJac.
        '        * 2,  returns  Jacobian  in  array FJac (in elements [1..M,1..N]), not
        '          changing FVec.
        '    The subroutine can change the IFlag parameter by setting it into a negative
        '    number. It will terminate program.
        '
        '    Programmer  can  also  redefine  LevenbergMarquardtNewIteration subroutine
        '    which is called on each new step.   Current  point  X  is  passed into the
        '    subroutine.   It  is  reasonable  to  redefine  the  subroutine for better
        '    debugging, for example, to visualize the solution process.
        '
        '    The AdditionalLevenbergMarquardtStoppingCriterion could  be  redefined  to
        '    modify stopping conditions.
        '
        '    Input parameters:
        '        N       �   number of unknowns, N>0.
        '        M       �   number of summable functions, M>=N.
        '        X       �   initial solution approximation.
        '                    Array whose index ranges from 1 to N.
        '        EpsG    �   stopping criterion. Iterations are stopped, if  cosine  of
        '                    the angle between vector of function values  and  each  of
        '                    the  Jacobian  columns  if  less or equal EpsG by absolute
        '                    value. In fact this value defines stopping condition which
        '                    is based on the function gradient smallness.
        '        EpsF    �   stopping criterion. Iterations are  stopped,  if  relative
        '                    decreasing of sum of function values squares (real and
        '                    predicted on the base of extrapolation)  is  less or equal
        '                    EpsF.
        '        EpsX    �   stopping criterion. Iterations are  stopped,  if  relative
        '                    change of solution is less or equal EpsX.
        '        MaxIts  �   stopping  criterion.  Iterations  are  stopped,  if  their
        '                    number exceeds MaxIts.
        '
        '    Output parameters:
        '        X       �   solution
        '                    Array whose index ranges from 1 to N.
        '        Info    �   a reason of a program completion:
        '                        * -1 wrong parameters were specified,
        '                        * 0 interrupted by user,
        '                        * 1 relative decrease of sum of function values
        '                            squares (real and predicted on the base  of
        '                            extrapolation) is less or equal EpsF.
        '                        * 2 relative change of solution is less or equal
        '                            EpsX.
        '                        * 3 conditions (1) and (2) are fulfilled.
        '                        * 4 cosine of the angle between vector of function
        '                            values and each of the Jacobian columns is less
        '                            or equal EpsG by absolute value.
        '                        * 5 number of iterations exceeds MaxIts.
        '                        * 6 EpsF is too small.
        '                            It is impossible to get a better result.
        '                        * 7 EpsX is too small.
        '                            It is impossible to get a better result.
        '                        * 8 EpsG is too small. Vector of functions is
        '                            orthogonal to Jacobian columns with near-machine
        '                            precision.
        '    argonne national laboratory. minpack project. march 1980.
        '    burton s. garbow, kenneth e. hillstrom, jorge j. more
        '
        '    Contributors:
        '        * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
        '          pseudocode.
        '    ************************************************************************

        Public Sub levenbergmarquardtminimize(ByVal n As Integer, ByVal m As Integer, ByRef x As Double(), ByVal epsg As Double, ByVal epsf As Double, ByVal epsx As Double, _
         ByVal maxits As Integer, ByRef info As Integer)
            Dim fvec As Double() = New Double(-1) {}
            Dim qtf As Double() = New Double(-1) {}
            Dim ipvt As Integer() = New Integer(-1) {}
            Dim fjac As Double(,) = New Double(-1, -1) {}
            Dim w2 As Double(,) = New Double(-1, -1) {}
            Dim wa1 As Double() = New Double(-1) {}
            Dim wa2 As Double() = New Double(-1) {}
            Dim wa3 As Double() = New Double(-1) {}
            Dim wa4 As Double() = New Double(-1) {}
            Dim diag As Double() = New Double(-1) {}
            Dim mode As Integer = 0
            Dim nfev As Integer = 0
            Dim njev As Integer = 0
            Dim factor As Double = 0
            Dim i As Integer = 0
            Dim iflag As Integer = 0
            Dim iter As Integer = 0
            Dim j As Integer = 0
            Dim l As Integer = 0
            Dim actred As Double = 0
            Dim delta As Double = 0
            Dim dirder As Double = 0
            Dim fnorm As Double = 0
            Dim fnorm1 As Double = 0
            Dim gnorm As Double = 0
            Dim par As Double = 0
            Dim pnorm As Double = 0
            Dim prered As Double = 0
            Dim ratio As Double = 0
            Dim sum As Double = 0
            Dim temp As Double = 0
            Dim temp1 As Double = 0
            Dim temp2 As Double = 0
            Dim xnorm As Double = 0
            Dim p1 As Double = 0
            Dim p5 As Double = 0
            Dim p25 As Double = 0
            Dim p75 As Double = 0
            Dim p0001 As Double = 0
            Dim i_ As Integer = 0


            '
            ' Factor is a positive input variable used in determining the
            ' initial 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.0 is a generally recommended value.
            '
            factor = 100.0R

            '
            ' mode is an integer input variable. if mode = 1, the
            ' variables will be scaled internally. if mode = 2,
            ' the scaling is specified by the input diag. other
            ' values of mode are equivalent to mode = 1.
            '
            mode = 1

            '
            ' diag is an array of length n. if mode = 1
            ' diag is internally set. if mode = 2, diag
            ' must contain positive entries that serve as
            ' multiplicative scale factors for the variables.
            '
            diag = New Double(n) {}

            '
            ' Initialization
            '
            qtf = New Double(n) {}
            fvec = New Double(m) {}
            fjac = New Double(m, n) {}
            w2 = New Double(n, m) {}
            ipvt = New Integer(n) {}
            wa1 = New Double(n) {}
            wa2 = New Double(n) {}
            wa3 = New Double(n) {}
            wa4 = New Double(m) {}
            p1 = 0.1R
            p5 = 0.5R
            p25 = 0.25R
            p75 = 0.75R
            p0001 = 0.0001
            info = 0
            iflag = 0
            nfev = 0
            njev = 0

            '
            ' check the input parameters for errors.
            '
            If n <= 0 Or m < n Then
                info = -1
                Exit Sub
            End If
            If epsf < 0 Or epsx < 0 Or epsg < 0 Then
                info = -1
                Exit Sub
            End If
            If factor <= 0 Then
                info = -1
                Exit Sub
            End If
            If mode = 2 Then
                For j = 1 To n
                    If diag(j) <= 0 Then
                        info = -1
                        Exit Sub
                    End If
                Next
            End If

            '
            ' evaluate the function at the starting point
            ' and calculate its norm.
            '
            iflag = 1
            funcvecjac(x, fvec, fjac, iflag)
            nfev = 1
            If iflag < 0 Then
                info = 0
                Exit Sub
            End If
            fnorm = 0.0R
            For i_ = 1 To m
                fnorm += fvec(i_) * fvec(i_)
            Next
            fnorm = Math.Sqrt(fnorm)

            '
            ' initialize levenberg-marquardt parameter and iteration counter.
            '
            par = 0
            iter = 1

            '
            ' beginning of the outer loop.
            '
            While True

                '
                ' New iteration
                '
                levenbergmarquardtnewiteration(x)

                '
                ' calculate the jacobian matrix.
                '
                iflag = 2
                funcvecjac(x, fvec, fjac, iflag)
                njev = njev + 1
                If iflag < 0 Then
                    info = 0
                    Exit Sub
                End If

                '
                ' compute the qr factorization of the jacobian.
                '
                levenbergmarquardtqrfac(m, n, fjac, True, ipvt, wa1, _
                 wa2, wa3, w2)

                '
                ' 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
                    If mode <> 2 Then
                        For j = 1 To n
                            diag(j) = wa2(j)
                            If wa2(j) = 0 Then
                                diag(j) = 1
                            End If
                        Next
                    End If

                    '
                    ' 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
                    xnorm = 0.0R
                    For i_ = 1 To n
                        xnorm += wa3(i_) * wa3(i_)
                    Next
                    xnorm = Math.Sqrt(xnorm)
                    delta = factor * xnorm
                    If delta = 0 Then
                        delta = factor
                    End If
                End If

                '
                ' form (q transpose)*fvec and store the first n components in
                ' qtf.
                '
                For i = 1 To m
                    wa4(i) = fvec(i)
                Next
                For j = 1 To n
                    If fjac(j, j) <> 0 Then
                        sum = 0
                        For i = j To m
                            sum = sum + fjac(i, j) * wa4(i)
                        Next
                        temp = -(sum / fjac(j, j))
                        For i = j To m
                            wa4(i) = wa4(i) + fjac(i, j) * temp
                        Next
                    End If
                    fjac(j, j) = wa1(j)
                    qtf(j) = wa4(j)
                Next

                '
                ' compute the norm of the scaled gradient.
                '
                gnorm = 0
                If fnorm <> 0 Then
                    For j = 1 To n
                        l = ipvt(j)
                        If wa2(l) <> 0 Then
                            sum = 0
                            For i = 1 To j
                                sum = sum + fjac(i, j) * (qtf(i) / fnorm)
                            Next
                            gnorm = Math.Max(gnorm, Math.Abs(sum / wa2(l)))
                        End If
                    Next
                End If

                '
                ' test for convergence of the gradient norm.
                '
                If gnorm <= epsg Then
                    info = 4
                End If
                If info <> 0 Then
                    Exit Sub
                End If

                '
                ' rescale if necessary.
                '
                If mode <> 2 Then
                    For j = 1 To n
                        diag(j) = Math.Max(diag(j), wa2(j))
                    Next
                End If

                '
                ' beginning of the inner loop.
                '
                While True

                    '
                    ' determine the levenberg-marquardt parameter.
                    '
                    levenbergmarquardtpar(n, fjac, 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
                    pnorm = 0.0R
                    For i_ = 1 To n
                        pnorm += wa3(i_) * wa3(i_)
                    Next
                    pnorm = Math.Sqrt(pnorm)

                    '
                    ' on the first iteration, adjust the initial step bound.
                    '
                    If iter = 1 Then
                        delta = Math.Min(delta, pnorm)
                    End If

                    '
                    ' evaluate the function at x + p and calculate its norm.
                    '
                    iflag = 1
                    funcvecjac(wa2, wa4, fjac, iflag)
                    nfev = nfev + 1
                    If iflag < 0 Then
                        info = 0
                        Exit Sub
                    End If
                    fnorm1 = 0.0R
                    For i_ = 1 To m
                        fnorm1 += wa4(i_) * wa4(i_)
                    Next
                    fnorm1 = Math.Sqrt(fnorm1)

                    '
                    ' compute the scaled actual reduction.
                    '
                    actred = -1
                    If p1 * fnorm1 < fnorm Then
                        actred = 1 - AP.MathEx.Sqr(fnorm1 / fnorm)
                    End If

                    '
                    ' compute the scaled predicted reduction and
                    ' the scaled directional derivative.
                    '
                    For j = 1 To n
                        wa3(j) = 0
                        l = ipvt(j)
                        temp = wa1(l)
                        For i = 1 To j
                            wa3(i) = wa3(i) + fjac(i, j) * temp
                        Next
                    Next
                    temp1 = 0.0R
                    For i_ = 1 To n
                        temp1 += wa3(i_) * wa3(i_)
                    Next
                    temp1 = Math.Sqrt(temp1) / fnorm
                    temp2 = Math.Sqrt(par) * pnorm / fnorm
                    prered = AP.MathEx.Sqr(temp1) + AP.MathEx.Sqr(temp2) / p5
                    dirder = -(AP.MathEx.Sqr(temp1) + AP.MathEx.Sqr(temp2))

                    '
                    ' compute the ratio of the actual to the predicted
                    ' reduction.
                    '
                    ratio = 0
                    If prered <> 0 Then
                        ratio = actred / prered
                    End If

                    '
                    ' update the step bound.
                    '
                    If ratio > p25 Then
                        If par = 0 Or ratio >= p75 Then
                            delta = pnorm / p5
                            par = p5 * par
                        End If
                    Else
                        If actred >= 0 Then
                            temp = p5
                        End If
                        If actred < 0 Then
                            temp = p5 * dirder / (dirder + p5 * actred)
                        End If
                        If p1 * fnorm1 >= fnorm Or temp < p1 Then
                            temp = p1
                        End If
                        delta = temp * Math.Min(delta, pnorm / p1)
                        par = par / temp
                    End If

                    '
                    ' test for successful iteration.
                    '
                    If ratio >= p0001 Then

                        '
                        ' successful iteration. update x, fvec, and their norms.
                        '
                        For j = 1 To n
                            x(j) = wa2(j)
                            wa2(j) = diag(j) * x(j)
                        Next
                        For i = 1 To m
                            fvec(i) = wa4(i)
                        Next
                        xnorm = 0.0R
                        For i_ = 1 To n
                            xnorm += wa2(i_) * wa2(i_)
                        Next
                        xnorm = Math.Sqrt(xnorm)
                        fnorm = fnorm1
                        iter = iter + 1
                    End If

                    '
                    ' tests for convergence.
                    '
                    If Math.Abs(actred) <= epsf And prered <= epsf And p5 * ratio <= 1 Then
                        info = 1
                    End If
                    If delta <= epsx * xnorm Then
                        info = 2
                    End If
                    If Math.Abs(actred) <= epsf And prered <= epsf And p5 * ratio <= 1 And info = 2 Then
                        info = 3
                    End If
                    If info <> 0 Then
                        Exit Sub
                    End If

                    '
                    ' tests for termination and stringent tolerances.
                    '
                    If iter >= maxits And maxits > 0 Then
                        info = 5
                    End If
                    If Math.Abs(actred) <= AP.MathEx.MachineEpsilon And prered <= AP.MathEx.MachineEpsilon And p5 * ratio <= 1 Then
                        info = 6
                    End If
                    If delta <= AP.MathEx.MachineEpsilon * xnorm Then
                        info = 7
                    End If
                    If gnorm <= AP.MathEx.MachineEpsilon Then
                        info = 8
                    End If
                    If info <> 0 Then
                        Exit Sub
                    End If

                    '
                    ' end of the inner loop. repeat if iteration unsuccessful.
                    '
                    If ratio < p0001 Then
                        Continue While
                    End If
                    Exit While
                End While

                '
                ' Termination criterion
                '
                If additionallevenbergmarquardtstoppingcriterion(iter) Then
                    info = 0
                    Exit Sub
                End If

                '
                ' end of the outer loop.
                '
            End While
        End Sub


        Private Shared Sub levenbergmarquardtqrfac(ByVal m As Integer, ByVal n As Integer, ByRef a As Double(,), ByVal pivot As Boolean, ByRef ipvt As Integer(), ByRef rdiag As Double(), _
         ByRef acnorm As Double(), ByRef wa As Double(), ByRef w2 As Double(,))
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim jp1 As Integer = 0
            Dim k As Integer = 0
            Dim kmax As Integer = 0
            Dim minmn As Integer = 0
            Dim ajnorm As Double = 0
            Dim sum As Double = 0
            Dim temp As Double = 0
            Dim v As Double = 0
            Dim i_ As Integer = 0


            '
            ' Copy from a to w2 and transpose
            '
            For i = 1 To m
                For i_ = 1 To n
                    w2(i_, i) = a(i, i_)
                Next
            Next

            '
            ' compute the initial column norms and initialize several arrays.
            '
            For j = 1 To n
                v = 0.0R
                For i_ = 1 To m
                    v += w2(j, i_) * w2(j, i_)
                Next
                acnorm(j) = Math.Sqrt(v)
                rdiag(j) = acnorm(j)
                wa(j) = rdiag(j)
                If pivot Then
                    ipvt(j) = j
                End If
            Next

            '
            ' reduce a to r with householder transformations.
            '
            minmn = Math.Min(m, n)
            For j = 1 To minmn
                If pivot Then

                    '
                    ' 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
                        End If
                    Next
                    If kmax <> j Then
                        For i = 1 To m
                            temp = w2(j, i)
                            w2(j, i) = w2(kmax, i)
                            w2(kmax, i) = temp
                        Next
                        rdiag(kmax) = rdiag(j)
                        wa(kmax) = wa(j)
                        k = ipvt(j)
                        ipvt(j) = ipvt(kmax)
                        ipvt(kmax) = k
                    End If
                End If

                '
                ' compute the householder transformation to reduce the
                ' j-th column of a to a multiple of the j-th unit vector.
                '
                v = 0.0R
                For i_ = j To m
                    v += w2(j, i_) * w2(j, i_)
                Next
                ajnorm = Math.Sqrt(v)
                If ajnorm <> 0 Then
                    If w2(j, j) < 0 Then
                        ajnorm = -ajnorm
                    End If
                    v = 1 / ajnorm
                    For i_ = j To m
                        w2(j, i_) = v * w2(j, i_)
                    Next
                    w2(j, j) = w2(j, j) + 1.0R

                    '
                    ' apply the transformation to the remaining columns
                    ' and update the norms.
                    '
                    jp1 = j + 1
                    If n >= jp1 Then
                        For k = jp1 To n
                            sum = 0.0R
                            For i_ = j To m
                                sum += w2(j, i_) * w2(k, i_)
                            Next
                            temp = sum / w2(j, j)
                            For i_ = j To m
                                w2(k, i_) = w2(k, i_) - temp * w2(j, i_)
                            Next
                            If pivot And rdiag(k) <> 0 Then
                                temp = w2(k, j) / rdiag(k)
                                rdiag(k) = rdiag(k) * Math.Sqrt(Math.Max(0, 1 - AP.MathEx.Sqr(temp)))
                                If 0.05 * AP.MathEx.Sqr(rdiag(k) / wa(k)) <= AP.MathEx.MachineEpsilon Then
                                    v = 0.0R
                                    For i_ = jp1 To jp1 + m - j - 1
                                        v += w2(k, i_) * w2(k, i_)
                                    Next
                                    rdiag(k) = Math.Sqrt(v)
                                    wa(k) = rdiag(k)
                                End If
                            End If
                        Next
                    End If
                End If
                rdiag(j) = -ajnorm
            Next

            '
            ' Copy from w2 to a and transpose
            '
            For i = 1 To m
                For i_ = 1 To n
                    a(i, i_) = w2(i_, i)
                Next
            Next
        End Sub


        Private Shared Sub levenbergmarquardtqrsolv(ByVal n As Integer, ByRef r As Double(,), ByRef ipvt As Integer(), ByRef diag As Double(), ByRef qtb As Double(), ByRef x As Double(), _
         ByRef sdiag As Double(), ByRef wa As Double())
            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim jp1 As Integer = 0
            Dim k As Integer = 0
            Dim kp1 As Integer = 0
            Dim l As Integer = 0
            Dim nsing As Integer = 0
            Dim cs As Double = 0
            Dim ct As Double = 0
            Dim qtbpj As Double = 0
            Dim sn As Double = 0
            Dim sum As Double = 0
            Dim t As Double = 0
            Dim temp As Double = 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
                x(j) = r(j, j)
                wa(j) = qtb(j)
            Next

            '
            ' eliminate the diagonal matrix d using a givens rotation.
            '
            For j = 1 To n

                '
                ' prepare the row of d to be eliminated, locating the
                ' diagonal element using p from the qr factorization.
                '
                l = ipvt(j)
                If diag(l) <> 0 Then
                    For k = j To n
                        sdiag(k) = 0
                    Next
                    sdiag(j) = diag(l)

                    '
                    ' the transformations to eliminate the row of d
                    ' modify only a single element of (q transpose)*b
                    ' beyond the first n, which is initially zero.
                    '
                    qtbpj = 0
                    For k = j To n

                        '
                        ' determine a givens rotation which eliminates the
                        ' appropriate element in the current row of d.
                        '
                        If sdiag(k) <> 0 Then
                            If Math.Abs(r(k, k)) >= Math.Abs(sdiag(k)) Then
                                t = sdiag(k) / r(k, k)
                                cs = 0.5 / Math.Sqrt(0.25 + 0.25 * AP.MathEx.Sqr(t))
                                sn = cs * t
                            Else
                                ct = r(k, k) / sdiag(k)
                                sn = 0.5 / Math.Sqrt(0.25 + 0.25 * AP.MathEx.Sqr(ct))
                                cs = sn * ct
                            End If

                            '
                            ' compute the modified diagonal element of r and
                            ' the modified element of ((q transpose)*b,0).
                            '
                            r(k, k) = cs * r(k, k) + sn * sdiag(k)
                            temp = cs * wa(k) + sn * qtbpj
                            qtbpj = -(sn * wa(k)) + cs * qtbpj
                            wa(k) = temp

                            '
                            ' accumulate the tranformation in the row of s.
                            '
                            kp1 = k + 1
                            If n >= kp1 Then
                                For i = kp1 To n
                                    temp = cs * r(i, k) + sn * sdiag(i)
                                    sdiag(i) = -(sn * r(i, k)) + cs * sdiag(i)
                                    r(i, k) = temp
                                Next
                            End If
                        End If
                    Next
                End If

                '
                ' store the diagonal element of s and restore
                ' the corresponding diagonal element of r.
                '
                sdiag(j) = r(j, j)
                r(j, j) = x(j)
            Next

            '
            ' solve the triangular system for z. if the system is
            ' singular, then obtain a least squares solution.
            '
            nsing = n
            For j = 1 To n
                If sdiag(j) = 0 And nsing = n Then
                    nsing = j - 1
                End If
                If nsing < n Then
                    wa(j) = 0
                End If
            Next
            If nsing >= 1 Then
                For k = 1 To nsing
                    j = nsing - k + 1
                    sum = 0
                    jp1 = j + 1
                    If nsing >= jp1 Then
                        For i = jp1 To nsing
                            sum = sum + r(i, j) * wa(i)
                        Next
                    End If
                    wa(j) = (wa(j) - sum) / sdiag(j)
                Next
            End If

            '
            ' permute the components of z back to components of x.
            '
            For j = 1 To n
                l = ipvt(j)
                x(l) = wa(j)
            Next
        End Sub


        Private Shared Sub levenbergmarquardtpar(ByVal n As Integer, ByRef r As Double(,), ByRef ipvt As Integer(), ByRef diag As Double(), ByRef qtb As Double(), ByVal delta As Double, _
         ByRef par As Double, ByRef x As Double(), ByRef sdiag As Double(), ByRef wa1 As Double(), ByRef wa2 As Double())
            Dim i As Integer = 0
            Dim iter As Integer = 0
            Dim j As Integer = 0
            Dim jm1 As Integer = 0
            Dim jp1 As Integer = 0
            Dim k As Integer = 0
            Dim l As Integer = 0
            Dim nsing As Integer = 0
            Dim dxnorm As Double = 0
            Dim dwarf As Double = 0
            Dim fp As Double = 0
            Dim gnorm As Double = 0
            Dim parc As Double = 0
            Dim parl As Double = 0
            Dim paru As Double = 0
            Dim sum As Double = 0
            Dim temp As Double = 0
            Dim v As Double = 0
            Dim i_ As Integer = 0

            dwarf = AP.MathEx.MinRealNumber

            '
            ' 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) = 0 And nsing = n Then
                    nsing = j - 1
                End If
                If nsing < n Then
                    wa1(j) = 0
                End If
            Next
            If nsing >= 1 Then
                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
                        For i = 1 To jm1
                            wa1(i) = wa1(i) - r(i, j) * temp
                        Next
                    End If
                Next
            End If
            For j = 1 To n
                l = ipvt(j)
                x(l) = wa1(j)
            Next

            '
            ' 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
            v = 0.0R
            For i_ = 1 To n
                v += wa2(i_) * wa2(i_)
            Next
            dxnorm = Math.Sqrt(v)
            fp = dxnorm - delta
            If fp <= 0.1 * delta Then

                '
                ' termination.
                '
                If iter = 0 Then
                    par = 0
                End If
                Exit Sub
            End If

            '
            ' 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 = 0
            If nsing >= n Then
                For j = 1 To n
                    l = ipvt(j)
                    wa1(j) = diag(l) * (wa2(l) / dxnorm)
                Next
                For j = 1 To n
                    sum = 0
                    jm1 = j - 1
                    If jm1 >= 1 Then
                        For i = 1 To jm1
                            sum = sum + r(i, j) * wa1(i)
                        Next
                    End If
                    wa1(j) = (wa1(j) - sum) / r(j, j)
                Next
                v = 0.0R
                For i_ = 1 To n
                    v += wa1(i_) * wa1(i_)
                Next
                temp = Math.Sqrt(v)
                parl = fp / delta / temp / temp
            End If

            '
            ' calculate an upper bound, paru, for the zero of the function.
            '
            For j = 1 To n
                sum = 0
                For i = 1 To j
                    sum = sum + r(i, j) * qtb(i)
                Next
                l = ipvt(j)
                wa1(j) = sum / diag(l)
            Next
            v = 0.0R
            For i_ = 1 To n
                v += wa1(i_) * wa1(i_)
            Next
            gnorm = Math.Sqrt(v)
            paru = gnorm / delta
            If paru = 0 Then
                paru = dwarf / Math.Min(delta, 0.1)
            End If

            '
            ' if the input par lies outside of the interval (parl,paru),
            ' set par to the closer endpoint.
            '
            par = Math.Max(par, parl)
            par = Math.Min(par, paru)
            If par = 0 Then
                par = gnorm / dxnorm
            End If

            '
            ' beginning of an iteration.
            '
            While True
                iter = iter + 1

                '
                ' evaluate the function at the current value of par.
                '
                If par = 0 Then
                    par = Math.Max(dwarf, 0.001 * paru)
                End If
                temp = Math.Sqrt(par)
                For j = 1 To n
                    wa1(j) = temp * diag(j)
                Next
                levenbergmarquardtqrsolv(n, r, ipvt, wa1, qtb, x, _
                 sdiag, wa2)
                For j = 1 To n
                    wa2(j) = diag(j) * x(j)
                Next
                v = 0.0R
                For i_ = 1 To n
                    v += wa2(i_) * wa2(i_)
                Next
                dxnorm = Math.Sqrt(v)
                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 Math.Abs(fp) <= 0.1 * delta Or parl = 0 And fp <= temp And temp < 0 Or iter = 10 Then
                    Exit While
                End If

                '
                ' compute the newton correction.
                '
                For j = 1 To n
                    l = ipvt(j)
                    wa1(j) = diag(l) * (wa2(l) / dxnorm)
                Next
                For j = 1 To n
                    wa1(j) = wa1(j) / sdiag(j)
                    temp = wa1(j)
                    jp1 = j + 1
                    If n >= jp1 Then
                        For i = jp1 To n
                            wa1(i) = wa1(i) - r(i, j) * temp
                        Next
                    End If
                Next
                v = 0.0R
                For i_ = 1 To n
                    v += wa1(i_) * wa1(i_)
                Next
                temp = Math.Sqrt(v)
                parc = fp / delta / temp / temp

                '
                ' depending on the sign of the function, update parl or paru.
                '
                If fp > 0 Then
                    parl = Math.Max(parl, par)
                End If
                If fp < 0 Then
                    paru = Math.Min(paru, par)
                End If

                '
                ' compute an improved estimate for par.
                '
                par = Math.Max(parl, par + parc)

                '
                ' end of an iteration.
                '
            End While

            '
            ' termination.
            '
            If iter = 0 Then
                par = 0
            End If
        End Sub


        Private Shared Sub levenbergmarquardtnewiteration(ByRef x As Double())
        End Sub


        Private Shared Function additionallevenbergmarquardtstoppingcriterion(ByVal iter As Integer) As Boolean
            Dim result As New Boolean()

            result = False
            Return result
        End Function
    End Class

    Public Class LMFit

        Public Enum FitType
            Pvap = 0
            Cp = 1
            LiqVisc = 2
            HVap = 3
            LiqDens = 4
            SecondDegreePoly = 5
        End Enum

        Private _x, _y As Double()
        Private sum As Double
        Private its As Integer = 0

        Public Function GetCoeffs(ByVal x As Double(), ByVal y As Double(), ByVal inest As Double(), ByVal fittype As FitType, _
                                ByVal epsg As Double, ByVal epsf As Double, ByVal epsx As Double, ByVal maxits As Integer) As Object

            Dim lmsolve As New MathEx.LM.levenbergmarquardt
            Select Case fittype
                Case LMFit.FitType.Pvap
                    lmsolve.DefineFuncGradDelegate(AddressOf fvpvap)
                Case LMFit.FitType.Cp
                    lmsolve.DefineFuncGradDelegate(AddressOf fvcp)
                Case LMFit.FitType.LiqVisc
                    lmsolve.DefineFuncGradDelegate(AddressOf fvlvisc)
                Case LMFit.FitType.HVap
                    lmsolve.DefineFuncGradDelegate(AddressOf fvhvap)
                Case LMFit.FitType.LiqDens
                    lmsolve.DefineFuncGradDelegate(AddressOf fvliqdens)
                Case LMFit.FitType.SecondDegreePoly
                    lmsolve.DefineFuncGradDelegate(AddressOf fvsdp)
            End Select

            Dim newc(UBound(inest) + 1) As Double
            Dim i As Integer = 1
            Do
                newc(i) = inest(i - 1)
                i = i + 1
            Loop Until i = UBound(inest) + 2

            Me._x = x
            Me._y = y

            Dim info As Integer = 56

            its = 0
            lmsolve.levenbergmarquardtminimize(inest.Length, _x.Length, newc, epsg, epsf, epsx, maxits, info)

            Dim coeffs(UBound(inest)) As Double

            i = 0
            Do
                coeffs(i) = newc(i + 1)
                i = i + 1
            Loop Until i = UBound(inest) + 1

            Return New Object() {coeffs, info, sum, its}

        End Function

        Public Sub fvpvap(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            sum = 0.0#
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (Math.Exp(x(1) + x(2) / _x(i - 1) + x(3) * Math.Log(_x(i - 1)) + x(4) * _x(i - 1) ^ x(5)))
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                Dim fval As Double = 0
                i = 1
                Do
                    'Math.Exp(A + B / T + C * Math.Log(T) + D * T ^ E)
                    fval = (Math.Exp(x(1) + x(2) / _x(i - 1) + x(3) * Math.Log(_x(i - 1)) + x(4) * _x(i - 1) ^ x(5)))
                    fjac(i, 1) = fval
                    fjac(i, 2) = fval * 1 / _x(i - 1)
                    fjac(i, 3) = fval * Math.Log(_x(i - 1))
                    fjac(i, 4) = fval * _x(i - 1) ^ x(5)
                    fjac(i, 5) = fval * x(5) * _x(i - 1) ^ x(5) * Math.Log(_x(i - 1))
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

        Public Sub fvcp(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            'A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4
            sum = 0.0#
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (x(1) + x(2) * _x(i - 1) + x(3) * _x(i - 1) ^ 2 + x(4) * _x(i - 1) ^ 3 + x(5) * _x(i - 1) ^ 4)
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                i = 1
                Do
                    'A + B * T + C * T ^ 2 + D * T ^ 3 + E * T ^ 4
                    fjac(i, 1) = 1
                    fjac(i, 2) = _x(i - 1)
                    fjac(i, 3) = _x(i - 1) ^ 2
                    fjac(i, 4) = _x(i - 1) ^ 3
                    fjac(i, 5) = _x(i - 1) ^ 4
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

        Public Sub fvlvisc(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            sum = 0
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (Math.Exp(x(1) + x(2) / _x(i - 1) + x(3) * Math.Log(_x(i - 1)) + x(4) * _x(i - 1) ^ x(5)))
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                Dim fval As Double = 0
                i = 1
                Do
                    'Math.Exp(A + B / T + C * Math.Log(T) + D * T ^ E)
                    fval = (Math.Exp(x(1) + x(2) / _x(i - 1) + x(3) * Math.Log(_x(i - 1)) + x(4) * _x(i - 1) ^ x(5)))
                    fjac(i, 1) = fval
                    fjac(i, 2) = fval * 1 / _x(i - 1)
                    fjac(i, 3) = fval * Math.Log(_x(i - 1))
                    fjac(i, 4) = fval * _x(i - 1) ^ x(5)
                    fjac(i, 5) = fval * x(5) * _x(i - 1) ^ x(5) * Math.Log(_x(i - 1))
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

        Public Sub fvhvap(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            'A * (1 - Tr) ^ (B + C * Tr + D * Tr ^ 2)
            sum = 0.0#
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (x(1) * (1 - _x(i - 1)) ^ (x(2) + x(3) * _x(i - 1) + x(4) * _x(i - 1) ^ 2))
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                i = 1
                Do
                    Dim fval As Double = 0
                    'A * (1 - Tr) ^ (B + C * Tr + D * Tr ^ 2)
                    fval = (x(1) * (1 - _x(i - 1)) ^ (x(2) + x(3) * _x(i - 1) + x(4) * _x(i - 1) ^ 2))
                    fjac(i, 1) = fval
                    fjac(i, 2) = fval
                    fjac(i, 3) = fval * _x(i - 1)
                    fjac(i, 4) = fval * _x(i - 1) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

        Public Sub fvliqdens(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            'a / b^[1 + (1 - t/c)^d]
            sum = 0.0#
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (x(1) / x(2) ^ (1 + (1 - _x(i - 1) / x(3)) ^ x(4)))
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                i = 1
                Do
                    'a / b^[1 + (1 - t/c)^d]
                    fjac(i, 1) = 1 / x(2) ^ (1 + (1 - _x(i - 1) / x(3)) ^ x(4))
                    fjac(i, 2) = -(x(1) * (x(3) - _x(i - 1)) ^ x(4) + x(1) * x(3) ^ x(4)) / (x(2) ^ (((x(3) - _x(i - 1)) ^ x(4) + 2 * x(3) ^ x(4)) / x(3) ^ x(4)) * x(3) ^ x(4))
                    fjac(i, 3) = x(1) * Log(x(2)) * x(4) * (x(3) - _x(i - 1)) ^ x(4) * _x(i - 1) / (x(2) ^ (((x(3) - _x(i - 1)) ^ x(4) + x(3) ^ x(4)) / x(3) ^ x(4)) * x(3) ^ (x(4) + 1) * _x(i - 1) - x(2) ^ (((x(3) - _x(i - 1)) ^ x(4) + x(3) ^ x(4)) / x(3) ^ x(4)) * x(3) ^ (x(4) + 2))
                    fjac(i, 4) = -(x(1) * Log(x(2)) * Log(x(3) - _x(i - 1)) - x(1) * Log(x(2)) * Log(x(3))) * (x(3) - _x(i - 1)) ^ x(4) / (x(2) ^ (((x(3) - _x(i - 1)) ^ x(4) + x(3) ^ x(4)) / x(3) ^ x(4)) * x(3) ^ x(4))
                    fjac(i, 5) = 0
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

        Public Sub fvsdp(ByRef x As Double(), ByRef fvec As Double(), ByRef fjac As Double(,), ByRef iflag As Integer)

            If Double.IsNaN(x(1)) Or Double.IsNegativeInfinity(x(1)) Or Double.IsPositiveInfinity(x(1)) Then iflag = -1
            If Double.IsNaN(fvec(1)) Or Double.IsNegativeInfinity(fvec(1)) Or Double.IsPositiveInfinity(fvec(1)) Then iflag = -1

            'A + B * T + C * T ^ 2
            sum = 0.0#
            Dim i As Integer
            If iflag = 1 Then
                i = 1
                Do
                    fvec(i) = -_y(i - 1) + (x(1) + x(2) * _x(i - 1) + x(3) * _x(i - 1) ^ 2)
                    sum += (fvec(i)) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            ElseIf iflag = 2 Then
                i = 1
                Do
                    'A + B * T + C * T ^ 2
                    fjac(i, 1) = 1
                    fjac(i, 2) = _x(i - 1)
                    fjac(i, 3) = _x(i - 1) ^ 2
                    i = i + 1
                Loop Until i = UBound(_y) + 2
            End If

            its += 1

        End Sub

    End Class


End Namespace