1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; Given the arrays X and Y, which tabulate a function (with the X[i] |
---|
5 | ; AND Y[i] in ascending order), and given an input value X2, the |
---|
6 | ; spl_incr function returns an interpolated value for the given values |
---|
7 | ; of X2. The interpolation method is based on cubic spline, corrected |
---|
8 | ; in a way that interpolated values are also monotonically increasing. |
---|
9 | ; |
---|
10 | ; @param x1 {in}{required} |
---|
11 | ; An n-elements (at least 2) input vector that specifies the tabulate points in |
---|
12 | ; a strict ascending order. |
---|
13 | ; |
---|
14 | ; @param y1 {in}{required} |
---|
15 | ; f(x) = y. An n-elements input vector that specifies the values |
---|
16 | ; of the tabulated function F(Xi) corresponding to Xi. As f is |
---|
17 | ; supposed to be monotonically increasing, y values must be |
---|
18 | ; monotonically increasing. y can have equal consecutive values. |
---|
19 | ; |
---|
20 | ; @param x2 {in}{required} |
---|
21 | ; The input values for which the interpolated values are |
---|
22 | ; desired. Its values must be strictly monotonically increasing. |
---|
23 | ; |
---|
24 | ; @param der2 |
---|
25 | ; |
---|
26 | ; @param x |
---|
27 | ; |
---|
28 | ; @returns |
---|
29 | ; y2: f(x2) = y2. Double precision array |
---|
30 | ; |
---|
31 | ; @restrictions |
---|
32 | ; It might be possible that y2[i+1]-y2[i] has very small negative |
---|
33 | ; values (amplitude smaller than 1.e-6)... |
---|
34 | ; |
---|
35 | ; @examples |
---|
36 | ; |
---|
37 | ; IDL> n = 100L |
---|
38 | ; IDL> x = (dindgen(n))^2 |
---|
39 | ; IDL> y = abs(randomn(0, n)) |
---|
40 | ; IDL> y[n/2:n/2+1] = 0. |
---|
41 | ; IDL> y[n-n/3] = 0. |
---|
42 | ; IDL> y[n-n/6:n-n/6+5] = 0. |
---|
43 | ; IDL> y = total(y, /cumulative, /double) |
---|
44 | ; IDL> x2 = dindgen((n-1)^2) |
---|
45 | ; IDL> n2 = n_elements(x2) |
---|
46 | ; IDL> print, min(y[1:n-1]-y[0:n-2]) LT 0 |
---|
47 | ; IDL> y2 = spl_incr( x, y, x2) |
---|
48 | ; IDL> splot, x, y, xstyle = 1, ystyle = 1, ysurx=.25, petit = [1, 2, 1], /land |
---|
49 | ; IDL> oplot, x2, y2, color = 100 |
---|
50 | ; IDL> c = y2[1:n2-1] - y2[0:n2-2] |
---|
51 | ; IDL> print, min(c) LT 0 |
---|
52 | ; IDL> print, min(c, max = ma), ma |
---|
53 | ; IDL> splot,c,xstyle=1,ystyle=1, yrange=[-.01,.05], ysurx=.25, petit = [1, 2, 2], /noerase |
---|
54 | ; IDL> oplot,[0, n_elements(c)], [0, 0], linestyle = 1 |
---|
55 | ; |
---|
56 | ; @history |
---|
57 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr): May-Dec 2005 |
---|
58 | ; |
---|
59 | ; @version |
---|
60 | ; $Id$ |
---|
61 | ; |
---|
62 | ;- |
---|
63 | FUNCTION pure_concave, x1, x2, y1, y2, der2, x |
---|
64 | ; |
---|
65 | compile_opt idl2, strictarrsubs |
---|
66 | ; |
---|
67 | ; X^n type |
---|
68 | ; |
---|
69 | xx = (double(x)-double(x1))/(double(x2)-double(x1)) |
---|
70 | f = (double(x2)-double(x1))/(double(y2)-double(y1)) |
---|
71 | n = der2*temporary(f) |
---|
72 | res = xx^(n) |
---|
73 | ; IF check_math() GT 0 THEN BEGIN |
---|
74 | ; zero = where(abs(res) LT 1.e-10) |
---|
75 | ; IF zero[0] NE -1 THEN res[zero] = 0.0d |
---|
76 | ; END |
---|
77 | res = temporary(res)*(double(y2)-double(y1))+y1 |
---|
78 | ; |
---|
79 | ; IF array_equal(sort(res), lindgen(n_elements(res)) ) NE 1 THEN stop |
---|
80 | RETURN, res |
---|
81 | END |
---|
82 | ; |
---|
83 | ;+ |
---|
84 | ; |
---|
85 | ; @param x1 {in}{required} |
---|
86 | ; An n-elements (at least 2) input vector that specifies the tabulate points in |
---|
87 | ; a strict ascending order. |
---|
88 | ; |
---|
89 | ; @param y1 {in}{required} |
---|
90 | ; f(x) = y. An n-elements input vector that specifies the values |
---|
91 | ; of the tabulated function F(Xi) corresponding to Xi. As f is |
---|
92 | ; supposed to be monotonically increasing, y values must be |
---|
93 | ; monotonically increasing. y can have equal consecutive values. |
---|
94 | ; |
---|
95 | ; @param x2 {in}{required} |
---|
96 | ; The input values for which the interpolated values are |
---|
97 | ; desired. Its values must be strictly monotonically increasing. |
---|
98 | ; |
---|
99 | ; @param der2 |
---|
100 | ; |
---|
101 | ; @param x |
---|
102 | ; |
---|
103 | ;- |
---|
104 | FUNCTION pure_convex, x1, x2, y1, y2, der2, x |
---|
105 | ; |
---|
106 | compile_opt idl2, strictarrsubs |
---|
107 | ; |
---|
108 | ; 1-(1-X)^n type |
---|
109 | ; |
---|
110 | xx = 1.0d - (double(x)-double(x1))/(double(x2)-double(x1)) |
---|
111 | f = (double(x2)-double(x1))/(double(y2)-double(y1)) |
---|
112 | n = der2*temporary(f) |
---|
113 | res = xx^(n) |
---|
114 | ; IF check_math() GT 0 THEN BEGIN |
---|
115 | ; zero = where(abs(res) LT 1.e-10) |
---|
116 | ; IF zero[0] NE -1 THEN res[zero] = 0.0d |
---|
117 | ; END |
---|
118 | res = 1.0d - temporary(res) |
---|
119 | res = temporary(res)*(y2-y1)+y1 |
---|
120 | ; |
---|
121 | ; IF array_equal(sort(res), lindgen(n_elements(res)) ) NE 1 THEN stop |
---|
122 | RETURN, res |
---|
123 | END |
---|
124 | ; |
---|
125 | ;+ |
---|
126 | ; |
---|
127 | ; @param x |
---|
128 | ; @param y |
---|
129 | ; @param x2 |
---|
130 | ; @keyword YP0 |
---|
131 | ; The first derivative of the interpolating function at the |
---|
132 | ; point X0. If YP0 is omitted, the second derivative at the |
---|
133 | ; boundary is set to zero, resulting in a "natural spline." |
---|
134 | ; |
---|
135 | ; @keyword YPN_1 |
---|
136 | ; The first derivative of the interpolating function at the |
---|
137 | ; point Xn-1. If YPN_1 is omitted, the second derivative at the |
---|
138 | ; boundary is set to zero, resulting in a "natural spline." |
---|
139 | ;- |
---|
140 | FUNCTION spl_incr, x, y, x2, YP0=yp0, YPN_1=ypn_1 |
---|
141 | ; |
---|
142 | compile_opt idl2, strictarrsubs |
---|
143 | ; |
---|
144 | ;--------------------------------- |
---|
145 | ; check and initialization ... |
---|
146 | ;--------------------------------- |
---|
147 | ; |
---|
148 | nx = n_elements(x) |
---|
149 | ny = n_elements(y) |
---|
150 | nx2 = n_elements(x2) |
---|
151 | ; x must have at least 2 elements |
---|
152 | IF nx LT 2 THEN stop |
---|
153 | ; y must have the same number of elements than x |
---|
154 | IF nx NE ny THEN stop |
---|
155 | ; x be monotonically increasing |
---|
156 | IF min(x[1:nx-1]-x[0:nx-2]) LE 0 THEN stop |
---|
157 | ; x2 be monotonically increasing |
---|
158 | IF N_ELEMENTS(X2) GE 2 THEN $ |
---|
159 | IF min(x2[1:nx2-1]-x2[0:nx2-2]) LE 0 THEN stop |
---|
160 | ; y be monotonically increasing |
---|
161 | IF min(y[1:ny-1]-y[0:ny-2]) LT 0 THEN stop |
---|
162 | ;--------------------------------- |
---|
163 | ; first check: check if two consecutive values are equal |
---|
164 | ;--------------------------------- |
---|
165 | bad = where(y[1:ny-1]-y[0:ny-2] EQ 0, cntbad) |
---|
166 | IF cntbad NE 0 THEN BEGIN |
---|
167 | ; define the results: y2 |
---|
168 | y2 = dblarr(nx2) |
---|
169 | ; define xinx2: see help of value_locate |
---|
170 | ; if xinx2[i] eq -1 : x[bad[i]] < x2[0] |
---|
171 | ; if xinx2[i] eq nx2-1: x[bad[i]] >= x2[nx2-1] |
---|
172 | ; else : x2[xinx2[i]] <= x[bad[i]] < x2[xinx2[i]+1] |
---|
173 | xinx2 = value_locate(x2, x[bad]) |
---|
174 | xinx2_1 = value_locate(x2, x[bad+1]) |
---|
175 | ; |
---|
176 | ; left side ... if there is x2 values smaller that x[bad[0]]. |
---|
177 | ; we force ypn_1 = 0.0d |
---|
178 | IF xinx2[0] NE -1 THEN BEGIN |
---|
179 | IF bad[0] EQ 0 THEN BEGIN |
---|
180 | IF xinx2[0] NE 0 THEN stop |
---|
181 | y2[0] = y[0] |
---|
182 | ENDIF ELSE BEGIN |
---|
183 | y2[0:xinx2[0]] $ |
---|
184 | = spl_incr(x[0:bad[0]], y[0:bad[0]], x2[0:xinx2[0]] $ |
---|
185 | , yp0 = yp0, ypn_1 = 0.0d) |
---|
186 | ENDELSE |
---|
187 | ENDIF |
---|
188 | ; flat section |
---|
189 | IF xinx2_1[0] NE -1 THEN $ |
---|
190 | y2[(xinx2[0]+1) < xinx2_1[0] : xinx2_1[0]] = y[bad[0]] |
---|
191 | ; middle pieces ... if cntbad gt 1 then we have to cut spl_incr in |
---|
192 | ; more than 2 pieces... |
---|
193 | IF cntbad GT 1 THEN BEGIN |
---|
194 | ; we take care of the piece located between bad[ib-1]+1 and bad[ib] |
---|
195 | FOR ib = 1, cntbad-1 DO BEGIN |
---|
196 | ; if there is x2 values smaller that x[bad[ib]], then the x2 values |
---|
197 | ; located between bad[ib-1]+1 and bad[ib] are (xinx2_1[ib-1]+1:xinx2[ib] |
---|
198 | ; and if we don't have two consecutive flat sections |
---|
199 | IF xinx2[ib] NE -1 AND (bad[ib-1] NE bad[ib]-1) THEN begin |
---|
200 | y2[(xinx2_1[ib-1]+1) < xinx2[ib]:xinx2[ib]] $ |
---|
201 | = spl_incr(x[bad[ib-1]+1:bad[ib]], y[bad[ib-1]+1:bad[ib]] $ |
---|
202 | , x2[(xinx2_1[ib-1]+1) < xinx2[ib]:xinx2[ib]] $ |
---|
203 | , yp0 = 0.0d, ypn_1 = 0.0d) |
---|
204 | ENDIF |
---|
205 | ; flat section |
---|
206 | IF xinx2_1[ib] NE -1 THEN $ |
---|
207 | y2[(xinx2[ib]+1) < xinx2_1[ib] : xinx2_1[ib]] = y[bad[ib]] |
---|
208 | ENDFOR |
---|
209 | ENDIF |
---|
210 | ; right side ... if there is x2 values larger that x[bad[cntbad-1]+1]. |
---|
211 | ; we force yp0 = 0.0d |
---|
212 | IF xinx2_1[cntbad-1] NE nx2-1 THEN $ |
---|
213 | y2[xinx2_1[cntbad-1]+1:nx2-1] $ |
---|
214 | = spl_incr(x[bad[cntbad-1]+1:nx-1], y[bad[cntbad-1]+1:nx-1] $ |
---|
215 | , x2[xinx2_1[cntbad-1]+1:nx2-1] $ |
---|
216 | , yp0 = 0.0d, ypn_1 = ypn_1new) |
---|
217 | |
---|
218 | RETURN, y2 |
---|
219 | |
---|
220 | ENDIF |
---|
221 | ;----------- |
---|
222 | ; compute the second derivative of the cubic spline on each x. |
---|
223 | ;----------- |
---|
224 | yscd = spl_init(x, y, yp0 = yp0, ypn_1 = ypn_1, /double) |
---|
225 | ;--------------------------------- |
---|
226 | ; second check: none of the first derivative on x values must be negative. |
---|
227 | ;--------------------------------- |
---|
228 | ; |
---|
229 | ; compute the first derivative on x |
---|
230 | ; |
---|
231 | yifrst = spl_fstdrv(x, y, yscd, x) |
---|
232 | ; |
---|
233 | ; we force the negative first derivative to 0 by calling again |
---|
234 | ; spl_incr with the keywords yp0 and ypn_1 to specify the |
---|
235 | ; first derivative equal to 0 |
---|
236 | ; |
---|
237 | bad = where(yifrst LT 0.0d, cntbad) |
---|
238 | IF cntbad NE 0 THEN BEGIN |
---|
239 | ; |
---|
240 | ; we define the new values of the keyword ypn_1: |
---|
241 | ; if the first derivative of the last value of x is negative |
---|
242 | ; we define the new values of the keyword ypn_1 to 0.0d0 |
---|
243 | IF bad[cntbad-1] EQ nx-1 THEN BEGIN |
---|
244 | ypn_1new = 0.0d |
---|
245 | ; we remove this case from the list |
---|
246 | IF cntbad GE 2 THEN bad = bad[0:cntbad-2] |
---|
247 | cntbad = cntbad-1 |
---|
248 | ; else we take the value of ypn_1 if it was already defined |
---|
249 | ENDIF ELSE IF n_elements(ypn_1) NE 0 THEN ypn_1new = ypn_1 |
---|
250 | ; |
---|
251 | ; we define the new values of the keyword yp0: |
---|
252 | ; if the first derivative of the first value of x is negative |
---|
253 | ; we define the new values of the keyword yp0 to 0.0 |
---|
254 | IF bad[0] EQ 0 THEN BEGIN |
---|
255 | yp0new = 0.0d |
---|
256 | ; we remove this case from the list |
---|
257 | IF cntbad GE 2 THEN bad = bad[1:cntbad-1] |
---|
258 | cntbad = cntbad-1 |
---|
259 | ; else we take the value of yp0 if it was already defined |
---|
260 | ENDIF ELSE IF n_elements(yp0) NE 0 THEN yp0new = yp0 |
---|
261 | ; |
---|
262 | ; if all the negative derivative corresponded to one of the cases above, |
---|
263 | ; then we can directly call spl_incr with the new yp0new and ypn_1new |
---|
264 | IF cntbad LE 0 THEN BEGIN |
---|
265 | y2 = spl_incr(x, y, x2, yp0 = yp0new, ypn_1 = ypn_1new) |
---|
266 | ; |
---|
267 | ; else: there is still cases with negative derivative ... |
---|
268 | ; we will cut spl_incr in n spl_incr and specify yp0, ypn_1 |
---|
269 | ; for each of this n spl_incr |
---|
270 | ENDIF ELSE BEGIN |
---|
271 | ; define xinx2: see help of value_locate |
---|
272 | ; if xinx2[i] eq -1 : x[bad[i]] < x2[0] |
---|
273 | ; if xinx2[i] eq nx2-1: x[bad[i]] >= x2[nx2-1] |
---|
274 | ; else : x2[xinx2[i]] <= x[bad[i]] < x2[xinx2[i]+1] |
---|
275 | xinx2 = value_locate(x2, x[bad]) |
---|
276 | y2 = dblarr(nx2) |
---|
277 | ; left side ... if there is x2 values smaller that x[bad[0]]. |
---|
278 | ; we force ypn_1 = 0.0d |
---|
279 | IF xinx2[0] NE -1 THEN $ |
---|
280 | y2[0:xinx2[0]] $ |
---|
281 | = spl_incr(x[0:bad[0]], y[0:bad[0]], x2[0:xinx2[0]] $ |
---|
282 | , yp0 = yp0new, ypn_1 = 0.0d) |
---|
283 | ; middle pieces ... if cntbad gt 1 then we have to cut spl_incr in |
---|
284 | ; more than 2 pieces -> we have middle pieces for which |
---|
285 | ; we force yp0 = 0.0d and ypn_1 = 0.0d |
---|
286 | IF cntbad GT 1 THEN BEGIN |
---|
287 | ; we take care of the piece located between bad[ib-1] and bad[ib] |
---|
288 | FOR ib = 1, cntbad-1 DO BEGIN |
---|
289 | ; if there is x2 values smaller that x[bad[ib]], then the x2 values |
---|
290 | ; located between bad[ib-1] and bad[ib] are (xinx2[ib-1]+1:xinx2[ib] |
---|
291 | IF xinx2[ib] NE -1 THEN begin |
---|
292 | y2[(xinx2[ib-1]+1) < xinx2[ib]:xinx2[ib]] $ |
---|
293 | = spl_incr(x[bad[ib-1]:bad[ib]], y[bad[ib-1]:bad[ib]] $ |
---|
294 | , x2[(xinx2[ib-1]+1) < xinx2[ib]:xinx2[ib]] $ |
---|
295 | , yp0 = 0.0d, ypn_1 = 0.0d) |
---|
296 | endif |
---|
297 | ENDFOR |
---|
298 | ENDIF |
---|
299 | ; right side ... if there is x2 values larger that x[bad[cntbad-1]]. |
---|
300 | ; we force yp0 = 0.0d |
---|
301 | IF xinx2[cntbad-1] NE nx2-1 THEN $ |
---|
302 | y2[xinx2[cntbad-1]+1:nx2-1] $ |
---|
303 | = spl_incr(x[bad[cntbad-1]:nx-1], y[bad[cntbad-1]:nx-1] $ |
---|
304 | , x2[xinx2[cntbad-1]+1:nx2-1] $ |
---|
305 | , yp0 = 0.0d, ypn_1 = ypn_1new) |
---|
306 | ENDELSE |
---|
307 | ; we return the checked and corrected value of yfrst |
---|
308 | ; FOR i = 0, nx-1 DO BEGIN |
---|
309 | ; same = where(abs(x2- x[i]) LT 1.e-10, cnt) |
---|
310 | ; ; IF cnt NE 0 THEN y2[same] = y[i] |
---|
311 | ; ENDFOR |
---|
312 | RETURN, y2 |
---|
313 | ENDIF |
---|
314 | ; |
---|
315 | ; we can be in this part of the code only if: |
---|
316 | ; (1) spl_incr is called by itself |
---|
317 | ; (2) none are the first derivative in x are negative (because they have been |
---|
318 | ; checked and corrected by the previous call to spl_incr, see above) |
---|
319 | ;--------------------------------- |
---|
320 | ; third check: we have to make sure that the first derivative cannot |
---|
321 | ; have negative values between on x[0] and x[nx-1] |
---|
322 | ;--------------------------------- |
---|
323 | ; |
---|
324 | ; first we compute the first derivative, next we correct the values |
---|
325 | ; where we know that the first derivative can be negative. |
---|
326 | ; |
---|
327 | y2 = spl_interp(x, y, yscd, x2, /double) |
---|
328 | ; |
---|
329 | ; between x[i] and x[i+1], the cubic spline is a cubic function: |
---|
330 | ; y = a*X^3 + b*X^2 + c*X + d |
---|
331 | ; y' = 3a*X^2 + 2b*X + c |
---|
332 | ; y''= 6a*X + 2b |
---|
333 | ; if we take X = x[i+1]-x[i] then |
---|
334 | ; d = y[i]; c = y'[i]; b = 0.5 * y''[i], |
---|
335 | ; a = 1/6 * (y''[i+1]-y''[i])/(x[i+1]-x[i]) |
---|
336 | ; |
---|
337 | ; y'[i] and y'[i+1] are positive so y' can be negative |
---|
338 | ; between x[i] and x[i+1] only if |
---|
339 | ; 1) a > 0 |
---|
340 | ; ==> y''[i+1] > y''[i] |
---|
341 | ; 2) y' reach its minimum value between x[i] and x[i+1] |
---|
342 | ; -> 0 < - b/(3a) < x[i+1]-x[i] |
---|
343 | ; ==> y''[i+1] > 0 > y''[i] |
---|
344 | ; |
---|
345 | ; we do a first selection by looking for those points... |
---|
346 | ; |
---|
347 | loc = lindgen(nx-1) |
---|
348 | maybebad = where(yscd[loc] LE 0.0d AND yscd[loc+1] GE 0.0d, cntbad) |
---|
349 | ; |
---|
350 | IF cntbad NE 0 THEN BEGIN |
---|
351 | |
---|
352 | mbbloc = loc[maybebad] |
---|
353 | |
---|
354 | aaa = (yscd[mbbloc+1]-yscd[mbbloc])/(6.0d*(x[mbbloc+1]-x[mbbloc])) |
---|
355 | bbb = 0.5d * yscd[mbbloc] |
---|
356 | ccc = yifrst[mbbloc] |
---|
357 | ddd = y[mbbloc] |
---|
358 | ; |
---|
359 | ; definitive selection: |
---|
360 | ; y' can become negative if and only if (2b)^2 - 4(3a)c > 0 |
---|
361 | ; y' can become negative if and only if b^2 - (3a)c > 0 |
---|
362 | ; |
---|
363 | delta = bbb*bbb - 3.0d*aaa*ccc |
---|
364 | ; |
---|
365 | bad = where(delta GT 0, cntbad) |
---|
366 | ; |
---|
367 | IF cntbad NE 0 THEN BEGIN |
---|
368 | delta = delta[bad] |
---|
369 | aaa = aaa[bad] |
---|
370 | bbb = bbb[bad] |
---|
371 | ccc = ccc[bad] |
---|
372 | ddd = ddd[bad] |
---|
373 | bad = maybebad[bad] |
---|
374 | ; define xinx2_1: see help of value_locate |
---|
375 | ; if xinx2_1[i] eq -1 : x[bad[i]] < x2[0] |
---|
376 | ; if xinx2_1[i] eq nx2-1: x[bad[i]] >= x2[nx2-1] |
---|
377 | ; else : x2[xinx2_1[i]] <= x[bad[i]] < x2[xinx2_1[i]+1] |
---|
378 | xinx2_1 = value_locate(x2, x[bad]) |
---|
379 | ; define xinx2_2: see help of value_locate |
---|
380 | ; if xinx2_2[i] eq -1 : x[bad[i]+1] < x2[0] |
---|
381 | ; if xinx2_2[i] eq nx2-1: x[bad[i]+1] >= x2[nx2-1] |
---|
382 | ; else : x2[xinx2_2[i]] <= x[bad[i]+1] < x2[xinx2_2[i]+1] |
---|
383 | xinx2_2 = value_locate(x2, x[bad+1]) |
---|
384 | ; to avoid the particular case when x2 = x[bad[i]] |
---|
385 | ; and there is no other x2 point until x[bad[i]+1] |
---|
386 | xinx2_1 = xinx2_1 < (xinx2_2-1) |
---|
387 | ; |
---|
388 | FOR ib = 0, cntbad-1 DO BEGIN |
---|
389 | ; |
---|
390 | ; at least one of the x2 points must be located between |
---|
391 | ; x[bad[ib]] and x[bad[ib]+1] |
---|
392 | IF x2[0] LE x[bad[ib]+1] AND x2[nx2-1] GE x[bad[ib]] THEN BEGIN |
---|
393 | ; |
---|
394 | CASE 1 OF |
---|
395 | yifrst[bad[ib]+1] EQ 0.0d:BEGIN |
---|
396 | ; case pur convex: we use the first derivative of 1-(1-x)^n |
---|
397 | ; and ajust n to get the good value: yifrst[bad[ib]] in x[bad[ib]] |
---|
398 | y2[xinx2_1[ib]+1:xinx2_2[ib]] $ |
---|
399 | = pure_convex(x[bad[ib]], x[bad[ib]+1] $ |
---|
400 | , y[bad[ib]], y[bad[ib]+1] $ |
---|
401 | , yifrst[bad[ib]] $ |
---|
402 | , x2[xinx2_1[ib]+1:xinx2_2[ib]]) |
---|
403 | END |
---|
404 | yifrst[bad[ib]] EQ 0.0d:BEGIN |
---|
405 | ; case pur concave: we use the first derivative of x^n |
---|
406 | ; and ajust n to get the good value: yifrst[bad[ib]+1] in x[bad[ib]+1] |
---|
407 | y2[xinx2_1[ib]+1:xinx2_2[ib]] $ |
---|
408 | = pure_concave(x[bad[ib]], x[bad[ib]+1] $ |
---|
409 | , y[bad[ib]], y[bad[ib]+1] $ |
---|
410 | , yifrst[bad[ib]+1] $ |
---|
411 | , x2[xinx2_1[ib]+1:xinx2_2[ib]]) |
---|
412 | END |
---|
413 | ELSE:BEGIN |
---|
414 | ; in those cases, the first derivative has 2 zero between |
---|
415 | ; x[bad[ib]] and x[bad[ib]+1]. We look for the minimum value of the |
---|
416 | ; first derivative that correspond to the inflection point of y |
---|
417 | xinfl = -bbb[ib]/(3.0d*aaa[ib]) |
---|
418 | ; we compute the y value for xinfl |
---|
419 | yinfl = aaa[ib]*xinfl*xinfl*xinfl + bbb[ib]*xinfl*xinfl $ |
---|
420 | + ccc[ib]*xinfl + ddd[ib] |
---|
421 | ; |
---|
422 | CASE 1 OF |
---|
423 | ; if y[xinfl] smaller than y[bad[ib]] then we conserve y2 until |
---|
424 | ; the first zero of y2 and from this point we use x^n and ajust n to |
---|
425 | ; get the good value: yifrst[bad[ib]+1] in x[bad[ib]+1] |
---|
426 | yinfl LT y[bad[ib]]:BEGIN |
---|
427 | ; value of the first zero (y'[xzero]=0) |
---|
428 | xzero = (-bbb[ib]-sqrt(delta[ib]))/(3.0d*aaa[ib]) |
---|
429 | ; value of y[xzero]... |
---|
430 | yzero = aaa[ib]*xzero*xzero*xzero + bbb[ib]*xzero*xzero $ |
---|
431 | + ccc[ib]*xzero + ddd[ib] |
---|
432 | ; if yzero > y[bad[ib]+1] then we cannot applay the method we want to |
---|
433 | ; apply => we use then convex-concave case by changing by hand the |
---|
434 | ; value of yinfl and xinfl |
---|
435 | IF yzero GT y[bad[ib]+1] THEN BEGIN |
---|
436 | yinfl = 0.5d*(y[bad[ib]+1]+y[bad[ib]]) |
---|
437 | xinfl = 0.5d*(x[bad[ib]+1]-x[bad[ib]]) |
---|
438 | GOTO, convexconcave |
---|
439 | ENDIF |
---|
440 | ; define xinx2_3: see help of value_locate |
---|
441 | ; if xinx2_3[ib] eq -1 : x[bad[ib]]+xzero < x2[0] |
---|
442 | ; if xinx2_3[ib] eq nx2-1: x[bad[ib]]+xzero >= x2[nx2-1] |
---|
443 | ; else : x2[xinx2_3] <= x[bad[ib]]+xzero < x2[xinx3_2+1] |
---|
444 | xinx2_3 = value_locate(x2, x[bad[ib]]+xzero) |
---|
445 | ; to avoid the particular case when x2 = x[bad[ib]]+xzero |
---|
446 | ; and there is no other x2 point until x[bad[ib]+1] |
---|
447 | xinx2_3 = xinx2_3 < (xinx2_2[ib]-1) |
---|
448 | IF xinx2_2[ib] GE xinx2_3+1 THEN BEGIN |
---|
449 | y2[xinx2_3+1:xinx2_2[ib]] $ |
---|
450 | = pure_concave(x[bad[ib]]+xzero, x[bad[ib]+1] $ |
---|
451 | , yzero, y[bad[ib]+1] $ |
---|
452 | , yifrst[bad[ib]+1] $ |
---|
453 | , x2[xinx2_3+1:xinx2_2[ib]]) |
---|
454 | ENDIF |
---|
455 | END |
---|
456 | ; if y[xinfl] bigger than y[bad[ib]+1] then we conserve y2 from |
---|
457 | ; the second zero of y2 and before this point we use 1-(1-x)^n and |
---|
458 | ; ajust n to get the good value: yifrst[bad[ib]] in x[bad[ib]] |
---|
459 | yinfl GT y[bad[ib]+1]:BEGIN |
---|
460 | ; value of the second zero (y'[xzero]=0) |
---|
461 | xzero = (-bbb[ib]+sqrt(delta[ib]))/(3.0d*aaa[ib]) |
---|
462 | ; value of y[xzero]... |
---|
463 | yzero = aaa[ib]*xzero*xzero*xzero + bbb[ib]*xzero*xzero $ |
---|
464 | + ccc[ib]*xzero + ddd[ib] |
---|
465 | ; if yzero < y[bad[ib]] then we cannot applay the method we want to |
---|
466 | ; apply => we use then convex-concave case by changing by hand the |
---|
467 | ; value of yinfl and xinfl |
---|
468 | IF yzero lt y[bad[ib]] THEN BEGIN |
---|
469 | yinfl = 0.5d*(y[bad[ib]+1]+y[bad[ib]]) |
---|
470 | xinfl = 0.5d*(x[bad[ib]+1]-x[bad[ib]]) |
---|
471 | GOTO, convexconcave |
---|
472 | ENDIF |
---|
473 | ; define xinx2_3: see help of value_locate |
---|
474 | ; if xinx2_3[ib] eq -1 : x[bad[ib]]+xzero < x2[0] |
---|
475 | ; if xinx2_3[ib] eq nx2-1: x[bad[ib]]+xzero >= x2[nx2-1] |
---|
476 | ; else : x2[xinx2_3] <= x[bad[ib]]+xzero < x2[xinx3_2+1] |
---|
477 | xinx2_3 = value_locate(x2, x[bad[ib]]+xzero) |
---|
478 | IF xinx2_3 ge xinx2_1[ib]+1 THEN BEGIN |
---|
479 | y2[xinx2_1[ib]+1:xinx2_3] $ |
---|
480 | = pure_convex(x[bad[ib]], x[bad[ib]]+xzero $ |
---|
481 | , y[bad[ib]], yzero $ |
---|
482 | , yifrst[bad[ib]] $ |
---|
483 | , x2[xinx2_1[ib]+1:xinx2_3]) |
---|
484 | ENDIF |
---|
485 | END |
---|
486 | ELSE:BEGIN |
---|
487 | convexconcave: |
---|
488 | ; define xinx2_3: see help of value_locate |
---|
489 | ; if xinx2_3[ib] eq -1 : x[bad[ib]]+xzero < x2[0] |
---|
490 | ; if xinx2_3[ib] eq nx2-1: x[bad[ib]]+xzero >= x2[nx2-1] |
---|
491 | ; else : x2[xinx2_3] <= x[bad[ib]]+xzero < x2[xinx3_2+1] |
---|
492 | xinx2_3 = value_locate(x2, x[bad[ib]]+xinfl) |
---|
493 | |
---|
494 | IF xinx2_3 ge xinx2_1[ib]+1 THEN BEGIN |
---|
495 | y2[xinx2_1[ib]+1:xinx2_3] $ |
---|
496 | = pure_convex(x[bad[ib]], x[bad[ib]]+xinfl $ |
---|
497 | , y[bad[ib]], yinfl $ |
---|
498 | , yifrst[bad[ib]] $ |
---|
499 | , x2[xinx2_1[ib]+1:xinx2_3]) |
---|
500 | |
---|
501 | ENDIF |
---|
502 | IF xinx2_2[ib] GE xinx2_3+1 THEN BEGIN |
---|
503 | y2[xinx2_3+1:xinx2_2[ib]] $ |
---|
504 | = pure_concave(x[bad[ib]]+xinfl, x[bad[ib]+1] $ |
---|
505 | , yinfl, y[bad[ib]+1] $ |
---|
506 | , yifrst[bad[ib]+1] $ |
---|
507 | , x2[xinx2_3+1:xinx2_2[ib]]) |
---|
508 | ENDIF |
---|
509 | END |
---|
510 | ENDCASE |
---|
511 | |
---|
512 | END |
---|
513 | ENDCASE |
---|
514 | ENDIF |
---|
515 | ENDFOR |
---|
516 | |
---|
517 | ENDIF |
---|
518 | ENDIF |
---|
519 | ; |
---|
520 | RETURN, y2 |
---|
521 | ; |
---|
522 | END |
---|