RcppTrust

Codecov test coverage R-CMD-check CRAN version CRAN downloads

RcppTrust is a thread-safe C++ port of the trust-region optimizer in Charles J. Geyer’s CRAN package trust, built for use in nlmixr2est. It implements the exact same algorithm – same Newton / easy-easy / hard-easy / hard-hard trust-region subproblem, same termination criteria – and exposes it three ways:

See vignette("RcppTrust") for what’s the same as upstream trust, what’s different, and a worked example of the C interface and the registration pattern.

Note this package was generated with the help of AI (Claude/Gemini).

Installation

You can install the development version of RcppTrust from GitHub with:

# install.packages("pak")
pak::pak("nlmixr2/RcppTrust")

Example

trust() is a straight substitute for trust::trust():

library(RcppTrust)

# Rosenbrock's function, the example from ?trust::trust
objfun <- function(x) {
  f <- expression(100 * (x2 - x1^2)^2 + (1 - x1)^2)
  g1 <- D(f, "x1"); g2 <- D(f, "x2")
  h11 <- D(g1, "x1"); h12 <- D(g1, "x2"); h22 <- D(g2, "x2")
  x1 <- x[1]; x2 <- x[2]
  list(
    value = eval(f), gradient = c(eval(g1), eval(g2)),
    hessian = rbind(c(eval(h11), eval(h12)), c(eval(h12), eval(h22)))
  )
}

out <- trust(objfun, c(3, 1), 1, 5)
out[c("value", "argument", "converged", "iterations")]
#> $value
#> [1] 5.165437e-15
#> 
#> $argument
#> [1] 1 1
#> 
#> $converged
#> [1] TRUE
#> 
#> $iterations
#> [1] 21

Authors