• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lballabio / QuantLib / 22220236320

20 Feb 2026 10:18AM UTC coverage: 74.259% (+0.06%) from 74.204%
22220236320

push

github

web-flow
Add `ZabrSwaptionVolatilityCube` for ZABR-based swaption volatility cubes (#2434)

196 of 235 new or added lines in 6 files covered. (83.4%)

1 existing line in 1 file now uncovered.

57595 of 77560 relevant lines covered (74.26%)

8769211.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/ql/math/optimization/lmdif.cpp
1
/************************lmdif*************************/
2

3
/*
4
The original Fortran version is Copyright (C) 1999 University of Chicago.
5
All rights reserved.
6

7
Redistribution and use in source and binary forms, with or
8
without modification, are permitted provided that the
9
following conditions are met:
10

11
1. Redistributions of source code must retain the above
12
copyright notice, this list of conditions and the following
13
disclaimer.
14

15
2. Redistributions in binary form must reproduce the above
16
copyright notice, this list of conditions and the following
17
disclaimer in the documentation and/or other materials
18
provided with the distribution.
19

20
3. The end-user documentation included with the
21
redistribution, if any, must include the following
22
acknowledgment:
23

24
   "This product includes software developed by the
25
   University of Chicago, as Operator of Argonne National
26
   Laboratory.
27

28
Alternately, this acknowledgment may appear in the software
29
itself, if and wherever such third-party acknowledgments
30
normally appear.
31

32
4. WARRANTY DISCLAIMER. THE SOFTWARE IS SUPPLIED "AS IS"
33
WITHOUT WARRANTY OF ANY KIND. THE COPYRIGHT HOLDER, THE
34
UNITED STATES, THE UNITED STATES DEPARTMENT OF ENERGY, AND
35
THEIR EMPLOYEES: (1) DISCLAIM ANY WARRANTIES, EXPRESS OR
36
IMPLIED, INCLUDING BUT NOT LIMITED TO ANY IMPLIED WARRANTIES
37
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE
38
OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY
39
OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR
40
USEFULNESS OF THE SOFTWARE, (3) DO NOT REPRESENT THAT USE OF
41
THE SOFTWARE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS, (4)
42
DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION
43
UNINTERRUPTED, THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL
44
BE CORRECTED.
45

46
5. LIMITATION OF LIABILITY. IN NO EVENT WILL THE COPYRIGHT
47
HOLDER, THE UNITED STATES, THE UNITED STATES DEPARTMENT OF
48
ENERGY, OR THEIR EMPLOYEES: BE LIABLE FOR ANY INDIRECT,
49
INCIDENTAL, CONSEQUENTIAL, SPECIAL OR PUNITIVE DAMAGES OF
50
ANY KIND OR NATURE, INCLUDING BUT NOT LIMITED TO LOSS OF
51
PROFITS OR LOSS OF DATA, FOR ANY REASON WHATSOEVER, WHETHER
52
SUCH LIABILITY IS ASSERTED ON THE BASIS OF CONTRACT, TORT
53
(INCLUDING NEGLIGENCE OR STRICT LIABILITY), OR OTHERWISE,
54
EVEN IF ANY OF SAID PARTIES HAS BEEN WARNED OF THE
55
POSSIBILITY OF SUCH LOSS OR DAMAGES.
56

57

58
C translation Copyright (C) Steve Moshier
59

60
What you see here may be used freely but it comes with no support
61
or guarantee.
62
*/
63

64
#include <ql/math/optimization/lmdif.hpp>
65
#include <cmath>
66
#include <cstdio>
67

68
namespace QuantLib::MINPACK {
69
#define BUG 0
70
/* resolution of arithmetic */
71
double MACHEP = 1.2e-16;
72
/* smallest nonzero number */
73
double DWARF = 1.0e-38;
74

75

76

77

78

79
Real enorm(int n,Real* x)
12,822,872✔
80
{
81
/*
82
*     **********
83
*
84
*     function enorm
85
*
86
*     given an n-vector x, this function calculates the
87
*     euclidean norm of x.
88
*
89
*     the euclidean norm is computed by accumulating the sum of
90
*     squares in three different sums. the sums of squares for the
91
*     small and large components are scaled so that no overflows
92
*     occur. non-destructive underflows are permitted. underflows
93
*     and overflows do not occur in the computation of the unscaled
94
*     sum of squares for the intermediate components.
95
*     the definitions of small, intermediate and large components
96
*     depend on two constants, rdwarf and rgiant. the main
97
*     restrictions on these constants are that rdwarf**2 not
98
*     underflow and rgiant**2 not overflow. the constants
99
*     given here are suitable for every known computer.
100
*
101
*     the function statement is
102
*
103
*   double precision function enorm(n,x)
104
*
105
*     where
106
*
107
*   n is a positive integer input variable.
108
*
109
*   x is an input array of length n.
110
*
111
*     subprograms called
112
*
113
*   fortran-supplied ... dabs,dsqrt
114
*
115
*     argonne national laboratory. minpack project. march 1980.
116
*     burton s. garbow, kenneth e. hillstrom, jorge j. more
117
*
118
*     **********
119
*/
120
int i;
121
Real agiant,floatn,s1,s2,s3,xabs,x1max,x3max;
122
Real ans, temp;
123
static double rdwarf = 3.834e-20;
124
static double rgiant = 1.304e19;
125
static double zero = 0.0;
126
static double one = 1.0;
127

128
s1 = zero;
12,822,872✔
129
s2 = zero;
130
s3 = zero;
131
x1max = zero;
132
x3max = zero;
133
floatn = n;
12,822,872✔
134
agiant = rgiant/floatn;
12,822,872✔
135

136
for( i=0; i<n; i++ )
83,726,702✔
137
{
138
xabs = std::fabs(x[i]);
70,903,830✔
139
if( (xabs > rdwarf) && (xabs < agiant) )
70,903,830✔
140
    {
141
/*
142
*       sum for intermediate components.
143
*/
144
    s2 += xabs*xabs;
70,311,727✔
145
    continue;
70,311,727✔
146
    }
147

148
if(xabs > rdwarf)
592,103✔
149
    {
150
/*
151
*          sum for large components.
152
*/
153
    if(xabs > x1max)
21✔
154
        {
155
        temp = x1max/xabs;
21✔
156
        s1 = one + s1*temp*temp;
21✔
157
        x1max = xabs;
158
        }
159
    else
160
        {
161
        temp = xabs/x1max;
×
162
        s1 += temp*temp;
×
163
        }
164
    continue;
21✔
165
    }
166
/*
167
*          sum for small components.
168
*/
169
if(xabs > x3max)
592,082✔
170
    {
171
    temp = x3max/xabs;
606✔
172
    s3 = one + s3*temp*temp;
606✔
173
    x3max = xabs;
174
    }
175
else
176
    {
177
    if(xabs != zero)
591,476✔
178
        {
179
        temp = xabs/x3max;
429✔
180
        s3 += temp*temp;
429✔
181
        }
182
    }
183
}
184
/*
185
*     calculation of norm.
186
*/
187
if(s1 != zero)
12,822,872✔
188
    {
189
    temp = s1 + (s2/x1max)/x1max;
21✔
190
    ans = x1max*std::sqrt(temp);
21✔
191
    return(ans);
21✔
192
    }
193
if(s2 != zero)
12,822,851✔
194
    {
195
    if(s2 >= x3max)
12,792,088✔
196
        temp = s2*(one+(x3max/s2)*(x3max*s3));
12,791,999✔
197
    else
198
        temp = x3max*((s2/x3max)+(x3max*s3));
89✔
199
    ans = std::sqrt(temp);
12,792,088✔
200
    }
201
else
202
    {
203
    ans = x3max*std::sqrt(s3);
30,763✔
204
    }
205
return(ans);
206
/*
207
*     last card of function enorm.
208
*/
209
}
210
/************************lmmisc.c*************************/
211

212
Real dmax1(Real a,Real b)
12,862,901✔
213
{
214
if( a >= b )
12,862,901✔
215
    return(a);
216
else
217
    return(b);
9,856,869✔
218
}
219

220
Real dmin1(Real a,Real b)
882,967✔
221
{
222
if( a <= b )
882,967✔
223
    return(a);
224
else
225
    return(b);
195,535✔
226
}
227

228
int min0(int a,int b)
763,574✔
229

230
{
231
if( a <= b )
763,574✔
232
    return(a);
233
else
234
    return(b);
760,927✔
235
}
236

237
int mod( int k, int m )
×
238
{
239
return( k % m );
×
240
}
241

242

243
/***********Sample of user supplied function****************
244
 * m = number of functions
245
 * n = number of variables
246
 * x = vector of function arguments
247
 * fvec = vector of function values
248
 * iflag = error return variable
249
 */
250
//void fcn(int m,int n, Real* x, Real* fvec,int *iflag)
251
//{
252
//  QuantLib::LevenbergMarquardt::fcn(m, n, x, fvec, iflag);
253
//}
254

255
void fdjac2(int m,
763,377✔
256
            int n,
257
            Real* x,
258
            const Real* fvec,
259
            Real* fjac,
260
            int,
261
            int* iflag,
262
            Real epsfcn,
263
            Real* wa,
264
            const QuantLib::MINPACK::LmdifCostFunction& fcn) {
265
    /*
266
     *     **********
267
     *
268
     *     subroutine fdjac2
269
     *
270
     *     this subroutine computes a forward-difference approximation
271
     *     to the m by n jacobian matrix associated with a specified
272
     *     problem of m functions in n variables.
273
     *
274
     *     the subroutine statement is
275
     *
276
     *   subroutine fdjac2(fcn,m,n,x,fvec,fjac,ldfjac,iflag,epsfcn,wa)
277
     *
278
     *     where
279
     *
280
     *   fcn is the name of the user-supplied subroutine which
281
     *     calculates the functions. fcn must be declared
282
     *     in an external statement in the user calling
283
     *     program, and should be written as follows.
284
     *
285
     *     subroutine fcn(m,n,x,fvec,iflag)
286
     *     integer m,n,iflag
287
     *     double precision x(n),fvec(m)
288
     *     ----------
289
     *     calculate the functions at x and
290
     *     return this vector in fvec.
291
     *     ----------
292
     *     return
293
     *     end
294
     *
295
     *     the value of iflag should not be changed by fcn unless
296
     *     the user wants to terminate execution of fdjac2.
297
     *     in this case set iflag to a negative integer.
298
     *
299
     *   m is a positive integer input variable set to the number
300
     *     of functions.
301
     *
302
     *   n is a positive integer input variable set to the number
303
     *     of variables. n must not exceed m.
304
     *
305
     *   x is an input array of length n.
306
     *
307
     *   fvec is an input array of length m which must contain the
308
     *     functions evaluated at x.
309
     *
310
     *   fjac is an output m by n array which contains the
311
     *     approximation to the jacobian matrix evaluated at x.
312
     *
313
     *   ldfjac is a positive integer input variable not less than m
314
     *     which specifies the leading dimension of the array fjac.
315
     *
316
     *   iflag is an integer variable which can be used to terminate
317
     *     the execution of fdjac2. see description of fcn.
318
     *
319
     *   epsfcn is an input variable used in determining a suitable
320
     *     step length for the forward-difference approximation. this
321
     *     approximation assumes that the relative errors in the
322
     *     functions are of the order of epsfcn. if epsfcn is less
323
     *     than the machine precision, it is assumed that the relative
324
     *     errors in the functions are of the order of the machine
325
     *     precision.
326
     *
327
     *   wa is a work array of length m.
328
     *
329
     *     subprograms called
330
     *
331
     *   user-supplied ...... fcn
332
     *
333
     *   minpack-supplied ... dpmpar
334
     *
335
     *   fortran-supplied ... dabs,dmax1,dsqrt
336
     *
337
     *     argonne national laboratory. minpack project. march 1980.
338
     *     burton s. garbow, kenneth e. hillstrom, jorge j. more
339
     *
340
     **********
341
     */
342
    int i, j, ij;
343
    Real eps, h, temp;
344
    static double zero = 0.0;
345

346

347
    temp = dmax1(epsfcn, MACHEP);
763,377✔
348
    eps = std::sqrt(temp);
763,377✔
349
    ij = 0;
350
    for (j = 0; j < n; j++) {
3,834,541✔
351
        temp = x[j];
3,071,164✔
352
        h = eps * std::fabs(temp);
3,071,164✔
353
        if (h == zero)
3,071,164✔
354
            h = eps;
355
        x[j] = temp + h;
3,071,164✔
356
        fcn(m, n, x, wa, iflag);
3,071,164✔
357
        if (*iflag < 0)
3,071,164✔
358
            return;
359
        x[j] = temp;
3,071,164✔
360
        for (i = 0; i < m; i++) {
25,645,003✔
361
            fjac[ij] = (wa[i] - fvec[i]) / h;
22,573,839✔
362
            ij += 1; /* fjac[i+m*j] */
22,573,839✔
363
        }
364
    }
365
/*
366
*     last card of subroutine fdjac2.
367
*/
368
}
369
/************************qrfac.c*************************/
370

371

372
void
373
qrfac(int m,int n,Real* a,int,int pivot,int* ipvt,
763,574✔
374
      int,Real* rdiag,Real* acnorm,Real* wa)
375
{
376
/*
377
*     **********
378
*
379
*     subroutine qrfac
380
*
381
*     this subroutine uses householder transformations with column
382
*     pivoting (optional) to compute a qr factorization of the
383
*     m by n matrix a. that is, qrfac determines an orthogonal
384
*     matrix q, a permutation matrix p, and an upper trapezoidal
385
*     matrix r with diagonal elements of nonincreasing magnitude,
386
*     such that a*p = q*r. the householder transformation for
387
*     column k, k = 1,2,...,min(m,n), is of the form
388
*
389
*               t
390
*       i - (1/u(k))*u*u
391
*
392
*     where u has zeros in the first k-1 positions. the form of
393
*     this transformation and the method of pivoting first
394
*     appeared in the corresponding linpack subroutine.
395
*
396
*     the subroutine statement is
397
*
398
*   subroutine qrfac(m,n,a,lda,pivot,ipvt,lipvt,rdiag,acnorm,wa)
399
*
400
*     where
401
*
402
*   m is a positive integer input variable set to the number
403
*     of rows of a.
404
*
405
*   n is a positive integer input variable set to the number
406
*     of columns of a.
407
*
408
*   a is an m by n array. on input a contains the matrix for
409
*     which the qr factorization is to be computed. on output
410
*     the strict upper trapezoidal part of a contains the strict
411
*     upper trapezoidal part of r, and the lower trapezoidal
412
*     part of a contains a factored form of q (the non-trivial
413
*     elements of the u vectors described above).
414
*
415
*   lda is a positive integer input variable not less than m
416
*     which specifies the leading dimension of the array a.
417
*
418
*   pivot is a logical input variable. if pivot is set true,
419
*     then column pivoting is enforced. if pivot is set false,
420
*     then no column pivoting is done.
421
*
422
*   ipvt is an integer output array of length lipvt. ipvt
423
*     defines the permutation matrix p such that a*p = q*r.
424
*     column j of p is column ipvt(j) of the identity matrix.
425
*     if pivot is false, ipvt is not referenced.
426
*
427
*   lipvt is a positive integer input variable. if pivot is false,
428
*     then lipvt may be as small as 1. if pivot is true, then
429
*     lipvt must be at least n.
430
*
431
*   rdiag is an output array of length n which contains the
432
*     diagonal elements of r.
433
*
434
*   acnorm is an output array of length n which contains the
435
*     norms of the corresponding columns of the input matrix a.
436
*     if this information is not needed, then acnorm can coincide
437
*     with rdiag.
438
*
439
*   wa is a work array of length n. if pivot is false, then wa
440
*     can coincide with rdiag.
441
*
442
*     subprograms called
443
*
444
*   minpack-supplied ... dpmpar,enorm
445
*
446
*   fortran-supplied ... dmax1,dsqrt,min0
447
*
448
*     argonne national laboratory. minpack project. march 1980.
449
*     burton s. garbow, kenneth e. hillstrom, jorge j. more
450
*
451
*     **********
452
*/
453
int i,ij,jj,j,jp1,k,kmax,minmn;
454
Real ajnorm,sum,temp;
455
static double zero = 0.0;
456
static double one = 1.0;
457
static double p05 = 0.05;
458

459
/*
460
*     compute the initial column norms and initialize several arrays.
461
*/
462
ij = 0;
463
for( j=0; j<n; j++ )
3,839,378✔
464
    {
465
    acnorm[j] = enorm(m,&a[ij]);
3,075,804✔
466
    rdiag[j] = acnorm[j];
3,075,804✔
467
    wa[j] = rdiag[j];
3,075,804✔
468
    if(pivot != 0)
3,075,804✔
469
        ipvt[j] = j;
3,075,777✔
470
    ij += m; /* m*j */
3,075,804✔
471
    }
472
/*
473
*     reduce a to r with householder transformations.
474
*/
475
minmn = min0(m,n);
763,574✔
476
for( j=0; j<minmn; j++ )
3,837,354✔
477
{
478
if(pivot == 0)
3,073,780✔
479
    goto L40;
25✔
480
/*
481
*    bring the column of largest norm into the pivot position.
482
*/
483
kmax = j;
484
for( k=j; k<n; k++ )
11,108,603✔
485
    {
486
    if(rdiag[k] > rdiag[kmax])
8,034,848✔
487
        kmax = k;
488
    }
489
if(kmax == j)
3,073,755✔
490
    goto L40;
1,550,792✔
491

492
ij = m * j;
1,522,963✔
493
jj = m * kmax;
1,522,963✔
494
for( i=0; i<m; i++ )
11,611,328✔
495
    {
496
    temp = a[ij]; /* [i+m*j] */
10,088,365✔
497
    a[ij] = a[jj]; /* [i+m*kmax] */
10,088,365✔
498
    a[jj] = temp;
10,088,365✔
499
    ij += 1;
10,088,365✔
500
    jj += 1;
10,088,365✔
501
    }
502
rdiag[kmax] = rdiag[j];
1,522,963✔
503
wa[kmax] = wa[j];
1,522,963✔
504
k = ipvt[j];
1,522,963✔
505
ipvt[j] = ipvt[kmax];
1,522,963✔
506
ipvt[kmax] = k;
1,522,963✔
507

508
L40:
3,073,780✔
509
/*
510
*    compute the householder transformation to reduce the
511
*    j-th column of a to a multiple of the j-th unit vector.
512
*/
513
jj = j + m*j;
3,073,780✔
514
ajnorm = enorm(m-j,&a[jj]);
3,073,780✔
515
if(ajnorm == zero)
3,073,780✔
516
    goto L100;
14,341✔
517
if(a[jj] < zero)
3,059,439✔
518
    ajnorm = -ajnorm;
581,283✔
519
ij = jj;
520
for( i=j; i<m; i++ )
20,865,731✔
521
    {
522
    a[ij] /= ajnorm;
17,806,292✔
523
    ij += 1; /* [i+m*j] */
17,806,292✔
524
    }
525
a[jj] += one;
3,059,439✔
526
/*
527
*    apply the transformation to the remaining columns
528
*    and update the norms.
529
*/
530
jp1 = j + 1;
3,059,439✔
531
if(jp1 < n )
3,059,439✔
532
{
533
for( k=jp1; k<n; k++ )
7,178,228✔
534
    {
535
    sum = zero;
536
    ij = j + m*k;
4,874,962✔
537
    jj = j + m*j;
538
    for( i=j; i<m; i++ )
41,216,133✔
539
        {
540
        sum += a[jj]*a[ij];
36,341,171✔
541
        ij += 1; /* [i+m*k] */
36,341,171✔
542
        jj += 1; /* [i+m*j] */
36,341,171✔
543
        }
544
    temp = sum/a[j+m*j];
4,874,962✔
545
    ij = j + m*k;
546
    jj = j + m*j;
547
    for( i=j; i<m; i++ )
41,216,133✔
548
        {
549
        a[ij] -= temp*a[jj];
36,341,171✔
550
        ij += 1; /* [i+m*k] */
36,341,171✔
551
        jj += 1; /* [i+m*j] */
36,341,171✔
552
        }
553
    if( (pivot != 0) && (rdiag[k] != zero) )
4,874,962✔
554
        {
555
        temp = a[j+m*k]/rdiag[k];
4,828,515✔
556
        temp = dmax1( zero, one-temp*temp );
4,828,515✔
557
        rdiag[k] *= std::sqrt(temp);
4,828,515✔
558
        temp = rdiag[k]/wa[k];
4,828,515✔
559
        if( (p05*temp*temp) <= MACHEP)
4,828,515✔
560
            {
561
            rdiag[k] = enorm(m-j-1,&a[jp1+m*k]);
1,555✔
562
            wa[k] = rdiag[k];
1,555✔
563
            }
564
        }
565
    }
566
}
567

568
L100:
756,173✔
569
    rdiag[j] = -ajnorm;
3,073,780✔
570
}
571
/*
572
*     last card of subroutine qrfac.
573
*/
574
}
763,574✔
575

576
/************************qrsolv.c*************************/
577

578

579
void qrsolv(int n,
996,221✔
580
            Real* r,
581
            int ldr,
582
            const int* ipvt,
583
            const Real* diag,
584
            const Real* qtb,
585
            Real* x,
586
            Real* sdiag,
587
            Real* wa) {
588
    /*
589
     *     **********
590
     *
591
     *     subroutine qrsolv
592
     *
593
     *     given an m by n matrix a, an n by n diagonal matrix d,
594
     *     and an m-vector b, the problem is to determine an x which
595
     *     solves the system
596
     *
597
     *       a*x = b ,     d*x = 0 ,
598
     *
599
     *     in the least squares sense.
600
     *
601
     *     this subroutine completes the solution of the problem
602
     *     if it is provided with the necessary information from the
603
     *     qr factorization, with column pivoting, of a. that is, if
604
     *     a*p = q*r, where p is a permutation matrix, q has orthogonal
605
     *     columns, and r is an upper triangular matrix with diagonal
606
     *     elements of nonincreasing magnitude, then qrsolv expects
607
     *     the full upper triangle of r, the permutation matrix p,
608
     *     and the first n components of (q transpose)*b. the system
609
     *     a*x = b, d*x = 0, is then equivalent to
610
     *
611
     *          t       t
612
     *       r*z = q *b ,  p *d*p*z = 0 ,
613
     *
614
     *     where x = p*z. if this system does not have full rank,
615
     *     then a least squares solution is obtained. on output qrsolv
616
     *     also provides an upper triangular matrix s such that
617
     *
618
     *        t   t       t
619
     *       p *(a *a + d*d)*p = s *s .
620
     *
621
     *     s is computed within qrsolv and may be of separate interest.
622
     *
623
     *     the subroutine statement is
624
     *
625
     *   subroutine qrsolv(n,r,ldr,ipvt,diag,qtb,x,sdiag,wa)
626
     *
627
     *     where
628
     *
629
     *   n is a positive integer input variable set to the order of r.
630
     *
631
     *   r is an n by n array. on input the full upper triangle
632
     *     must contain the full upper triangle of the matrix r.
633
     *     on output the full upper triangle is unaltered, and the
634
     *     strict lower triangle contains the strict upper triangle
635
     *     (transposed) of the upper triangular matrix s.
636
     *
637
     *   ldr is a positive integer input variable not less than n
638
     *     which specifies the leading dimension of the array r.
639
     *
640
     *   ipvt is an integer input array of length n which defines the
641
     *     permutation matrix p such that a*p = q*r. column j of p
642
     *     is column ipvt(j) of the identity matrix.
643
     *
644
     *   diag is an input array of length n which must contain the
645
     *     diagonal elements of the matrix d.
646
     *
647
     *   qtb is an input array of length n which must contain the first
648
     *     n elements of the vector (q transpose)*b.
649
     *
650
     *   x is an output array of length n which contains the least
651
     *     squares solution of the system a*x = b, d*x = 0.
652
     *
653
     *   sdiag is an output array of length n which contains the
654
     *     diagonal elements of the upper triangular matrix s.
655
     *
656
     *   wa is a work array of length n.
657
     *
658
     *     subprograms called
659
     *
660
     *   fortran-supplied ... dabs,dsqrt
661
     *
662
     *     argonne national laboratory. minpack project. march 1980.
663
     *     burton s. garbow, kenneth e. hillstrom, jorge j. more
664
     *
665
     *     **********
666
     */
667
    int i, ij, ik, kk, j, jp1, k, kp1, l, nsing;
668
    Real cos, cotan, qtbpj, sin, sum, tan, temp;
669
    static double zero = 0.0;
670
    static double p25 = 0.25;
671
    static double p5 = 0.5;
672

673
    /*
674
     *     copy r and (q transpose)*b to preserve input and initialize s.
675
     *     in particular, save the diagonal elements of r in x.
676
     */
677
    kk = 0;
678
    for (j = 0; j < n; j++) {
4,995,709✔
679
        ij = kk;
680
        ik = kk;
681
        for (i = j; i < n; i++) {
14,380,077✔
682
            r[ij] = r[ik];
10,380,589✔
683
            ij += 1;   /* [i+ldr*j] */
10,380,589✔
684
            ik += ldr; /* [j+ldr*i] */
10,380,589✔
685
        }
686
    x[j] = r[kk];
3,999,488✔
687
    wa[j] = qtb[j];
3,999,488✔
688
    kk += ldr+1; /* j+ldr*j */
3,999,488✔
689
    }
690
/*
691
*     eliminate the diagonal matrix d using a givens rotation.
692
*/
693
for( j=0; j<n; j++ )
4,995,709✔
694
{
695
/*
696
*    prepare the row of d to be eliminated, locating the
697
*    diagonal element using p from the qr factorization.
698
*/
699
l = ipvt[j];
3,999,488✔
700
if(diag[l] == zero)
3,999,488✔
701
    goto L90;
4,574✔
702
for( k=j; k<n; k++ )
14,095,782✔
703
    sdiag[k] = zero;
10,100,868✔
704
sdiag[j] = diag[l];
3,994,914✔
705
/*
706
*    the transformations to eliminate the row of d
707
*    modify only a single element of (q transpose)*b
708
*    beyond the first n, which is initially zero.
709
*/
710
qtbpj = zero;
711
for( k=j; k<n; k++ )
14,095,782✔
712
    {
713
/*
714
*       determine a givens rotation which eliminates the
715
*       appropriate element in the current row of d.
716
*/
717
    if(sdiag[k] == zero)
10,100,868✔
718
        continue;
35,828✔
719
    kk = k + ldr * k;
10,065,040✔
720
    if(std::fabs(r[kk]) < std::fabs(sdiag[k]))
10,065,040✔
721
        {
722
        cotan = r[kk]/sdiag[k];
6,490,524✔
723
        sin = p5/std::sqrt(p25+p25*cotan*cotan);
6,490,524✔
724
        cos = sin*cotan;
6,490,524✔
725
        }
726
    else
727
        {
728
        tan = sdiag[k]/r[kk];
3,574,516✔
729
        cos = p5/std::sqrt(p25+p25*tan*tan);
3,574,516✔
730
        sin = cos*tan;
3,574,516✔
731
        }
732
/*
733
*       compute the modified diagonal element of r and
734
*       the modified element of ((q transpose)*b,0).
735
*/
736
    r[kk] = cos*r[kk] + sin*sdiag[k];
10,065,040✔
737
    temp = cos*wa[k] + sin*qtbpj;
10,065,040✔
738
    qtbpj = -sin*wa[k] + cos*qtbpj;
10,065,040✔
739
    wa[k] = temp;
10,065,040✔
740
/*
741
*       accumulate the tranformation in the row of s.
742
*/
743
    kp1 = k + 1;
10,065,040✔
744
    if( n > kp1 )
10,065,040✔
745
        {
746
        ik = kk + 1;
6,103,959✔
747
        for( i=kp1; i<n; i++ )
16,810,185✔
748
            {
749
            temp = cos*r[ik] + sin*sdiag[i];
10,706,226✔
750
            sdiag[i] = -sin*r[ik] + cos*sdiag[i];
10,706,226✔
751
            r[ik] = temp;
10,706,226✔
752
            ik += 1; /* [i+ldr*k] */
10,706,226✔
753
            }
754
        }
755
    }
756
L90:
3,994,914✔
757
/*
758
*    store the diagonal element of s and restore
759
*    the corresponding diagonal element of r.
760
*/
761
    kk = j + ldr*j;
3,999,488✔
762
    sdiag[j] = r[kk];
3,999,488✔
763
    r[kk] = x[j];
3,999,488✔
764
}
765
/*
766
*     solve the triangular system for z. if the system is
767
*     singular, then obtain a least squares solution.
768
*/
769
nsing = n;
770
for( j=0; j<n; j++ )
4,995,709✔
771
    {
772
    if( (sdiag[j] == zero) && (nsing == n) )
3,999,488✔
773
        nsing = j;
774
    if(nsing < n)
3,999,488✔
775
        wa[j] = zero;
2,020✔
776
    }
777
if(nsing < 1)
996,221✔
778
    goto L150;
×
779

780
for( k=0; k<nsing; k++ )
4,993,689✔
781
    {
782
    j = nsing - k - 1;
3,997,468✔
783
    sum = zero;
3,997,468✔
784
    jp1 = j + 1;
785
    if(nsing > jp1)
3,997,468✔
786
        {
787
        ij = jp1 + ldr * j;
3,001,247✔
788
        for( i=jp1; i<nsing; i++ )
9,158,288✔
789
            {
790
            sum += r[ij]*wa[i];
6,157,041✔
791
            ij += 1; /* [i+ldr*j] */
6,157,041✔
792
            }
793
        }
794
    wa[j] = (wa[j] - sum)/sdiag[j];
3,997,468✔
795
    }
796
L150:
996,221✔
797
/*
798
*     permute the components of z back to components of x.
799
*/
800
for( j=0; j<n; j++ )
4,995,709✔
801
    {
802
    l = ipvt[j];
3,999,488✔
803
    x[l] = wa[j];
3,999,488✔
804
    }
805
/*
806
*     last card of subroutine qrsolv.
807
*/
808
}
996,221✔
809

810
/************************lmpar.c*************************/
811

812

813
void lmpar(int n,
786,550✔
814
           Real* r,
815
           int ldr,
816
           int* ipvt,
817
           const Real* diag,
818
           Real* qtb,
819
           Real delta,
820
           Real* par,
821
           Real* x,
822
           Real* sdiag,
823
           Real* wa1,
824
           Real* wa2) {
825
    /*     **********
826
     *
827
     *     subroutine lmpar
828
     *
829
     *     given an m by n matrix a, an n by n nonsingular diagonal
830
     *     matrix d, an m-vector b, and a positive number delta,
831
     *     the problem is to determine a value for the parameter
832
     *     par such that if x solves the system
833
     *
834
     *       a*x = b ,     sqrt(par)*d*x = 0 ,
835
     *
836
     *     in the least squares sense, and dxnorm is the euclidean
837
     *     norm of d*x, then either par is zero and
838
     *
839
     *       (dxnorm-delta) .le. 0.1*delta ,
840
     *
841
     *     or par is positive and
842
     *
843
     *       abs(dxnorm-delta) .le. 0.1*delta .
844
     *
845
     *     this subroutine completes the solution of the problem
846
     *     if it is provided with the necessary information from the
847
     *     qr factorization, with column pivoting, of a. that is, if
848
     *     a*p = q*r, where p is a permutation matrix, q has orthogonal
849
     *     columns, and r is an upper triangular matrix with diagonal
850
     *     elements of nonincreasing magnitude, then lmpar expects
851
     *     the full upper triangle of r, the permutation matrix p,
852
     *     and the first n components of (q transpose)*b. on output
853
     *     lmpar also provides an upper triangular matrix s such that
854
     *
855
     *        t   t           t
856
     *       p *(a *a + par*d*d)*p = s *s .
857
     *
858
     *     s is employed within lmpar and may be of separate interest.
859
     *
860
     *     only a few iterations are generally needed for convergence
861
     *     of the algorithm. if, however, the limit of 10 iterations
862
     *     is reached, then the output par will contain the best
863
     *     value obtained so far.
864
     *
865
     *     the subroutine statement is
866
     *
867
     *   subroutine lmpar(n,r,ldr,ipvt,diag,qtb,delta,par,x,sdiag,
868
     *            wa1,wa2)
869
     *
870
     *     where
871
     *
872
     *   n is a positive integer input variable set to the order of r.
873
     *
874
     *   r is an n by n array. on input the full upper triangle
875
     *     must contain the full upper triangle of the matrix r.
876
     *     on output the full upper triangle is unaltered, and the
877
     *     strict lower triangle contains the strict upper triangle
878
     *     (transposed) of the upper triangular matrix s.
879
     *
880
     *   ldr is a positive integer input variable not less than n
881
     *     which specifies the leading dimension of the array r.
882
     *
883
     *   ipvt is an integer input array of length n which defines the
884
     *     permutation matrix p such that a*p = q*r. column j of p
885
     *     is column ipvt(j) of the identity matrix.
886
     *
887
     *   diag is an input array of length n which must contain the
888
     *     diagonal elements of the matrix d.
889
     *
890
     *   qtb is an input array of length n which must contain the first
891
     *     n elements of the vector (q transpose)*b.
892
     *
893
     *   delta is a positive input variable which specifies an upper
894
     *     bound on the euclidean norm of d*x.
895
     *
896
     *   par is a nonnegative variable. on input par contains an
897
     *     initial estimate of the levenberg-marquardt parameter.
898
     *     on output par contains the final estimate.
899
     *
900
     *   x is an output array of length n which contains the least
901
     *     squares solution of the system a*x = b, sqrt(par)*d*x = 0,
902
     *     for the output par.
903
     *
904
     *   sdiag is an output array of length n which contains the
905
     *     diagonal elements of the upper triangular matrix s.
906
     *
907
     *   wa1 and wa2 are work arrays of length n.
908
     *
909
     *     subprograms called
910
     *
911
     *   minpack-supplied ... dpmpar,enorm,qrsolv
912
     *
913
     *   fortran-supplied ... dabs,dmax1,dmin1,dsqrt
914
     *
915
     *     argonne national laboratory. minpack project. march 1980.
916
     *     burton s. garbow, kenneth e. hillstrom, jorge j. more
917
     *
918
     *     **********
919
     */
920
    int i, iter, ij, jj, j, jm1, jp1, k, l, nsing;
921
    Real dxnorm, fp, gnorm, parc, parl, paru;
922
    Real sum, temp;
923
    static double zero = 0.0;
924
    static double p1 = 0.1;
925
    static double p001 = 0.001;
926

927

928
    /*
929
     *     compute and store in x the gauss-newton direction. if the
930
     *     jacobian is rank-deficient, obtain a least squares solution.
931
     */
932
    nsing = n;
933
    jj = 0;
934
    for (j = 0; j < n; j++) {
3,943,772✔
935
        wa1[j] = qtb[j];
3,157,222✔
936
        if ((r[jj] == zero) && (nsing == n))
3,157,222✔
937
            nsing = j;
938
        if (nsing < n)
3,157,222✔
939
            wa1[j] = zero;
8,759✔
940
        jj += ldr + 1; /* [j+ldr*j] */
3,157,222✔
941
    }
942
if(nsing >= 1)
786,550✔
943
    {
944
    for( k=0; k<nsing; k++ )
3,935,013✔
945
        {
946
        j = nsing - k - 1;
3,148,463✔
947
        wa1[j] = wa1[j]/r[j+ldr*j];
3,148,463✔
948
        temp = wa1[j];
949
        jm1 = j - 1;
3,148,463✔
950
        if(jm1 >= 0)
3,148,463✔
951
            {
952
            ij = ldr * j;
953
            for( i=0; i<=jm1; i++ )
7,203,071✔
954
                {
955
                wa1[i] -= r[ij]*temp;
4,841,158✔
956
                ij += 1;
4,841,158✔
957
                }
958
            }
959
        }
960
    }
961

962
for( j=0; j<n; j++ )
3,943,772✔
963
    {
964
    l = ipvt[j];
3,157,222✔
965
    x[l] = wa1[j];
3,157,222✔
966
    }
967
/*
968
*     initialize the iteration counter.
969
*     evaluate the function at the origin, and test
970
*     for acceptance of the gauss-newton direction.
971
*/
972
iter = 0;
973
for( j=0; j<n; j++ )
3,943,772✔
974
    wa2[j] = diag[j]*x[j];
3,157,222✔
975
dxnorm = enorm(n,wa2);
786,550✔
976
fp = dxnorm - delta;
786,550✔
977
if(fp <= p1*delta)
786,550✔
978
    {
979
    goto L220;
15,744✔
980
    }
981
/*
982
*     if the jacobian is not rank deficient, the newton
983
*     step provides a lower bound, parl, for the zero of
984
*     the function. otherwise set this bound to zero.
985
*/
986
parl = zero;
770,806✔
987
if(nsing >= n)
770,806✔
988
    {
989
    for( j=0; j<n; j++ )
3,830,769✔
990
        {
991
        l = ipvt[j];
3,066,706✔
992
        wa1[j] = diag[l]*(wa2[l]/dxnorm);
3,066,706✔
993
        }
994
    jj = 0;
995
    for( j=0; j<n; j++ )
3,830,769✔
996
        {
997
        sum = zero;
998
        jm1 = j - 1;
3,066,706✔
999
        if(jm1 >= 0)
3,066,706✔
1000
            {
1001
            ij = jj;
1002
            for( i=0; i<=jm1; i++ )
6,992,797✔
1003
                {
1004
                sum += r[ij]*wa1[i];
4,690,154✔
1005
                ij += 1;
4,690,154✔
1006
                }
1007
            }
1008
        wa1[j] = (wa1[j] - sum)/r[j+ldr*j];
3,066,706✔
1009
        jj += ldr; /* [i+ldr*j] */
3,066,706✔
1010
        }
1011
    temp = enorm(n,wa1);
764,063✔
1012
    parl = ((fp/delta)/temp)/temp;
764,063✔
1013
    }
1014
/*
1015
*     calculate an upper bound, paru, for the zero of the function.
1016
*/
1017
jj = 0;
1018
for( j=0; j<n; j++ )
3,865,108✔
1019
    {
1020
    sum = zero;
1021
    ij = jj;
1022
    for( i=0; i<=j; i++ )
10,928,183✔
1023
        {
1024
        sum += r[ij]*qtb[i];
7,833,881✔
1025
        ij += 1;
7,833,881✔
1026
        }
1027
    l = ipvt[j];
3,094,302✔
1028
    wa1[j] = sum/diag[l];
3,094,302✔
1029
    jj += ldr; /* [i+ldr*j] */
3,094,302✔
1030
    }
1031
gnorm = enorm(n,wa1);
770,806✔
1032
paru = gnorm/delta;
770,806✔
1033
if(paru == zero)
770,806✔
1034
    paru = DWARF/dmin1(delta,p1);
×
1035
/*
1036
*     if the input par lies outside of the interval (parl,paru),
1037
*     set par to the closer endpoint.
1038
*/
1039
*par = dmax1( *par,parl);
770,806✔
1040
*par = dmin1( *par,paru);
770,806✔
1041
if( *par == zero)
770,806✔
1042
    *par = gnorm/dxnorm;
53✔
1043
/*
1044
*     beginning of an iteration.
1045
*/
1046
L150:
770,753✔
1047
iter += 1;
996,052✔
1048
/*
1049
*    evaluate the function at the current value of par.
1050
*/
1051
if( *par == zero)
996,052✔
1052
    *par = dmax1(DWARF,p001*paru);
145✔
1053
temp = std::sqrt( *par );
996,052✔
1054
for( j=0; j<n; j++ )
4,990,966✔
1055
    wa1[j] = temp*diag[j];
3,994,914✔
1056
qrsolv(n,r,ldr,ipvt,wa1,qtb,x,sdiag,wa2);
996,052✔
1057
for( j=0; j<n; j++ )
4,990,966✔
1058
    wa2[j] = diag[j]*x[j];
3,994,914✔
1059
dxnorm = enorm(n,wa2);
996,052✔
1060
temp = fp;
1061
fp = dxnorm - delta;
996,052✔
1062
/*
1063
*    if the function is small enough, accept the current value
1064
*    of par. also test for the exceptional cases where parl
1065
*    is zero or the number of iterations has reached 10.
1066
*/
1067
if( (std::fabs(fp) <= p1*delta)
996,052✔
1068
 || ((parl == zero) && (fp <= temp) && (temp < zero))
225,246✔
1069
 || (iter == 10) )
225,246✔
1070
    goto L220;
770,806✔
1071
/*
1072
*    compute the newton correction.
1073
*/
1074
for( j=0; j<n; j++ )
1,125,858✔
1075
    {
1076
    l = ipvt[j];
900,612✔
1077
    wa1[j] = diag[l]*(wa2[l]/dxnorm);
900,612✔
1078
    }
1079
jj = 0;
1080
for( j=0; j<n; j++ )
1,125,858✔
1081
    {
1082
    wa1[j] = wa1[j]/sdiag[j];
900,612✔
1083
    temp = wa1[j];
1084
    jp1 = j + 1;
900,612✔
1085
    if(jp1 < n)
900,612✔
1086
        {
1087
        ij = jp1 + jj;
675,366✔
1088
        for( i=jp1; i<n; i++ )
2,041,741✔
1089
            {
1090
            wa1[i] -= r[ij]*temp;
1,366,375✔
1091
            ij += 1; /* [i+ldr*j] */
1,366,375✔
1092
            }
1093
        }
1094
    jj += ldr; /* ldr*j */
900,612✔
1095
    }
1096
temp = enorm(n,wa1);
225,246✔
1097
parc = ((fp/delta)/temp)/temp;
225,246✔
1098
/*
1099
*    depending on the sign of the function, update parl or paru.
1100
*/
1101
if(fp > zero)
225,246✔
1102
    parl = dmax1(parl, *par);
154,364✔
1103
if(fp < zero)
225,246✔
1104
    paru = dmin1(paru, *par);
70,882✔
1105
/*
1106
*    compute an improved estimate for par.
1107
*/
1108
*par = dmax1(parl, *par + parc);
225,246✔
1109
/*
1110
*    end of an iteration.
1111
*/
1112
goto L150;
225,246✔
1113

1114
L220:
1115
/*
1116
*     termination.
1117
*/
1118
if(iter == 0)
770,806✔
1119
    *par = zero;
15,744✔
1120
/*
1121
*     last card of subroutine lmpar.
1122
*/
1123
}
786,550✔
1124

1125
/*********************** lmdif.c ****************************/
1126

1127

1128

1129

1130
void lmdif(int m,int n,Real* x,Real* fvec,Real ftol,
3,598✔
1131
      Real xtol,Real gtol,int maxfev,Real epsfcn,
1132
      Real* diag, int mode, Real factor,
1133
      int nprint, int* info,int* nfev,Real* fjac,
1134
      int ldfjac,int* ipvt,Real* qtf,
1135
      Real* wa1,Real* wa2,Real* wa3,Real* wa4,
1136
      const QuantLib::MINPACK::LmdifCostFunction& fcn,
1137
      const QuantLib::MINPACK::LmdifCostFunction& jacFcn)
1138
{
1139
/*
1140
*     **********
1141
*
1142
*     subroutine lmdif
1143
*
1144
*     the purpose of lmdif is to minimize the sum of the squares of
1145
*     m nonlinear functions in n variables by a modification of
1146
*     the levenberg-marquardt algorithm. the user must provide a
1147
*     subroutine which calculates the functions. the jacobian is
1148
*     then calculated by a forward-difference approximation.
1149
*
1150
*     the subroutine statement is
1151
*
1152
*   subroutine lmdif(fcn,m,n,x,fvec,ftol,xtol,gtol,maxfev,epsfcn,
1153
*            diag,mode,factor,nprint,info,nfev,fjac,
1154
*            ldfjac,ipvt,qtf,wa1,wa2,wa3,wa4)
1155
*
1156
*     where
1157
*
1158
*   fcn is the name of the user-supplied subroutine which
1159
*     calculates the functions. fcn must be declared
1160
*     in an external statement in the user calling
1161
*     program, and should be written as follows.
1162
*
1163
*     subroutine fcn(m,n,x,fvec,iflag)
1164
*     integer m,n,iflag
1165
*     double precision x(n),fvec(m)
1166
*     ----------
1167
*     calculate the functions at x and
1168
*     return this vector in fvec.
1169
*     ----------
1170
*     return
1171
*     end
1172
*
1173
*     the value of iflag should not be changed by fcn unless
1174
*     the user wants to terminate execution of lmdif.
1175
*     in this case set iflag to a negative integer.
1176
*
1177
*   m is a positive integer input variable set to the number
1178
*     of functions.
1179
*
1180
*   n is a positive integer input variable set to the number
1181
*     of variables. n must not exceed m.
1182
*
1183
*   x is an array of length n. on input x must contain
1184
*     an initial estimate of the solution vector. on output x
1185
*     contains the final estimate of the solution vector.
1186
*
1187
*   fvec is an output array of length m which contains
1188
*     the functions evaluated at the output x.
1189
*
1190
*   ftol is a nonnegative input variable. termination
1191
*     occurs when both the actual and predicted relative
1192
*     reductions in the sum of squares are at most ftol.
1193
*     therefore, ftol measures the relative error desired
1194
*     in the sum of squares.
1195
*
1196
*   xtol is a nonnegative input variable. termination
1197
*     occurs when the relative error between two consecutive
1198
*     iterates is at most xtol. therefore, xtol measures the
1199
*     relative error desired in the approximate solution.
1200
*
1201
*   gtol is a nonnegative input variable. termination
1202
*     occurs when the cosine of the angle between fvec and
1203
*     any column of the jacobian is at most gtol in absolute
1204
*     value. therefore, gtol measures the orthogonality
1205
*     desired between the function vector and the columns
1206
*     of the jacobian.
1207
*
1208
*   maxfev is a positive integer input variable. termination
1209
*     occurs when the number of calls to fcn is at least
1210
*     maxfev by the end of an iteration.
1211
*
1212
*   epsfcn is an input variable used in determining a suitable
1213
*     step length for the forward-difference approximation. this
1214
*     approximation assumes that the relative errors in the
1215
*     functions are of the order of epsfcn. if epsfcn is less
1216
*     than the machine precision, it is assumed that the relative
1217
*     errors in the functions are of the order of the machine
1218
*     precision.
1219
*
1220
*   diag is an array of length n. if mode = 1 (see
1221
*     below), diag is internally set. if mode = 2, diag
1222
*     must contain positive entries that serve as
1223
*     multiplicative scale factors for the variables.
1224
*
1225
*   mode is an integer input variable. if mode = 1, the
1226
*     variables will be scaled internally. if mode = 2,
1227
*     the scaling is specified by the input diag. other
1228
*     values of mode are equivalent to mode = 1.
1229
*
1230
*   factor is a positive input variable used in determining the
1231
*     initial step bound. this bound is set to the product of
1232
*     factor and the euclidean norm of diag*x if nonzero, or else
1233
*     to factor itself. in most cases factor should lie in the
1234
*     interval (.1,100.). 100. is a generally recommended value.
1235
*
1236
*   nprint is an integer input variable that enables controlled
1237
*     printing of iterates if it is positive. in this case,
1238
*     fcn is called with iflag = 0 at the beginning of the first
1239
*     iteration and every nprint iterations thereafter and
1240
*     immediately prior to return, with x and fvec available
1241
*     for printing. if nprint is not positive, no special calls
1242
*     of fcn with iflag = 0 are made.
1243
*
1244
*   info is an integer output variable. if the user has
1245
*     terminated execution, info is set to the (negative)
1246
*     value of iflag. see description of fcn. otherwise,
1247
*     info is set as follows.
1248
*
1249
*     info = 0  improper input parameters.
1250
*
1251
*     info = 1  both actual and predicted relative reductions
1252
*           in the sum of squares are at most ftol.
1253
*
1254
*     info = 2  relative error between two consecutive iterates
1255
*           is at most xtol.
1256
*
1257
*     info = 3  conditions for info = 1 and info = 2 both hold.
1258
*
1259
*     info = 4  the cosine of the angle between fvec and any
1260
*           column of the jacobian is at most gtol in
1261
*           absolute value.
1262
*
1263
*     info = 5  number of calls to fcn has reached or
1264
*           exceeded maxfev.
1265
*
1266
*     info = 6  ftol is too small. no further reduction in
1267
*           the sum of squares is possible.
1268
*
1269
*     info = 7  xtol is too small. no further improvement in
1270
*           the approximate solution x is possible.
1271
*
1272
*     info = 8  gtol is too small. fvec is orthogonal to the
1273
*           columns of the jacobian to machine precision.
1274
*
1275
*   nfev is an integer output variable set to the number of
1276
*     calls to fcn.
1277
*
1278
*   fjac is an output m by n array. the upper n by n submatrix
1279
*     of fjac contains an upper triangular matrix r with
1280
*     diagonal elements of nonincreasing magnitude such that
1281
*
1282
*        t     t       t
1283
*       p *(jac *jac)*p = r *r,
1284
*
1285
*     where p is a permutation matrix and jac is the final
1286
*     calculated jacobian. column j of p is column ipvt(j)
1287
*     (see below) of the identity matrix. the lower trapezoidal
1288
*     part of fjac contains information generated during
1289
*     the computation of r.
1290
*
1291
*   ldfjac is a positive integer input variable not less than m
1292
*     which specifies the leading dimension of the array fjac.
1293
*
1294
*   ipvt is an integer output array of length n. ipvt
1295
*     defines a permutation matrix p such that jac*p = q*r,
1296
*     where jac is the final calculated jacobian, q is
1297
*     orthogonal (not stored), and r is upper triangular
1298
*     with diagonal elements of nonincreasing magnitude.
1299
*     column j of p is column ipvt(j) of the identity matrix.
1300
*
1301
*   qtf is an output array of length n which contains
1302
*     the first n elements of the vector (q transpose)*fvec.
1303
*
1304
*   wa1, wa2, and wa3 are work arrays of length n.
1305
*
1306
*   wa4 is a work array of length m.
1307
*
1308
*     subprograms called
1309
*
1310
*   user-supplied ...... fcn, jacFcn
1311
*
1312
*   minpack-supplied ... dpmpar,enorm,fdjac2,lmpar,qrfac
1313
*
1314
*   fortran-supplied ... dabs,dmax1,dmin1,dsqrt,mod
1315
*
1316
*     argonne national laboratory. minpack project. march 1980.
1317
*     burton s. garbow, kenneth e. hillstrom, jorge j. more
1318
*
1319
*     **********
1320
*/
1321
int i,iflag,ij,jj,iter,j,l;
1322
Real actred,delta=0,dirder,fnorm,fnorm1,gnorm;
1323
Real par,pnorm,prered,ratio;
1324
Real sum,temp,temp1,temp2,temp3,xnorm=0;
1325
static double one = 1.0;
1326
static double p1 = 0.1;
1327
static double p5 = 0.5;
1328
static double p25 = 0.25;
1329
static double p75 = 0.75;
1330
static double p0001 = 1.0e-4;
1331
static double zero = 0.0;
1332

1333
*info = 0;
3,598✔
1334
iflag = 0;
3,598✔
1335
*nfev = 0;
3,598✔
1336
/*
1337
*     check the input parameters for errors.
1338
*/
1339
if( (n <= 0) || (m < n) || (ldfjac < m) || (ftol < zero)
3,598✔
1340
    || (xtol < zero) || (gtol < zero) || (maxfev <= 0)
3,598✔
1341
    || (factor <= zero) )
3,598✔
1342
    goto L300;
×
1343

1344
if( mode == 2 )
3,598✔
1345
    { /* scaling by diag[] */
1346
    for( j=0; j<n; j++ )
×
1347
        {
1348
        if( diag[j] <= 0.0 )
×
1349
            goto L300;
×
1350
        }
1351
    }
1352
/*
1353
*     evaluate the function at the starting point
1354
*     and calculate its norm.
1355
*/
1356
iflag = 1;
3,598✔
1357
fcn(m,n,x,fvec,&iflag);
3,598✔
1358
*nfev = 1;
3,598✔
1359
if(iflag < 0)
3,598✔
1360
    goto L300;
×
1361
fnorm = enorm(m,fvec);
3,598✔
1362
/*
1363
*     initialize levenberg-marquardt parameter and iteration counter.
1364
*/
1365
par = zero;
3,598✔
1366
iter = 1;
1367
/*
1368
*     beginning of the outer loop.
1369
*/
1370

1371
L30:
763,389✔
1372

1373
/*
1374
*    calculate the jacobian matrix.
1375
*/
1376
iflag = 2;
763,389✔
1377
if (!jacFcn)
763,389✔
1378
    fdjac2(m,n,x,fvec,fjac,ldfjac,&iflag,epsfcn,wa4, fcn);
763,377✔
1379
else // use user supplied jacobian calculation
1380
    jacFcn(m,n,x,fjac,&iflag);
12✔
1381
*nfev += n;
763,389✔
1382
if(iflag < 0)
763,389✔
1383
    goto L300;
×
1384
/*
1385
*    if requested, call fcn to enable printing of iterates.
1386
*/
1387
if( nprint > 0 )
763,389✔
1388
    {
1389
    iflag = 0;
×
1390
    if(mod(iter-1,nprint) == 0)
×
1391
        {
1392
        fcn(m,n,x,fvec,&iflag);
×
1393
        if(iflag < 0)
×
1394
            goto L300;
×
1395
        }
1396
    }
1397
/*
1398
*    compute the qr factorization of the jacobian.
1399
*/
1400
qrfac(m,n,fjac,ldfjac,1,ipvt,n,wa1,wa2,wa3);
763,389✔
1401
/*
1402
*    on the first iteration and if mode is 1, scale according
1403
*    to the norms of the columns of the initial jacobian.
1404
*/
1405
if(iter == 1)
763,389✔
1406
    {
1407
    if(mode != 2)
3,598✔
1408
        {
1409
        for( j=0; j<n; j++ )
23,528✔
1410
            {
1411
            diag[j] = wa2[j];
19,930✔
1412
            if( wa2[j] == zero )
19,930✔
1413
                diag[j] = one;
7,112✔
1414
            }
1415
        }
1416

1417
/*
1418
*    on the first iteration, calculate the norm of the scaled x
1419
*    and initialize the step bound delta.
1420
*/
1421
    for( j=0; j<n; j++ )
23,528✔
1422
        wa3[j] = diag[j] * x[j];
19,930✔
1423

1424
    xnorm = enorm(n,wa3);
3,598✔
1425
    delta = factor*xnorm;
3,598✔
1426
    if(delta == zero)
3,598✔
1427
        delta = factor;
1428
    }
1429

1430
/*
1431
*    form (q transpose)*fvec and store the first n components in
1432
*    qtf.
1433
*/
1434
for( i=0; i<m; i++ )
6,803,941✔
1435
    wa4[i] = fvec[i];
6,040,552✔
1436
jj = 0;
1437
for( j=0; j<n; j++ )
3,834,565✔
1438
    {
1439
    temp3 = fjac[jj];
3,071,176✔
1440
    if(temp3 != zero)
3,071,176✔
1441
        {
1442
        sum = zero;
1443
        ij = jj;
1444
        for( i=j; i<m; i++ )
20,709,310✔
1445
            {
1446
            sum += fjac[ij] * wa4[i];
17,652,475✔
1447
            ij += 1;    /* fjac[i+m*j] */
17,652,475✔
1448
            }
1449
        temp = -sum / temp3;
3,056,835✔
1450
        ij = jj;
1451
        for( i=j; i<m; i++ )
20,709,310✔
1452
            {
1453
            wa4[i] += fjac[ij] * temp;
17,652,475✔
1454
            ij += 1;    /* fjac[i+m*j] */
17,652,475✔
1455
            }
1456
        }
1457
    fjac[jj] = wa1[j];
3,071,176✔
1458
    jj += m+1;  /* fjac[j+m*j] */
3,071,176✔
1459
    qtf[j] = wa4[j];
3,071,176✔
1460
    }
1461

1462
/*
1463
*    compute the norm of the scaled gradient.
1464
*/
1465
 gnorm = zero;
763,389✔
1466
 if(fnorm != zero)
763,389✔
1467
    {
1468
    jj = 0;
1469
    for( j=0; j<n; j++ )
3,834,557✔
1470
        {
1471
        l = ipvt[j];
3,071,171✔
1472
        if(wa2[l] != zero)
3,071,171✔
1473
            {
1474
            sum = zero;
1475
            ij = jj;
1476
            for( i=0; i<=j; i++ )
10,816,006✔
1477
                {
1478
                sum += fjac[ij]*(qtf[i]/fnorm);
7,759,175✔
1479
                ij += 1; /* fjac[i+m*j] */
7,759,175✔
1480
                }
1481
            gnorm = dmax1(gnorm,std::fabs(sum/wa2[l]));
3,056,831✔
1482
            }
1483
        jj += m;
3,071,171✔
1484
        }
1485
    }
1486

1487
/*
1488
*    test for convergence of the gradient norm.
1489
*/
1490
 if(gnorm <= gtol)
763,389✔
1491
    *info = 4;
525✔
1492
 if( *info != 0)
763,389✔
1493
    goto L300;
525✔
1494
/*
1495
*    rescale if necessary.
1496
*/
1497
 if(mode != 2)
762,864✔
1498
    {
1499
    for( j=0; j<n; j++ )
3,826,481✔
1500
        diag[j] = dmax1(diag[j],wa2[j]);
3,063,617✔
1501
    }
1502

1503
/*
1504
*    beginning of the inner loop.
1505
*/
1506
L200:
×
1507
/*
1508
*       determine the levenberg-marquardt parameter.
1509
*/
1510
lmpar(n,fjac,ldfjac,ipvt,diag,qtf,delta,&par,wa1,wa2,wa3,wa4);
786,550✔
1511
/*
1512
*       store the direction p and x + p. calculate the norm of p.
1513
*/
1514
for( j=0; j<n; j++ )
3,943,772✔
1515
    {
1516
       wa1[j] = -wa1[j];
3,157,222✔
1517
       wa2[j] = x[j] + wa1[j];
3,157,222✔
1518
       wa3[j] = diag[j]*wa1[j];
3,157,222✔
1519
    }
1520
pnorm = enorm(n,wa3);
786,550✔
1521
/*
1522
*       on the first iteration, adjust the initial step bound.
1523
*/
1524
if(iter == 1)
786,550✔
1525
    delta = dmin1(delta,pnorm);
6,291✔
1526
/*
1527
*       evaluate the function at x + p and calculate its norm.
1528
*/
1529
iflag = 1;
786,550✔
1530
fcn(m,n,wa2,wa4,&iflag);
786,550✔
1531
*nfev += 1;
786,550✔
1532
if(iflag < 0)
786,550✔
1533
    goto L300;
×
1534
fnorm1 = enorm(m,wa4);
786,550✔
1535
/*
1536
*       compute the scaled actual reduction.
1537
*/
1538
actred = -one;
786,550✔
1539
if( (p1*fnorm1) < fnorm)
786,550✔
1540
    {
1541
    temp = fnorm1/fnorm;
783,993✔
1542
    actred = one - temp * temp;
783,993✔
1543
    }
1544
/*
1545
*       compute the scaled predicted reduction and
1546
*       the scaled directional derivative.
1547
*/
1548
jj = 0;
1549
for( j=0; j<n; j++ )
3,943,772✔
1550
    {
1551
    wa3[j] = zero;
3,157,222✔
1552
    l = ipvt[j];
3,157,222✔
1553
    temp = wa1[l];
3,157,222✔
1554
    ij = jj;
1555
    for( i=0; i<=j; i++ )
11,181,834✔
1556
        {
1557
        wa3[i] += fjac[ij]*temp;
8,024,612✔
1558
        ij += 1; /* fjac[i+m*j] */
8,024,612✔
1559
        }
1560
    jj += m;
3,157,222✔
1561
    }
1562
temp1 = enorm(n,wa3)/fnorm;
786,550✔
1563
temp2 = (std::sqrt(par)*pnorm)/fnorm;
786,550✔
1564
prered = temp1*temp1 + (temp2*temp2)/p5;
786,550✔
1565
dirder = -(temp1*temp1 + temp2*temp2);
786,550✔
1566
/*
1567
*       compute the ratio of the actual to the predicted
1568
*       reduction.
1569
*/
1570
ratio = zero;
786,550✔
1571
if(prered != zero)
786,550✔
1572
    ratio = actred/prered;
786,550✔
1573
/*
1574
*       update the step bound.
1575
*/
1576
if(ratio <= p25)
786,550✔
1577
    {
1578
    if(actred >= zero)
34,988✔
1579
        temp = p5;
1580
    else
1581
        temp = p5*dirder/(dirder + p5*actred);
24,354✔
1582
    if( ((p1*fnorm1) >= fnorm)
34,988✔
1583
    || (temp < p1) )
32,431✔
1584
        temp = p1;
1585
       delta = temp*dmin1(delta,pnorm/p1);
34,988✔
1586
       par = par/temp;
34,988✔
1587
    }
1588
else
1589
    {
1590
    if( (par == zero) || (ratio >= p75) )
751,562✔
1591
        {
1592
        delta = pnorm/p5;
57,224✔
1593
        par = p5*par;
57,224✔
1594
        }
1595
    }
1596
/*
1597
*       test for successful iteration.
1598
*/
1599
if(ratio >= p0001)
786,550✔
1600
    {
1601
/*
1602
*       successful iteration. update x, fvec, and their norms.
1603
*/
1604
    for( j=0; j<n; j++ )
3,823,034✔
1605
        {
1606
        x[j] = wa2[j];
3,060,864✔
1607
        wa2[j] = diag[j]*x[j];
3,060,864✔
1608
        }
1609
    for( i=0; i<m; i++ )
6,789,373✔
1610
        fvec[i] = wa4[i];
6,027,203✔
1611
    xnorm = enorm(n,wa2);
762,170✔
1612
    fnorm = fnorm1;
1613
    iter += 1;
762,170✔
1614
    }
1615
/*
1616
*       tests for convergence.
1617
*/
1618
if( (std::fabs(actred) <= ftol)
786,550✔
1619
  && (prered <= ftol)
3,997✔
1620
  && (p5*ratio <= one) )
2,854✔
1621
    *info = 1;
2,276✔
1622
if(delta <= xtol*xnorm)
786,550✔
1623
    *info = 2;
1,486✔
1624
if( (std::fabs(actred) <= ftol)
786,550✔
1625
  && (prered <= ftol)
3,997✔
1626
  && (p5*ratio <= one)
2,854✔
1627
  && ( *info == 2) )
2,276✔
1628
    *info = 3;
698✔
1629
if( *info != 0)
786,550✔
1630
    goto L300;
3,064✔
1631
/*
1632
*       tests for termination and stringent tolerances.
1633
*/
1634
if( *nfev >= maxfev)
783,486✔
1635
    *info = 5;
9✔
1636
if( (std::fabs(actred) <= MACHEP)
783,486✔
UNCOV
1637
  && (prered <= MACHEP)
×
1638
  && (p5*ratio <= one) )
×
1639
    *info = 6;
×
1640
if(delta <= MACHEP*xnorm)
783,486✔
1641
    *info = 7;
×
1642
if(gnorm <= MACHEP)
783,486✔
1643
    *info = 8;
×
1644
if( *info != 0)
783,486✔
1645
    goto L300;
9✔
1646
/*
1647
*       end of the inner loop. repeat if iteration unsuccessful.
1648
*/
1649
if(ratio < p0001)
783,477✔
1650
    goto L200;
23,686✔
1651
/*
1652
*    end of the outer loop.
1653
*/
1654
goto L30;
759,791✔
1655

1656
L300:
3,598✔
1657
/*
1658
*     termination, either normal or user imposed.
1659
*/
1660
if(iflag < 0)
3,598✔
1661
    *info = iflag;
×
1662
iflag = 0;
3,598✔
1663
if(nprint > 0)
3,598✔
1664
    fcn(m,n,x,fvec,&iflag);
×
1665
/*
1666
      last card of subroutine lmdif.
1667
*/
1668
}
3,598✔
1669
}
1670

1671
/************************fdjac2.c*************************/
1672

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc