tests/testthat/test-newton-system.R

test_that("square Newton reuses Jacobians and converges on analytic systems", {
  f <- function(v)c(v[1]^2+v[2]^2-1,v[1]-v[2])
  r <- f |> vc_newton_system(c(.8,.2))
  expect_true(r$converged)
  expect_identical(r$status,"converged")
  expect_equal(r$estimate,rep(1/sqrt(2),2),tolerance=1e-8)
  expect_equal(r$value,f(r$estimate))
  expect_equal(r$jacobian_diagnostics[[1]]$result,vc_jacobian(f,c(.8,.2),warn=FALSE))
  expect_equal(nrow(r$points),nrow(r$iterations))
  expect_equal(nrow(r$values),nrow(r$iterations))
  expect_equal(as.numeric(r$points[nrow(r$points),]),r$estimate)
  expect_equal(as.numeric(r$values[nrow(r$values),]),r$value)
  expect_equal(r$iterations$residual,apply(abs(r$values),1,max))
  expect_equal(r$iterations$n,seq_len(nrow(r$points))-1L)
  expect_true(all(vapply(r$jacobian_diagnostics,function(d)d$rcond>0,logical(1))))
  J <- function(v)rbind(c(2*v[1],2*v[2]),c(1,-1))
  analytic <- vc_newton_system(f,c(.8,.2),jacobian=J)
  expect_true(analytic$converged)
  expect_equal(analytic$estimate,r$estimate,tolerance=1e-8)
  expect_identical(analytic$jacobian_diagnostics[[1]]$source,"supplied")
  r3 <- vc_newton_system(function(v)v-c(1,2,3),c(0,0,0))
  expect_true(r3$converged)
  expect_equal(r3$estimate,c(1,2,3),tolerance=1e-8)
  named <- vc_newton_system(function(v)c(a=unname(v['x']-1),b=unname(v['y']-2)),c(x=0,y=0))
  expect_identical(names(named$estimate),c("x","y"))
  expect_identical(colnames(named$values),c("a","b"))
  expect_equal(unname(named$estimate),c(1,2),tolerance=1e-8)
})

test_that("initial roots and iteration limits retain complete initial/terminal rows", {
  calls <- 0
  r <- vc_newton_system(function(v)v,c(0,0),jacobian=function(v){calls <<- calls+1;stop("unused")})
  expect_true(r$converged)
  expect_equal(calls,0)
  expect_equal(nrow(r$iterations),1L)
  expect_length(r$jacobian_diagnostics,0)
  expect_length(r$trials,0)
  f <- function(v)c(v[1]^2-2,v[2])
  zero <- vc_newton_system(f,c(1,0),max_iter=0)
  expect_false(zero$converged)
  expect_identical(zero$status,"max_iter")
  expect_equal(zero$estimate,c(1,0))
  one <- vc_newton_system(f,c(1,0),max_iter=1)
  expect_identical(one$status,"max_iter")
  expect_equal(nrow(one$points),2L)
  expect_equal(one$estimate,c(1.5,0),tolerance=1e-8)
  expect_equal(one$iterations$n,c(0L,1L))
})

test_that("singular, unreliable and invalid Jacobians do not imply convergence", {
  singular <- vc_newton_system(function(v)c(v[1]^2+1,v[2]),c(0,0),jacobian=function(v)diag(c(0,1)))
  expect_identical(singular$status,"jacobian_singular")
  expect_false(singular$converged)
  expect_equal(nrow(singular$points),1L)
  expect_length(singular$trials,0)
  unreliable <- vc_newton_system(function(v)c(abs(v[1]),v[2]),c(0,1))
  expect_identical(unreliable$status,"jacobian_unreliable")
  expect_false(unreliable$jacobian_diagnostics[[1]]$result$all_reliable)
  ill <- vc_newton_system(function(v)c(1,1),c(0,0),jacobian=function(v)diag(c(1,1e-15)))
  expect_identical(ill$status,"jacobian_singular")
  wrong <- vc_newton_system(function(v)v+1,c(0,0),jacobian=function(v)matrix(1,2,3))
  expect_identical(wrong$status,"jacobian_unavailable")
  expect_match(wrong$jacobian_diagnostics[[1]]$result$error,"n-by-n")
  domain <- vc_newton_system(function(v)c(sqrt(v[1])+1,v[2]),c(0,0))
  expect_identical(domain$status,"jacobian_unavailable")
  expect_equal(domain$estimate,c(0,0))
})

test_that("backtracking retains all rejections and can recover finite domain steps", {
  f <- function(v)c(v[1]^2-2,v[2])
  J <- function(v)diag(c(2*v[1],1))
  r <- vc_newton_system(f,c(.1,0),jacobian=J)
  expect_true(r$converged)
  expect_equal(r$estimate,c(sqrt(2),0),tolerance=1e-8)
  expect_equal(r$iterations$damping[2],1/8)
  expect_true(all(vapply(r$trials[1:3],function(t)!t$accepted,logical(1))))
  expect_true(r$trials[[4]]$accepted)
  expect_equal(r$trials[[1]]$point,c(10.05,0))
  expect_true(all(diff(r$iterations$residual)<=0))
  expect_equal(sum(vapply(r$trials,function(t)t$accepted,logical(1))),nrow(r$iterations)-1L)
  logf <- function(v)c(log(v[1])+10,v[2])
  logJ <- function(v)diag(c(1/v[1],1))
  d <- vc_newton_system(logf,c(1,0),jacobian=logJ)
  expect_true(d$converged)
  expect_equal(d$estimate,c(exp(-10),0),tolerance=1e-8)
  expect_true(any(vapply(d$trials,function(t)nzchar(t$error),logical(1))))
  raw <- vc_newton_system(logf,c(1,0),jacobian=logJ,backtrack=FALSE)
  expect_identical(raw$status,"invalid_trial")
  expect_equal(raw$estimate,c(1,0))
  expect_length(raw$trials,1)
  fail <- vc_newton_system(function(v)c(1,1),c(0,0),jacobian=function(v)diag(2),max_backtrack=2)
  expect_identical(fail$status,"line_search_failed")
  expect_length(fail$trials,3)
  expect_equal(nrow(fail$points),1L)
})

test_that("small steps, cycles and overflow are failures with residuals retained", {
  constant <- function(v)c(1,1)
  s <- vc_newton_system(constant,c(1,1),jacobian=function(v)diag(1e16,2),backtrack=FALSE)
  expect_identical(s$status,"stagnation")
  expect_false(s$converged)
  expect_gt(s$iterations$residual[nrow(s$iterations)],s$control$f_tol)
  collapsed <- vc_newton_system(constant,c(1,1),jacobian=function(v)diag(1e300,2))
  expect_identical(collapsed$status,"stagnation")
  expect_equal(nrow(collapsed$points),1L)
  f <- function(v)c(v[1]^3-2*v[1]+2,v[2])
  J <- function(v)diag(c(3*v[1]^2-2,1))
  cycle <- vc_newton_system(f,c(0,0),jacobian=J,backtrack=FALSE)
  expect_identical(cycle$status,"cycle_detected")
  expect_false(cycle$converged)
  expect_equal(cycle$points[,1],c(0,1,0))
  overflow <- vc_newton_system(function(v)c(1e308,1e308),c(-1e308,-1e308),
    jacobian=function(v)diag(2),backtrack=FALSE)
  expect_identical(overflow$status,"nonfinite_step")
  expect_equal(overflow$estimate,c(-1e308,-1e308))
  shape <- vc_newton_system(function(v)if(v[1]==0)c(a=1,b=1)else c(x=1,y=1),c(0,0),
    jacobian=function(v)diag(2),backtrack=FALSE)
  expect_identical(shape$status,"invalid_trial")
  expect_match(shape$trials[[1]]$error,"names changed")
})

test_that("square-system arguments and initial results are validated", {
  f <- function(v)v+1
  expect_error(vc_newton_system(f,1),"one-variable")
  expect_error(vc_newton_system(f,c(NA,1)),"x0")
  expect_error(vc_newton_system(function(v)c(1,2,3),c(0,0)),"square")
  expect_error(vc_newton_system(function(v)c(1,Inf),c(0,0)),"finite")
  expect_error(vc_newton_system(f,c(0,0),jacobian=1),"jacobian")
  expect_error(vc_newton_system(f,c(0,0),h=c(1,2,3)),"h")
  expect_error(vc_newton_system(f,c(0,0),method="bogus"),"method")
  expect_error(vc_newton_system(f,c(0,0),max_iter=-1),"max_iter")
  expect_error(vc_newton_system(f,c(0,0),f_tol=-1),"f_tol")
  expect_error(vc_newton_system(f,c(0,0),x_tol=Inf),"x_tol")
  expect_error(vc_newton_system(f,c(0,0),jacobian_tol=-1),"jacobian_tol")
  expect_error(vc_newton_system(f,c(0,0),backtrack=NA),"backtrack")
  expect_error(vc_newton_system(f,c(0,0),max_backtrack=.5),"max_backtrack")
  expect_error(vc_newton_system(f,c(0,0),atol=-1),"atol")
})

test_that("system graphics retain zero contours, iteration frames and terminal point", {
  skip_if_not_installed("plotly")
  f <- function(v)c(v[1]^2+v[2]^2-1,v[1]-v[2])
  r <- vc_newton_system(f,c(.8,.2))
  p <- r |> vc_plot_newton_system(f=f,xlim=c(-1,2),ylim=c(-2,1),n=5)
  b <- plotly::plotly_build(p)
  expect_equal(matrix(as.numeric(b$x$data[[1]]$z),5),
    outer(seq(-2,1,length.out=5),seq(-1,2,length.out=5),function(y,x)x^2+y^2-1))
  expect_equal(matrix(as.numeric(b$x$data[[2]]$z),5),
    outer(seq(-2,1,length.out=5),seq(-1,2,length.out=5),function(y,x)x-y))
  expect_equal(b$x$data[[1]]$contours$start,0)
  expect_equal(b$x$data[[1]]$contours$end,0)
  expect_length(b$x$frames,nrow(r$points))
  terminal <- b$x$frames[[length(b$x$frames)]]$data
  expect_equal(b$x$frames[[length(b$x$frames)]]$traces,c(2,3))
  current <- terminal[[2]]
  expect_equal(as.numeric(current$x),r$estimate[1])
  expect_equal(as.numeric(current$y),r$estimate[2])
  root <- vc_newton_system(function(v)v,c(0,0))
  expect_length(plotly::plotly_build(vc_plot_newton_system(root,function(v)v,n=5))$x$frames,0)
  fail <- vc_newton_system(function(v)c(1,1),c(0,0),jacobian=function(v)diag(2),max_backtrack=1)
  rejected <- plotly::plotly_build(vc_plot_newton_system(fail,function(v)c(1,1),n=5,show_rejected=TRUE))
  overlay <- Filter(function(a)grepl("All rejected",a$name),rejected$x$data)[[1]]
  expect_length(overlay$x,2)
  expect_match(as.character(rejected$x$layout$title),"line_search_failed")
  expect_error(vc_plot_newton_system(r,function(v)f(v)+1),"match")
  expect_error(vc_plot_newton_system(r,f,n=1),"n")
  expect_error(vc_plot_newton_system(r,f,animate=NA),"animate")
  expect_error(vc_plot_newton_system(vc_newton_system(function(v)v,c(0,0,0)),function(v)v),"two-dimensional")
})

Try the vectorialcalculus package in your browser

Any scripts or data that you put into this service are public.

vectorialcalculus documentation built on Nov. 17, 2026, 1:09 a.m.