fnslrec2.gms : Test correctness of slrec intrinsic

Description

slrec(x,S) = 1/x           if x >= S
             1/S - r/S^2   otherwise, r = x - S
for S >= 1e-10, default S = 1e-10.

Contributor: Steve, Mar 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : fnslrec2.gms   includes :   fnset_xy.inc [htmlfntest_xy.inc [html]

$title 'Test correctness of slrec intrinsic' (FNSLREC2,SEQ=726)

$onText
slrec(x,S) = 1/x           if x >= S
             1/S - r/S^2   otherwise, r = x - S
for S >= 1e-10, default S = 1e-10.

Contributor: Steve, Mar 2017
$offText

$include fnset_xy.inc

reps = 8e-16;
sets
  T /
    t1  * t6,
    t11 * t16
    t21 * t26
  /
  torig(t) 'unsmoothed inputs'
  ;

table data(T,V)
        x          y       f_      fx_     fxx_    rc_     ec_
t1      1       1e-10
t2    1e-6      1e-10
t3    1e-9      1e-10
t4    1e-20     1e-10
t5     EPS      1e-10
t6   -1e+2      1e-10

t11     1       1e-5
t12   1e-3      1e-5
t13   1e-4      1e-5
t14   1e-99     1e-5
t15    EPS      1e-5
t16  -1e+20     1e-5

t21   4000       2000
t22   2001       2000
t23   2000       2000
t24  1999.999    2000
t25    EPS       2000
t26  -1e200      2000
;

scalars
  r
  ;
* problematic to test exactly at the breakpoint: should 2nd deriv be 0
* or taken from the smooth part?
loop{T$[data(T,'x') = data(T,'y')],
  data(T,'x') = (1+3e-16) * data(T,'x');
};
torig(t) = [data(T,'x') > data(T,'y')];

loop{torig(T),
  data(T,  'f_') = 1 / data(T,'x');
  data(T, 'fx_') = -data(T,'f_') / data(T,'x');
  data(T,'fxx_') = -2 * data(T,'fx_') / data(T,'x');
};
loop{t$[not torig(t)],
  r = data(t,'x') - data(t,'y');
  data(t,  'f_') = (1 - r/data(t,'y')) / data(t,'y');
  data(t, 'fx_') = (-1  / data(t,'y')) / data(t,'y');
  data(t,'fxx_') = 0;
};

loop {T,
  data(T,'fyx_') = data(T,'fxy_');

  data(T,  'f')  = slrec.value(   data(T,'x'),data(T,'y'));
  data(T, 'fx')  = slrec.grad(1:  data(T,'x'),data(T,'y'));
  data(T, 'fy')  = slrec.grad(2:  data(T,'x'),data(T,'y'));
  data(T,'fxx')  = slrec.hess(1:1:data(T,'x'),data(T,'y'));
  data(T,'fxy')  = slrec.hess(1:2:data(T,'x'),data(T,'y'));
  data(T,'fyx')  = slrec.hess(2:1:data(T,'x'),data(T,'y'));
  data(T,'fyy')  = slrec.hess(2:2:data(T,'x'),data(T,'y'));
  data(T, 'rc')  = mathlastrc;
  data(T, 'ec')  = mathlastec;
};

display data;

$include fntest_xy.inc