## ----set-options, echo = FALSE------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", dev = "png", fig.width = 7, fig.height = 3.5, message = FALSE, warning = FALSE)
options(width = 80, tibble.width = Inf)

## ----out.width='20%', out.extra='style="float:right; padding:10px"',echo=FALSE----
knitr::include_graphics(system.file("help/figures/Brobdingnag.png", package = "Brobdingnag"))

## ----loadpkg------------------------------------------------------------------
library("Brobdingnag")

## ----firstbrobmat-------------------------------------------------------------
M1 <- brobmat(-10:13,4,6)
colnames(M1) <- state.abb[1:6]
M1

## ----secondbrobmat------------------------------------------------------------
M2 <- brobmat(
c(1,104,-66,45,1e40,-2e40,1e-200,232.2),2,4,
positive=c(T,F,T,T,T,F,T,T))
M2

## ----standardarith------------------------------------------------------------
rownames(M2) <- c("a","b")
colnames(M2) <- month.abb[1:4]
M2
M2[2,3] <- 0
M2
M2+1000

## ----matmult------------------------------------------------------------------
M2 %*% M1

## ----verifymatmult------------------------------------------------------------
nrows <- 11
ncols <- 18
M3 <- as.brobmat(matrix(rnorm(nrows*ncols),nrows,ncols))
M4 <- as.brobmat(matrix(rnorm(nrows*ncols),ncols,nrows))
M3[1:3,1:3]

## ----usebaseR-----------------------------------------------------------------
p1 <- as.matrix(M3) %*% as.matrix(M4)

## ----usebrob------------------------------------------------------------------
p2 <- as.matrix(M3 %*% M4)

## ----showdiff-----------------------------------------------------------------
max(abs(p1-p2))

## ----otherway-----------------------------------------------------------------
q1 <- M3 %*% M4
q2 <- as.brobmat(as.matrix(M3) %*% as.matrix(M4))
max(abs(as.brob(q1-q2)))

## ----label = numericalintegration---------------------------------------------
library("cubature")

f.numeric <- function(x){x^2 - 4}

out.num <- cubature::hcubature(f = f.numeric, lowerLimit = 0, upperLimit = 4, vectorInterface = TRUE)
out.num

## ----label = numericalintegrationbrob-----------------------------------------

f.brob <- function(x) {
	x <- as.brob(x[1, ])
	as.matrix( brobmat(x^2 - 4, ncol = length(x)))
}

out.brob <- cubature::hcubature(f = f.brob, lowerLimit = 0, upperLimit = 4, vectorInterface = TRUE)
out.brob

## ----label=comparebrobandnumeric----------------------------------------------
out.brob$integral - out.num$integral

