R Under development (unstable) (2026-02-09 r89390 ucrt) -- "Unsuffered Consequences" Copyright (C) 2026 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > > objfun <- function(x) { + ##### Rosenbrock's function ##### + stopifnot(is.numeric(x)) + stopifnot(length(x) == 2) + 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] + f <- eval(f) + g <- c(eval(g1), eval(g2)) + B <- rbind(c(eval(h11), eval(h12)), c(eval(h12), eval(h22))) + list(value = f, gradient = g, hessian = B) + } > > library(trust) > > parinit <- c(3, 1) > > out <- trust(objfun, parinit, 1, 1e5, blather = TRUE) > out$converged [1] TRUE > length(out$r) [1] 21 > > parscale <- c(5, 1) > shift <- 4 > theta <- parscale * (parinit + shift) > > pobjfun <- function(x) { + out <- objfun(x / parscale - shift) + out$gradient <- out$gradient / parscale + out$hessian <- out$hessian / outer(parscale, parscale) + return(out) + } > > pout <- trust(pobjfun, theta, 1, 1e5, blather = TRUE) > pout$converged [1] TRUE > length(pout$r) [1] 25 > all.equal(out$argument, pout$argument / parscale - shift) [1] "Mean relative difference: 1.011409e-07" > > qout <- trust(objfun, parinit, 1, 1e5, parscale = parscale, blather = TRUE) > qout$converged [1] TRUE > length(qout$r) [1] 25 > > all.equal(pout$valpath, qout$valpath) [1] TRUE > transpath <- pout$argpath > transpath <- sweep(transpath, 2, parscale, "/") > transpath <- sweep(transpath, 2, shift) > all.equal(transpath, qout$argpath) [1] TRUE > > > proc.time() user system elapsed 0.18 0.06 0.20