dpchfe.f 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. *DECK DPCHFE
  2. SUBROUTINE DPCHFE (N, X, F, D, INCFD, SKIP, NE, XE, FE, IERR)
  3. C***BEGIN PROLOGUE DPCHFE
  4. C***PURPOSE Evaluate a piecewise cubic Hermite function at an array of
  5. C points. May be used by itself for Hermite interpolation,
  6. C or as an evaluator for DPCHIM or DPCHIC.
  7. C***LIBRARY SLATEC (PCHIP)
  8. C***CATEGORY E3
  9. C***TYPE DOUBLE PRECISION (PCHFE-S, DPCHFE-D)
  10. C***KEYWORDS CUBIC HERMITE EVALUATION, HERMITE INTERPOLATION, PCHIP,
  11. C PIECEWISE CUBIC EVALUATION
  12. C***AUTHOR Fritsch, F. N., (LLNL)
  13. C Lawrence Livermore National Laboratory
  14. C P.O. Box 808 (L-316)
  15. C Livermore, CA 94550
  16. C FTS 532-4275, (510) 422-4275
  17. C***DESCRIPTION
  18. C
  19. C DPCHFE: Piecewise Cubic Hermite Function Evaluator
  20. C
  21. C Evaluates the cubic Hermite function defined by N, X, F, D at
  22. C the points XE(J), J=1(1)NE.
  23. C
  24. C To provide compatibility with DPCHIM and DPCHIC, includes an
  25. C increment between successive values of the F- and D-arrays.
  26. C
  27. C ----------------------------------------------------------------------
  28. C
  29. C Calling sequence:
  30. C
  31. C PARAMETER (INCFD = ...)
  32. C INTEGER N, NE, IERR
  33. C DOUBLE PRECISION X(N), F(INCFD,N), D(INCFD,N), XE(NE), FE(NE)
  34. C LOGICAL SKIP
  35. C
  36. C CALL DPCHFE (N, X, F, D, INCFD, SKIP, NE, XE, FE, IERR)
  37. C
  38. C Parameters:
  39. C
  40. C N -- (input) number of data points. (Error return if N.LT.2 .)
  41. C
  42. C X -- (input) real*8 array of independent variable values. The
  43. C elements of X must be strictly increasing:
  44. C X(I-1) .LT. X(I), I = 2(1)N.
  45. C (Error return if not.)
  46. C
  47. C F -- (input) real*8 array of function values. F(1+(I-1)*INCFD) is
  48. C the value corresponding to X(I).
  49. C
  50. C D -- (input) real*8 array of derivative values. D(1+(I-1)*INCFD)
  51. C is the value corresponding to X(I).
  52. C
  53. C INCFD -- (input) increment between successive values in F and D.
  54. C (Error return if INCFD.LT.1 .)
  55. C
  56. C SKIP -- (input/output) logical variable which should be set to
  57. C .TRUE. if the user wishes to skip checks for validity of
  58. C preceding parameters, or to .FALSE. otherwise.
  59. C This will save time in case these checks have already
  60. C been performed (say, in DPCHIM or DPCHIC).
  61. C SKIP will be set to .TRUE. on normal return.
  62. C
  63. C NE -- (input) number of evaluation points. (Error return if
  64. C NE.LT.1 .)
  65. C
  66. C XE -- (input) real*8 array of points at which the function is to
  67. C be evaluated.
  68. C
  69. C NOTES:
  70. C 1. The evaluation will be most efficient if the elements
  71. C of XE are increasing relative to X;
  72. C that is, XE(J) .GE. X(I)
  73. C implies XE(K) .GE. X(I), all K.GE.J .
  74. C 2. If any of the XE are outside the interval [X(1),X(N)],
  75. C values are extrapolated from the nearest extreme cubic,
  76. C and a warning error is returned.
  77. C
  78. C FE -- (output) real*8 array of values of the cubic Hermite
  79. C function defined by N, X, F, D at the points XE.
  80. C
  81. C IERR -- (output) error flag.
  82. C Normal return:
  83. C IERR = 0 (no errors).
  84. C Warning error:
  85. C IERR.GT.0 means that extrapolation was performed at
  86. C IERR points.
  87. C "Recoverable" errors:
  88. C IERR = -1 if N.LT.2 .
  89. C IERR = -2 if INCFD.LT.1 .
  90. C IERR = -3 if the X-array is not strictly increasing.
  91. C IERR = -4 if NE.LT.1 .
  92. C (The FE-array has not been changed in any of these cases.)
  93. C NOTE: The above errors are checked in the order listed,
  94. C and following arguments have **NOT** been validated.
  95. C
  96. C***REFERENCES (NONE)
  97. C***ROUTINES CALLED DCHFEV, XERMSG
  98. C***REVISION HISTORY (YYMMDD)
  99. C 811020 DATE WRITTEN
  100. C 820803 Minor cosmetic changes for release 1.
  101. C 870707 Corrected XERROR calls for d.p. name(s).
  102. C 890206 Corrected XERROR calls.
  103. C 890531 Changed all specific intrinsics to generic. (WRB)
  104. C 890831 Modified array declarations. (WRB)
  105. C 891006 Cosmetic changes to prologue. (WRB)
  106. C 891006 REVISION DATE from Version 3.2
  107. C 891214 Prologue converted to Version 4.0 format. (BAB)
  108. C 900315 CALLs to XERROR changed to CALLs to XERMSG. (THJ)
  109. C***END PROLOGUE DPCHFE
  110. C Programming notes:
  111. C
  112. C 1. To produce a single precision version, simply:
  113. C a. Change DPCHFE to PCHFE, and DCHFEV to CHFEV, wherever they
  114. C occur,
  115. C b. Change the double precision declaration to real,
  116. C
  117. C 2. Most of the coding between the call to DCHFEV and the end of
  118. C the IR-loop could be eliminated if it were permissible to
  119. C assume that XE is ordered relative to X.
  120. C
  121. C 3. DCHFEV does not assume that X1 is less than X2. thus, it would
  122. C be possible to write a version of DPCHFE that assumes a
  123. C decreasing X-array by simply running the IR-loop backwards
  124. C (and reversing the order of appropriate tests).
  125. C
  126. C 4. The present code has a minor bug, which I have decided is not
  127. C worth the effort that would be required to fix it.
  128. C If XE contains points in [X(N-1),X(N)], followed by points .LT.
  129. C X(N-1), followed by points .GT.X(N), the extrapolation points
  130. C will be counted (at least) twice in the total returned in IERR.
  131. C
  132. C DECLARE ARGUMENTS.
  133. C
  134. INTEGER N, INCFD, NE, IERR
  135. DOUBLE PRECISION X(*), F(INCFD,*), D(INCFD,*), XE(*), FE(*)
  136. LOGICAL SKIP
  137. C
  138. C DECLARE LOCAL VARIABLES.
  139. C
  140. INTEGER I, IERC, IR, J, JFIRST, NEXT(2), NJ
  141. C
  142. C VALIDITY-CHECK ARGUMENTS.
  143. C
  144. C***FIRST EXECUTABLE STATEMENT DPCHFE
  145. IF (SKIP) GO TO 5
  146. C
  147. IF ( N.LT.2 ) GO TO 5001
  148. IF ( INCFD.LT.1 ) GO TO 5002
  149. DO 1 I = 2, N
  150. IF ( X(I).LE.X(I-1) ) GO TO 5003
  151. 1 CONTINUE
  152. C
  153. C FUNCTION DEFINITION IS OK, GO ON.
  154. C
  155. 5 CONTINUE
  156. IF ( NE.LT.1 ) GO TO 5004
  157. IERR = 0
  158. SKIP = .TRUE.
  159. C
  160. C LOOP OVER INTERVALS. ( INTERVAL INDEX IS IL = IR-1 . )
  161. C ( INTERVAL IS X(IL).LE.X.LT.X(IR) . )
  162. JFIRST = 1
  163. IR = 2
  164. 10 CONTINUE
  165. C
  166. C SKIP OUT OF LOOP IF HAVE PROCESSED ALL EVALUATION POINTS.
  167. C
  168. IF (JFIRST .GT. NE) GO TO 5000
  169. C
  170. C LOCATE ALL POINTS IN INTERVAL.
  171. C
  172. DO 20 J = JFIRST, NE
  173. IF (XE(J) .GE. X(IR)) GO TO 30
  174. 20 CONTINUE
  175. J = NE + 1
  176. GO TO 40
  177. C
  178. C HAVE LOCATED FIRST POINT BEYOND INTERVAL.
  179. C
  180. 30 CONTINUE
  181. IF (IR .EQ. N) J = NE + 1
  182. C
  183. 40 CONTINUE
  184. NJ = J - JFIRST
  185. C
  186. C SKIP EVALUATION IF NO POINTS IN INTERVAL.
  187. C
  188. IF (NJ .EQ. 0) GO TO 50
  189. C
  190. C EVALUATE CUBIC AT XE(I), I = JFIRST (1) J-1 .
  191. C
  192. C ----------------------------------------------------------------
  193. CALL DCHFEV (X(IR-1),X(IR), F(1,IR-1),F(1,IR), D(1,IR-1),D(1,IR)
  194. * ,NJ, XE(JFIRST), FE(JFIRST), NEXT, IERC)
  195. C ----------------------------------------------------------------
  196. IF (IERC .LT. 0) GO TO 5005
  197. C
  198. IF (NEXT(2) .EQ. 0) GO TO 42
  199. C IF (NEXT(2) .GT. 0) THEN
  200. C IN THE CURRENT SET OF XE-POINTS, THERE ARE NEXT(2) TO THE
  201. C RIGHT OF X(IR).
  202. C
  203. IF (IR .LT. N) GO TO 41
  204. C IF (IR .EQ. N) THEN
  205. C THESE ARE ACTUALLY EXTRAPOLATION POINTS.
  206. IERR = IERR + NEXT(2)
  207. GO TO 42
  208. 41 CONTINUE
  209. C ELSE
  210. C WE SHOULD NEVER HAVE GOTTEN HERE.
  211. GO TO 5005
  212. C ENDIF
  213. C ENDIF
  214. 42 CONTINUE
  215. C
  216. IF (NEXT(1) .EQ. 0) GO TO 49
  217. C IF (NEXT(1) .GT. 0) THEN
  218. C IN THE CURRENT SET OF XE-POINTS, THERE ARE NEXT(1) TO THE
  219. C LEFT OF X(IR-1).
  220. C
  221. IF (IR .GT. 2) GO TO 43
  222. C IF (IR .EQ. 2) THEN
  223. C THESE ARE ACTUALLY EXTRAPOLATION POINTS.
  224. IERR = IERR + NEXT(1)
  225. GO TO 49
  226. 43 CONTINUE
  227. C ELSE
  228. C XE IS NOT ORDERED RELATIVE TO X, SO MUST ADJUST
  229. C EVALUATION INTERVAL.
  230. C
  231. C FIRST, LOCATE FIRST POINT TO LEFT OF X(IR-1).
  232. DO 44 I = JFIRST, J-1
  233. IF (XE(I) .LT. X(IR-1)) GO TO 45
  234. 44 CONTINUE
  235. C NOTE-- CANNOT DROP THROUGH HERE UNLESS THERE IS AN ERROR
  236. C IN DCHFEV.
  237. GO TO 5005
  238. C
  239. 45 CONTINUE
  240. C RESET J. (THIS WILL BE THE NEW JFIRST.)
  241. J = I
  242. C
  243. C NOW FIND OUT HOW FAR TO BACK UP IN THE X-ARRAY.
  244. DO 46 I = 1, IR-1
  245. IF (XE(J) .LT. X(I)) GO TO 47
  246. 46 CONTINUE
  247. C NB-- CAN NEVER DROP THROUGH HERE, SINCE XE(J).LT.X(IR-1).
  248. C
  249. 47 CONTINUE
  250. C AT THIS POINT, EITHER XE(J) .LT. X(1)
  251. C OR X(I-1) .LE. XE(J) .LT. X(I) .
  252. C RESET IR, RECOGNIZING THAT IT WILL BE INCREMENTED BEFORE
  253. C CYCLING.
  254. IR = MAX(1, I-1)
  255. C ENDIF
  256. C ENDIF
  257. 49 CONTINUE
  258. C
  259. JFIRST = J
  260. C
  261. C END OF IR-LOOP.
  262. C
  263. 50 CONTINUE
  264. IR = IR + 1
  265. IF (IR .LE. N) GO TO 10
  266. C
  267. C NORMAL RETURN.
  268. C
  269. 5000 CONTINUE
  270. RETURN
  271. C
  272. C ERROR RETURNS.
  273. C
  274. 5001 CONTINUE
  275. C N.LT.2 RETURN.
  276. IERR = -1
  277. CALL XERMSG ('SLATEC', 'DPCHFE',
  278. + 'NUMBER OF DATA POINTS LESS THAN TWO', IERR, 1)
  279. RETURN
  280. C
  281. 5002 CONTINUE
  282. C INCFD.LT.1 RETURN.
  283. IERR = -2
  284. CALL XERMSG ('SLATEC', 'DPCHFE', 'INCREMENT LESS THAN ONE', IERR,
  285. + 1)
  286. RETURN
  287. C
  288. 5003 CONTINUE
  289. C X-ARRAY NOT STRICTLY INCREASING.
  290. IERR = -3
  291. CALL XERMSG ('SLATEC', 'DPCHFE',
  292. + 'X-ARRAY NOT STRICTLY INCREASING', IERR, 1)
  293. RETURN
  294. C
  295. 5004 CONTINUE
  296. C NE.LT.1 RETURN.
  297. IERR = -4
  298. CALL XERMSG ('SLATEC', 'DPCHFE',
  299. + 'NUMBER OF EVALUATION POINTS LESS THAN ONE', IERR, 1)
  300. RETURN
  301. C
  302. 5005 CONTINUE
  303. C ERROR RETURN FROM DCHFEV.
  304. C *** THIS CASE SHOULD NEVER OCCUR ***
  305. IERR = -5
  306. CALL XERMSG ('SLATEC', 'DPCHFE',
  307. + 'ERROR RETURN FROM DCHFEV -- FATAL', IERR, 2)
  308. RETURN
  309. C------------- LAST LINE OF DPCHFE FOLLOWS -----------------------------
  310. END