tests/testthat/test-cylindrical-integrate.R

test_that("cylindrical Jacobian and Cartesian field mapping are correct", {
 calc <- function(f,interval=c(0,2*pi),lo=function(t)0,hi=function(t)1,top=function(v)2)
   vc_cylindrical_integrate(f,interval,lo,hi,function(v)0,top,n_outer=8,n_middle=2,n_inner=2)
 a <- calc(function(v)1)
 expect_equal(a$estimate,2*pi)
 expect_true(a$tolerance_met)
 expect_equal(calc(function(v)v[3])$estimate,2*pi)
 expect_equal(calc(function(v)sum(v[1:2]^2))$estimate,pi)
 expect_equal(calc(function(v)v[1],c(0,pi/2))$estimate,2/3,tolerance=2e-6)
 expect_equal(calc(function(v)v[2],c(0,pi/2))$estimate,2/3,tolerance=2e-6)
 expect_equal(calc(function(v)-1,lo=function(t)1,hi=function(t)2)$estimate,-6*pi)
 expect_equal(calc(function(v)1,c(2*pi,0))$estimate,-2*pi)
 expect_equal(calc(function(v)1,top=function(v)1+v[2])$estimate,5*pi/3)
 nested <- vc_triple_integrate(function(v)v[2],c(0,2*pi),function(t)0,function(t)1,
   function(v)0,function(v)2,n_outer=8,n_middle=2,n_inner=2)
 expect_equal(a$outer,nested$outer)
 expect_equal(a$slices,nested$slices)
 expect_equal(a$error_estimate,nested$error_estimate)
 expect_equal(a$type,"cylindrical_integral")
})
test_that("cylindrical validation and empty widths remain explicit", {
 bad <- function(...)stop("unexpected")
 z <- function(t)0; one <- function(t)1
 expect_equal(vc_cylindrical_integrate(bad,c(0,0),bad,bad,bad,bad)$estimate,0)
 expect_equal(vc_cylindrical_integrate(bad,c(0,1),z,z,bad,bad)$estimate,0)
 expect_equal(vc_cylindrical_integrate(bad,c(0,1),z,one,z,z)$estimate,0)
 expect_error(vc_cylindrical_integrate(sum,c(0,7),z,one,z,one),"span")
 expect_error(vc_cylindrical_integrate(sum,c(0,1),function(t)-1,one,z,one),"nonnegative")
 expect_error(vc_cylindrical_integrate(sum,c(0,1),one,z,z,one),"lower")
 expect_error(vc_cylindrical_integrate(sum,c(0,1),z,one,one,z),"lower")
 expect_error(vc_cylindrical_integrate(sum,c(0,1),z,one,function(v)NA_real_,one))
 expect_error(vc_cylindrical_integrate(function(v)1/sqrt(sum(v[1:2]^2)),c(0,1),z,one,z,one))
 expect_error(vc_cylindrical_integrate(function(v)c(1,2),c(0,1),z,one,z,one))
 expect_error(vc_cylindrical_integrate(sum,c(0,0),z,one,z,one,n_middle=3))
 expect_error(vc_cylindrical_integrate(sum,c(0,0),z,one,z,one,abs_tol=0,rel_tol=0))
 expect_error(vc_cylindrical_integrate(NULL,c(0,0),z,one,z,one))
})
test_that("cylindrical refinement retains nested uncertainty", {
 calc <- function(n)vc_cylindrical_integrate(function(v)exp(v[3]),c(0,1),
   function(t)0,function(t)1,function(v)0,function(v)1,method="trapezoid",
   n_outer=2,n_middle=2,n_inner=n,abs_tol=1e-12,rel_tol=1e-12)
 a <- calc(2); b <- calc(8)
 expect_lt(abs(b$estimate-(exp(1)-1)/2),abs(a$estimate-(exp(1)-1)/2))
 expect_false(a$tolerance_met)
 expect_gt(a$slice_error,0)
 expect_equal(a$error_estimate,a$outer_error+a$slice_error)
})
test_that("cylindrical graphics use only stored Cartesian fibers and base", {
 skip_if_not_installed("plotly")
 r <- vc_cylindrical_integrate(sum,c(0,pi/2),function(t)1,function(t)2,
   function(v)-1,function(v)v[2],n_outer=2,n_middle=2,n_inner=2)
 p <- plotly::plotly_build(vc_plot_cylindrical_integral(r))
 a <- p$x$data[[1]]
 expect_equal(as.numeric(a$x)[1:3],c(1,1,NA))
 expect_equal(as.numeric(a$y)[1:3],c(0,0,NA))
 expect_equal(as.numeric(a$z)[1:3],c(-1,1,NA))
 expect_equal(a$type,"scatter3d")
 q <- plotly::plotly_build(vc_plot_cylindrical_integral(r,"projection"))
 d <- r$projection$sections;d <- d[order(d$outer),]
 t <- c(d$outer,rev(d$outer),d$outer[1]); radius <- c(d$upper,rev(d$lower),d$upper[1])
 expect_equal(as.numeric(q$x$data[[1]]$x),radius*cos(t))
 expect_equal(as.numeric(q$x$data[[1]]$y),radius*sin(t))
 expect_error(vc_plot_cylindrical_integral(list()),"Invalid")
 z <- vc_cylindrical_integrate(sum,c(0,0),identity,identity,sum,sum)
 expect_error(vc_plot_cylindrical_integral(z),"No stored")
})

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.