2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.optimization.general;
19
import java.util.Arrays;
21
import org.apache.commons.math.FunctionEvaluationException;
22
import org.apache.commons.math.optimization.OptimizationException;
23
import org.apache.commons.math.optimization.VectorialPointValuePair;
27
* This class solves a least squares problem using the Levenberg-Marquardt algorithm.
29
* <p>This implementation <em>should</em> work even for over-determined systems
30
* (i.e. systems having more point than equations). Over-determined systems
31
* are solved by ignoring the point which have the smallest impact according
32
* to their jacobian column norm. Only the rank of the matrix and some loop bounds
33
* are changed to implement this.</p>
35
* <p>The resolution engine is a simple translation of the MINPACK <a
36
* href="http://www.netlib.org/minpack/lmder.f">lmder</a> routine with minor
37
* changes. The changes include the over-determined resolution and the Q.R.
38
* decomposition which has been rewritten following the algorithm described in the
39
* P. Lascaux and R. Theodor book <i>Analyse numérique matricielle
40
* appliquée à l'art de l'ingénieur</i>, Masson 1986. The
41
* redistribution policy for MINPACK is available <a
42
* href="http://www.netlib.org/minpack/disclaimer">here</a>, for convenience, it
43
* is reproduced below.</p>
45
* <table border="0" width="80%" cellpadding="10" align="center" bgcolor="#E0E0E0">
47
* Minpack Copyright Notice (1999) University of Chicago.
51
* Redistribution and use in source and binary forms, with or without
52
* modification, are permitted provided that the following conditions
55
* <li>Redistributions of source code must retain the above copyright
56
* notice, this list of conditions and the following disclaimer.</li>
57
* <li>Redistributions in binary form must reproduce the above
58
* copyright notice, this list of conditions and the following
59
* disclaimer in the documentation and/or other materials provided
60
* with the distribution.</li>
61
* <li>The end-user documentation included with the redistribution, if any,
62
* must include the following acknowledgment:
63
* <code>This product includes software developed by the University of
64
* Chicago, as Operator of Argonne National Laboratory.</code>
65
* Alternately, this acknowledgment may appear in the software itself,
66
* if and wherever such third-party acknowledgments normally appear.</li>
67
* <li><strong>WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
68
* WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
69
* UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
70
* THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
71
* IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
72
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
73
* OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
74
* OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
75
* USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
76
* THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
77
* DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
78
* UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
79
* BE CORRECTED.</strong></li>
80
* <li><strong>LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
81
* HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
82
* ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
83
* INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
84
* ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
85
* PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
86
* SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
87
* (INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
88
* EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
89
* POSSIBILITY OF SUCH LOSS OR DAMAGES.</strong></li>
93
* @author Argonne National Laboratory. MINPACK project. March 1980 (original fortran)
94
* @author Burton S. Garbow (original fortran)
95
* @author Kenneth E. Hillstrom (original fortran)
96
* @author Jorge J. More (original fortran)
98
* @version $Revision: 795978 $ $Date: 2009-07-20 15:57:08 -0400 (Mon, 20 Jul 2009) $
102
public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer {
104
/** Number of solved point. */
105
private int solvedCols;
107
/** Diagonal elements of the R matrix in the Q.R. decomposition. */
108
private double[] diagR;
110
/** Norms of the columns of the jacobian matrix. */
111
private double[] jacNorm;
113
/** Coefficients of the Householder transforms vectors. */
114
private double[] beta;
116
/** Columns permutation array. */
117
private int[] permutation;
119
/** Rank of the jacobian matrix. */
122
/** Levenberg-Marquardt parameter. */
123
private double lmPar;
125
/** Parameters evolution direction associated with lmPar. */
126
private double[] lmDir;
128
/** Positive input variable used in determining the initial step bound. */
129
private double initialStepBoundFactor;
131
/** Desired relative error in the sum of squares. */
132
private double costRelativeTolerance;
134
/** Desired relative error in the approximate solution parameters. */
135
private double parRelativeTolerance;
137
/** Desired max cosine on the orthogonality between the function vector
138
* and the columns of the jacobian. */
139
private double orthoTolerance;
142
* Build an optimizer for least squares problems.
143
* <p>The default values for the algorithm settings are:
145
* <li>{@link #setInitialStepBoundFactor initial step bound factor}: 100.0</li>
146
* <li>{@link #setMaxIterations maximal iterations}: 1000</li>
147
* <li>{@link #setCostRelativeTolerance cost relative tolerance}: 1.0e-10</li>
148
* <li>{@link #setParRelativeTolerance parameters relative tolerance}: 1.0e-10</li>
149
* <li>{@link #setOrthoTolerance orthogonality tolerance}: 1.0e-10</li>
153
public LevenbergMarquardtOptimizer() {
155
// set up the superclass with a default max cost evaluations setting
156
setMaxIterations(1000);
158
// default values for the tuning parameters
159
setInitialStepBoundFactor(100.0);
160
setCostRelativeTolerance(1.0e-10);
161
setParRelativeTolerance(1.0e-10);
162
setOrthoTolerance(1.0e-10);
167
* Set the positive input variable used in determining the initial step bound.
168
* This bound is set to the product of initialStepBoundFactor and the euclidean
169
* norm of diag*x if nonzero, or else to initialStepBoundFactor itself. In most
170
* cases factor should lie in the interval (0.1, 100.0). 100.0 is a generally
173
* @param initialStepBoundFactor initial step bound factor
175
public void setInitialStepBoundFactor(double initialStepBoundFactor) {
176
this.initialStepBoundFactor = initialStepBoundFactor;
180
* Set the desired relative error in the sum of squares.
182
* @param costRelativeTolerance desired relative error in the sum of squares
184
public void setCostRelativeTolerance(double costRelativeTolerance) {
185
this.costRelativeTolerance = costRelativeTolerance;
189
* Set the desired relative error in the approximate solution parameters.
191
* @param parRelativeTolerance desired relative error
192
* in the approximate solution parameters
194
public void setParRelativeTolerance(double parRelativeTolerance) {
195
this.parRelativeTolerance = parRelativeTolerance;
199
* Set the desired max cosine on the orthogonality.
201
* @param orthoTolerance desired max cosine on the orthogonality
202
* between the function vector and the columns of the jacobian
204
public void setOrthoTolerance(double orthoTolerance) {
205
this.orthoTolerance = orthoTolerance;
210
protected VectorialPointValuePair doOptimize()
211
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {
213
// arrays shared with the other private methods
214
solvedCols = Math.min(rows, cols);
215
diagR = new double[cols];
216
jacNorm = new double[cols];
217
beta = new double[cols];
218
permutation = new int[cols];
219
lmDir = new double[cols];
222
double delta = 0, xNorm = 0;
223
double[] diag = new double[cols];
224
double[] oldX = new double[cols];
225
double[] oldRes = new double[rows];
226
double[] work1 = new double[cols];
227
double[] work2 = new double[cols];
228
double[] work3 = new double[cols];
230
// evaluate the function at the starting point and calculate its norm
231
updateResidualsAndCost();
235
boolean firstIteration = true;
238
incrementIterationsCounter();
240
// compute the Q.R. decomposition of the jacobian matrix
247
// now we don't need Q anymore,
248
// so let jacobian contain the R matrix with its diagonal elements
249
for (int k = 0; k < solvedCols; ++k) {
250
int pk = permutation[k];
251
jacobian[k][pk] = diagR[pk];
254
if (firstIteration) {
256
// scale the point according to the norms of the columns
257
// of the initial jacobian
259
for (int k = 0; k < cols; ++k) {
260
double dk = jacNorm[k];
264
double xk = dk * point[k];
268
xNorm = Math.sqrt(xNorm);
270
// initialize the step bound delta
271
delta = (xNorm == 0) ? initialStepBoundFactor : (initialStepBoundFactor * xNorm);
275
// check orthogonality between function vector and jacobian columns
276
double maxCosine = 0;
278
for (int j = 0; j < solvedCols; ++j) {
279
int pj = permutation[j];
280
double s = jacNorm[pj];
283
for (int i = 0; i <= j; ++i) {
284
sum += jacobian[i][pj] * residuals[i];
286
maxCosine = Math.max(maxCosine, Math.abs(sum) / (s * cost));
290
if (maxCosine <= orthoTolerance) {
291
// convergence has been reached
292
return new VectorialPointValuePair(point, objective);
295
// rescale if necessary
296
for (int j = 0; j < cols; ++j) {
297
diag[j] = Math.max(diag[j], jacNorm[j]);
301
for (double ratio = 0; ratio < 1.0e-4;) {
304
for (int j = 0; j < solvedCols; ++j) {
305
int pj = permutation[j];
306
oldX[pj] = point[pj];
308
double previousCost = cost;
309
double[] tmpVec = residuals;
313
// determine the Levenberg-Marquardt parameter
314
determineLMParameter(oldRes, delta, diag, work1, work2, work3);
316
// compute the new point and the norm of the evolution direction
318
for (int j = 0; j < solvedCols; ++j) {
319
int pj = permutation[j];
320
lmDir[pj] = -lmDir[pj];
321
point[pj] = oldX[pj] + lmDir[pj];
322
double s = diag[pj] * lmDir[pj];
325
lmNorm = Math.sqrt(lmNorm);
327
// on the first iteration, adjust the initial step bound.
328
if (firstIteration) {
329
delta = Math.min(delta, lmNorm);
332
// evaluate the function at x + p and calculate its norm
333
updateResidualsAndCost();
335
// compute the scaled actual reduction
336
double actRed = -1.0;
337
if (0.1 * cost < previousCost) {
338
double r = cost / previousCost;
339
actRed = 1.0 - r * r;
342
// compute the scaled predicted reduction
343
// and the scaled directional derivative
344
for (int j = 0; j < solvedCols; ++j) {
345
int pj = permutation[j];
346
double dirJ = lmDir[pj];
348
for (int i = 0; i <= j; ++i) {
349
work1[i] += jacobian[i][pj] * dirJ;
353
for (int j = 0; j < solvedCols; ++j) {
354
coeff1 += work1[j] * work1[j];
356
double pc2 = previousCost * previousCost;
357
coeff1 = coeff1 / pc2;
358
double coeff2 = lmPar * lmNorm * lmNorm / pc2;
359
double preRed = coeff1 + 2 * coeff2;
360
double dirDer = -(coeff1 + coeff2);
362
// ratio of the actual to the predicted reduction
363
ratio = (preRed == 0) ? 0 : (actRed / preRed);
365
// update the step bound
368
(actRed < 0) ? (0.5 * dirDer / (dirDer + 0.5 * actRed)) : 0.5;
369
if ((0.1 * cost >= previousCost) || (tmp < 0.1)) {
372
delta = tmp * Math.min(delta, 10.0 * lmNorm);
374
} else if ((lmPar == 0) || (ratio >= 0.75)) {
379
// test for successful iteration.
380
if (ratio >= 1.0e-4) {
381
// successful iteration, update the norm
382
firstIteration = false;
384
for (int k = 0; k < cols; ++k) {
385
double xK = diag[k] * point[k];
388
xNorm = Math.sqrt(xNorm);
390
// failed iteration, reset the previous values
392
for (int j = 0; j < solvedCols; ++j) {
393
int pj = permutation[j];
394
point[pj] = oldX[pj];
401
// tests for convergence.
402
if (((Math.abs(actRed) <= costRelativeTolerance) &&
403
(preRed <= costRelativeTolerance) &&
405
(delta <= parRelativeTolerance * xNorm)) {
406
return new VectorialPointValuePair(point, objective);
409
// tests for termination and stringent tolerances
410
// (2.2204e-16 is the machine epsilon for IEEE754)
411
if ((Math.abs(actRed) <= 2.2204e-16) && (preRed <= 2.2204e-16) && (ratio <= 2.0)) {
412
throw new OptimizationException("cost relative tolerance is too small ({0})," +
413
" no further reduction in the" +
414
" sum of squares is possible",
415
costRelativeTolerance);
416
} else if (delta <= 2.2204e-16 * xNorm) {
417
throw new OptimizationException("parameters relative tolerance is too small" +
418
" ({0}), no further improvement in" +
419
" the approximate solution is possible",
420
parRelativeTolerance);
421
} else if (maxCosine <= 2.2204e-16) {
422
throw new OptimizationException("orthogonality tolerance is too small ({0})," +
423
" solution is orthogonal to the jacobian",
434
* Determine the Levenberg-Marquardt parameter.
435
* <p>This implementation is a translation in Java of the MINPACK
436
* <a href="http://www.netlib.org/minpack/lmpar.f">lmpar</a>
438
* <p>This method sets the lmPar and lmDir attributes.</p>
439
* <p>The authors of the original fortran function are:</p>
441
* <li>Argonne National Laboratory. MINPACK project. March 1980</li>
442
* <li>Burton S. Garbow</li>
443
* <li>Kenneth E. Hillstrom</li>
444
* <li>Jorge J. More</li>
446
* <p>Luc Maisonobe did the Java translation.</p>
448
* @param qy array containing qTy
449
* @param delta upper bound on the euclidean norm of diagR * lmDir
450
* @param diag diagonal matrix
451
* @param work1 work array
452
* @param work2 work array
453
* @param work3 work array
455
private void determineLMParameter(double[] qy, double delta, double[] diag,
456
double[] work1, double[] work2, double[] work3) {
458
// compute and store in x the gauss-newton direction, if the
459
// jacobian is rank-deficient, obtain a least squares solution
460
for (int j = 0; j < rank; ++j) {
461
lmDir[permutation[j]] = qy[j];
463
for (int j = rank; j < cols; ++j) {
464
lmDir[permutation[j]] = 0;
466
for (int k = rank - 1; k >= 0; --k) {
467
int pk = permutation[k];
468
double ypk = lmDir[pk] / diagR[pk];
469
for (int i = 0; i < k; ++i) {
470
lmDir[permutation[i]] -= ypk * jacobian[i][pk];
475
// evaluate the function at the origin, and test
476
// for acceptance of the Gauss-Newton direction
478
for (int j = 0; j < solvedCols; ++j) {
479
int pj = permutation[j];
480
double s = diag[pj] * lmDir[pj];
484
dxNorm = Math.sqrt(dxNorm);
485
double fp = dxNorm - delta;
486
if (fp <= 0.1 * delta) {
491
// if the jacobian is not rank deficient, the Newton step provides
492
// a lower bound, parl, for the zero of the function,
493
// otherwise set this bound to zero
494
double sum2, parl = 0;
495
if (rank == solvedCols) {
496
for (int j = 0; j < solvedCols; ++j) {
497
int pj = permutation[j];
498
work1[pj] *= diag[pj] / dxNorm;
501
for (int j = 0; j < solvedCols; ++j) {
502
int pj = permutation[j];
504
for (int i = 0; i < j; ++i) {
505
sum += jacobian[i][pj] * work1[permutation[i]];
507
double s = (work1[pj] - sum) / diagR[pj];
511
parl = fp / (delta * sum2);
514
// calculate an upper bound, paru, for the zero of the function
516
for (int j = 0; j < solvedCols; ++j) {
517
int pj = permutation[j];
519
for (int i = 0; i <= j; ++i) {
520
sum += jacobian[i][pj] * qy[i];
525
double gNorm = Math.sqrt(sum2);
526
double paru = gNorm / delta;
528
// 2.2251e-308 is the smallest positive real for IEE754
529
paru = 2.2251e-308 / Math.min(delta, 0.1);
532
// if the input par lies outside of the interval (parl,paru),
533
// set par to the closer endpoint
534
lmPar = Math.min(paru, Math.max(lmPar, parl));
536
lmPar = gNorm / dxNorm;
539
for (int countdown = 10; countdown >= 0; --countdown) {
541
// evaluate the function at the current value of lmPar
543
lmPar = Math.max(2.2251e-308, 0.001 * paru);
545
double sPar = Math.sqrt(lmPar);
546
for (int j = 0; j < solvedCols; ++j) {
547
int pj = permutation[j];
548
work1[pj] = sPar * diag[pj];
550
determineLMDirection(qy, work1, work2, work3);
553
for (int j = 0; j < solvedCols; ++j) {
554
int pj = permutation[j];
555
double s = diag[pj] * lmDir[pj];
559
dxNorm = Math.sqrt(dxNorm);
560
double previousFP = fp;
563
// if the function is small enough, accept the current value
564
// of lmPar, also test for the exceptional cases where parl is zero
565
if ((Math.abs(fp) <= 0.1 * delta) ||
566
((parl == 0) && (fp <= previousFP) && (previousFP < 0))) {
570
// compute the Newton correction
571
for (int j = 0; j < solvedCols; ++j) {
572
int pj = permutation[j];
573
work1[pj] = work3[pj] * diag[pj] / dxNorm;
575
for (int j = 0; j < solvedCols; ++j) {
576
int pj = permutation[j];
577
work1[pj] /= work2[j];
578
double tmp = work1[pj];
579
for (int i = j + 1; i < solvedCols; ++i) {
580
work1[permutation[i]] -= jacobian[i][pj] * tmp;
584
for (int j = 0; j < solvedCols; ++j) {
585
double s = work1[permutation[j]];
588
double correction = fp / (delta * sum2);
590
// depending on the sign of the function, update parl or paru.
592
parl = Math.max(parl, lmPar);
594
paru = Math.min(paru, lmPar);
597
// compute an improved estimate for lmPar
598
lmPar = Math.max(parl, lmPar + correction);
604
* Solve a*x = b and d*x = 0 in the least squares sense.
605
* <p>This implementation is a translation in Java of the MINPACK
606
* <a href="http://www.netlib.org/minpack/qrsolv.f">qrsolv</a>
608
* <p>This method sets the lmDir and lmDiag attributes.</p>
609
* <p>The authors of the original fortran function are:</p>
611
* <li>Argonne National Laboratory. MINPACK project. March 1980</li>
612
* <li>Burton S. Garbow</li>
613
* <li>Kenneth E. Hillstrom</li>
614
* <li>Jorge J. More</li>
616
* <p>Luc Maisonobe did the Java translation.</p>
618
* @param qy array containing qTy
619
* @param diag diagonal matrix
620
* @param lmDiag diagonal elements associated with lmDir
621
* @param work work array
623
private void determineLMDirection(double[] qy, double[] diag,
624
double[] lmDiag, double[] work) {
626
// copy R and Qty to preserve input and initialize s
627
// in particular, save the diagonal elements of R in lmDir
628
for (int j = 0; j < solvedCols; ++j) {
629
int pj = permutation[j];
630
for (int i = j + 1; i < solvedCols; ++i) {
631
jacobian[i][pj] = jacobian[j][permutation[i]];
633
lmDir[j] = diagR[pj];
637
// eliminate the diagonal matrix d using a Givens rotation
638
for (int j = 0; j < solvedCols; ++j) {
640
// prepare the row of d to be eliminated, locating the
641
// diagonal element using p from the Q.R. factorization
642
int pj = permutation[j];
643
double dpj = diag[pj];
645
Arrays.fill(lmDiag, j + 1, lmDiag.length, 0);
649
// the transformations to eliminate the row of d
650
// modify only a single element of Qty
651
// beyond the first n, which is initially zero.
653
for (int k = j; k < solvedCols; ++k) {
654
int pk = permutation[k];
656
// determine a Givens rotation which eliminates the
657
// appropriate element in the current row of d
658
if (lmDiag[k] != 0) {
661
double rkk = jacobian[k][pk];
662
if (Math.abs(rkk) < Math.abs(lmDiag[k])) {
663
double cotan = rkk / lmDiag[k];
664
sin = 1.0 / Math.sqrt(1.0 + cotan * cotan);
667
double tan = lmDiag[k] / rkk;
668
cos = 1.0 / Math.sqrt(1.0 + tan * tan);
672
// compute the modified diagonal element of R and
673
// the modified element of (Qty,0)
674
jacobian[k][pk] = cos * rkk + sin * lmDiag[k];
675
double temp = cos * work[k] + sin * qtbpj;
676
qtbpj = -sin * work[k] + cos * qtbpj;
679
// accumulate the tranformation in the row of s
680
for (int i = k + 1; i < solvedCols; ++i) {
681
double rik = jacobian[i][pk];
682
temp = cos * rik + sin * lmDiag[i];
683
lmDiag[i] = -sin * rik + cos * lmDiag[i];
684
jacobian[i][pk] = temp;
690
// store the diagonal element of s and restore
691
// the corresponding diagonal element of R
692
lmDiag[j] = jacobian[j][permutation[j]];
693
jacobian[j][permutation[j]] = lmDir[j];
697
// solve the triangular system for z, if the system is
698
// singular, then obtain a least squares solution
699
int nSing = solvedCols;
700
for (int j = 0; j < solvedCols; ++j) {
701
if ((lmDiag[j] == 0) && (nSing == solvedCols)) {
704
if (nSing < solvedCols) {
709
for (int j = nSing - 1; j >= 0; --j) {
710
int pj = permutation[j];
712
for (int i = j + 1; i < nSing; ++i) {
713
sum += jacobian[i][pj] * work[i];
715
work[j] = (work[j] - sum) / lmDiag[j];
719
// permute the components of z back to components of lmDir
720
for (int j = 0; j < lmDir.length; ++j) {
721
lmDir[permutation[j]] = work[j];
727
* Decompose a matrix A as A.P = Q.R using Householder transforms.
728
* <p>As suggested in the P. Lascaux and R. Theodor book
729
* <i>Analyse numérique matricielle appliquée à
730
* l'art de l'ingénieur</i> (Masson, 1986), instead of representing
731
* the Householder transforms with u<sub>k</sub> unit vectors such that:
733
* H<sub>k</sub> = I - 2u<sub>k</sub>.u<sub>k</sub><sup>t</sup>
735
* we use <sub>k</sub> non-unit vectors such that:
737
* H<sub>k</sub> = I - beta<sub>k</sub>v<sub>k</sub>.v<sub>k</sub><sup>t</sup>
739
* where v<sub>k</sub> = a<sub>k</sub> - alpha<sub>k</sub> e<sub>k</sub>.
740
* The beta<sub>k</sub> coefficients are provided upon exit as recomputing
741
* them from the v<sub>k</sub> vectors would be costly.</p>
742
* <p>This decomposition handles rank deficient cases since the tranformations
743
* are performed in non-increasing columns norms order thanks to columns
744
* pivoting. The diagonal elements of the R matrix are therefore also in
745
* non-increasing absolute values order.</p>
746
* @exception OptimizationException if the decomposition cannot be performed
748
private void qrDecomposition() throws OptimizationException {
751
for (int k = 0; k < cols; ++k) {
754
for (int i = 0; i < jacobian.length; ++i) {
755
double akk = jacobian[i][k];
758
jacNorm[k] = Math.sqrt(norm2);
761
// transform the matrix column after column
762
for (int k = 0; k < cols; ++k) {
764
// select the column with the greatest norm on active components
766
double ak2 = Double.NEGATIVE_INFINITY;
767
for (int i = k; i < cols; ++i) {
769
for (int j = k; j < jacobian.length; ++j) {
770
double aki = jacobian[j][permutation[i]];
773
if (Double.isInfinite(norm2) || Double.isNaN(norm2)) {
774
throw new OptimizationException(
775
"unable to perform Q.R decomposition on the {0}x{1} jacobian matrix",
787
int pk = permutation[nextColumn];
788
permutation[nextColumn] = permutation[k];
791
// choose alpha such that Hk.u = alpha ek
792
double akk = jacobian[k][pk];
793
double alpha = (akk > 0) ? -Math.sqrt(ak2) : Math.sqrt(ak2);
794
double betak = 1.0 / (ak2 - akk * alpha);
797
// transform the current column
799
jacobian[k][pk] -= alpha;
801
// transform the remaining columns
802
for (int dk = cols - 1 - k; dk > 0; --dk) {
804
for (int j = k; j < jacobian.length; ++j) {
805
gamma += jacobian[j][pk] * jacobian[j][permutation[k + dk]];
808
for (int j = k; j < jacobian.length; ++j) {
809
jacobian[j][permutation[k + dk]] -= gamma * jacobian[j][pk];
820
* Compute the product Qt.y for some Q.R. decomposition.
822
* @param y vector to multiply (will be overwritten with the result)
824
private void qTy(double[] y) {
825
for (int k = 0; k < cols; ++k) {
826
int pk = permutation[k];
828
for (int i = k; i < rows; ++i) {
829
gamma += jacobian[i][pk] * y[i];
832
for (int i = k; i < rows; ++i) {
833
y[i] -= gamma * jacobian[i][pk];