Differentiable helper functions available to any gadget3 model

Details

These functions are part of g3_env is the top-level environment that any gadget3 model uses.

dif_pmax

dif_pmax(a, b, scale)

Returns the maximum of a & b. If a is a vector/array, then all members of a are compared against b. If b is also a vector, then all members of a are compared against the matching member of b (repeating b if necessary).

scale influences the sharpness of inflection points, should be about 1e5, depending on ranges of input values.

dif_pmin

dif_pmin(a, b, scale)

Returns the minimum of a & b, otherwise works like dif_pmax.

scale influences the sharpness of inflection points, should be about 1e5, depending on ranges of input values.

dif_pminmax

dif_pminmax(a, lower, upper, scale)

Returns values of a bounded between lower & upper.

scale influences the sharpness of inflection points at lower & upper, should be about 1e5, depending on ranges of input values.

Examples

## dif_pmax
g3_eval(quote( dif_pmax(1:10, 5, 1e5) ))
#>  [1]  5.000000  5.000000  5.000000  5.000000  5.000007  6.000000  7.000000
#>  [8]  8.000000  9.000000 10.000000
g3_eval(quote( dif_pmax(1:10, c(4, 7), 1e5) ))
#>  [1]  4  7  4  7  5  7  7  8  9 10
g3_eval(quote( dif_pmax(array(1:9, dim = c(3,3)), c(3,6,8), 1e5) ))
#>      [,1] [,2] [,3]
#> [1,]    3    4    7
#> [2,]    6    6    8
#> [3,]    8    8    9

## dif_pmin
g3_eval(quote( dif_pmin(1:10, 5, 1e5) ))
#>  [1] 1.000000 2.000000 3.000000 4.000000 4.999993 5.000000 5.000000 5.000000
#>  [9] 5.000000 5.000000
g3_eval(quote( dif_pmin(1:10, c(4, 7), 1e5) ))
#>  [1] 1 2 3 4 4 6 4 7 4 7

## dif_pminmax
g3_eval(quote( dif_pminmax(1:10, 3, 6, 1e5) ))
#>  [1] 3.000000 3.000000 3.000007 4.000000 5.000000 5.999993 6.000000 6.000000
#>  [9] 6.000000 6.000000