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