fsolve.f90
Go to the documentation of this file.
1 #include "../macros.h"
2 subroutine dogleg ( n, r, lr, diag, qtb, delta, x )
3 
4 !*****************************************************************************80
5 !
6 !! dogleg() finds the minimizing combination of Gauss-Newton and gradient steps.
7 !
8 ! Discussion:
9 !
10 ! Given an M by N matrix A, an N by N nonsingular diagonal
11 ! matrix D, an M-vector B, and a positive number DELTA, the
12 ! problem is to determine the convex combination X of the
13 ! Gauss-Newton and scaled gradient directions that minimizes
14 ! (A*X - B) in the least squares sense, subject to the
15 ! restriction that the euclidean norm of D*X be at most DELTA.
16 !
17 ! This function completes the solution of the problem
18 ! if it is provided with the necessary information from the
19 ! QR factorization of A. That is, if A = Q*R, where Q has
20 ! orthogonal columns and R is an upper triangular matrix,
21 ! then DOGLEG expects the full upper triangle of R and
22 ! the first N components of Q'*B.
23 !
24 ! Licensing:
25 !
26 ! This code is distributed under the GNU LGPL license.
27 !
28 ! Modified:
29 !
30 ! 06 April 2010
31 !
32 ! Author:
33 !
34 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
35 ! FORTRAN90 version by John Burkardt.
36 !
37 ! Reference:
38 !
39 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
40 ! User Guide for MINPACK-1,
41 ! Technical Report ANL-80-74,
42 ! Argonne National Laboratory, 1980.
43 !
44 ! Parameters:
45 !
46 ! Input, integer N, the order of the matrix R.
47 !
48 ! Input, real ( kind = rk ) R(LR), the upper triangular matrix R stored
49 ! by rows.
50 !
51 ! Input, integer LR, the size of the R array, which must be
52 ! no less than (N*(N+1))/2.
53 !
54 ! Input, real ( kind = rk ) DIAG(N), the diagonal elements of the matrix D.
55 !
56 ! Input, real ( kind = rk ) QTB(N), the first N elements of the vector Q'* B.
57 !
58 ! Input, real ( kind = rk ) DELTA, is a positive upper bound on the
59 ! euclidean norm of D*X(1:N).
60 !
61 ! Output, real ( kind = rk ) X(N), the desired convex combination of the
62 ! Gauss-Newton direction and the scaled gradient direction.
63 !
64  implicit none
65 
66  integer, parameter :: rk = r_kind
67 
68  integer lr
69  integer n
70 
71  real ( kind = rk ) alpha
72  real ( kind = rk ) bnorm
73  real ( kind = rk ) delta
74  real ( kind = rk ) diag(n)
75  real ( kind = rk ) enorm
76  real ( kind = rk ) epsmch
77  real ( kind = rk ) gnorm
78  integer i
79  integer j
80  integer jj
81  integer k
82  integer l
83  real ( kind = rk ) qnorm
84  real ( kind = rk ) qtb(n)
85  real ( kind = rk ) r(lr)
86  real ( kind = rk ) sgnorm
87  real ( kind = rk ) sum2
88  real ( kind = rk ) temp
89  real ( kind = rk ) wa1(n)
90  real ( kind = rk ) wa2(n)
91  real ( kind = rk ) x(n)
92 
93  epsmch = epsilon( epsmch )
94 !
95 ! Calculate the Gauss-Newton direction.
96 !
97  jj = ( n * ( n + 1 ) ) / 2 + 1
98 
99  do k = 1, n
100 
101  j = n - k + 1
102  jj = jj - k
103  l = jj + 1
104  sum2 = 0.0d+00
105 
106  do i = j + 1, n
107  sum2 = sum2 + r(l) * x(i)
108  l = l + 1
109  end do
110 
111  temp = r(jj)
112 
113  if ( temp == 0.0d+00 ) then
114 
115  l = j
116  do i = 1, j
117  temp = max( temp, abs( r(l)) )
118  l = l + n - i
119  end do
120 
121  if ( temp == 0.0d+00 ) then
122  temp = epsmch
123  else
124  temp = epsmch * temp
125  end if
126 
127  end if
128 
129  x(j) = ( qtb(j) - sum2 ) / temp
130 
131  end do
132 !
133 ! Test whether the Gauss-Newton direction is acceptable.
134 !
135  wa1(1:n) = 0.0d+00
136  wa2(1:n) = diag(1:n) * x(1:n)
137  qnorm = enorm( n, wa2 )
138 
139  if ( qnorm <= delta ) then
140  return
141  end if
142 !
143 ! The Gauss-Newton direction is not acceptable.
144 ! Calculate the scaled gradient direction.
145 !
146  l = 1
147  do j = 1, n
148  temp = qtb(j)
149  do i = j, n
150  wa1(i) = wa1(i) + r(l) * temp
151  l = l + 1
152  end do
153  wa1(j) = wa1(j) / diag(j)
154  end do
155 !
156 ! Calculate the norm of the scaled gradient.
157 ! Test for the special case in which the scaled gradient is zero.
158 !
159  gnorm = enorm( n, wa1 )
160  sgnorm = 0.0d+00
161  alpha = delta / qnorm
162 
163  if ( gnorm /= 0.0d+00 ) then
164 !
165 ! Calculate the point along the scaled gradient which minimizes the quadratic.
166 !
167  wa1(1:n) = ( wa1(1:n) / gnorm ) / diag(1:n)
168 
169  l = 1
170  do j = 1, n
171  sum2 = 0.0d+00
172  do i = j, n
173  sum2 = sum2 + r(l) * wa1(i)
174  l = l + 1
175  end do
176  wa2(j) = sum2
177  end do
178 
179  temp = enorm( n, wa2 )
180  sgnorm = ( gnorm / temp ) / temp
181 !
182 ! Test whether the scaled gradient direction is acceptable.
183 !
184  alpha = 0.0d+00
185 !
186 ! The scaled gradient direction is not acceptable.
187 ! Calculate the point along the dogleg at which the quadratic is minimized.
188 !
189  if ( sgnorm < delta ) then
190 
191  bnorm = enorm( n, qtb )
192  temp = ( bnorm / gnorm ) * ( bnorm / qnorm ) * ( sgnorm / delta )
193  temp = temp - ( delta / qnorm ) * ( sgnorm / delta) ** 2 &
194  + sqrt( ( temp - ( delta / qnorm ) ) ** 2 &
195  + ( 1.0d+00 - ( delta / qnorm ) ** 2 ) &
196  * ( 1.0d+00 - ( sgnorm / delta ) ** 2 ) )
197 
198  alpha = ( ( delta / qnorm ) * ( 1.0d+00 - ( sgnorm / delta ) ** 2 ) ) &
199  / temp
200 
201  end if
202 
203  end if
204 !
205 ! Form appropriate convex combination of the Gauss-Newton
206 ! direction and the scaled gradient direction.
207 !
208  temp = ( 1.0d+00 - alpha ) * min( sgnorm, delta )
209 
210  x(1:n) = temp * wa1(1:n) + alpha * x(1:n)
211 
212  return
213 end
214 function enorm ( n, x )
215 
216 !*****************************************************************************80
217 !
218 !! enorm() computes the Euclidean norm of a vector.
219 !
220 ! Discussion:
221 !
222 ! The Euclidean norm is computed by accumulating the sum of
223 ! squares in three different sums. The sums of squares for the
224 ! small and large components are scaled so that no overflows
225 ! occur. Non-destructive underflows are permitted. Underflows
226 ! and overflows do not occur in the computation of the unscaled
227 ! sum of squares for the intermediate components.
228 !
229 ! The definitions of small, intermediate and large components
230 ! depend on two constants, RDWARF and RGIANT. The main
231 ! restrictions on these constants are that RDWARF^2 not
232 ! underflow and RGIANT^2 not overflow.
233 !
234 ! Licensing:
235 !
236 ! This code is distributed under the GNU LGPL license.
237 !
238 ! Modified:
239 !
240 ! 06 April 2010
241 !
242 ! Author:
243 !
244 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
245 ! FORTRAN90 version by John Burkardt.
246 !
247 ! Reference:
248 !
249 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
250 ! User Guide for MINPACK-1
251 ! Argonne National Laboratory,
252 ! Argonne, Illinois.
253 !
254 ! Input:
255 !
256 ! integer N, is the length of the vector.
257 !
258 ! real ( kind = rk ) X(N), the vector whose norm is desired.
259 !
260 ! Output:
261 !
262 ! real ( kind = rk ) ENORM, the Euclidean norm of the vector.
263 !
264  implicit none
265 
266  integer, parameter :: rk = r_kind
267 
268  integer n
269 
270  real ( kind = rk ) agiant
271  real ( kind = rk ) enorm
272  integer i
273  real ( kind = rk ) rdwarf
274  real ( kind = rk ) rgiant
275  real ( kind = rk ) s1
276  real ( kind = rk ) s2
277  real ( kind = rk ) s3
278  real ( kind = rk ) x(n)
279  real ( kind = rk ) xabs
280  real ( kind = rk ) x1max
281  real ( kind = rk ) x3max
282 
283  rdwarf = sqrt( tiny( rdwarf ) )
284  rgiant = sqrt( huge( rgiant ) )
285 
286  s1 = 0.0d+00
287  s2 = 0.0d+00
288  s3 = 0.0d+00
289  x1max = 0.0d+00
290  x3max = 0.0d+00
291  agiant = rgiant / real( n, kind = rk )
292 
293  do i = 1, n
294 
295  xabs = abs( x(i) )
296 
297  if ( xabs <= rdwarf ) then
298 
299  if ( x3max < xabs ) then
300  s3 = 1.0d+00 + s3 * ( x3max / xabs ) ** 2
301  x3max = xabs
302  else if ( xabs /= 0.0d+00 ) then
303  s3 = s3 + ( xabs / x3max ) ** 2
304  end if
305 
306  else if ( agiant <= xabs ) then
307 
308  if ( x1max < xabs ) then
309  s1 = 1.0d+00 + s1 * ( x1max / xabs ) ** 2
310  x1max = xabs
311  else
312  s1 = s1 + ( xabs / x1max ) ** 2
313  end if
314 
315  else
316 
317  s2 = s2 + xabs ** 2
318 
319  end if
320 
321  end do
322 !
323 ! Calculation of norm.
324 !
325  if ( s1 /= 0.0d+00 ) then
326 
327  enorm = x1max * sqrt( s1 + ( s2 / x1max ) / x1max )
328 
329  else if ( s2 /= 0.0d+00 ) then
330 
331  if ( x3max <= s2 ) then
332  enorm = sqrt( s2 * ( 1.0d+00 + ( x3max / s2 ) * ( x3max * s3 ) ) )
333  else
334  enorm = sqrt( x3max * ( ( s2 / x3max ) + ( x3max * s3 ) ) )
335  end if
336 
337  else
338 
339  enorm = x3max * sqrt( s3 )
340 
341  end if
342 
343  return
344 end
345 subroutine fdjac1 ( fcn, n, x, fvec, fjac, ldfjac, ml, mu, epsfcn )
346 
347 !*****************************************************************************80
348 !
349 !! fdjac1() estimates a jacobian matrix using forward differences.
350 !
351 ! Discussion:
352 !
353 ! This function computes a forward-difference approximation
354 ! to the N by N jacobian matrix associated with a specified
355 ! problem of N functions in N variables. If the jacobian has
356 ! a banded form, then function evaluations are saved by only
357 ! approximating the nonzero terms.
358 !
359 ! Licensing:
360 !
361 ! This code is distributed under the GNU LGPL license.
362 !
363 ! Modified:
364 !
365 ! 06 April 2010
366 !
367 ! Author:
368 !
369 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
370 ! FORTRAN90 version by John Burkardt.
371 !
372 ! Reference:
373 !
374 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
375 ! User Guide for MINPACK-1,
376 ! Technical Report ANL-80-74,
377 ! Argonne National Laboratory, 1980.
378 !
379 ! Parameters:
380 !
381 ! Input, external FCN, the name of the user-supplied subroutine which
382 ! calculates the functions. The routine should have the form:
383 ! subroutine fcn ( n, x, fvec )
384 ! integer n
385 ! real ( kind = rk ) fvec(n)
386 ! real ( kind = rk ) x(n)
387 !
388 ! Input, integer N, the number of functions and variables.
389 !
390 ! Input, real ( kind = rk ) X(N), the point where the jacobian is evaluated.
391 !
392 ! Input, real ( kind = rk ) FVEC(N), the functions evaluated at X.
393 !
394 ! Output, real ( kind = rk ) FJAC(LDFJAC,N), the N by N approximate
395 ! jacobian matrix.
396 !
397 ! Input, integer LDFJAC, the leading dimension of FJAC, which
398 ! must not be less than N.
399 !
400 ! Input, integer ML, MU, specify the number of subdiagonals and
401 ! superdiagonals within the band of the jacobian matrix. If the
402 ! jacobian is not banded, set ML and MU to N-1.
403 !
404 ! Input, real ( kind = rk ) EPSFCN, is used in determining a suitable step
405 ! length for the forward-difference approximation. This approximation
406 ! assumes that the relative errors in the functions are of the order of
407 ! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
408 ! the relative errors in the functions are of the order of the machine
409 ! precision.
410 !
411  implicit none
412 
413  integer, parameter :: rk = r_kind
414 
415  integer ldfjac
416  integer n
417 
418  real ( kind = rk ) eps
419  real ( kind = rk ) epsfcn
420  real ( kind = rk ) epsmch
421  external fcn
422  real ( kind = rk ) fjac(ldfjac,n)
423  real ( kind = rk ) fvec(n)
424  real ( kind = rk ) h
425  integer i
426  integer j
427  integer k
428  integer ml
429  integer msum
430  integer mu
431  real ( kind = rk ) temp
432  real ( kind = rk ) wa1(n)
433  real ( kind = rk ) wa2(n)
434  real ( kind = rk ) x(n)
435 
436  epsmch = epsilon( epsmch )
437 
438  eps = sqrt( max( epsfcn, epsmch ) )
439  msum = ml + mu + 1
440 !
441 ! Computation of dense approximate jacobian.
442 !
443  if ( n <= msum ) then
444 
445  do j = 1, n
446 
447  temp = x(j)
448  h = eps * abs( temp )
449  if ( h == 0.0d+00 ) then
450  h = eps
451  end if
452 
453  x(j) = temp + h
454  call fcn ( n, x, wa1 )
455 
456  x(j) = temp
457  fjac(1:n,j) = ( wa1(1:n) - fvec(1:n) ) / h
458 
459  end do
460 
461  else
462 !
463 ! Computation of banded approximate jacobian.
464 !
465  do k = 1, msum
466 
467  do j = k, n, msum
468  wa2(j) = x(j)
469  h = eps * abs( wa2(j) )
470  if ( h == 0.0d+00 ) then
471  h = eps
472  end if
473  x(j) = wa2(j) + h
474  end do
475 
476  call fcn ( n, x, wa1 )
477 
478  do j = k, n, msum
479 
480  x(j) = wa2(j)
481 
482  h = eps * abs( wa2(j) )
483  if ( h == 0.0d+00 ) then
484  h = eps
485  end if
486 
487  fjac(1:n,j) = 0.0d+00
488 
489  do i = 1, n
490  if ( j - mu <= i .and. i <= j + ml ) then
491  fjac(i,j) = ( wa1(i) - fvec(i) ) / h
492  end if
493  end do
494 
495  end do
496 
497  end do
498 
499  end if
500 
501  return
502 end
503 subroutine fsolve ( fcn, n, x, fvec, tol, info )
504 
505 !*****************************************************************************80
506 !
507 !! fsolve() seeks a zero of N nonlinear equations in N variables.
508 !
509 ! Discussion:
510 !
511 ! fsolve() finds a zero of a system of N nonlinear functions in N variables
512 ! by a modification of the Powell hybrid method. This is done by using the
513 ! more general nonlinear equation solver HYBRD. The user provides a
514 ! subroutine which calculates the functions.
515 !
516 ! The jacobian is calculated by a forward-difference approximation.
517 !
518 ! Licensing:
519 !
520 ! This code is distributed under the GNU LGPL license.
521 !
522 ! Modified:
523 !
524 ! 07 April 2021
525 !
526 ! Author:
527 !
528 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
529 ! FORTRAN90 version by John Burkardt.
530 !
531 ! Reference:
532 !
533 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
534 ! User Guide for MINPACK-1,
535 ! Technical Report ANL-80-74,
536 ! Argonne National Laboratory, 1980.
537 !
538 ! Input:
539 !
540 ! external FCN, the user subroutine which calculates the functions.
541 ! The routine should have the form:
542 ! subroutine fcn ( n, x, fvec )
543 ! integer n
544 ! real ( kind = rk ) fvec(n)
545 ! real ( kind = rk ) x(n)
546 !
547 ! integer N, the number of functions and variables.
548 !
549 ! real ( kind = rk ) X(N), an initial estimate of the solution vector.
550 !
551 ! real ( kind = rk ) TOL. Satisfactory termination occurs when the algorithm
552 ! estimates that the relative error between X and the solution is at
553 ! most TOL. TOL should be nonnegative.
554 !
555 ! Output:
556 !
557 ! real ( kind = rk ) X(N), the estimate of the solution vector.
558 !
559 ! real ( kind = rk ) FVEC(N), the functions evaluated at the output X.
560 !
561 ! integer INFO, error flag.
562 ! 0, improper input parameters.
563 ! 1, algorithm estimates that the relative error between X and the
564 ! solution is at most TOL.
565 ! 2, number of calls to FCN has reached or exceeded 200*(N+1).
566 ! 3, TOL is too small. No further improvement in the approximate
567 ! solution X is possible.
568 ! 4, the iteration is not making good progress.
569 !
570  implicit none
571 
572  integer, parameter :: rk = r_kind
573 
574  integer n
575 
576  real ( kind = rk ) diag(n)
577  real ( kind = rk ) epsfcn
578  real ( kind = rk ) factor
579  external fcn
580  real ( kind = rk ) fjac(n,n)
581  real ( kind = rk ) fvec(n)
582  integer info
583  integer ldfjac
584  integer lr
585  integer maxfev
586  integer ml
587  integer mode
588  integer mu
589  integer nfev
590  real ( kind = rk ) qtf(n)
591  real ( kind = rk ) r((n*(n+1))/2)
592  real ( kind = rk ) tol
593  real ( kind = rk ) x(n)
594  real ( kind = rk ) xtol
595 
596  if ( n <= 0 ) then
597  info = 0
598  return
599  end if
600 
601  if ( tol < 0.0d+00 ) then
602  info = 0
603  return
604  end if
605 
606  xtol = tol
607  maxfev = 200 * ( n + 1 )
608  ml = n - 1
609  mu = n - 1
610  epsfcn = 0.0d+00
611  diag(1:n) = 1.0d+00
612  mode = 2
613  factor = 100.0d+00
614  info = 0
615  nfev = 0
616  fjac(1:n,1:n) = 0.0d+00
617  ldfjac = n
618  r(1:(n*(n+1))/2) = 0.0d+00
619  lr = ( n * ( n + 1 ) ) / 2
620  qtf(1:n) = 0.0d+00
621 
622  call hybrd ( fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, &
623  factor, info, nfev, fjac, ldfjac, r, lr, qtf )
624 
625  if ( info == 5 ) then
626  info = 4
627  end if
628 
629  return
630 end
631 subroutine hybrd ( fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, &
632  factor, info, nfev, fjac, ldfjac, r, lr, qtf )
633 
634 !*****************************************************************************80
635 !
636 !! hybrd() seeks a zero of N nonlinear equations in N variables.
637 !
638 ! Discussion:
639 !
640 ! HYBRD finds a zero of a system of N nonlinear functions in N variables
641 ! by a modification of the Powell hybrid method. The user must provide a
642 ! subroutine which calculates the functions.
643 !
644 ! The jacobian is then calculated by a forward-difference approximation.
645 !
646 ! Licensing:
647 !
648 ! This code is distributed under the GNU LGPL license.
649 !
650 ! Modified:
651 !
652 ! 06 April 2010
653 !
654 ! Author:
655 !
656 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
657 ! FORTRAN90 version by John Burkardt.
658 !
659 ! Reference:
660 !
661 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
662 ! User Guide for MINPACK-1,
663 ! Technical Report ANL-80-74,
664 ! Argonne National Laboratory, 1980.
665 !
666 ! Parameters:
667 !
668 ! Input, external FCN, the name of the user-supplied subroutine which
669 ! calculates the functions. The routine should have the form:
670 ! subroutine fcn ( n, x, fvec )
671 ! integer n
672 ! real ( kind = rk ) fvec(n)
673 ! real ( kind = rk ) x(n)
674 !
675 ! Input, integer N, the number of functions and variables.
676 !
677 ! Input/output, real ( kind = rk ) X(N). On input, X must contain an initial
678 ! estimate of the solution vector. On output X contains the final
679 ! estimate of the solution vector.
680 !
681 ! Output, real ( kind = rk ) FVEC(N), the functions evaluated at the output X.
682 !
683 ! Input, real ( kind = rk ) XTOL. Termination occurs when the relative error
684 ! between two consecutive iterates is at most XTOL. XTOL should be
685 ! nonnegative.
686 !
687 ! Input, integer MAXFEV. Termination occurs when the number of
688 ! calls to FCN is at least MAXFEV by the end of an iteration.
689 !
690 ! Input, integer ML, MU, specify the number of subdiagonals and
691 ! superdiagonals within the band of the jacobian matrix. If the jacobian
692 ! is not banded, set ML and MU to at least n - 1.
693 !
694 ! Input, real ( kind = rk ) EPSFCN, is used in determining a suitable step
695 ! length for the forward-difference approximation. This approximation
696 ! assumes that the relative errors in the functions are of the order of
697 ! EPSFCN. If EPSFCN is less than the machine precision, it is assumed that
698 ! the relative errors in the functions are of the order of the machine
699 ! precision.
700 !
701 ! Input/output, real ( kind = rk ) DIAG(N). If MODE = 1, then DIAG is set
702 ! internally. If MODE = 2, then DIAG must contain positive entries that
703 ! serve as multiplicative scale factors for the variables.
704 !
705 ! Input, integer MODE, scaling option.
706 ! 1, variables will be scaled internally.
707 ! 2, scaling is specified by the input DIAG vector.
708 !
709 ! Input, real ( kind = rk ) FACTOR, determines the initial step bound. This
710 ! bound is set to the product of FACTOR and the euclidean norm of DIAG*X if
711 ! nonzero, or else to FACTOR itself. In most cases, FACTOR should lie
712 ! in the interval (0.1, 100) with 100 the recommended value.
713 !
714 ! Output, integer INFO, error flag.
715 ! 0, improper input parameters.
716 ! 1, relative error between two consecutive iterates is at most XTOL.
717 ! 2, number of calls to FCN has reached or exceeded MAXFEV.
718 ! 3, XTOL is too small. No further improvement in the approximate
719 ! solution X is possible.
720 ! 4, iteration is not making good progress, as measured by the improvement
721 ! from the last five jacobian evaluations.
722 ! 5, iteration is not making good progress, as measured by the improvement
723 ! from the last ten iterations.
724 !
725 ! Output, integer NFEV, the number of calls to FCN.
726 !
727 ! Output, real ( kind = rk ) FJAC(LDFJAC,N), an N by N array which contains
728 ! the orthogonal matrix Q produced by the QR factorization of the final
729 ! approximate jacobian.
730 !
731 ! Input, integer LDFJAC, the leading dimension of FJAC.
732 ! LDFJAC must be at least N.
733 !
734 ! Output, real ( kind = rk ) R(LR), the upper triangular matrix produced by
735 ! the QR factorization of the final approximate jacobian, stored rowwise.
736 !
737 ! Input, integer LR, the size of the R array, which must be no
738 ! less than (N*(N+1))/2.
739 !
740 ! Output, real ( kind = rk ) QTF(N), contains the vector Q'*FVEC.
741 !
742  implicit none
743 
744  integer, parameter :: rk = r_kind
745 
746  integer ldfjac
747  integer lr
748  integer n
749 
750  real ( kind = rk ) actred
751  real ( kind = rk ) delta
752  real ( kind = rk ) diag(n)
753  real ( kind = rk ) enorm
754  real ( kind = rk ) epsfcn
755  real ( kind = rk ) epsmch
756  real ( kind = rk ) factor
757  external fcn
758  real ( kind = rk ) fjac(ldfjac,n)
759  real ( kind = rk ) fnorm
760  real ( kind = rk ) fnorm1
761  real ( kind = rk ) fvec(n)
762  integer i
763  integer info
764  integer iter
765  integer iwa(1)
766  integer j
767  logical jeval
768  integer l
769  integer maxfev
770  integer ml
771  integer mode
772  integer msum
773  integer mu
774  integer ncfail
775  integer nslow1
776  integer nslow2
777  integer ncsuc
778  integer nfev
779  logical pivot
780  real ( kind = rk ) pnorm
781  real ( kind = rk ) prered
782  real ( kind = rk ) qtf(n)
783  real ( kind = rk ) r(lr)
784  real ( kind = rk ) ratio
785  logical sing
786  real ( kind = rk ) sum2
787  real ( kind = rk ) temp
788  real ( kind = rk ) wa1(n)
789  real ( kind = rk ) wa2(n)
790  real ( kind = rk ) wa3(n)
791  real ( kind = rk ) wa4(n)
792  real ( kind = rk ) x(n)
793  real ( kind = rk ) xnorm
794  real ( kind = rk ) xtol
795 
796  epsmch = epsilon( epsmch )
797 
798  info = 0
799  nfev = 0
800 !
801 ! Check the input parameters for errors.
802 !
803  if ( n <= 0 ) then
804  return
805  else if ( xtol < 0.0d+00 ) then
806  return
807  else if ( maxfev <= 0 ) then
808  return
809  else if ( ml < 0 ) then
810  return
811  else if ( mu < 0 ) then
812  return
813  else if ( factor <= 0.0d+00 ) then
814  return
815  else if ( ldfjac < n ) then
816  return
817  else if ( lr < ( n * ( n + 1 ) ) / 2 ) then
818  return
819  end if
820 
821  if ( mode == 2 ) then
822 
823  do j = 1, n
824  if ( diag(j) <= 0.0d+00 ) then
825  return
826  end if
827  end do
828 
829  end if
830 !
831 ! Evaluate the function at the starting point
832 ! and calculate its norm.
833 !
834  call fcn ( n, x, fvec )
835  nfev = 1
836 
837  fnorm = enorm( n, fvec )
838 !
839 ! Determine the number of calls to FCN needed to compute the jacobian matrix.
840 !
841  msum = min( ml + mu + 1, n )
842 !
843 ! Initialize iteration counter and monitors.
844 !
845  iter = 1
846  ncsuc = 0
847  ncfail = 0
848  nslow1 = 0
849  nslow2 = 0
850 !
851 ! Beginning of the outer loop.
852 !
853 30 continue
854 
855  jeval = .true.
856 !
857 ! Calculate the jacobian matrix.
858 !
859  call fdjac1 ( fcn, n, x, fvec, fjac, ldfjac, ml, mu, epsfcn )
860 
861  nfev = nfev + msum
862 !
863 ! Compute the QR factorization of the jacobian.
864 !
865  pivot = .false.
866  call qrfac ( n, n, fjac, ldfjac, pivot, iwa, 1, wa1, wa2 )
867 !
868 ! On the first iteration, if MODE is 1, scale according
869 ! to the norms of the columns of the initial jacobian.
870 !
871  if ( iter == 1 ) then
872 
873  if ( mode /= 2 ) then
874 
875  diag(1:n) = wa2(1:n)
876  do j = 1, n
877  if ( wa2(j) == 0.0d+00 ) then
878  diag(j) = 1.0d+00
879  end if
880  end do
881 
882  end if
883 !
884 ! On the first iteration, calculate the norm of the scaled X
885 ! and initialize the step bound DELTA.
886 !
887  wa3(1:n) = diag(1:n) * x(1:n)
888  xnorm = enorm( n, wa3 )
889  delta = factor * xnorm
890  if ( delta == 0.0d+00 ) then
891  delta = factor
892  end if
893 
894  end if
895 !
896 ! Form Q' * FVEC and store in QTF.
897 !
898  qtf(1:n) = fvec(1:n)
899 
900  do j = 1, n
901 
902  if ( fjac(j,j) /= 0.0d+00 ) then
903  temp = - dot_product( qtf(j:n), fjac(j:n,j) ) / fjac(j,j)
904  qtf(j:n) = qtf(j:n) + fjac(j:n,j) * temp
905  end if
906 
907  end do
908 !
909 ! Copy the triangular factor of the QR factorization into R.
910 !
911  sing = .false.
912 
913  do j = 1, n
914  l = j
915  do i = 1, j - 1
916  r(l) = fjac(i,j)
917  l = l + n - i
918  end do
919  r(l) = wa1(j)
920  if ( wa1(j) == 0.0d+00 ) then
921  sing = .true.
922  end if
923  end do
924 !
925 ! Accumulate the orthogonal factor in FJAC.
926 !
927  call qform ( n, n, fjac, ldfjac )
928 !
929 ! Rescale if necessary.
930 !
931  if ( mode /= 2 ) then
932  do j = 1, n
933  diag(j) = max( diag(j), wa2(j) )
934  end do
935  end if
936 !
937 ! Beginning of the inner loop.
938 !
939 180 continue
940 !
941 ! Determine the direction P.
942 !
943  call dogleg ( n, r, lr, diag, qtf, delta, wa1 )
944 !
945 ! Store the direction P and X + P.
946 ! Calculate the norm of P.
947 !
948  wa1(1:n) = - wa1(1:n)
949  wa2(1:n) = x(1:n) + wa1(1:n)
950  wa3(1:n) = diag(1:n) * wa1(1:n)
951 
952  pnorm = enorm( n, wa3 )
953 !
954 ! On the first iteration, adjust the initial step bound.
955 !
956  if ( iter == 1 ) then
957  delta = min( delta, pnorm )
958  end if
959 !
960 ! Evaluate the function at X + P and calculate its norm.
961 !
962  call fcn ( n, wa2, wa4 )
963  nfev = nfev + 1
964  fnorm1 = enorm( n, wa4 )
965 !
966 ! Compute the scaled actual reduction.
967 !
968  actred = -1.0d+00
969  if ( fnorm1 < fnorm ) then
970  actred = 1.0d+00 - ( fnorm1 / fnorm ) ** 2
971  endif
972 !
973 ! Compute the scaled predicted reduction.
974 !
975  l = 1
976  do i = 1, n
977  sum2 = 0.0d+00
978  do j = i, n
979  sum2 = sum2 + r(l) * wa1(j)
980  l = l + 1
981  end do
982  wa3(i) = qtf(i) + sum2
983  end do
984 
985  temp = enorm( n, wa3 )
986  prered = 0.0d+00
987  if ( temp < fnorm ) then
988  prered = 1.0d+00 - ( temp / fnorm ) ** 2
989  end if
990 !
991 ! Compute the ratio of the actual to the predicted reduction.
992 !
993  ratio = 0.0d+00
994  if ( 0.0d+00 < prered ) then
995  ratio = actred / prered
996  end if
997 !
998 ! Update the step bound.
999 !
1000  if ( ratio < 0.1d+00 ) then
1001 
1002  ncsuc = 0
1003  ncfail = ncfail + 1
1004  delta = 0.5d+00 * delta
1005 
1006  else
1007 
1008  ncfail = 0
1009  ncsuc = ncsuc + 1
1010 
1011  if ( 0.5d+00 <= ratio .or. 1 < ncsuc ) then
1012  delta = max( delta, pnorm / 0.5d+00 )
1013  end if
1014 
1015  if ( abs( ratio - 1.0d+00 ) <= 0.1d+00 ) then
1016  delta = pnorm / 0.5d+00
1017  end if
1018 
1019  end if
1020 !
1021 ! Test for successful iteration.
1022 !
1023 ! Successful iteration.
1024 ! Update X, FVEC, and their norms.
1025 !
1026  if ( 0.0001d+00 <= ratio ) then
1027  x(1:n) = wa2(1:n)
1028  wa2(1:n) = diag(1:n) * x(1:n)
1029  fvec(1:n) = wa4(1:n)
1030  xnorm = enorm( n, wa2 )
1031  fnorm = fnorm1
1032  iter = iter + 1
1033  end if
1034 !
1035 ! Determine the progress of the iteration.
1036 !
1037  nslow1 = nslow1 + 1
1038  if ( 0.001d+00 <= actred ) then
1039  nslow1 = 0
1040  end if
1041 
1042  if ( jeval ) then
1043  nslow2 = nslow2 + 1
1044  end if
1045 
1046  if ( 0.1d+00 <= actred ) then
1047  nslow2 = 0
1048  end if
1049 !
1050 ! Test for convergence.
1051 !
1052  if ( delta <= xtol * xnorm .or. fnorm == 0.0d+00 ) then
1053  info = 1
1054  end if
1055 
1056  if ( info /= 0 ) then
1057  return
1058  end if
1059 !
1060 ! Tests for termination and stringent tolerances.
1061 !
1062  if ( maxfev <= nfev ) then
1063  info = 2
1064  end if
1065 
1066  if ( 0.1d+00 * max( 0.1d+00 * delta, pnorm ) <= epsmch * xnorm ) then
1067  info = 3
1068  end if
1069 
1070  if ( nslow2 == 5 ) then
1071  info = 4
1072  end if
1073 
1074  if ( nslow1 == 10 ) then
1075  info = 5
1076  end if
1077 
1078  if ( info /= 0 ) then
1079  return
1080  end if
1081 !
1082 ! Criterion for recalculating jacobian approximation
1083 ! by forward differences.
1084 !
1085  if ( ncfail == 2 ) then
1086  go to 290
1087  end if
1088 !
1089 ! Calculate the rank one modification to the jacobian
1090 ! and update QTF if necessary.
1091 !
1092  do j = 1, n
1093  sum2 = dot_product( wa4(1:n), fjac(1:n,j) )
1094  wa2(j) = ( sum2 - wa3(j) ) / pnorm
1095  wa1(j) = diag(j) * ( ( diag(j) * wa1(j) ) / pnorm )
1096  if ( 0.0001d+00 <= ratio ) then
1097  qtf(j) = sum2
1098  end if
1099  end do
1100 !
1101 ! Compute the QR factorization of the updated jacobian.
1102 !
1103  call r1updt ( n, n, r, lr, wa1, wa2, wa3, sing )
1104  call r1mpyq ( n, n, fjac, ldfjac, wa2, wa3 )
1105  call r1mpyq ( 1, n, qtf, 1, wa2, wa3 )
1106 !
1107 ! End of the inner loop.
1108 !
1109  jeval = .false.
1110  go to 180
1111 
1112  290 continue
1113 !
1114 ! End of the outer loop.
1115 !
1116  go to 30
1117 
1118  return
1119 end
1120 subroutine qform ( m, n, q, ldq )
1121 
1122 !*****************************************************************************80
1123 !
1124 !! qform() produces the explicit QR factorization of a matrix.
1125 !
1126 ! Discussion:
1127 !
1128 ! The QR factorization of a matrix is usually accumulated in implicit
1129 ! form, that is, as a series of orthogonal transformations of the
1130 ! original matrix. This routine carries out those transformations,
1131 ! to explicitly exhibit the factorization constructed by QRFAC.
1132 !
1133 ! Licensing:
1134 !
1135 ! This code is distributed under the GNU LGPL license.
1136 !
1137 ! Modified:
1138 !
1139 ! 06 April 2010
1140 !
1141 ! Author:
1142 !
1143 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1144 ! FORTRAN90 version by John Burkardt.
1145 !
1146 ! Reference:
1147 !
1148 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
1149 ! User Guide for MINPACK-1,
1150 ! Technical Report ANL-80-74,
1151 ! Argonne National Laboratory, 1980.
1152 !
1153 ! Parameters:
1154 !
1155 ! Input, integer M, is a positive integer input variable set
1156 ! to the number of rows of A and the order of Q.
1157 !
1158 ! Input, integer N, is a positive integer input variable set
1159 ! to the number of columns of A.
1160 !
1161 ! Input/output, real ( kind = rk ) Q(LDQ,M). Q is an M by M array.
1162 ! On input the full lower trapezoid in the first min(M,N) columns of Q
1163 ! contains the factored form.
1164 ! On output, Q has been accumulated into a square matrix.
1165 !
1166 ! Input, integer LDQ, is a positive integer input variable
1167 ! not less than M which specifies the leading dimension of the array Q.
1168 !
1169  implicit none
1170 
1171  integer, parameter :: rk = r_kind
1172 
1173  integer ldq
1174  integer m
1175  integer n
1176 
1177  integer j
1178  integer k
1179  integer l
1180  integer minmn
1181  real ( kind = rk ) q(ldq,m)
1182  real ( kind = rk ) temp
1183  real ( kind = rk ) wa(m)
1184 
1185  minmn = min( m, n )
1186 
1187  do j = 2, minmn
1188  q(1:j-1,j) = 0.0d+00
1189  end do
1190 !
1191 ! Initialize remaining columns to those of the identity matrix.
1192 !
1193  q(1:m,n+1:m) = 0.0d+00
1194 
1195  do j = n + 1, m
1196  q(j,j) = 1.0d+00
1197  end do
1198 !
1199 ! Accumulate Q from its factored form.
1200 !
1201  do l = 1, minmn
1202 
1203  k = minmn - l + 1
1204 
1205  wa(k:m) = q(k:m,k)
1206 
1207  q(k:m,k) = 0.0d+00
1208  q(k,k) = 1.0d+00
1209 
1210  if ( wa(k) /= 0.0d+00 ) then
1211 
1212  do j = k, m
1213  temp = dot_product( wa(k:m), q(k:m,j) ) / wa(k)
1214  q(k:m,j) = q(k:m,j) - temp * wa(k:m)
1215  end do
1216 
1217  end if
1218 
1219  end do
1220 
1221  return
1222 end
1223 subroutine qrfac ( m, n, a, lda, pivot, ipvt, lipvt, rdiag, acnorm )
1224 
1225 !*****************************************************************************80
1226 !
1227 !! qrfac() computes a QR factorization using Householder transformations.
1228 !
1229 ! Discussion:
1230 !
1231 ! This function uses Householder transformations with optional column
1232 ! pivoting to compute a QR factorization of the
1233 ! M by N matrix A. That is, QRFAC determines an orthogonal
1234 ! matrix Q, a permutation matrix P, and an upper trapezoidal
1235 ! matrix R with diagonal elements of nonincreasing magnitude,
1236 ! such that A*P = Q*R.
1237 !
1238 ! The Householder transformation for column K, K = 1,2,...,min(M,N),
1239 ! is of the form
1240 !
1241 ! I - ( 1 / U(K) ) * U * U'
1242 !
1243 ! where U has zeros in the first K-1 positions.
1244 !
1245 ! The form of this transformation and the method of pivoting first
1246 ! appeared in the corresponding LINPACK routine.
1247 !
1248 ! Licensing:
1249 !
1250 ! This code is distributed under the GNU LGPL license.
1251 !
1252 ! Modified:
1253 !
1254 ! 06 April 2010
1255 !
1256 ! Author:
1257 !
1258 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1259 ! FORTRAN90 version by John Burkardt.
1260 !
1261 ! Reference:
1262 !
1263 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
1264 ! User Guide for MINPACK-1,
1265 ! Technical Report ANL-80-74,
1266 ! Argonne National Laboratory, 1980.
1267 !
1268 ! Parameters:
1269 !
1270 ! Input, integer M, the number of rows of A.
1271 !
1272 ! Input, integer N, the number of columns of A.
1273 !
1274 ! Input/output, real ( kind = rk ) A(LDA,N), the M by N array.
1275 ! On input, A contains the matrix for which the QR factorization is to
1276 ! be computed. On output, the strict upper trapezoidal part of A contains
1277 ! the strict upper trapezoidal part of R, and the lower trapezoidal
1278 ! part of A contains a factored form of Q, the non-trivial elements of
1279 ! the U vectors described above.
1280 !
1281 ! Input, integer LDA, the leading dimension of A, which must
1282 ! be no less than M.
1283 !
1284 ! Input, logical PIVOT, is TRUE if column pivoting is to be carried out.
1285 !
1286 ! Output, integer IPVT(LIPVT), defines the permutation matrix P
1287 ! such that A*P = Q*R. Column J of P is column IPVT(J) of the identity
1288 ! matrix. If PIVOT is false, IPVT is not referenced.
1289 !
1290 ! Input, integer LIPVT, the dimension of IPVT, which should
1291 ! be N if pivoting is used.
1292 !
1293 ! Output, real ( kind = rk ) RDIAG(N), contains the diagonal elements of R.
1294 !
1295 ! Output, real ( kind = rk ) ACNORM(N), the norms of the corresponding
1296 ! columns of the input matrix A. If this information is not needed,
1297 ! then ACNORM can coincide with RDIAG.
1298 !
1299  implicit none
1300 
1301  integer, parameter :: rk = r_kind
1302 
1303  integer lda
1304  integer lipvt
1305  integer m
1306  integer n
1307 
1308  real ( kind = rk ) a(lda,n)
1309  real ( kind = rk ) acnorm(n)
1310  real ( kind = rk ) ajnorm
1311  real ( kind = rk ) enorm
1312  real ( kind = rk ) epsmch
1313  integer i4_temp
1314  integer ipvt(lipvt)
1315  integer j
1316  integer k
1317  integer kmax
1318  integer minmn
1319  logical pivot
1320  real ( kind = rk ) r8_temp(m)
1321  real ( kind = rk ) rdiag(n)
1322  real ( kind = rk ) temp
1323  real ( kind = rk ) wa(n)
1324 
1325  epsmch = epsilon( epsmch )
1326 !
1327 ! Compute the initial column norms and initialize several arrays.
1328 !
1329  do j = 1, n
1330  acnorm(j) = enorm( m, a(1:m,j) )
1331  end do
1332 
1333  rdiag(1:n) = acnorm(1:n)
1334  wa(1:n) = acnorm(1:n)
1335 
1336  if ( pivot ) then
1337  do j = 1, n
1338  ipvt(j) = j
1339  end do
1340  end if
1341 !
1342 ! Reduce A to R with Householder transformations.
1343 !
1344  minmn = min( m, n )
1345 
1346  do j = 1, minmn
1347 !
1348 ! Bring the column of largest norm into the pivot position.
1349 !
1350  if ( pivot ) then
1351 
1352  kmax = j
1353 
1354  do k = j, n
1355  if ( rdiag(kmax) < rdiag(k) ) then
1356  kmax = k
1357  end if
1358  end do
1359 
1360  if ( kmax /= j ) then
1361 
1362  r8_temp(1:m) = a(1:m,j)
1363  a(1:m,j) = a(1:m,kmax)
1364  a(1:m,kmax) = r8_temp(1:m)
1365 
1366  rdiag(kmax) = rdiag(j)
1367  wa(kmax) = wa(j)
1368 
1369  i4_temp = ipvt(j)
1370  ipvt(j) = ipvt(kmax)
1371  ipvt(kmax) = i4_temp
1372 
1373  end if
1374 
1375  end if
1376 !
1377 ! Compute the Householder transformation to reduce the
1378 ! J-th column of A to a multiple of the J-th unit vector.
1379 !
1380  ajnorm = enorm( m-j+1, a(j,j) )
1381 
1382  if ( ajnorm /= 0.0d+00 ) then
1383 
1384  if ( a(j,j) < 0.0d+00 ) then
1385  ajnorm = -ajnorm
1386  end if
1387 
1388  a(j:m,j) = a(j:m,j) / ajnorm
1389  a(j,j) = a(j,j) + 1.0d+00
1390 !
1391 ! Apply the transformation to the remaining columns and update the norms.
1392 !
1393  do k = j + 1, n
1394 
1395  temp = dot_product( a(j:m,j), a(j:m,k) ) / a(j,j)
1396 
1397  a(j:m,k) = a(j:m,k) - temp * a(j:m,j)
1398 
1399  if ( pivot .and. rdiag(k) /= 0.0d+00 ) then
1400 
1401  temp = a(j,k) / rdiag(k)
1402  rdiag(k) = rdiag(k) * sqrt( max( 0.0d+00, 1.0d+00 - temp ** 2 ) )
1403 
1404  if ( 0.05d+00 * ( rdiag(k) / wa(k) ) ** 2 <= epsmch ) then
1405  rdiag(k) = enorm( m-j, a(j+1,k) )
1406  wa(k) = rdiag(k)
1407  end if
1408 
1409  end if
1410 
1411  end do
1412 
1413  end if
1414 
1415  rdiag(j) = - ajnorm
1416 
1417  end do
1418 
1419  return
1420 end
1421 subroutine r1mpyq ( m, n, a, lda, v, w )
1422 
1423 !*****************************************************************************80
1424 !
1425 !! r1mpyq() computes A*Q, where Q is the product of Householder transformations.
1426 !
1427 ! Discussion:
1428 !
1429 ! Given an M by N matrix A, this function computes A*Q where
1430 ! Q is the product of 2*(N - 1) transformations
1431 !
1432 ! GV(N-1)*...*GV(1)*GW(1)*...*GW(N-1)
1433 !
1434 ! and GV(I), GW(I) are Givens rotations in the (I,N) plane which
1435 ! eliminate elements in the I-th and N-th planes, respectively.
1436 ! Q itself is not given, rather the information to recover the
1437 ! GV, GW rotations is supplied.
1438 !
1439 ! Licensing:
1440 !
1441 ! This code is distributed under the GNU LGPL license.
1442 !
1443 ! Modified:
1444 !
1445 ! 06 April 2010
1446 !
1447 ! Author:
1448 !
1449 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1450 ! FORTRAN90 version by John Burkardt.
1451 !
1452 ! Reference:
1453 !
1454 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
1455 ! User Guide for MINPACK-1,
1456 ! Technical Report ANL-80-74,
1457 ! Argonne National Laboratory, 1980.
1458 !
1459 ! Parameters:
1460 !
1461 ! Input, integer M, the number of rows of A.
1462 !
1463 ! Input, integer N, the number of columns of A.
1464 !
1465 ! Input/output, real ( kind = rk ) A(LDA,N), the M by N array.
1466 ! On input, the matrix A to be postmultiplied by the orthogonal matrix Q.
1467 ! On output, the value of A*Q.
1468 !
1469 ! Input, integer LDA, the leading dimension of A, which must not
1470 ! be less than M.
1471 !
1472 ! Input, real ( kind = rk ) V(N), W(N), contain the information necessary
1473 ! to recover the Givens rotations GV and GW.
1474 !
1475  implicit none
1476 
1477  integer, parameter :: rk = r_kind
1478 
1479  integer lda
1480  integer m
1481  integer n
1482 
1483  real ( kind = rk ) a(lda,n)
1484  real ( kind = rk ) c
1485  integer i
1486  integer j
1487  real ( kind = rk ) s
1488  real ( kind = rk ) temp
1489  real ( kind = rk ) v(n)
1490  real ( kind = rk ) w(n)
1491 !
1492 ! Apply the first set of Givens rotations to A.
1493 !
1494  do j = n - 1, 1, -1
1495 
1496  if ( 1.0d+00 < abs( v(j) ) ) then
1497  c = 1.0d+00 / v(j)
1498  s = sqrt( 1.0d+00 - c ** 2 )
1499  else
1500  s = v(j)
1501  c = sqrt( 1.0d+00 - s ** 2 )
1502  end if
1503 
1504  do i = 1, m
1505  temp = c * a(i,j) - s * a(i,n)
1506  a(i,n) = s * a(i,j) + c * a(i,n)
1507  a(i,j) = temp
1508  end do
1509 
1510  end do
1511 !
1512 ! Apply the second set of Givens rotations to A.
1513 !
1514  do j = 1, n - 1
1515 
1516  if ( 1.0d+00 < abs( w(j) ) ) then
1517  c = 1.0d+00 / w(j)
1518  s = sqrt( 1.0d+00 - c ** 2 )
1519  else
1520  s = w(j)
1521  c = sqrt( 1.0d+00 - s ** 2 )
1522  end if
1523 
1524  do i = 1, m
1525  temp = c * a(i,j) + s * a(i,n)
1526  a(i,n) = - s * a(i,j) + c * a(i,n)
1527  a(i,j) = temp
1528  end do
1529 
1530  end do
1531 
1532  return
1533 end
1534 subroutine r1updt ( m, n, s, ls, u, v, w, sing )
1535 
1536 !*****************************************************************************80
1537 !
1538 !! r1updt() re-triangularizes a matrix after a rank one update.
1539 !
1540 ! Discussion:
1541 !
1542 ! Given an M by N lower trapezoidal matrix S, an M-vector U, and an
1543 ! N-vector V, the problem is to determine an orthogonal matrix Q such that
1544 !
1545 ! (S + U * V' ) * Q
1546 !
1547 ! is again lower trapezoidal.
1548 !
1549 ! This function determines Q as the product of 2 * (N - 1)
1550 ! transformations
1551 !
1552 ! GV(N-1)*...*GV(1)*GW(1)*...*GW(N-1)
1553 !
1554 ! where GV(I), GW(I) are Givens rotations in the (I,N) plane
1555 ! which eliminate elements in the I-th and N-th planes,
1556 ! respectively. Q itself is not accumulated, rather the
1557 ! information to recover the GV and GW rotations is returned.
1558 !
1559 ! Licensing:
1560 !
1561 ! This code is distributed under the GNU LGPL license.
1562 !
1563 ! Modified:
1564 !
1565 ! 06 April 2010
1566 !
1567 ! Author:
1568 !
1569 ! Original FORTRAN77 version by Jorge More, Burton Garbow, Kenneth Hillstrom.
1570 ! FORTRAN90 version by John Burkardt.
1571 !
1572 ! Reference:
1573 !
1574 ! Jorge More, Burton Garbow, Kenneth Hillstrom,
1575 ! User Guide for MINPACK-1,
1576 ! Technical Report ANL-80-74,
1577 ! Argonne National Laboratory, 1980.
1578 !
1579 ! Parameters:
1580 !
1581 ! Input, integer M, the number of rows of S.
1582 !
1583 ! Input, integer N, the number of columns of S.
1584 ! N must not exceed M.
1585 !
1586 ! Input/output, real ( kind = rk ) S(LS). On input, the lower trapezoidal
1587 ! matrix S stored by columns. On output S contains the lower trapezoidal
1588 ! matrix produced as described above.
1589 !
1590 ! Input, integer LS, the length of the S array. LS must be at
1591 ! least (N*(2*M-N+1))/2.
1592 !
1593 ! Input, real ( kind = rk ) U(M), the U vector.
1594 !
1595 ! Input/output, real ( kind = rk ) V(N). On input, V must contain the
1596 ! vector V. On output V contains the information necessary to recover the
1597 ! Givens rotations GV described above.
1598 !
1599 ! Output, real ( kind = rk ) W(M), contains information necessary to
1600 ! recover the Givens rotations GW described above.
1601 !
1602 ! Output, logical SING, is set to TRUE if any of the diagonal elements
1603 ! of the output S are zero. Otherwise SING is set FALSE.
1604 !
1605  implicit none
1606 
1607  integer, parameter :: rk = r_kind
1608 
1609  integer ls
1610  integer m
1611  integer n
1612 
1613  real ( kind = rk ) cos
1614  real ( kind = rk ) cotan
1615  real ( kind = rk ) giant
1616  integer i
1617  integer j
1618  integer jj
1619  integer l
1620  real ( kind = rk ) s(ls)
1621  real ( kind = rk ) sin
1622  logical sing
1623  real ( kind = rk ) tan
1624  real ( kind = rk ) tau
1625  real ( kind = rk ) temp
1626  real ( kind = rk ) u(m)
1627  real ( kind = rk ) v(n)
1628  real ( kind = rk ) w(m)
1629 !
1630 ! GIANT is the largest magnitude.
1631 !
1632  giant = huge( giant )
1633 !
1634 ! Initialize the diagonal element pointer.
1635 !
1636  jj = ( n * ( 2 * m - n + 1 ) ) / 2 - ( m - n )
1637 !
1638 ! Move the nontrivial part of the last column of S into W.
1639 !
1640  l = jj
1641  do i = n, m
1642  w(i) = s(l)
1643  l = l + 1
1644  end do
1645 !
1646 ! Rotate the vector V into a multiple of the N-th unit vector
1647 ! in such a way that a spike is introduced into W.
1648 !
1649  do j = n - 1, 1, -1
1650 
1651  jj = jj - ( m - j + 1 )
1652  w(j) = 0.0d+00
1653 
1654  if ( v(j) /= 0.0d+00 ) then
1655 !
1656 ! Determine a Givens rotation which eliminates the J-th element of V.
1657 !
1658  if ( abs( v(n) ) < abs( v(j) ) ) then
1659  cotan = v(n) / v(j)
1660  sin = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
1661  cos = sin * cotan
1662  tau = 1.0d+00
1663  if ( abs( cos ) * giant > 1.0d+00 ) then
1664  tau = 1.0d+00 / cos
1665  end if
1666  else
1667  tan = v(j) / v(n)
1668  cos = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * tan ** 2 )
1669  sin = cos * tan
1670  tau = sin
1671  end if
1672 !
1673 ! Apply the transformation to V and store the information
1674 ! necessary to recover the Givens rotation.
1675 !
1676  v(n) = sin * v(j) + cos * v(n)
1677  v(j) = tau
1678 !
1679 ! Apply the transformation to S and extend the spike in W.
1680 !
1681  l = jj
1682  do i = j, m
1683  temp = cos * s(l) - sin * w(i)
1684  w(i) = sin * s(l) + cos * w(i)
1685  s(l) = temp
1686  l = l + 1
1687  end do
1688 
1689  end if
1690 
1691  end do
1692 !
1693 ! Add the spike from the rank 1 update to W.
1694 !
1695  w(1:m) = w(1:m) + v(n) * u(1:m)
1696 !
1697 ! Eliminate the spike.
1698 !
1699  sing = .false.
1700 
1701  do j = 1, n-1
1702 
1703  if ( w(j) /= 0.0d+00 ) then
1704 !
1705 ! Determine a Givens rotation which eliminates the
1706 ! J-th element of the spike.
1707 !
1708  if ( abs( s(jj) ) < abs( w(j) ) ) then
1709 
1710  cotan = s(jj) / w(j)
1711  sin = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * cotan ** 2 )
1712  cos = sin * cotan
1713 
1714  if ( 1.0d+00 < abs( cos ) * giant ) then
1715  tau = 1.0d+00 / cos
1716  else
1717  tau = 1.0d+00
1718  end if
1719 
1720  else
1721 
1722  tan = w(j) / s(jj)
1723  cos = 0.5d+00 / sqrt( 0.25d+00 + 0.25d+00 * tan ** 2 )
1724  sin = cos * tan
1725  tau = sin
1726 
1727  end if
1728 !
1729 ! Apply the transformation to S and reduce the spike in W.
1730 !
1731  l = jj
1732  do i = j, m
1733  temp = cos * s(l) + sin * w(i)
1734  w(i) = - sin * s(l) + cos * w(i)
1735  s(l) = temp
1736  l = l + 1
1737  end do
1738 !
1739 ! Store the information necessary to recover the Givens rotation.
1740 !
1741  w(j) = tau
1742 
1743  end if
1744 !
1745 ! Test for zero diagonal elements in the output S.
1746 !
1747  if ( s(jj) == 0.0d+00 ) then
1748  sing = .true.
1749  end if
1750 
1751  jj = jj + ( m - j + 1 )
1752 
1753  end do
1754 !
1755 ! Move W back into the last column of the output S.
1756 !
1757  l = jj
1758  do i = n, m
1759  s(l) = w(i)
1760  l = l + 1
1761  end do
1762 
1763  if ( s(jj) == 0.0d+00 ) then
1764  sing = .true.
1765  end if
1766 
1767  return
1768 end
1769 
qform
subroutine qform(m, n, q, ldq)
Definition: fsolve.f90:1121
r1mpyq
subroutine r1mpyq(m, n, a, lda, v, w)
Definition: fsolve.f90:1422
dogleg
subroutine dogleg(n, r, lr, diag, qtb, delta, x)
Definition: fsolve.f90:3
qrfac
subroutine qrfac(m, n, a, lda, pivot, ipvt, lipvt, rdiag, acnorm)
Definition: fsolve.f90:1224
r_kind
#define r_kind
Definition: macros.h:46
fsolve
subroutine fsolve(fcn, n, x, fvec, tol, info)
Definition: fsolve.f90:504
hybrd
subroutine hybrd(fcn, n, x, fvec, xtol, maxfev, ml, mu, epsfcn, diag, mode, factor, info, nfev, fjac, ldfjac, r, lr, qtf)
Definition: fsolve.f90:633
fdjac1
subroutine fdjac1(fcn, n, x, fvec, fjac, ldfjac, ml, mu, epsfcn)
Definition: fsolve.f90:346
enorm
real(kind=rk) function enorm(n, x)
Definition: fsolve.f90:215
r1updt
subroutine r1updt(m, n, s, ls, u, v, w, sing)
Definition: fsolve.f90:1535