eenticott / gamstackr

Tools for stacking probabilistic densities.
GNU General Public License v3.0
0 stars 0 forks source link

F_diff #2

Closed capezza closed 1 year ago

capezza commented 1 year ago

This should be better and faster. If I am not wrong these functions are called many times.

  F_diff <- function(x) {
    abs_x <- abs(x)
    exp_x <- exp(-abs_x)
    exp_2x <- exp_x^2
    exp_x_p1_2 <- (1 + exp_x)^2
    out <- exp_x / exp_x_p1_2
    return(out)
  }

Simlarly for F_diff2

  F_diff2 <- function(x) {
    abs_x <- abs(x)
    exp_x <- exp(-abs_x)
    exp_2x <- exp_x^2
    exp_3x <- exp_x * exp_2x
    exp_x_p1_3 <- (1 + exp_3x + 3 * exp_2x + 3 * exp_x)
    out <- sign(x) * (exp_2x - exp_x) / exp_x_p1_3
  }
eenticott commented 1 year ago

Yes I noticed a good increase in speed after changing this