Nothing
test_that("regular equality candidates recover analytic multipliers and reduced curvature", {
f <- function(v)sum(v^2);g <- function(v)sum(v)-1
r <- f |> vc_lagrange(g=g,point=c(.5,.5),assume_c2=TRUE,hessian_h=.01,warn=FALSE)
expect_identical(r$classification,"minimum")
expect_true(r$feasible);expect_true(r$regular);expect_true(r$stationary_evidence)
expect_equal(unname(r$lambda),1,tolerance=1e-7)
expect_equal(r$eigenvalues,2,tolerance=1e-7)
expect_equal(dim(r$bordered_hessian),c(3L,3L))
expect_equal(r$gradient,vc_gradient(f,c(.5,.5),warn=FALSE))
expect_equal(r$jacobian,vc_jacobian(g,c(.5,.5),warn=FALSE))
expect_equal(r$hessians$objective,vc_hessian(f,c(.5,.5),h=.01,warn=FALSE))
expect_equal(unname(r$jacobian$estimate%*%r$tangent_basis),matrix(0,1,1),tolerance=1e-8)
expect_equal(crossprod(r$tangent_basis),matrix(1,1,1),tolerance=1e-8)
expect_equal(r$reduced_hessian,crossprod(r$tangent_basis,r$lagrangian_hessian%*%r$tangent_basis))
maximum <- vc_lagrange(function(v)-sum(v^2),g,c(.5,.5),TRUE,hessian_h=.01,warn=FALSE)
expect_identical(maximum$classification,"maximum")
expect_equal(unname(maximum$lambda),-1,tolerance=1e-7)
expect_identical(vc_lagrange(f,g,c(.5,.5),hessian_h=.01,warn=FALSE)$classification,"inconclusive")
expect_warning(vc_lagrange(f,g,c(.5,.5),hessian_h=.01),"C2")
})
test_that("curved constraints, tangent-space signs and smooth functions have known answers", {
circle <- function(v)sum(v^2)-1
r <- vc_lagrange(function(v)v[1],circle,c(1,0),TRUE,hessian_h=.01,warn=FALSE)
expect_identical(r$classification,"maximum")
expect_equal(unname(r$lambda),.5,tolerance=1e-7)
expect_equal(r$eigenvalues,-1,tolerance=1e-7)
expect_false(r$jacobian$all_reliable) # original strict zero-partial flag survives
expect_true(r$stationary_evidence)
expect_identical(vc_lagrange(function(v)v[1],circle,c(-1,0),TRUE,hessian_h=.01,warn=FALSE)$classification,"minimum")
saddle <- vc_lagrange(function(v)v[1]^2-v[2]^2,function(v)v[3],c(0,0,0),TRUE,warn=FALSE)
expect_identical(saddle$classification,"saddle")
constrained_min <- vc_lagrange(function(v)v[1]^2-v[2]^2,function(v)v[2],c(0,0),TRUE,warn=FALSE)
expect_identical(constrained_min$classification,"minimum")
expect_identical(vc_lagrange(function(v)cos(v[1])+cos(v[2]),function(v)v[1],c(0,0),
TRUE,hessian_h=.01,warn=FALSE)$classification,"maximum")
expect_identical(vc_lagrange(function(v)exp(sum(v^2)),function(v)v[1],c(0,0),
TRUE,hessian_h=.01,warn=FALSE)$classification,"minimum")
degenerate <- vc_lagrange(function(v)v[1]^2+v[2]^4,function(v)v[1],c(0,0),TRUE,warn=FALSE)
expect_identical(degenerate$classification,"inconclusive")
expect_identical(vc_lagrange(function(v)0,function(v)v[1],c(0,0),TRUE,warn=FALSE)$classification,"inconclusive")
})
test_that("multiple constraints, permutations and scaling preserve the regular result", {
f <- function(v)sum(v^2)
g <- list(plane=function(v)sum(v)-1,balance=function(v)v[1]-v[3])
a <- vc_lagrange(f,g,rep(1/3,3),TRUE,hessian_h=.01,warn=FALSE)
b <- vc_lagrange(f,rev(g),rep(1/3,3),TRUE,hessian_h=.01,warn=FALSE)
expect_identical(a$classification,"minimum")
expect_equal(a$rank,2L)
expect_equal(unname(a$lambda),c(2/3,0),tolerance=1e-7)
expect_equal(a$lambda,rev(b$lambda),tolerance=1e-7)
expect_equal(a$eigenvalues,b$eigenvalues,tolerance=1e-7)
expect_identical(rownames(a$jacobian$estimate),names(g))
scaled_raw <- vc_lagrange(f,function(v)1000*(sum(v)-1),c(.5,.5),TRUE,hessian_h=.01,warn=FALSE)
expect_identical(scaled_raw$classification,"inconclusive")
scaled <- vc_lagrange(f,function(v)1000*(sum(v)-1),c(.5,.5),TRUE,hessian_h=.01,atol=1e-7,warn=FALSE)
expect_identical(scaled$classification,"minimum")
expect_equal(unname(scaled$lambda),.001,tolerance=1e-7)
named <- vc_lagrange(function(v)sum(v^2),function(v)unname(sum(v)-1),c(x=.5,y=.5),
TRUE,hessian_h=.01,warn=FALSE)
expect_identical(names(named$point),c("x","y"))
expect_equal(named$eigenvalues,2,tolerance=1e-7)
})
test_that("infeasibility, failed stationarity and singular rank never force extrema", {
f <- function(v)sum(v^2);g <- function(v)sum(v)-1
infeasible <- vc_lagrange(f,g,c(0,0),TRUE,warn=FALSE)
expect_identical(infeasible$classification,"infeasible")
expect_false(infeasible$feasible)
expect_null(infeasible$hessians)
wrong <- vc_lagrange(f,g,c(1,0),TRUE,warn=FALSE)
expect_identical(wrong$classification,"not_stationary")
expect_true(wrong$feasible)
expect_gt(max(abs(wrong$stationarity_residual)),.9)
singular <- vc_lagrange(function(v)v[1],function(v)sum(v^2),c(0,0),TRUE,warn=FALSE)
expect_identical(singular$classification,"inconclusive")
expect_false(singular$regular)
expect_equal(singular$rank,0L)
redundant <- vc_lagrange(f,function(v)c(sum(v)-1,2*(sum(v)-1)),c(.5,.5),TRUE,warn=FALSE)
expect_false(redundant$regular)
expect_equal(unname(redundant$lambda),c(.2,.4),tolerance=1e-7)
expect_identical(redundant$classification,"inconclusive")
isolated <- vc_lagrange(f,function(v)v-1,c(1,1),TRUE,hessian_h=.01,warn=FALSE)
expect_true(isolated$regular)
expect_equal(ncol(isolated$tangent_basis),0L)
expect_identical(isolated$classification,"inconclusive")
expect_match(isolated$reason,"Zero-dimensional")
over <- vc_lagrange(f,function(v)c(v,sum(v)),c(0,0),TRUE,warn=FALSE)
expect_false(over$regular)
expect_identical(over$classification,"inconclusive")
cusp <- vc_lagrange(function(v)abs(v[1])+v[2]^2,function(v)v[2],c(0,0),TRUE,warn=FALSE)
expect_false(cusp$stationary_evidence)
expect_identical(cusp$classification,"inconclusive")
})
test_that("stencil failures, precision limits and invalid arguments remain explicit", {
f <- function(v)sum(v^2);g <- function(v)sum(v)-1
offset <- vc_lagrange(function(v)1e12+sum(v^2),function(v)v[1],c(0,0),TRUE,warn=FALSE)
expect_identical(offset$classification,"inconclusive")
expect_false(offset$hessians$objective$all_reliable)
domain <- function(v){if(max(abs(v))>.05)stop("domain");sum(v^2)}
failed <- vc_lagrange(domain,function(v)v[1],c(0,0),TRUE,hessian_h=.1,warn=FALSE)
expect_identical(failed$classification,"inconclusive")
expect_match(failed$hessians$objective$error,"domain")
changed <- vc_lagrange(f,function(v)if(v[1]>0)c(0,0)else 0,c(0,0),TRUE,warn=FALSE)
expect_identical(changed$classification,"inconclusive")
expect_match(changed$jacobian$error,"dimension/names changed")
expect_error(vc_lagrange(f,g,0),"two coordinates")
expect_error(vc_lagrange(f,NULL,c(0,0)),"g")
expect_error(vc_lagrange(f,list(),c(0,0)),"scalar functions")
expect_error(vc_lagrange(f,list(1),c(0,0)),"scalar functions")
expect_error(vc_lagrange(f,function(v)c(a=0,a=0),c(0,0)),"unique")
expect_error(vc_lagrange(f,function(v)Inf,c(0,0)),"finite")
expect_error(vc_lagrange(f,g,c(.5,.5),h=-1),"h")
expect_error(vc_lagrange(f,g,c(.5,.5),hessian_h=0),"h")
expect_error(vc_lagrange(f,g,c(.5,.5),rank_tol=0),"rank_tol")
expect_error(vc_lagrange(f,g,c(.5,.5),tol_stationary=NA),"tol_stationary")
expect_error(vc_lagrange(f,g,c(.5,.5),assume_c2=NA),"assume_c2")
})
test_that("Lagrange plots preserve actual zero sets and stored gradient magnitudes", {
skip_if_not_installed("plotly")
f <- function(v)v[1]^2+2*v[2]^2
g <- function(v)v[1]+2*v[2]-1
r <- vc_lagrange(f,g,c(1/3,1/3),TRUE,hessian_h=.01,warn=FALSE)
b <- plotly::plotly_build(vc_plot_lagrange(r,f,g,xlim=c(-1,2),ylim=c(-2,1),n=5,
show_weighted=TRUE,vector_scale=.25))
x <- seq(-1,2,length.out=5);y <- seq(-2,1,length.out=5)
expect_equal(matrix(as.numeric(b$x$data[[1]]$z),5),outer(y,x,function(y,x)x^2+2*y^2))
expect_equal(matrix(as.numeric(b$x$data[[2]]$z),5),outer(y,x,function(y,x)x+2*y-1))
expect_equal(b$x$data[[2]]$contours$start,0)
expect_equal(b$x$data[[2]]$contours$end,0)
vector <- Filter(function(t)identical(t$name,"Objective gradient"),b$x$data)[[1]]
expect_equal(as.numeric(vector$x),unname(c(r$point[1],r$point[1]+.25*r$gradient$estimate[1])))
expect_equal(as.numeric(vector$y),unname(c(r$point[2],r$point[2]+.25*r$gradient$estimate[2])))
expect_length(b$x$layout$annotations,3)
expect_length(plotly::plotly_build(vc_plot_lagrange(r,f,g,n=5,show_gradients=FALSE))$x$layout$annotations,0)
infeasible <- vc_lagrange(f,g,c(0,0),warn=FALSE)
expect_s3_class(vc_plot_lagrange(infeasible,f,g,n=5),"plotly")
expect_error(vc_plot_lagrange(r,function(v)f(v)+1,g),"match")
expect_error(vc_plot_lagrange(r,f,function(v)g(v)+1),"match")
expect_error(vc_plot_lagrange(r,f,g,n=1),"n")
expect_error(vc_plot_lagrange(r,f,g,constraint_colors=NA_character_),"constraint_colors")
expect_error(vc_plot_lagrange(r,f,g,show_weighted=NA),"show_weighted")
})
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.