Nothing
test_that("C2 declaration and both coordinate sides gate conditional labels", {
f <- function(v)v[1]^2+2*v[2]^2
a <- vc_classify_stationary(f,c(0,0),warn=FALSE)
expect_identical(a$classification,"inconclusive")
expect_true(a$stationary_evidence)
expect_false(a$gradient$reliable) # existing stricter flag is preserved
b <- f |> vc_classify_stationary(c(0,0),assume_c2=TRUE,warn=FALSE)
expect_identical(b$classification,"minimum")
expect_equal(b$eigenvalues,c(4,2),tolerance=1e-6)
expect_equal(b$gradient,vc_gradient(f,c(0,0),warn=FALSE))
expect_identical(vc_classify_stationary(function(v)-f(v),c(0,0),TRUE,warn=FALSE)$classification,"maximum")
expect_identical(vc_classify_stationary(function(v)v[1]^2-v[2]^2,c(0,0),TRUE,warn=FALSE)$classification,"saddle")
expect_identical(vc_classify_stationary(function(v)v[1]^2+v[2]^4,c(0,0),TRUE,warn=FALSE)$classification,"inconclusive")
expect_identical(vc_classify_stationary(function(v)0,c(0,0),TRUE,warn=FALSE)$classification,"inconclusive")
expect_identical(vc_classify_stationary(f,c(1,1),TRUE,warn=FALSE)$classification,"not_stationary")
cusp <- vc_classify_stationary(function(v)abs(v[1])+v[2]^2,c(0,0),TRUE,warn=FALSE)
expect_equal(cusp$gradient$estimate,c(x1=0,x2=0))
expect_false(cusp$stationary_evidence)
expect_identical(cusp$classification,"inconclusive")
jump <- function(v)if(all(v==0))1 else sum(v^2)
expect_false(vc_classify_stationary(jump,c(0,0),TRUE,warn=FALSE)$stationary_evidence)
expect_warning(vc_classify_stationary(f,c(0,0)),"C2")
})
test_that("classification retains sensitivity, scaling and mixed curvature limits", {
f <- function(v)v[1]^2+v[1]*v[2]+2*v[2]^2
a <- vc_classify_stationary(f,c(x=0,y=0),TRUE,warn=FALSE)
expect_identical(names(a$point),c("x","y"))
expect_identical(a$classification,"minimum")
expect_equal(a$hessian$estimate,matrix(c(2,1,1,4),2,dimnames=list(c("x","y"),c("x","y"))),tolerance=1e-6)
expect_gt(a$eigen_margin,1e-6)
expect_identical(vc_classify_stationary(function(v)sum(v^2),c(0,0,0),TRUE,warn=FALSE)$classification,"minimum")
expect_identical(vc_classify_stationary(function(v)cos(v[1])+cos(v[2]),c(0,0),TRUE,hessian_h=.01,warn=FALSE)$classification,"maximum")
expect_identical(vc_classify_stationary(function(v)exp(sum(v^2)),c(0,0),TRUE,hessian_h=.01,warn=FALSE)$classification,"minimum")
offset <- vc_classify_stationary(function(v)1e12+sum(v^2),c(0,0),TRUE,warn=FALSE)
expect_false(offset$hessian$all_reliable)
expect_identical(offset$classification,"inconclusive")
alias <- function(v)if(all(v==0))0 else prod(v)*(v[1]^2-v[2]^2)/sum(v^2)
expect_identical(vc_classify_stationary(alias,c(0,0),warn=FALSE)$classification,"inconclusive")
expect_identical(vc_classify_stationary(f,c(0,0),TRUE,tol_eigen=10,warn=FALSE)$classification,"inconclusive")
expect_error(vc_classify_stationary(f,0),"one-variable")
expect_error(vc_classify_stationary(f,c(0,0),assume_c2=NA),"assume_c2")
expect_error(vc_classify_stationary(f,c(0,0),tol_grad=0),"tol_grad")
expect_error(vc_classify_stationary(f,c(0,0),hessian_h=-1),"h")
expect_error(vc_classify_stationary(f,c(0,0),tol_eigen=Inf),"tol_eigen")
})
test_that("multistart search recovers candidates without changing RNG or old engines", {
f <- function(v)v[1]^2+2*v[2]^2
set.seed(180)
before <- .Random.seed
r <- f |> vc_stationary_points(rbind(c(.1,.2),c(-.2,.1)),assume_c2=TRUE,warn=FALSE)
expect_identical(.Random.seed,before)
expect_equal(dim(r$points),c(1L,2L))
expect_lt(max(abs(r$points)),1e-5)
expect_identical(r$table$classification,"minimum")
expect_identical(r$runs[[2]]$status,"duplicate")
expect_equal(r$runs[[1]]$candidate_id,r$runs[[2]]$candidate_id)
expect_true(all(vapply(r$runs,function(a)a$accepted,logical(1))))
h <- r$runs[[1]]$history
expect_gt(length(h),2)
expect_equal(h[[1]]$point,c(.1,.2))
expect_equal(h[[1]]$objective,h[[1]]$residual^2)
expect_true(all(vapply(h,function(a)a$status=="ok",logical(1))))
expect_identical(r$assessments[[1]]$gradient,
vc_gradient(f,r$runs[[1]]$point,warn=FALSE))
s <- vc_stationary_points(function(v)v[1]^2-v[2]^2,c(.1,.2),assume_c2=TRUE,warn=FALSE)
expect_identical(s$table$classification,"saddle")
b <- vc_stationary_points(f,c(.1,.2),bounds=rbind(c(-1,1),c(-1,1)),assume_c2=TRUE,warn=FALSE)
expect_equal(nrow(b$points),1L)
expect_identical(b$table$classification,"minimum")
named <- vc_stationary_points(function(v)v['a']^2+v['b']^2,c(a=.1,b=.2),warn=FALSE)
expect_identical(colnames(named$points),c("a","b"))
expect_identical(named$table$classification,"inconclusive")
double_well <- function(v)(v[1]^2-1)^2+v[2]^2
wells <- vc_stationary_points(double_well,rbind(c(-1,0),c(0,0),c(1,0)),
assume_c2=TRUE,hessian_h=.01,warn=FALSE)
expect_equal(nrow(wells$points),3L)
expect_equal(as.numeric(wells$points[,1]),c(-1,0,1),tolerance=1e-5)
expect_identical(wells$table$classification,c("minimum","saddle","minimum"))
})
test_that("search rejects nonzero residuals, cusps and boundary hits", {
fake <- function(v)v[1]+v[1]^3+v[2]^2
a <- vc_stationary_points(fake,c(0,0),warn=FALSE)
expect_equal(nrow(a$points),0L)
expect_identical(a$runs[[1]]$status,"stationarity_rejected")
expect_gt(a$runs[[1]]$assessment$side_max,.9)
cusp <- vc_stationary_points(function(v)abs(v[1])+v[2]^2,c(0,0),warn=FALSE)
expect_equal(nrow(cusp$points),0L)
outside <- vc_stationary_points(function(v)(v[1]-2)^2+v[2]^2,c(.5,0),
bounds=rbind(c(0,1),c(-1,1)),warn=FALSE)
expect_identical(outside$runs[[1]]$status,"boundary_rejected")
expect_equal(nrow(outside$points),0L)
failed <- vc_stationary_points(function(v)stop("domain"),c(0,0),max_iter=3,warn=FALSE)
expect_equal(nrow(failed$points),0L)
expect_true(any(vapply(failed$runs[[1]]$history,function(a)grepl("domain",a$status),logical(1))))
expect_warning(vc_stationary_points(fake,c(0,0)),"not an absence proof")
limited <- vc_stationary_points(function(v)sum(v^2),c(10,10),max_iter=1,warn=FALSE)
expect_false(limited$runs[[1]]$accepted)
expect_equal(nrow(limited$points),0L)
})
test_that("search validates shapes, windows, steps and tolerances", {
f <- function(v)sum(v^2)
expect_error(vc_stationary_points(3,c(0,0)),"scalar field")
expect_error(vc_stationary_points(f,0),"starts")
expect_error(vc_stationary_points(f,matrix(numeric(),0,2)),"starts")
expect_error(vc_stationary_points(f,c(NA,0)),"starts")
expect_error(vc_stationary_points(f,c(0,0),bounds=matrix(0,2,2)),"bounds")
expect_error(vc_stationary_points(f,c(0,0),bounds=matrix(1,3,2)),"bounds")
expect_error(vc_stationary_points(f,c(0,2),bounds=rbind(c(-1,1),c(-1,1))),"within")
expect_error(vc_stationary_points(f,c(0,0),h=c(1,2,3)),"h")
expect_error(vc_stationary_points(f,c(0,0),max_iter=0),"max_iter")
expect_error(vc_stationary_points(f,c(0,0),tol_merge=0),"tol_merge")
expect_error(vc_stationary_points(f,c(a=0,a=0)),"unique")
})
test_that("stationary graphics preserve contour orientation, candidates and probe playback", {
skip_if_not_installed("plotly")
f <- function(v)v[1]^2+2*v[2]^2
r <- vc_stationary_points(f,c(.1,.2),assume_c2=TRUE,warn=FALSE)
p <- vc_plot_stationary(r,f,xlim=c(-1,2),ylim=c(-2,1),n=5,max_frames=5)
b <- plotly::plotly_build(p)
surface <- b$x$data[[1]]
expect_equal(matrix(as.numeric(surface$z),5),outer(seq(-2,1,length.out=5),seq(-1,2,length.out=5),function(y,x)x^2+2*y^2))
candidate <- Filter(function(a)identical(a$name,"Conditional minimum"),b$x$data)[[1]]
expect_equal(as.numeric(candidate$x),as.numeric(r$points[,1]))
expect_equal(as.numeric(candidate$y),as.numeric(r$points[,2]))
expect_length(b$x$frames,5)
expect_match(as.character(b$x$layout$title),"not iterations")
expect_length(plotly::plotly_build(vc_plot_stationary(r,f,show_history=FALSE,n=5))$x$frames,0)
no <- vc_stationary_points(function(v)v[1]+v[2],c(0,0),max_iter=2,warn=FALSE)
expect_s3_class(vc_plot_stationary(no,function(v)v[1]+v[2],animate=FALSE,n=5),"plotly")
expect_error(vc_plot_stationary(r,function(v)f(v)+1),"match")
expect_error(vc_plot_stationary(r,f,run=2),"run")
expect_error(vc_plot_stationary(r,f,max_frames=1),"max_frames")
expect_error(vc_plot_stationary(r,f,alpha=2),"alpha")
expect_error(vc_plot_stationary(r,f,show_history=NA),"show_history")
expect_error(vc_plot_stationary(r,f,n=1),"n")
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.