# title: "Replication codes for Table 1 and Figure 1"
# author: "Andreï V. Kostyrka (andrei.kostyrka@gmail.com)"
# date: '23rd of November 2026'
# Tested with R v. 4.6.2 and pnd v. 0.2.0

rm(list = ls())

# For EXACT replication of the results, use the SAME package version
# Warning: this option is aggressive and may overwrite your existing installed pnd
library(devtools)
# devtools::install_version("pnd", "0.2.0")

library(pnd)
library(parallel)

##########################################
# Figure 1
##########################################

plotTruncation <- function(result) {
  h <- result$iterations$h
  estimate <- result$iterations$est.error[, 1]
  resolved <- is.finite(estimate) & estimate > 0
  # In FP arithmetic, zero errors may stem from multiple reasons: cancellation, exact summation etc.
  # We use log axes -- mark the known step sizes with zero truncation estimates on the axis
  # Assume that sum(resolved) > 0, otherwise change the chosen function
  plot(h[resolved], estimate[resolved], log = "xy", xlim = range(h), bty = "n",
       pch = 16, cex = 0.7, xlab = "Step size", ylab = "Empirical truncation error")
  rug(h[!resolved])
}

a  <- step.K(sin, 1)
aa <- step.K(function(x) sqrt((x^2 - 2*pi*x + pi^2) + 1), pi, range = c(1e-13, 1e+6))
pdf("figure1.pdf", 6, 3)  # Not cairo_pdf because some symbols won't show
par(mar = c(4, 4, 0.5, 0.5) + .1, mfrow = c(1, 2))
plotTruncation(a)
mtext(expression(f(x) == sin(x)), 3, -1)
mtext(expression(x == 1), 3, -2)
plotTruncation(aa)
mtext(expression(f(x) == sqrt((x^2 - 2*pi*x + pi^2) + 1)), 3, -1)
mtext(expression(x == pi), 3, -2)
dev.off()

# Vertical variant
pdf("figure1v.pdf", 3, 5.5)
par(mar = c(4, 4, 0.5, 0.5) + .1, mfrow = c(2, 1))
plotTruncation(a)
mtext(expression(f(x) == sin(x)), 3, -1)
mtext(expression(x == 1), 3, -2)
plotTruncation(aa)
mtext(expression(f(x) == sqrt((x^2 - 2*pi*x + pi^2) + 1)), 3, -1)
mtext(expression(x == pi), 3, -2)
dev.off()

# Same in EPS format
setEPS()
postscript("figure1.eps", width = 6, height = 3)
par(mar = c(4, 4, 0.5, 0.5) + .1, mfrow = c(1, 2))
plotTruncation(a)
mtext(expression(f(x) == sin(x)), 3, -1)
mtext(expression(x == 1), 3, -2)
plotTruncation(aa)
mtext(expression(f(x) == sqrt((x^2 - 2*pi*x + pi^2) + 1)), 3, -1)
mtext(expression(x == pi), 3, -2)
dev.off()

setEPS()
postscript("figure1v.eps", width = 3, height = 5)
par(mar = c(4, 4, 0, 0.5) + .1, mfrow = c(2, 1))
plotTruncation(a)
mtext(expression(f(x) == sin(x)), 3, -1)
mtext(expression(x == 1), 3, -2)
plotTruncation(aa)
mtext(expression(f(x) == sqrt((x^2 - 2*pi*x + pi^2) + 1)), 3, -1)
mtext(expression(x == pi), 3, -2)
dev.off()


###########################################
# Table 1
###########################################
n <- 10000L   # To obtain the official Table 1 in an hour
# n <- 1000L  # To obtain crude results in 5 minutes
imgh <- 720
imgv <- 320
cores <- detectCores() / 2  # Assuming detectCores() returns the number of logical cores which is usually 2x physical cores

linear <- TRUE
k <- 1

for (k in 1:8) {

  if (k == 1) {
    f  <- sin
    fp <- cos
    fppp <- function(x) -cos(x)
    # xsl <- c(-pi, pi)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-1, 1e+5)
    flab <- "sin"
  } else if (k == 2) {
    f  <- exp
    fp <- exp
    fppp <- exp
    # xsl <- c(-10, 1)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e1)
    flab <- "exp"
  } else if (k == 3) {
    f  <- log
    fp <- function(x) 1/x
    fppp <- function(x) 2/x^3
    # xsl <- c(1e-2, 10)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+5)
    flab <- "log"
  } else if (k == 4) {
    f  <- sqrt
    fp <- function(x) 0.5/sqrt(x)
    fppp <- function(x) 3/8*x^(-5/2)
    # xsl <- c(1e-2, 10)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+5)
    flab <- "sqrt"
  } else if (k == 5) {
    f  <- atan
    fp <- function(x) 1/(x^2 + 1)
    fppp <- function(x) (6*x^2 - 2)/(1+x^2)^3  # Simplify[D[ArcTan[x], {x, 3}]]
    # xsl <- c(-10, 10)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+5)
    flab <- "atan"
  } else if (k == 6) {
    f  <- function(x) pi*x + 2
    fp <- function(x) rep(pi, length(x))
    fppp <- function(x) x*0
    # xsl <- c(-1e2, 1e2)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+5)
    flab <- "poly1"
  } else if (k == 7) {
    f  <- function(x) x^2
    fp <- function(x) 2*x
    fppp <- function(x) x*0
    # xsl <- c(-1e2, 1e2)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+4)
    flab <- "poly2"
  } else if (k == 8) {
    f  <- function(x) sin(x^2 + 10^6 * x)
    fp <- function(x) (1e6 + 2*x) * cos(x^2 + 10^6 * x)
    # D[Sin[x^2 + 10^6*x], {x, 3}]
    fppp <- function(x) -(1e6 + 2*x)^3 * cos(1e6*x + x^2) -  6*(1e6 + 2*x)*sin(1e6*x + x^2)
    # xsl <- c(-10, 10)
    xsl <- c(0.1, 12.5)
    xse <- c(1e-2, 1e+1)
    flab <- "illsin"
  }

  print(k)
  print(f)

  set.seed(1)
  xgrid <- sort(runif(n, min = xsl[1], max = xsl[2]))
  xax.vals <- pretty(xsl)
  xl <- xsl

  # Analytical minimiser of etrunc + eround under certain assumptions
  fHopt <- function(x, FUN, third) {
    f1 <- FUN(x)
    f3 <- third(x)
    if (abs(f1) < 16 * .Machine$double.eps) f1 <- 16 * .Machine$double.eps  # Safeguarding against zero steps
    if (abs(f3) < .Machine$double.eps) return(list(par = NA_real_, value = NA_real_))  # Division by zero
    h <- (1.5 * abs(f1 / f3) * .Machine$double.eps)^(1/3)
    v <- (-0.5*FUN(x-h) + 0.5*FUN(x+h)) / h
    return(list(par = h, value = v))
  }

  fHRoT  <- function(x, FUN) {
    h <- .Machine$double.eps^(1/3) * (max(1, abs(x)))
    v <- (-0.5*FUN(x-h) + 0.5*FUN(x+h)) / h
    return(list(par = h, value = v))
  }

  # Save resources, return only the two quantities used below without each method's diagnostic history
  # Keep parallelism across evaluation points because each individual objective is cheap
  evaluateSteps <- function(xgrid, selector, workers = 1, ...) {
    force(selector)
    arguments <- list(...)
    runParallel(function(x) {
      result <- do.call(selector, c(list(x = x), arguments))
      result[c("par", "value")]
    }, xgrid, cores = workers, preschedule = TRUE)
  }

  me <- .Machine$double.eps / 2
  # Set cores <- 1 to get overhead-free timings
  t.opt    <- system.time(getHopt    <- evaluateSteps(xgrid, fHopt, FUN = f, third = fppp))
  t.RoT    <- system.time(getHRoT    <- evaluateSteps(xgrid, fHRoT, FUN = f))
  t.plugin <- system.time(getHplugin <- evaluateSteps(xgrid, step.plugin, cores, FUN = f, max.rel.error = me, cores = 1))
  t.CR     <- system.time(getHCR     <- evaluateSteps(xgrid, step.CR, cores, FUN = f, max.rel.error = me, acc.order = 1, cores = 1))
  t.CRm    <- system.time(getHCRm    <- evaluateSteps(xgrid, step.CR, cores, FUN = f, max.rel.error = me, acc.order = 2, cores = 1))
  t.DV     <- system.time(getHDV     <- evaluateSteps(xgrid, step.DV, cores, FUN = f, max.rel.error = me, cores = 1))
  t.SW     <- system.time(getHSW     <- evaluateSteps(xgrid, step.SW, cores, FUN = f, max.rel.error = me, cores = 1))
  t.M      <- system.time(getHM      <- evaluateSteps(xgrid, step.M, cores, FUN = f, max.rel.error = me, cores = 1))
  t.K      <- system.time(getHK      <- evaluateSteps(xgrid, step.K, cores, FUN = f, max.rel.error = me, cores = 1))

  tms <- c(t.opt[3], t.RoT[3], t.plugin[3], t.CR[3], t.CRm[3], t.DV[3], t.SW[3], t.M[3], t.K[3])
  ms <- tms / length(xgrid) * 1000

  res <- list(getHopt, getHRoT, getHplugin, getHCR, getHCRm, getHDV, getHSW, getHM, getHK)
  names(res) <- c("opt", "RoT", "plugin", "CR", "CRm", "DV", "SW", "M", "K")

  h <- do.call(cbind, lapply(res, function(x) unlist(lapply(x, "[[", "par"))))

  # d <- apply(h, 2, function(x) tryCatch(density(log10(x), bw = "bcv"), error = function(e) density(log10(x), bw = "nrd")))
  # plot(NULL, NULL, xlim = range(unlist(lapply(d, "[[", "x"))), ylim = range(unlist(lapply(d, "[[", "y"))), bty = "n")
  # for (i in 1:ncol(h)) lines(d[[i]], col = i %% 6 + 1, lty = i %% 5 + 1, lwd = 1.5)

  # # Visualise the average step size as a function of the application point (x)
  # for (i in 1:ncol(h)) {
  #   if (all(!is.finite(h[, i]))) next
  #   plot(xgrid, log10(h[, i]), log = if (linear) "" else "x", col = i %% 6 + 1, main = colnames(h)[i], pch = ".", ylim = quantile(log10(h[is.finite(h)]), c(0.01, 0.99)))
  #   l <- predict(loess(log10(h[, i]) ~ xgrid, span = 0.1, degree = 1))
  #   lines(xgrid, l, col = "#FFFFFFEE", lwd = 5)
  #   lines(xgrid, l, col = i %% 6 + 1, lwd = 2)
  #   # Sys.sleep(1)
  # }

  g.true <- fp(xgrid)
  g <- do.call(cbind, lapply(res, function(x) unlist(lapply(x, "[[", "value"))))


  err <- g.true - g
  abserr  <- as.data.frame(abs(err))
  absrerr <- as.data.frame(abs(err / g.true))

  jj <- c(6, 7, 8, 9)

  cat(paste0(sprintf("%1.1e", apply(absrerr, 2, median)[jj]), collapse = " & "), "-- ")

  ranks <- t(apply(absrerr[, jj], 1, rank))
  cat("new = best in", round(100*mean(ranks[, "K"] == apply(ranks, 1, min))), "%\n")

  lxgrid <- if (!linear) log10(xgrid) else xgrid
  labserr <- log10(abserr)
  labserr[labserr == -Inf] <- min(unlist(labserr)[is.finite(unlist(labserr))])
  xnice <- seq(quantile(lxgrid, 0.001), quantile(lxgrid, 0.999), length.out = 201)
  smoothErrors <- function(values, x, newx, workers) {
    runParallel(function(y) {
      if (all(!is.finite(y))) return(newx * NA)
      predict(loess(y ~ x, degree = 1, family = "symmetric",
                    control = loess.control(surface = "direct", statistics = "none"), span = 0.2),
              newdata = newx)  # The figure needs only fitted values
    }, values, cores = workers, preschedule = TRUE)
  }
  l <- do.call(cbind, smoothErrors(labserr, lxgrid, xnice, cores))

  mycols <- c("#ebce2b", "#702c8c", "#db6917", "#96cde6", "#ba1c30", "#c0bd7f", "#7f7e80", "#5fa641", "#d485b2", "#4277b6")

  # One plot with everything
  cairo_pdf(paste0("compare-methods-", flab, "-", if (linear) "lin" else "exp", ".pdf"), imgh/90, imgv/60)
  par(mar = c(4, 4, 0, 0) + .1)
  matplot(if (linear) xnice else 10^xnice, l, type = "l", log = if (linear) "" else "x", lty = 1:5, col = mycols, lwd = 2, xlab = "Evaluation point", ylab = "log10(|error|)", bty = "n")
  legend("top", legend = colnames(abserr), ncol = 4, lwd = 2, col = mycols, lty = 1:5, bg = "#FFFFFFCC", box.col = "#FFFFFF00")
  dev.off()

}
