Improve the translation of notequal
[maxima.git] / share / hompack / fortran / rootqf.f
blob502df800127ae6c01cb31fd94723ccdc37de4b25
1 SUBROUTINE ROOTQF(N,NFE,IFLAG,RELERR,ABSERR,Y,YP,YOLD,
2 $ YPOLD,A,QT,R,DZ,Z,W,T,F0,F1,PAR,IPAR)
4 C ROOTQF FINDS THE POINT YBAR = (1, XBAR) ON THE ZERO CURVE OF THE
5 C HOMOTOPY MAP. IT STARTS WITH TWO POINTS YOLD=(LAMBDAOLD,XOLD) AND
6 C Y=(LAMBDA,X) SUCH THAT LAMBDAOLD < 1 <= LAMBDA, AND ALTERNATES
7 C BETWEEN USING A SECANT METHOD TO FIND A PREDICTED POINT ON THE
8 C HYPERPLANE LAMBDA=1, AND TAKING A QUASI-NEWTON STEP TO RETURN TO THE
9 C ZERO CURVE OF THE HOMOTOPY MAP.
12 C ON INPUT:
14 C N = DIMENSION OF X.
16 C NFE = NUMBER OF JACOBIAN MATRIX EVALUATIONS.
18 C IFLAG = -2, -1, OR 0, INDICATING THE PROBLEM TYPE.
20 C RELERR, ABSERR = RELATIVE AND ABSOLUTE ERROR VALUES. THE ITERATION IS
21 C CONSIDERED TO HAVE CONVERGED WHEN A POINT Y=(LAMBDA,X) IS FOUND
22 C SUCH THAT
24 C |Y(1) - 1| <= RELERR + ABSERR AND
26 C ||DZ|| <= RELERR*||Y|| + ABSERR, WHERE
28 C DZ IS THE QUASI-NEWTON STEP TO Y.
30 C Y(1:N+1) = POINT (LAMBDA(S), X(S)) ON ZERO CURVE OF HOMOTOPY MAP.
32 C YP(1:N+1) = UNIT TANGENT VECTOR TO THE ZERO CURVE OF THE HOMOTOPY MAP
33 C AT Y.
35 C YOLD(1:N+1) = A POINT DIFFERENT FROM Y ON THE ZERO CURVE.
37 C YPOLD(1:N+1) = UNIT TANGENT VECTOR TO THE ZERO CURVE OF THE HOMOTOPY
38 C MAP AT YOLD.
40 C A(1:*) = PARAMETER VECTOR IN THE HOMOTOPY MAP.
42 C QT(1:N+1,1:N+1) CONTAINS Q TRANSPOSE OF THE QR FACTORIZATION OF
43 C THE AUGMENTED JACOBIAN MATRIX EVALUATED AT THE POINT Y.
45 C R((N+1)*(N+2)/2) CONTAINS THE UPPER TRIANGLE OF THE R PART OF
46 C OF THE QR FACTORIZATION, STORED BY ROWS.
48 C DZ(1:N+1), Z(1:N+1), W(1:N+1), T(1:N+1), F0(1:N+1), F1(1:N+1)
49 C ARE WORK ARRAYS USED FOR THE QUASI-NEWTON STEP AND THE SECANT
50 C STEP.
52 C PAR(1:*) AND IPAR(1:*) ARE ARRAYS FOR (OPTIONAL) USER PARAMETERS,
53 C WHICH ARE SIMPLY PASSED THROUGH TO THE USER WRITTEN SUBROUTINES
54 C RHO, RHOJAC.
57 C ON OUTPUT:
59 C N, RELERR, ABSERR, AND A ARE UNCHANGED.
61 C NFE HAS BEEN UPDATED.
63 C IFLAG
64 C = -2, -1, OR 0 (UNCHANGED) ON A NORMAL RETURN.
66 C = 4 IF A SINGULAR JACOBIAN MATRIX OCCURRED. THE
67 C ITERATION WAS NOT COMPLETED.
69 C = 6 IF THE ITERATION FAILED TO CONVERGE. Y AND YOLD CONTAIN
70 C THE LAST TWO POINTS OBTAINED BY QUASI-NEWTON STEPS, AND YP
71 C CONTAINS A POINT OPPOSITE OF THE HYPERPLANE LAMBDA=1 FROM
72 C Y.
74 C Y IS THE POINT ON THE ZERO CURVE OF THE HOMOTOPY MAP AT LAMBDA = 1.
76 C YP AND YOLD CONTAIN POINTS NEAR THE SOLUTION.
78 C CALLS D1MACH, DAXPY, DCOPY, DDOT, DNRM2, F (OR RHO),
79 C QRSLQF, ROOT, UPQRQF.
81 C ***** DECLARATIONS *****
83 C FUNCTION DECLARATIONS
85 DOUBLE PRECISION D1MACH, DDOT, DNRM2, QOFS
87 C LOCAL VARIABLES
89 DOUBLE PRECISION AERR, DD001, DD0011, DD01, DD011, DELS, ETA,
90 $ ONE, P0, P1, PP0, PP1, QSOUT, RERR, S, SA, SB, SOUT,
91 $ U, ZERO
92 INTEGER ISTEP, I, LCODE, LIMIT,NP1
93 LOGICAL BRACK
95 C SCALAR ARGUMENTS
97 DOUBLE PRECISION RELERR, ABSERR
98 INTEGER N, NFE, IFLAG
100 C ARRAY DECLARATIONS
102 DOUBLE PRECISION Y(N+1), YP(N+1), YOLD(N+1), YPOLD(N+1), A(N),
103 $ QT(N+1:N+1), R((N+1)*(N+2)/2), DZ(N+1), Z(N+1), W(N+1),
104 $ T(N+1), F0(N+1), F1(N+1), PAR(1)
105 INTEGER IPAR(1)
107 C ***** END OF DECLARATIONS *****
110 C DEFINITION OF HERMITE CUBIC INTERPOLANT VIA DIVIDED DIFFERENCES.
112 DD01(P0,P1,DELS)=(P1-P0)/DELS
113 DD001(P0,PP0,P1,DELS)=(DD01(P0,P1,DELS)-PP0)/DELS
114 DD011(P0,P1,PP1,DELS)=(PP1-DD01(P0,P1,DELS))/DELS
115 DD0011(P0,PP0,P1,PP1,DELS)=(DD011(P0,P1,PP1,DELS) -
116 $ DD001(P0,PP0,P1,DELS))/DELS
117 QOFS(P0,PP0,P1,PP1,DELS,S)=((DD0011(P0,PP0,P1,PP1,DELS)*
118 $ (S-DELS) + DD001(P0,PP0,P1,DELS))*S + PP0)*S + P0
120 C ***** FIRST EXECUTABLE STATEMENT *****
122 C ***** INITIALIZATION *****
124 C ETA = PARAMETER FOR BROYDEN'S UPDATE.
125 C LIMIT = MAXIMUM NUMBER OF ITERATIONS ALLOWED.
127 ONE=1.0
128 ZERO=0.0
129 U=D1MACH(4)
130 RERR=MAX(RELERR,U)
131 AERR=MAX(ABSERR,ZERO)
132 NP1=N+1
133 ETA = 100.0*U
134 LIMIT = 2*(INT(-LOG10(AERR+RERR*DNRM2(NP1,Y,1)))+1)
136 C F0 = (RHO(Y), YP*Y) TRANSPOSE.
138 IF (IFLAG .EQ. -2) THEN
140 C CURVE TRACKING PROBLEM.
142 CALL RHO(A,Y(1),Y(2),F0,PAR,IPAR)
143 ELSE IF (IFLAG .EQ. -1) THEN
145 C ZERO FINDING PROBLEM.
147 CALL F(Y(2),F0)
148 DO 10 I=1,N
149 F0(I) = Y(1)*F0(I) + (1.0-Y(1))*(Y(I+1)-A(I))
150 10 CONTINUE
151 ELSE
153 C FIXED POINT PROBLEM.
155 CALL F(Y(2),F0)
156 DO 20 I=1,N
157 F0(I) = Y(1)*(A(I)-F0(I))+Y(I+1)-A(I)
158 20 CONTINUE
159 END IF
160 F0(NP1) = DDOT(NP1,YP,1,Y,1)
162 C ***** END OF INITIALIZATION BLOCK *****
164 C ***** COMPUTE FIRST INTERPOLANT WITH A HERMITE CUBIC *****
166 C FIND DISTANCE BETWEEN Y AND YOLD. DZ=||Y-YOLD||.
168 CALL DCOPY(NP1,Y,1,DZ,1)
169 CALL DAXPY(NP1,-ONE,YOLD,1,DZ,1)
170 DELS=DNRM2(NP1,DZ,1)
172 C USING TWO POINTS AND TANGENTS ON THE HOMOTOPY ZERO CURVE, CONSTRUCT
173 C THE HERMITE CUBIC INTERPOLANT Q(S). THEN USE ROOT TO FIND THE S
174 C CORRESPONDING TO LAMBDA = 1. THE TWO POINTS ON THE ZERO CURVE ARE
175 C ALWAYS CHOSEN TO BRACKET LAMBDA=1, WITH THE BRACKETING INTERVAL
176 C ALWAYS BEING [0, DELS].
178 SA=0.0
179 SB=DELS
180 LCODE=1
181 40 CALL ROOT(SOUT,QSOUT,SA,SB,RERR,AERR,LCODE)
182 IF (LCODE .GT. 0) GO TO 50
183 QSOUT=QOFS(YOLD(1),YPOLD(1),Y(1),YP(1),DELS,SOUT) - 1.0
184 GO TO 40
186 C IF LAMBDA = 1 WERE BRACKETED, ROOT CANNOT FAIL.
188 50 IF (LCODE .GT. 2) THEN
189 IFLAG=6
190 RETURN
191 ENDIF
193 C CALCULATE Q(SA) AS THE INITIAL POINT FOR A NEWTON ITERATION.
195 DO 60 I=1,NP1
196 Z(I)=QOFS(YOLD(I),YPOLD(I),Y(I),YP(I),DELS,SA)
197 60 CONTINUE
199 C CALCULATE DZ = Z-Y.
201 CALL DCOPY(NP1,Z,1,DZ,1)
202 CALL DAXPY(NP1,-ONE,Y,1,DZ,1)
204 C ***** END OF CALCULATION OF CUBIC INTERPOLANT *****
206 C TANGENT INFORMATION YPOLD IS NO LONGER NEEDED. HEREAFTER, YPOLD
207 C REPRESENTS THE MOST RECENT POINT WHICH IS ON THE OPPOSITE SIDE OF
208 C LAMBDA=1 FROM Y.
210 C ***** PREPARE FOR MAIN LOOP *****
212 CALL DCOPY(NP1,YOLD,1,YPOLD,1)
214 C INITIALIZE BRACK TO INDICATE THAT THE POINTS Y AND YOLD BRACKET
215 C LAMBDA=1, THUS YOLD = YPOLD.
217 BRACK = .TRUE.
219 C ***** MAIN LOOP *****
221 DO 300 ISTEP=1,LIMIT
223 C UPDATE JACOBIAN MATRIX.
225 C F1=(RHO(Z), YP*Z) TRANSPOSE.
227 IF (IFLAG .EQ. -2) THEN
228 CALL RHO(A,Z(1),Z(2),F1,PAR,IPAR)
229 ELSE IF (IFLAG .EQ. -1) THEN
230 CALL F(Z(2),F1)
231 DO 80 I=1,N
232 F1(I) = Z(1)*F1(I) + (1-Z(1))*(Z(I+1)-A(I))
233 80 CONTINUE
234 ELSE
235 CALL F(Z(2),F1)
236 DO 90 I=1,N
237 F1(I) = Z(1)*(A(I)-F1(I))+Z(I+1)-A(I)
238 90 CONTINUE
239 END IF
240 F1(NP1) = DDOT(NP1,YP,1,Z,1)
243 C PERFORM BROYDEN UPDATE.
245 CALL UPQRQF(NP1,ETA,DZ,F0,F1,QT,R,W,T)
247 C QUASI-NEWTON STEP.
249 C COMPUTE NEWTON STEP.
251 CALL DCOPY(N,F1,1,DZ,1)
252 CALL DSCAL(N,-ONE,DZ,1)
253 DZ(NP1) = 0.0
254 CALL QRSLQF(QT,R,DZ,W,NP1)
256 C TAKE NEWTON STEP.
258 CALL DCOPY(NP1,Z,1,W,1)
259 CALL DAXPY(NP1,ONE,DZ,1,Z,1)
261 C CHECK FOR CONVERGENCE.
263 IF ((ABS(Z(1)-1.0) .LE. RERR+AERR) .AND.
264 $ (DNRM2(NP1,DZ,1) .LE. RERR*DNRM2(N,Z(2),1)+AERR)) THEN
265 CALL DCOPY(NP1,Z,1,Y,1)
266 RETURN
267 END IF
269 C PREPARE FOR NEXT ITERATION.
271 C F0 = F1.
273 CALL DCOPY(NP1,F1,1,F0,1)
275 C IF Z(1) = 1.0 THEN PERFORM QUASI-NEWTON ITERATION AGAIN
276 C WITHOUT COMPUTING A NEW PREDICTOR.
278 IF (ABS(Z(1)-1.0) .LE. RERR+AERR) THEN
279 CALL DCOPY(NP1,Z,1,DZ,1)
280 CALL DAXPY(NP1,-ONE,W,1,DZ,1)
281 GOTO 300
282 END IF
284 C UPDATE Y AND YOLD.
286 CALL DCOPY(NP1,Y,1,YOLD,1)
287 CALL DCOPY(NP1,Z,1,Y,1)
289 C UPDATE YPOLD SUCH THAT YPOLD IS THE MOST RECENT POINT
290 C OPPOSITE OF LAMBDA=1 FROM Y. SET BRACK = .TRUE. IFF
291 C Y & YOLD BRACKET LAMBDA=1 SO THAT YPOLD=YOLD.
293 IF ((Y(1)-1.0)*(YOLD(1)-1.0) .GT. 0) THEN
294 BRACK = .FALSE.
295 ELSE
296 BRACK = .TRUE.
297 CALL DCOPY(NP1,YOLD,1,YPOLD,1)
298 END IF
300 C COMPUTE DELS = ||Y-YPOLD||.
302 CALL DCOPY(NP1,Y,1,DZ,1)
303 CALL DAXPY(NP1,-ONE,YPOLD,1,DZ,1)
304 DELS=DNRM2(NP1,DZ,1)
306 C COMPUTE DZ FOR THE LINEAR PREDICTOR Z = Y + DZ,
307 C WHERE DZ = SA*(YOLD-Y).
309 SA = (1.0-Y(1))/(YOLD(1)-Y(1))
310 CALL DCOPY(NP1,YOLD,1,DZ,1)
311 CALL DAXPY(NP1,-ONE,Y,1,DZ,1)
312 CALL DSCAL(NP1,SA,DZ,1)
314 C TO INSURE STABILITY, THE LINEAR PREDICTION MUST BE NO FARTHER
315 C FROM Y THAN YPOLD IS. THIS IS GUARANTEED IF BRACK = .TRUE.
316 C IF LINEAR PREDICTION IS TOO FAR AWAY, USE BRACKETING POINTS
317 C TO COMPUTE LINEAR PREDICTION.
319 IF (.NOT. BRACK) THEN
320 IF (DNRM2(NP1,DZ,1) .GT. DELS) THEN
322 C COMPUTE DZ = SA*(YPOLD-Y).
324 SA = (1.0-Y(1))/(YPOLD(1)-Y(1))
325 CALL DCOPY(NP1,YPOLD,1,DZ,1)
326 CALL DAXPY(NP1,-ONE,Y,1,DZ,1)
327 CALL DSCAL(NP1,SA,DZ,1)
328 END IF
329 END IF
331 C COMPUTE PREDICTOR Z = Y+DZ, AND DZ = NEW Z - OLD Z (USED FOR
332 C QUASI-NEWTON UPDATE).
334 CALL DAXPY(NP1,ONE,DZ,1,Z,1)
335 CALL DCOPY(NP1,Z,1,DZ,1)
336 CALL DAXPY(NP1,-ONE,W,1,DZ,1)
337 300 CONTINUE
339 C ***** END OF MAIN LOOP. *****
341 C THE ALTERNATING OSCULATORY LINEAR PREDICTION AND QUASI-NEWTON
342 C CORRECTION HAS NOT CONVERGED IN LIMIT STEPS. ERROR RETURN.
343 IFLAG=6
344 RETURN
346 C ***** END OF SUBROUTINE ROOTQF *****