tests/testthat/test-derivative-matrices.R

test_that("Jacobian rows/columns and cached vector evaluations are coherent", {
  calls <- 0L
  f <- function(v) {calls <<- calls+1L;c(first=v[1]^2+v[2],second=v[2]*v[3])}
  p <- c(x=1,y=2,z=3)
  a <- f |> vc_jacobian(p)
  expect_equal(unname(a$estimate),matrix(c(2,0,1,3,0,2),2,3),tolerance=1e-6)
  expect_equal(rownames(a$estimate),c("first.x","second.y"))
  expect_equal(colnames(a$estimate),names(p))
  expect_equal(a$evaluation_count,19)
  expect_equal(calls,a$evaluation_count)
  expect_true(a$all_reliable)
  expect_length(a$refinements,3)
  expect_equal(a$refinements[[3]],a$estimate)
  scalar <- function(v)sin(v[1]*v[2])+exp(v[3])
  j <- vc_jacobian(scalar,c(.5,.7,.2))
  expect_equal(unname(j$estimate),matrix(c(.7*cos(.35),.5*cos(.35),exp(.2)),1),tolerance=1e-6)
  expect_equal(unname(j$estimate[1,]),unname(vc_gradient(scalar,c(.5,.7,.2))$estimate))
  for(method in c("forward","backward")) {
    a <- vc_jacobian(function(v)c(v[1]^2,v[2]^2),c(1,2),method=method)
    expect_equal(unname(a$estimate),diag(c(2,4)),tolerance=1e-6)
  }
  a <- vc_jacobian(function(v)c(sqrt(v[1]),v[2]^2),c(0,1),method=c("forward","central"),warn=FALSE)
  expect_true(is.finite(a$estimate[1,1]))
  expect_false(a$all_reliable)
})

test_that("direct Hessian entries recover scalar and mixed analytical derivatives", {
  f <- function(v)v[1]^2+v[1]*v[2]+2*v[2]^2
  a <- f |> vc_hessian(c(x=1,y=1),h=.01)
  expect_equal(unname(a$estimate),matrix(c(2,1,1,4),2),tolerance=1e-6)
  expect_true(a$all_reliable)
  expect_true(isSymmetric(a$estimate))
  expect_equal(a$refinements[[3]],a$estimate)
  expect_equal(a$evaluation_count,25)
  expect_length(a$results,3)
  expect_true(all(a$table$resolved))
  one <- vc_hessian(function(v)v[1]^3,2,h=.01)
  reference <- .vc_second_difference(function(x)x^3,2,.01,1e-8,1e-4)
  expect_equal(one$estimate[1,1],reference$estimate)
  expect_equal(one$results[[1]]$derivative,reference)
  f3 <- function(v)sin(v[1]*v[2])+exp(v[3])
  p <- c(.5,.7,.2)
  exact <- matrix(c(-.7^2*sin(.35),cos(.35)-.35*sin(.35),0,
                    cos(.35)-.35*sin(.35),-.5^2*sin(.35),0,0,0,exp(.2)),3)
  b <- vc_hessian(f3,p,h=c(.01,.02,.01),warn=FALSE)
  expect_equal(unname(b$estimate),exact,tolerance=1e-5)
  p <- c(2^30+.25,2^29+.5)
  centered <- function(v)(v[1]-p[1])^2+(v[1]-p[1])*(v[2]-p[2])+2*(v[2]-p[2])^2
  b <- vc_hessian(centered,p,h=c(.00012345,.00023456),warn=FALSE)
  expect_equal(unname(b$estimate),matrix(c(2,1,1,4),2),tolerance=1e-8)
})

test_that("Hessian refinement and roundoff failures are retained", {
  f <- function(v)exp(v[1]+v[2])
  p <- c(.2,.3)
  a <- vc_hessian(f,p,h=.1,warn=FALSE)
  b <- vc_hessian(f,p,h=.05,warn=FALSE)
  exact <- matrix(exp(.5),2,2)
  expect_lt(max(abs(b$estimate-exact)),max(abs(a$estimate-exact)))
  expect_true(all(abs(a$refinements[[3]]-exact)<abs(a$refinements[[1]]-exact)))
  offset <- vc_hessian(function(v)1e12+v[1]^2+v[1]*v[2]+2*v[2]^2,c(1,1),warn=FALSE)
  expect_false(offset$all_reliable)
  expect_true(all(!offset$reliable))
  expect_true(all(is.finite(offset$estimate)))
  zero <- vc_hessian(function(v)0,c(0,0))
  expect_equal(unname(zero$estimate),matrix(0,2,2))
  expect_true(zero$all_reliable)
})

test_that("symmetric rectangle estimates do not prove ordered mixed partial equality", {
  f <- function(v)if(all(v==0))0 else prod(v)*(v[1]^2-v[2]^2)/sum(v^2)
  equal_steps <- vc_hessian(f,c(0,0),h=.01)
  unequal_steps <- vc_hessian(f,c(0,0),h=c(.01,.02))
  expect_true(equal_steps$all_reliable)
  expect_true(unequal_steps$all_reliable)
  expect_equal(equal_steps$estimate[1,2],0)
  expect_equal(unequal_steps$estimate[1,2],-.6)
  expect_true(isSymmetric(equal_steps$estimate))
  expect_match(equal_steps$note,"not proof of C2")
})

test_that("matrix derivative invalid shapes, output changes and domains are rejected", {
  f <- function(v)c(v[1],v[2])
  expect_error(vc_jacobian(f,numeric()))
  expect_error(vc_jacobian(f,c(1,2),h=c(1,2,3)))
  expect_error(vc_jacobian(f,c(1,2),method=c("central","bad")))
  expect_error(vc_jacobian(function(v)c(1,Inf),c(1,2)))
  expect_error(vc_jacobian(function(v)matrix(v),c(1,2)))
  expect_error(vc_jacobian(function(v)if(v[1]>0)c(1,2,3) else c(1,2),c(0,0)),"dimension")
  expect_error(vc_jacobian(function(v)if(v[1]>0)c(a=1,b=2) else c(c=1,d=2),c(0,0)),"names")
  expect_error(vc_jacobian(f,c(1,2),h=1e-320),"collapsed")
  for(h in list(0,Inf,c(1,2,3),1i)) expect_error(vc_hessian(sum,c(1,2),h=h))
  expect_error(vc_hessian(sum,c(1,2),h=1e-320),"collapsed")
  expect_error(vc_hessian(function(v)c(1,2),c(1,2)))
  expect_error(suppressWarnings(vc_hessian(function(v)sqrt(v[1])+v[2]^2,c(0,1))))
  expect_error(vc_hessian(sum,c(1,2),atol=-1))
  expect_error(vc_jacobian(f,c(1,2),warn=NA))
})

test_that("matrix heatmap, labels and slider states use stored matrices", {
  skip_if_not_installed("plotly")
  f <- function(v)c(v[1]^2+v[2],v[2]*v[3])
  a <- vc_jacobian(f,c(x=1,y=2,z=3))
  p <- plotly::plotly_build(vc_plot_matrix(a))
  expect_length(p$x$data,3)
  for(i in 1:3) expect_equal(unname(p$x$data[[i]]$z),unname(a$refinements[[i]]),ignore_attr=TRUE)
  expect_equal(as.numeric(p$x$data[[3]]$x),1:3)
  expect_equal(as.numeric(p$x$data[[3]]$y),1:2)
  expect_equal(p$x$layout$xaxis$ticktext,c("x","y","z"))
  expect_equal(p$x$layout$yaxis$range,c(2.5,.5))
  expect_length(p$x$layout$annotations,6)
  steps <- p$x$layout$sliders[[1]]$steps
  expect_length(steps,3)
  expect_equal(unlist(steps[[1]]$args[[1]]$visible),c(TRUE,FALSE,FALSE))
  expect_equal(unlist(steps[[3]]$args[[1]]$visible),c(FALSE,FALSE,TRUE))
  expect_equal(p$x$data[[1]]$zmax,p$x$data[[3]]$zmax)
  b <- vc_hessian(function(v)1e12+sum(v^2),c(1,1),warn=FALSE)
  q <- plotly::plotly_build(vc_plot_matrix(b,slider=FALSE,show_values=FALSE))
  expect_length(q$x$data,2)
  expect_identical(q$x$data[[2]]$name,"Unresolved entry checks")
  expect_equal(sort(q$x$data[[2]]$x),c(1,1,2,2))
  expect_error(vc_plot_matrix(a,digits=16))
  expect_error(vc_plot_matrix(a,alpha=2))
  expect_error(vc_plot_matrix(a,slider=NA))
})

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.