tests/testthat/test-integrate.R

test_that("quadrature recovers analytical signed integrals", {
  for(method in c("trapezoid","simpson","adaptive")) {
    expect_equal(vc_integrate(function(x)3,c(0,2),method)$estimate,6)
    expect_equal(vc_integrate(identity,c(-1,1),method)$estimate,0,tolerance=1e-12)
    a <- vc_integrate(function(x)x^2,c(0,2),method,n=128)
    expect_equal(a$estimate,8/3,tolerance=3e-5)
    expect_equal(vc_integrate(sin,c(0,pi),method,n=256)$estimate,2,tolerance=1e-5)
    expect_equal(vc_integrate(exp,c(0,1),method,n=256)$estimate,exp(1)-1,tolerance=1e-6)
    expect_equal(vc_integrate(exp,c(1,0),method,n=256)$estimate,
                 -vc_integrate(exp,c(0,1),method,n=256)$estimate)
    z <- vc_integrate(function(x)stop("must not evaluate"),c(2,2),method)
    expect_identical(z$status,"zero_interval")
    expect_identical(z$estimate,0)
  }
  a <- (function(x)x^3) |> vc_integrate(c(-1,2),method="simpson",n=2)
  expect_equal(a$estimate,15/4)
  expect_equal(sum(a$table$contribution),a$estimate)
  expect_equal(sum(a$table$weight),3)
  b <- vc_integrate(function(x)x^3,c(2,-1),method="simpson",n=2)
  expect_equal(sum(b$table$weight),-3)
  expect_equal(sum(b$table$contribution),b$estimate)
  # Scalar-only user functions are supported even by adaptive vector calls.
  expect_equal(vc_integrate(function(x){stopifnot(length(x)==1);x^2},
                            c(0,2),"adaptive")$estimate,8/3)
  expect_equal(vc_integrate(identity,c(0,1),"trapezoid",n=1)$estimate,.5)
})

test_that("refinement and adaptive diagnostics retain their limitations", {
  a <- vc_integrate(function(x)x^2,c(0,2),n=4)
  b <- vc_integrate(function(x)x^2,c(0,2),n=8)
  expect_equal(abs(a$estimate-8/3)/abs(b$estimate-8/3),4)
  expect_equal(a$error_estimate,abs(a$estimate-8/3))
  expect_false(a$tolerance_met)
  s1 <- vc_integrate(exp,c(0,1),"simpson",n=4)
  s2 <- vc_integrate(exp,c(0,1),"simpson",n=8)
  expect_gt(abs(s1$estimate-(exp(1)-1))/abs(s2$estimate-(exp(1)-1)),15)
  expect_lt(s2$error_estimate,s1$error_estimate)
  a <- vc_integrate(sin,c(0,pi),"adaptive")
  expect_identical(a$status,"adaptive_ok")
  expect_true(a$tolerance_met)
  expect_null(a$table)
  # A tiny budget must not be reported as successful convergence.
  a <- vc_integrate(function(x)sin(100*x),c(0,1),"adaptive",subdivisions=1)
  expect_identical(a$status,"adaptive_failure")
  expect_false(a$tolerance_met)
  expect_match(a$message,"subdivisions")
  # A narrow missed bump defeats a zero fixed-grid diagnostic.
  bump <- function(x)if(abs(x-.12345)<.00001)1 else 0
  a <- vc_integrate(bump,c(0,1),n=2)
  expect_equal(a$estimate,0)
  expect_equal(a$error_estimate,0)
  expect_match(a$note,"not certified")
  a <- vc_integral_compare(function(x)x^2,c(0,2),reference=8/3)
  expect_equal(a$table$error,a$table$estimate-8/3)
  expect_equal(a$table$estimate[2:3],rep(8/3,2))
  expect_true(all(is.na(vc_integral_compare(sin,c(0,1))$table$error)))
})

test_that("invalid quadrature inputs and nonfinite samples are rejected", {
  for(interval in list(c(0,Inf),c(NA,1),1,c(-1e308,1e308)))
    expect_error(vc_integrate(sin,interval))
  expect_error(vc_integrate(1,c(0,1)))
  for(n in c(0,1.5,NA,Inf,.Machine$integer.max))
    expect_error(vc_integrate(sin,c(0,1),n=n))
  expect_error(vc_integrate(sin,c(0,1),"simpson",n=3))
  expect_error(vc_integrate(sin,c(0,1),abs_tol=0,rel_tol=0))
  expect_error(vc_integrate(sin,c(0,1),abs_tol=-1))
  expect_error(vc_integrate(sin,c(0,1),"adaptive",abs_tol=0,rel_tol=1e-20))
  expect_error(vc_integrate(sin,c(0,1),"adaptive",subdivisions=0))
  for(method in c("trapezoid","simpson","adaptive")) {
    expect_error(vc_integrate(function(x)NA_real_,c(0,1),method))
    expect_error(vc_integrate(function(x)c(x,x),c(0,1),method))
    expect_error(vc_integrate(function(x)1i,c(0,1),method))
    expect_error(vc_integrate(function(x)1e308,c(0,10),method))
  }
  expect_error(vc_integral_compare(sin,c(0,1),methods=character()))
  expect_error(vc_integral_compare(sin,c(0,1),methods=c("simpson","simpson")))
  expect_error(vc_integral_compare(sin,c(0,1),reference=Inf))
  expect_error(vc_integrate(sin,c(1e16,1e16+2),n=32))
})

test_that("quadrature graphics correspond to stored nodes and interpolants", {
  skip_if_not_installed("plotly")
  f <- function(x)x^2
  a <- vc_integrate(f,c(0,2),"simpson",n=2)
  p <- plotly::plotly_build(vc_plot_integral(a,f))
  expect_length(p$x$data,3)
  arc <- p$x$data[[1]]
  expect_equal(as.numeric(arc$y),as.numeric(arc$x)^2,tolerance=1e-12)
  expect_identical(arc$fill,"tozeroy")
  expect_equal(as.numeric(p$x$data[[2]]$x),a$table$x)
  expect_equal(as.numeric(p$x$data[[2]]$y),a$table$value)
  a <- vc_integrate(identity,c(-1,1),n=1)
  p <- plotly::plotly_build(vc_plot_integral(a,identity))
  expect_equal(as.numeric(p$x$data[[1]]$x),a$table$x)
  expect_true(any(p$x$data[[1]]$y<0))
  expect_error(vc_plot_integral(a,function(x)x+1),"does not match")
  a <- vc_integrate(sin,c(0,pi),"adaptive")
  p <- plotly::plotly_build(vc_plot_integral(a,sin))
  expect_length(p$x$data,1)
  expect_match(p$x$data[[1]]$name,"not adaptive mesh")
  expect_error(vc_plot_integral(a,sin,alpha=2))
  expect_error(vc_plot_integral(a,sin,xscale="bad"))
})

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.