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
|
//******************************************************************************
//
// File: NonLinearLeastSquares.java
// Package: edu.rit.numeric
// Unit: Class edu.rit.numeric.NonLinearLeastSquares
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.numeric;
/**
* Class NonLinearLeastSquares provides a method for minimizing the sum of the
* squares of a series of nonlinear functions. There are <I>M</I> functions,
* each of which has <I>N</I> inputs. These functions are represented by an
* object that implements interface {@linkplain VectorFunction}. The
* <TT>solve()</TT> method finds a vector <B>x</B> such that
* Σ<SUB><I>i</I></SUB> [<I>f</I><SUB><I>i</I></SUB>(<B>x</B>)]<SUP>2</SUP>
* is minimized. The inputs to and outputs from the <TT>solve()</TT> method are
* stored in the fields of an instance of class NonLinearLeastSquares. The
* Levenberg-Marquardt method is used to find the solution.
* <P>
* The Java code is a translation of the Fortran subroutine <TT>LMDER</TT> from
* the MINPACK library. MINPACK was developed by Jorge Moré, Burt Garbow,
* and Ken Hillstrom at Argonne National Laboratory. For further information,
* see
* <A HREF="http://www.netlib.org/minpack/" TARGET="_top">http://www.netlib.org/minpack/</A>.
*
* @author Alan Kaminsky
* @version 10-Jun-2008
*/
public class NonLinearLeastSquares
{
// Exported data members.
/**
* The nonlinear functions <I>f</I><SUB><I>i</I></SUB>(<B>x</B>) to be
* minimized.
*/
public final VectorFunction fcn;
/**
* The number of functions.
*/
public final int M;
/**
* The number of arguments for each function.
*/
public final int N;
/**
* The <I>N</I>-element <B>x</B> vector for the least squares problem. On
* input to the <TT>solve()</TT> method, <TT>x</TT> contains an initial
* estimate of the solution vector. On output from the <TT>solve()</TT>
* method, <TT>x</TT> contains the final solution vector.
*/
public final double[] x;
/**
* The <I>M</I>-element result vector. On output from the <TT>solve()</TT>
* method, for <I>i</I> = 0 to <I>M</I>−1,
* <TT>fvec</TT><SUB><I>i</I></SUB> = <I>f</I><SUB><I>i</I></SUB>(<B>x</B>),
* where <B>x</B> is the final solution vector.
*/
public final double[] fvec;
/**
* The <I>M</I>×<I>N</I>-element Jacobian matrix. On output from the
* <TT>solve()</TT> method, the upper <I>N</I>×<I>N</I> submatrix of
* <TT>fjac</TT> contains an upper triangular matrix <B>R</B> with diagonal
* elements of nonincreasing magnitude such that
* <P>
* <CENTER>
* <B>P</B><SUP>T</SUP> (<B>J</B><SUP>T</SUP> <B>J</B>) <B>P</B> = <B>R</B><SUP>T</SUP> <B>R</B>
* </CENTER>
* <P>
* where <B>P</B> is a permutation matrix and <B>J</B> is the final
* calculated Jacobian. Column <I>j</I> of <B>P</B> is column
* <TT>ipvt[j]</TT> (see below) of the identity matrix. The lower
* trapezoidal part of <TT>fjac</TT> contains information generated during
* the computation of <B>R</B>.
* <P>
* <TT>fjac</TT> is used to calculate the covariance matrix of the solution.
* This calculation is not yet implemented.
*/
public final double[][] fjac;
/**
* The <I>N</I>-element permutation vector for <TT>fjac</TT>. On output from
* the <TT>solve()</TT> method, <TT>ipvt</TT> defines a permutation matrix
* <B>P</B> such that <B>JP</B> = <B>QR</B>, where <B>J</B> is the final
* calculated Jacobian, <B>Q</B> is orthogonal (not stored), and <B>R</B> is
* upper triangular with diagonal elements of nonincreasing magnitude.
* Column <I>j</I> of <B>P</B> is column <TT>ipvt[j]</TT> of the identity
* matrix.
*/
public final int[] ipvt;
/**
* Tolerance. An input to the <TT>solve()</TT> method. Must be >= 0.
* Termination occurs when the algorithm estimates either that the relative
* error in the sum of squares is at most <TT>tol</TT> or that the relative
* error between <TT>x</TT> and the solution is at most <TT>tol</TT>. The
* default tolerance is 1×10<SUP>−6</SUP>.
*/
public double tol = 1.0e-6;
/**
* Information about the outcome. An output of the <TT>solve()</TT> method.
* The possible values are:
* <UL>
* <LI>
* 0 -- Improper input parameters. Also, an IllegalArgumentException is
* thrown.
* <LI>
* 1 -- Algorithm estimates that the relative error in the sum of squares is
* at most <TT>tol</TT>.
* <LI>
* 2 -- Algorithm estimates that the relative error between <TT>x</TT> and
* the solution is at most <TT>tol</TT>.
* <LI>
* 3 -- Conditions for <TT>info</TT> = 1 and <TT>info</TT> = 2 both hold.
* <LI>
* 4 -- <TT>fvec</TT> is orthogonal to the columns of the Jacobian to
* machine precision.
* <LI>
* 5 -- Number of function evaluations has reached 100(<I>N</I>+1). Also, a
* TooManyIterationsException is thrown.
* <LI>
* 6 -- <TT>tol</TT> is too small. No further reduction in the sum of
* squares is possible.
* <LI>
* 7 -- <TT>tol</TT> is too small. No further improvement in the approximate
* solution <TT>x</TT> is possible.
* </UL>
*/
public int info;
/**
* Debug printout flag. An input to the <TT>solve()</TT> method. If
* <TT>nprint</TT> > 0, the <TT>subclassDebug()</TT> method is called at
* the beginning of the first iteration and every <TT>nprint</TT> iterations
* thereafter and immediately prior to return. If <TT>nprint</TT> <= 0,
* the <TT>subclassDebug()</TT> method is not called. The default setting is
* 0.
*/
public int nprint = 0;
// Working variables.
private double[] diag;
private double[] qtf;
private double[] wa1;
private double[] wa2;
private double[] wa3;
private double[] wa4;
private int nfev;
private int njev;
// Hidden constants.
// Machine epsilon.
private static final double dpmpar_1 = 2.22044604926e-16;
// Smallest positive nonzero double value.
private static final double dpmpar_2 = 2.22507385852e-308;
// Largest positive double value.
private static final double dpmpar_3 = 1.79769313485e+308;
// Used in the enorm() method.
private static final double rdwarf = 3.834e-20;
private static final double rgiant = 1.304e+19;
// Exported constructors.
/**
* Construct a new nonlinear least squares problem for the given functions.
* Field <TT>fcn</TT> is set to <TT>theFunction</TT>. Fields <TT>M</TT> and
* <TT>N</TT> are set by calling the <TT>resultLength()</TT> and
* <TT>argumentLength()</TT> methods of <TT>theFunction</TT>. The vector and
* matrix fields <TT>x</TT>, <TT>fvec</TT>, <TT>fjac</TT>, and <TT>ipvt</TT>
* are allocated with the proper sizes but are not filled in.
*
* @param theFunction Nonlinear functions to be minimized.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <TT>theFunction</TT> is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if <I>M</I> <= 0, <I>N</I> <= 0,
* or <I>M</I> < <I>N</I>.
*/
public NonLinearLeastSquares
(VectorFunction theFunction)
{
fcn = theFunction;
M = theFunction.resultLength();
N = theFunction.argumentLength();
if (M <= 0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares(): M (= "+M+") <= 0, illegal");
}
if (N <= 0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares(): N (= "+N+") <= 0, illegal");
}
if (M < N)
{
throw new IllegalArgumentException
("NonLinearLeastSquares(): M (= "+M+") < N (= "+N+
"), illegal");
}
x = new double [N];
fvec = new double [M];
fjac = new double [M] [N];
ipvt = new int [N];
diag = new double [N];
qtf = new double [N];
wa1 = new double [N];
wa2 = new double [N];
wa3 = new double [N];
wa4 = new double [M];
}
// Exported operations.
/**
* Solve this nonlinear least squares minimization problem. The
* <TT>solve()</TT> method finds a vector <B>x</B> such that
* Σ<SUB><I>i</I></SUB> [<I>f</I><SUB><I>i</I></SUB>(<B>x</B>)]<SUP>2</SUP>
* is minimized. On input, the field <TT>x</TT> must be filled in with an
* initial estimate of the solution vector, and the field <TT>tol</TT> must
* be set to the desired tolerance. On output, the other fields are filled
* in with the solution as explained in the documentation for each field.
*
* @exception IllegalArgumentException
* (unchecked exception) Thrown if <TT>tol</TT> <= 0.
* @exception TooManyIterationsException
* (unchecked exception) Thrown if too many iterations occurred without
* finding a minimum (100(<I>N</I>+1) iterations).
*/
public void solve()
{
info = 0;
if (tol < 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.solve(): tol (= "+tol+") < 0, illegal");
}
lmder
(/*ftol */ tol,
/*xtol */ tol,
/*gtol */ 0.0,
/*maxfev*/ 100*(N+1),
/*mode */ 1,
/*factor*/ 100.0);
if (info == 5)
{
throw new TooManyIterationsException
("NonLinearLeastSquares.solve(): Too many iterations");
}
else if (info == 8)
{
info = 4;
}
}
// Hidden operations.
/**
* Levenberg-Marquardt algorithm with user-supplied derivatives.
* <P>
* The purpose of lmder() 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 subroutine which
* calculates the functions and the Jacobian.
*
* @param ftol
* 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.
* @param xtol
* 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.
* @param gtol
* 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.
* @param maxfev
* A positive integer input variable. Termination occurs when the number
* of calls to fcn.f() has reached maxfev.
* @param mode
* 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.
* @param factor
* 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 (0.1,100.0). 100.0 is a generally
* recommended value.
*/
private void lmder
(double ftol,
double xtol,
double gtol,
int maxfev,
int mode,
double factor)
{
int iter, l;
double actred, delta, dirder, epsmch, fnorm, fnorm1, gnorm, par, pnorm;
double prered, ratio, sum, temp, temp1, temp2, xnorm;
// epsmch is the machine precision.
epsmch = dpmpar_1;
info = 0;
nfev = 0;
njev = 0;
delta = 0.0;
xnorm = 0.0;
// Check the input parameters for errors.
if (ftol < 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): ftol (= "+ftol+
") < 0, illegal");
}
if (xtol < 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): xtol (= "+xtol+
") < 0, illegal");
}
if (gtol < 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): gtol (= "+gtol+
") < 0, illegal");
}
if (maxfev <= 0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): maxfev (= "+maxfev+
") <= 0, illegal");
}
if (factor <= 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): factor (= "+factor+
") <= 0, illegal");
}
if (mode == 2)
{
for (int j = 0; j < N; ++ j)
{
if (diag[j] <= 0.0)
{
throw new IllegalArgumentException
("NonLinearLeastSquares.lmder(): diag["+j+"] (= "+
diag[j]+") <= 0, illegal");
}
}
}
// Evaluate the function at the starting point and calculate its norm.
fcn.f(x,fvec);
++ nfev;
fnorm = enorm(M,fvec);
// Initialize Levenberg-Marquardt parameter and iteration counter.
par = 0.0;
iter = 1;
// Beginning of the outer loop.
outerloop: for (;;)
{
// Calculate the Jacobian matrix.
fcn.df(x,fjac);
++ njev;
// If requested, print debug information.
if (nprint > 0 && ((iter-1) % nprint) == 0) subclassDebug (iter);
// Compute the QR factorization of the Jacobian.
qrfac(M,N,fjac,true,ipvt,wa1,wa2,wa3);
if (iter == 1)
{
// On the first iteration and if mode is 1, scale according to
// the norms of the columns of the initial Jacobian.
if (mode != 2)
{
for (int j = 1; j < N; ++ j)
{
diag[j] = wa2[j];
if (wa2[j] == 0.0) diag[j] = 1.0;
}
}
// On the first iteration, calculate the norm of the scaled x
// and initialize the step bound delta.
for (int j = 0; j < N; ++ j)
{
wa3[j] = diag[j]*x[j];
}
xnorm = enorm(N,wa3);
delta = factor*xnorm;
if (delta == 0.0) delta = factor;
}
// Form (q transpose)*fvec and store the first N components in qtf.
for (int i = 0; i < M; ++ i)
{
wa4[i] = fvec[i];
}
for (int j = 0; j < N; ++ j)
{
if (fjac[j][j] != 0.0)
{
sum = 0.0;
for (int i = j; i < M; ++ i)
{
sum += fjac[i][j]*wa4[i];
}
temp = -sum/fjac[j][j];
for (int i = j; i < M; ++ i)
{
wa4[i] += fjac[i][j]*temp;
}
}
fjac[j][j] = wa1[j];
qtf[j] = wa4[j];
}
// Compute the norm of the scaled gradient.
gnorm = 0.0;
if (fnorm != 0.0)
{
for (int j = 0; j < N; ++ j)
{
l = ipvt[j];
if (wa2[l] != 0.0)
{
sum = 0.0;
for (int i = 0; i <= j; ++ i)
{
sum += fjac[i][j]*(qtf[i]/fnorm);
}
gnorm = Math.max(gnorm,Math.abs(sum/wa2[l]));
}
}
}
// Test for convergence of the gradient norm.
if (gnorm <= gtol)
{
info = 4;
break outerloop;
}
// Rescale if necessary.
if (mode != 2)
{
for (int j = 0; j < N; ++ j)
{
diag[j] = Math.max(diag[j],wa2[j]);
}
}
// Beginning of the inner loop.
innerloop: for (;;)
{
// Determine the Levenberg-Marquardt parameter.
par = lmpar(N,fjac,ipvt,diag,qtf,delta,par,wa1,wa2,wa3,wa4);
// Store the direction p and x + p. Calculate the norm of p.
for (int j = 0; j < N; ++ j)
{
wa1[j] = -wa1[j];
wa2[j] = x[j] + wa1[j];
wa3[j] = diag[j]*wa1[j];
}
pnorm = enorm(N,wa3);
// On the first iteration, adjust the initial step bound.
if (iter == 1) delta = Math.min(delta,pnorm);
// Evaluate the function at x + p and calculate its norm.
fcn.f(wa2,wa4);
++ nfev;
fnorm1 = enorm(M,wa4);
// Compute the scaled actual reduction.
actred = -1.0;
if (0.1*fnorm1 < fnorm) actred = 1.0 - sqr(fnorm1/fnorm);
// Compute the scaled predicted reduction and the scaled
// directional derivative.
for (int j = 0; j < N; ++ j)
{
wa3[j] = 0.0;
l = ipvt[j];
temp = wa1[l];
for (int i = 0; i <= j; ++ i)
{
wa3[i] += fjac[i][j]*temp;
}
}
temp1 = enorm(N,wa3)/fnorm;
temp2 = (Math.sqrt(par)*pnorm)/fnorm;
prered = sqr(temp1) + sqr(temp2)*2.0;
dirder = -(sqr(temp1) + sqr(temp2));
// Compute the ratio of the actual to the predicted reduction.
ratio = 0.0;
if (prered != 0.0) ratio = actred/prered;
// Update the step bound.
if (ratio <= 0.25)
{
temp =
actred >= 0.0 ?
0.5 :
0.5*dirder/(dirder + 0.5*actred);
if (0.1*fnorm1 >= fnorm || temp < 0.1) temp = 0.1;
delta = temp*Math.min(delta,pnorm*10.0);
par = par/temp;
}
else if (par == 0.0 || ratio >= 0.75)
{
delta = pnorm*2.0;
par = 0.5*par;
}
// Test for successful iteration.
if (ratio >= 0.0001)
{
// Successful iteration. Update x, fvec, and their norms.
for (int j = 0; j < N; ++ j)
{
x[j] = wa2[j];
wa2[j] = diag[j]*x[j];
}
for (int i = 0; i < M; ++ i)
{
fvec[i] = wa4[i];
}
xnorm = enorm(N,wa2);
fnorm = fnorm1;
++ iter;
}
// Tests for convergence.
boolean fconv =
Math.abs(actred) <= ftol && prered <= ftol && ratio <= 2.0;
boolean xconv =
delta <= xtol*xnorm;
info = (fconv ? 1 : 0) + (xconv ? 2 : 0);
if (info != 0) break outerloop;
// Tests for termination and stringent tolerances.
if (nfev >= maxfev) info = 5;
if (Math.abs(actred) <= epsmch &&
prered <= epsmch && ratio <= 2.0) info = 6;
if (delta <= epsmch*xnorm) info = 7;
if (gnorm <= epsmch) info = 8;
if (info != 0) break outerloop;
// End of the inner loop. Repeat if iteration unsuccessful.
if (ratio >= 0.0001) break innerloop;
}
// End of the outer loop.
}
// Finished.
if (nprint > 0) subclassDebug (iter);
}
/**
* Calculate the Levenberg-Marquardt parameter.
* <P>
* Given an M by N matrix a, an N by N nonsingular diagonal matrix d, an
* M-vector b, and a positive number delta, the problem is to determine a
* value for the parameter par such that if x solves the system
* <PRE>
* a*x = b , sqrt(par)*d*x = 0 ,
* </PRE>
* in the least squares sense, and dxnorm is the euclidean norm of d*x, then
* either par is zero and
* <PRE>
* (dxnorm-delta) .le. 0.1*delta ,
* </PRE>
* or par is positive and
* <PRE>
* abs(dxnorm-delta) .le. 0.1*delta .
* </PRE>
* <P>
* This subroutine completes the solution of the problem if it is provided
* with the necessary information from the QR factorization, with column
* pivoting, of a. That is, if a*p = q*r, where p is a permutation matrix, q
* has orthogonal columns, and r is an upper triangular matrix with diagonal
* elements of nonincreasing magnitude, then lmpar() expects the full upper
* triangle of r, the permutation matrix p, and the first n components of (q
* transpose)*b. On output lmpar() also provides an upper triangular matrix
* s such that
* <PRE>
* t t t
* p *(a *a + par*d*d)*p = s *s .
* </PRE>
* s is employed within lmpar and may be of separate interest.
* <P>
* Only a few iterations are generally needed for convergence of the
* algorithm. If, however, the limit of 10 iterations is reached, then the
* output par will contain the best value obtained so far.
*
* @param n
* A positive integer input variable set to the order of r.
* @param r
* An n by n array. On input the full upper triangle must contain the
* full upper triangle of the matrix r. On output the full upper
* triangle is unaltered, and the strict lower triangle contains the
* strict upper triangle (transposed) of the upper triangular matrix s.
* @param ipvt
* An integer input array of length n which defines the permutation
* matrix p such that a*p = q*r. Column j of p is column ipvt[j] of the
* identity matrix.
* @param diag
* An input array of length n which must contain the diagonal elements
* of the matrix d.
* @param qtb
* An input array of length n which must contain the first n elements of
* the vector (q transpose)*b.
* @param delta
* A positive input variable which specifies an upper bound on the
* Euclidean norm of d*x.
* @param par
* A nonnegative variable. On input par contains an initial estimate of
* the levenberg-marquardt parameter. The return value of lmpar() is the
* final estimate for par.
* @param x
* An output array of length n which contains the least squares solution
* of the system a*x = b, sqrt(par)*d*x = 0, for the returned par.
* @param sdiag
* An output array of length n which contains the diagonal elements of
* the upper triangular matrix s.
* @param wa1
* A work array of length n.
* @param wa2
* A work array of length n.
*
* @return Final estimate of the Levenberg-Marquardt parameter par.
*/
private static double lmpar
(int n,
double[][] r,
int[] ipvt,
double[] diag,
double[] qtb,
double delta,
double par,
double[] x,
double[] sdiag,
double[] wa1,
double[] wa2)
{
int iter, nsing;
double dxnorm, dwarf, fp, gnorm, parc, parl, paru, sum, temp;
// dwarf is the smallest positive magnitude.
dwarf = dpmpar_2;
// Compute and store in x the Gauss-Newton direction. If the Jacobian is
// rank-deficient, obtain a least-squares solution.
nsing = n;
for (int j = 0; j < n; ++ j)
{
wa1[j] = qtb[j];
if (r[j][j] == 0.0 && nsing == n) nsing = j;
if (nsing < n) wa1[j] = 0.0;
}
for (int j = nsing-1; j >= 0; -- j)
{
wa1[j] = wa1[j]/r[j][j];
temp = wa1[j];
for (int i = 0; i < j; ++ i)
{
wa1[i] -= r[i][j]*temp;
}
}
for (int j = 0; j < n; ++ j)
{
x[ipvt[j]] = wa1[j];
}
// Initialize the iteration counter. Evaluate the function at the
// origin, and test for acceptance of the Gauss-Newton direction.
iter = 0;
for (int j = 0; j < n; ++ j)
{
wa2[j] = diag[j]*x[j];
}
dxnorm = enorm(n,wa2);
fp = dxnorm - delta;
if (fp <= 0.1*delta) return 0.0;
// 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.0;
if (nsing == n)
{
for (int j = 0; j < n; ++ j)
{
int l = ipvt[j];
wa1[j] = diag[l]*(wa2[l]/dxnorm);
}
for (int j = 0; j < n; ++ j)
{
sum = 0.0;
for (int i = 0; i < j; ++ i)
{
sum += r[i][j]*wa1[i];
}
wa1[j] = (wa1[j] - sum)/r[j][j];
}
temp = enorm(n,wa1);
parl = ((fp/delta)/temp)/temp;
}
// Calculate an upper bound, paru, for the zero of the function.
for (int j = 0; j < n; ++ j)
{
sum = 0.0;
for (int i = 0; i <= j; ++ i)
{
sum += r[i][j]*qtb[i];
}
wa1[j] = sum/diag[ipvt[j]];
}
gnorm = enorm(n,wa1);
paru = gnorm/delta;
if (paru == 0.0) paru = dwarf/Math.min(delta,0.1);
// 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.0) par = gnorm/dxnorm;
iterloop: for (;;)
{
// Beginning of an iteration.
++ iter;
// Evaluate the function at the current value of par.
if (par == 0.0) par = Math.max(dwarf,0.001*paru);
temp = Math.sqrt(par);
for (int j = 0; j < n; ++ j)
{
wa1[j] = temp*diag[j];
}
qrsolv(n,r,ipvt,wa1,qtb,x,sdiag,wa2);
for (int j = 0; j < n; ++ j)
{
wa2[j] = diag[j]*x[j];
}
dxnorm = enorm(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 (Math.abs(fp) <= 0.1*delta ||
(parl == 0.0 && fp <= temp && temp < 0.0) ||
iter == 10)
{
break iterloop;
}
// Compute the Newton correction.
for (int j = 0; j < n; ++ j)
{
int l = ipvt[j];
wa1[j] = diag[l]*(wa2[l]/dxnorm);
}
for (int j = 0; j < n; ++ j)
{
wa1[j] /= sdiag[j];
temp = wa1[j];
for (int i = j+1; i < n; ++ i)
{
wa1[i] -= r[i][j]*temp;
}
}
temp = enorm(n,wa1);
parc = ((fp/delta)/temp)/temp;
// Depending on the sign of the function, update parl or paru.
if (fp > 0.0) parl = Math.max(parl,par);
if (fp < 0.0) paru = Math.min(paru,par);
// Compute an improved estimate for par.
par = Math.max(parl,par+parc);
// End of an iteration.
}
// Finished.
return par;
}
/**
* Compute the QR factorization of a matrix.
* <P>
* This subroutine uses Householder transformations with column pivoting
* (optional) to compute a QR factorization of the m by n matrix a. That is,
* qrfac() determines an orthogonal matrix q, a permutation matrix p, and an
* upper trapezoidal matrix r with diagonal elements of nonincreasing
* magnitude, such that a*p = q*r. The Householder transformation for column
* k, k = 1,2,...,min(m,n), is of the form
* <PRE>
* t
* i - (1/u(k))*u*u
* </PRE>
* where u has zeros in the first k-1 positions. The form of this
* transformation and the method of pivoting first appeared in the
* corresponding LINPACK subroutine.
*
* @param m
* A positive integer input variable set to the number of rows of a.
* @param n
* A positive integer input variable set to the number of columns of a.
* @param a
* An m by n array. On input a contains the matrix for which the QR
* factorization is to be computed. On output the strict upper
* trapezoidal part of a contains the strict upper trapezoidal part of
* r, and the lower trapezoidal part of a contains a factored form of q
* (the non-trivial elements of the u vectors described above).
* @param pivot
* A Boolean input variable. If pivot is set true, then column pivoting
* is enforced. If pivot is set false, then no column pivoting is done.
* @param ipvt
* An integer output array of length n. ipvt defines the permutation
* matrix p such that a*p = q*r. Column j of p is column ipvt(j) of the
* identity matrix. If pivot is false, ipvt is not referenced.
* @param rdiag
* An output array of length n which contains the diagonal elements of
* r.
* @param acnorm
* An output array of length n which contains the norms of the
* corresponding columns of the input matrix a. If this information is
* not needed, then acnorm can coincide with rdiag.
* @param wa
* A work array of length n. If pivot is false, then wa can coincide
* with rdiag.
*/
private static void qrfac
(int m,
int n,
double[][] a,
boolean pivot,
int[] ipvt,
double[] rdiag,
double[] acnorm,
double[] wa)
{
int kmax, minmn, itemp;
double ajnorm, epsmch, sum, temp;
// epsmch is the machine precision.
epsmch = dpmpar_1;
// Compute the initial column norms and initialize several arrays.
for (int j = 0; j < n; ++ j)
{
acnorm[j] = enorm(m,a,0,j);
rdiag[j] = acnorm[j];
wa[j] = rdiag[j];
if (pivot) ipvt[j] = j;
}
// Reduce a to r with Householder transformations.
minmn = Math.min(m,n);
for (int j = 0; j < minmn; ++ j)
{
if (pivot)
{
// Bring the column of largest norm into the pivot position.
kmax = j;
for (int k = j; k < n; ++ k)
{
if (rdiag[k] > rdiag[kmax]) kmax = k;
}
if (kmax != j)
{
for (int i = 0; i < m; ++ i)
{
temp = a[i][j];
a[i][j] = a[i][kmax];
a[i][kmax] = temp;
}
rdiag[kmax] = rdiag[j];
wa[kmax] = wa[j];
itemp = ipvt[j];
ipvt[j] = ipvt[kmax];
ipvt[kmax] = itemp;
}
}
// Compute the Householder transformation to reduce the j-th column
// of a to a multiple of the j-th unit vector.
ajnorm = enorm(m,a,j,j);
if (ajnorm != 0.0)
{
if (a[j][j] < 0.0) ajnorm = -ajnorm;
for (int i = j; i < m; ++ i)
{
a[i][j] /= ajnorm;
}
a[j][j] += 1.0;
// Apply the transformation to the remaining columns and update
// the norms.
for (int k = j+1; k < n; ++ k)
{
sum = 0.0;
for (int i = j; i < m; ++ i)
{
sum += a[i][j]*a[i][k];
}
temp = sum/a[j][j];
for (int i = j; i < m; ++ i)
{
a[i][k] -= temp*a[i][j];
}
if (pivot && rdiag[k] != 0.0)
{
temp = a[j][k]/rdiag[k];
rdiag[k] *= Math.sqrt(Math.max(0.0,1.0-sqr(temp)));
if (0.05*sqr(rdiag[k]/wa[k]) <= epsmch)
{
rdiag[k] = enorm(m,a,j+1,k);
wa[k] = rdiag[k];
}
}
}
}
rdiag[j] = -ajnorm;
}
}
/**
* Solve a linear system of equations using a QR factorization.
* <P>
* Given an m by n matrix a, an n by n diagonal matrix d, and an m-vector b,
* the problem is to determine an x which solves the system
* <PRE>
* a*x = b , d*x = 0 ,
* </PRE>
* in the least squares sense.
* <P>
* This subroutine completes the solution of the problem if it is provided
* with the necessary information from the QR factorization, with column
* pivoting, of a. That is, if a*p = q*r, where p is a permutation matrix, q
* has orthogonal columns, and r is an upper triangular matrix with diagonal
* elements of nonincreasing magnitude, then qrsolv() expects the full upper
* triangle of r, the permutation matrix p, and the first n components of (q
* transpose)*b. The system a*x = b, d*x = 0, is then equivalent to
* <PRE>
* t t
* r*z = q *b , p *d*p*z = 0 ,
* </PRE>
* where x = p*z. if this system does not have full rank, then a least
* squares solution is obtained. On output qrsolv() also provides an upper
* triangular matrix s such that
* <PRE>
* t t t
* p *(a *a + d*d)*p = s *s .
* </PRE>
* s is computed within qrsolv() and may be of separate interest.
*
* @param n
* A positive integer input variable set to the order of r.
* @param r
* An n by n array. On input the full upper triangle must contain the
* full upper triangle of the matrix r. On output the full upper
* triangle is unaltered, and the strict lower triangle contains the
* strict upper triangle (transposed) of the upper triangular matrix s.
* @param ipvt
* An integer input array of length n which defines the permutation
* matrix p such that a*p = q*r. Column j of p is column ipvt(j) of the
* identity matrix.
* @param diag
* An input array of length n which must contain the diagonal elements
* of the matrix d.
* @param qtb
* An input array of length n which must contain the first n elements of
* the vector (q transpose)*b.
* @param x
* An output array of length n which contains the least squares solution
* of the system a*x = b, d*x = 0.
* @param sdiag
* An output array of length n which contains the diagonal elements of
* the upper triangular matrix s.
* @param wa
* A work array of length n.
*/
private static void qrsolv
(int n,
double[][] r,
int[] ipvt,
double[] diag,
double[] qtb,
double[] x,
double[] sdiag,
double[] wa)
{
int nsing;
double cos, cotan, qtbpj, sin, sum, tan, temp;
// Copy r and (q transpose)*b to preserve input and initialize s. In
// particular, save the diagonal elements of r in x.
for (int j = 0; j < n; ++ j)
{
for (int i = j; i < n; ++ i)
{
r[i][j] = r[j][i];
}
x[j] = r[j][j];
wa[j] = qtb[j];
}
// Eliminate the diagonal matrix d using a Givens rotation.
for (int j = 0; j < n; ++ j)
{
// Prepare the row of d to be eliminated, locating the diagonal
// element using p from the QR factorization.
int l = ipvt[j];
if (diag[l] != 0.0)
{
for (int k = j; k < n; ++ k)
{
sdiag[k] = 0.0;
}
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.0;
for (int k = j; k < n; ++ k)
{
// Determine a Givens rotation which eliminates the
// appropriate element in the current row of d.
if (sdiag[k] != 0.0)
{
if (Math.abs(r[k][k]) < Math.abs(sdiag[k]))
{
cotan = r[k][k]/sdiag[k];
sin = 0.5/Math.sqrt(0.25+0.25*sqr(cotan));
cos = sin*cotan;
}
else
{
tan = sdiag[k]/r[k][k];
cos = 0.5/Math.sqrt(0.25+0.25*sqr(tan));
sin = cos*tan;
}
// Compute the modified diagonal element of r and the
// modified element of ((q transpose)*b, 0).
r[k][k] = cos*r[k][k] + sin*sdiag[k];
temp = cos*wa[k] + sin*qtbpj;
qtbpj = -sin*wa[k] + cos*qtbpj;
wa[k] = temp;
// Accumulate the transformation in the row of s.
for (int i = k+1; i < n; ++ i)
{
temp = cos*r[i][k] + sin*sdiag[i];
sdiag[i] = -sin*r[i][k] + cos*sdiag[i];
r[i][k] = temp;
}
}
}
}
// Store the diagonal element of s and restore the corresponding
// diagonal element of r.
sdiag[j] = r[j][j];
r[j][j] = x[j];
}
// Solve the triangular system for z. If the system is singular, then
// obtain a least squares solution.
nsing = n;
for (int j = 0; j < n; ++ j)
{
if (sdiag[j] == 0.0 && nsing == n) nsing = j;
if (nsing < n) wa[j] = 0.0;
}
for (int j = nsing-1; j >= 0; -- j)
{
sum = 0.0;
for (int i = j+1; i < nsing; ++ i)
{
sum += r[i][j]*wa[i];
}
wa[j] = (wa[j] - sum)/sdiag[j];
}
// Permute the components of z back to components of x.
for (int j = 0; j < n; ++ j)
{
x[ipvt[j]] = wa[j];
}
}
/**
* Calculate the Euclidean norm of the given vector.
*
* The Euclidean norm is computed by accumulating the sum of squares in
* three different sums. The sums of squares for the small and large
* components are scaled so that no overflows occur. Non-destructive
* underflows are permitted. Underflows and overflows do not occur in the
* computation of the unscaled sum of squares for the intermediate
* components. The definitions of small, intermediate and large components
* depend on two constants, rdwarf and rgiant. The main restrictions on
* these constants are that rdwarf**2 not underflow and rgiant**2 not
* overflow. The constants given here are suitable for every known computer.
*
* @param n Number of elements in the vector.
* @param x Vector.
*
* @return Euclidean norm of <TT>x</TT>.
*/
private static double enorm
(int n,
double[] x)
{
double s1 = 0.0;
double s2 = 0.0;
double s3 = 0.0;
double x1max = 0.0;
double x3max = 0.0;
double agiant = rgiant/n;
for (int i = 0; i < n; ++ i)
{
double xabs = Math.abs (x[i]);
// Sum for large components.
if (xabs >= agiant)
{
if (xabs > x1max)
{
s1 = 1.0 + s1*sqr(x1max/xabs);
x1max = xabs;
}
else
{
s1 += sqr(xabs/x1max);
}
}
// Sum for small components.
else if (xabs <= rdwarf)
{
if (xabs > x3max)
{
s3 = 1.0 + s3*sqr(x3max/xabs);
x3max = xabs;
}
else
{
if (xabs != 0.0) s3 += sqr(xabs/x3max);
}
}
// Sum for intermediate components.
else
{
s2 += sqr(xabs);
}
}
// Calculation of norm.
if (s1 != 0.0)
{
return x1max*Math.sqrt(s1+(s2/x1max)/x1max);
}
else if (s2 != 0.0)
{
if (s2 >= x3max)
{
return Math.sqrt(s2*(1.0+(x3max/s2)*(x3max*s3)));
}
else
{
return Math.sqrt(x3max*((s2/x3max)+(x3max*s3)));
}
}
else
{
return x3max*Math.sqrt(s3);
}
}
/**
* Calculate the Euclidean norm of a portion of one column of the given
* matrix.
*
* @param n Number of rows in the matrix.
* @param x Matrix.
* @param r Initial row index.
* @param j Column index.
*
* @return Euclidean norm of rows <TT>r</TT> through <TT>n</TT>-1 of column
* <TT>j</TT> of matrix <TT>x</TT>.
*/
private static double enorm
(int n,
double[][] x,
int r,
int j)
{
double s1 = 0.0;
double s2 = 0.0;
double s3 = 0.0;
double x1max = 0.0;
double x3max = 0.0;
double agiant = rgiant/n;
for (int i = r; i < n; ++ i)
{
double xabs = Math.abs (x[i][j]);
// Sum for large components.
if (xabs >= agiant)
{
if (xabs > x1max)
{
s1 = 1.0 + s1*sqr(x1max/xabs);
x1max = xabs;
}
else
{
s1 += sqr(xabs/x1max);
}
}
// Sum for small components.
else if (xabs <= rdwarf)
{
if (xabs > x3max)
{
s3 = 1.0 + s3*sqr(x3max/xabs);
x3max = xabs;
}
else
{
if (xabs != 0.0) s3 += sqr(xabs/x3max);
}
}
// Sum for intermediate components.
else
{
s2 += sqr(xabs);
}
}
// Calculation of norm.
if (s1 != 0.0)
{
return x1max*Math.sqrt(s1+(s2/x1max)/x1max);
}
else if (s2 != 0.0)
{
if (s2 >= x3max)
{
return Math.sqrt(s2*(1.0+(x3max/s2)*(x3max*s3)));
}
else
{
return Math.sqrt(x3max*((s2/x3max)+(x3max*s3)));
}
}
else
{
return x3max*Math.sqrt(s3);
}
}
/**
* Returns the square of x.
*/
private static double sqr
(double x)
{
return x*x;
}
/**
* Print debugging information. If the <TT>nprint</TT> field is greater than
* zero, the <TT>subclassDebug()</TT> method is called at the beginning of
* the first iteration and every <TT>nprint</TT> iterations thereafter and
* immediately prior to return. The fields of this object contain the
* current state of the algorithm. The fields of this object must not be
* altered.
* <P>
* The default implementation of the <TT>subclassDebug()</TT> method does
* nothing. A subclass can override the <TT>subclassDebug()</TT> method to
* do something, such as print debugging information.
*
* @param iter Iteration number.
*/
protected void subclassDebug
(int iter)
{
}
}
|