ADvector: The AD vector and its methods

ADvectorR Documentation

The AD vector and its methods

Description

An advector is a class used behind the scenes to replace normal R numeric objects during automatic differentiation. An advector has a temporary lifetime and therefore you do not see / need to know it as a normal user.

Usage

advector(x)

## S3 method for class 'advector'
Ops(e1, e2)

## S3 method for class 'advector'
Math(x, ...)

## S3 method for class 'advector'
as.vector(x, mode = "any")

## S3 method for class 'advector'
as.complex(x, ...)

## S3 method for class 'advector'
aperm(a, perm, ...)

## S3 method for class 'advector'
c(...)

## S3 method for class 'advector'
x[...]

## S3 replacement method for class 'advector'
x[...] <- value

## S3 method for class 'advector'
x[[...]]

## S3 method for class 'advector'
rep(x, ...)

## S3 method for class 'advector'
is.nan(x)

## S3 method for class 'advector'
is.finite(x)

## S3 method for class 'advector'
is.infinite(x)

## S3 method for class 'advector'
is.na(x)

## S3 method for class 'advector'
sum(x, ..., na.rm = FALSE)

## S3 method for class 'advector'
mean(x, ...)

## S3 method for class 'advector'
prod(x, ..., na.rm = FALSE)

## S3 method for class 'advector'
min(..., na.rm = FALSE)

## S3 method for class 'advector'
max(..., na.rm = FALSE)

## S3 method for class 'advector'
is.numeric(x)

## S3 method for class 'advector'
as.double(x, ...)

## S3 method for class 'advector'
Complex(z)

## S3 method for class 'advector'
Summary(..., na.rm = FALSE)

## S3 method for class 'advector'
diff(x, lag = 1L, differences = 1L, ...)

## S3 method for class 'advector'
print(x, ...)

## S4 method for signature 'num,ad,ad'
ifelse(test, yes, no)

## S4 method for signature 'num,num,num'
ifelse(test, yes, no)

## S4 method for signature 'advector'
sort(x)

## S4 method for signature 'advector,advector,ANY,ANY'
x[i]

## S4 method for signature 'advector,advector'
findInterval(x, vec)

Arguments

x

numeric or advector

e1

advector

e2

advector

...

Additional arguments

mode

FIXME might not be handled correctly by as.vector

a

advector with dimension attribute

perm

Permutation as in aperm

value

Replacement value implicitly converted to AD

na.rm

Must be FALSE (default)

z

Complex (not allowed)

lag

As diff

differences

As diff

test

logical vector

yes

advector

no

advector

i

Variable indices for taped subset

vec

Sorted vector defining the intervals to lookup

Details

An AD vector (class='advector') is an atomic R vector of 'codes' that are internally interpretable as 'AD scalars'. A substantial part of R's existing S3 matrix and array functionality can be re-used for AD vectors.

Value

Object of class "advector".

Functions

  • advector(): Construct a new advector

  • Ops(advector): Binary operations

  • Math(advector): Unary operations

  • as.vector(advector): Makes array(x) work.

  • as.complex(advector): Convert to ADcomplex. Note that dimensions are dropped for consistency with base R.

  • aperm(advector): Equivalent of aperm

  • c(advector): Equivalent of c. However note the limitation for mixed types: If x is an AD type, c(x,1) works while c(1,x) does not!

  • [: Equivalent of [

  • `[`(advector) <- value: Equivalent of [<-

  • [[: Equivalent of [[

  • rep(advector): Equivalent of rep. Makes outer(x,x,...) work.

  • is.nan(advector): Equivalent of is.nan. Check NaN status of a constant advector expression. If not constant throw an error.

  • is.finite(advector): Equivalent of is.finite. Check finite status of a constant advector expression. If not constant throw an error.

  • is.infinite(advector): Equivalent of is.infinite. Check infinity status of a constant advector expression. If not constant throw an error.

  • is.na(advector): Equivalent of is.na. Check NA status of an advector. NAs can only occur directly (as constants) or indirectly as the result of an operation with NA operands. For a tape built with non-NA parameters the NA status of any expression is constant and can therefore safely be used as part of the calculations. (assuming correct propagation of NAs via C-level arithmetic).

  • sum(advector): Equivalent of sum. na.rm=TRUE is allowed, but note that this feature assumes correct propagation of NAs via C-level arithmetic.

  • mean(advector): Equivalent of mean except no arguments beyond x are supported.

  • prod(advector): Equivalent of prod.

  • min(advector): Equivalent of min.

  • max(advector): Equivalent of max.

  • is.numeric(advector): Makes cov2cor() work. FIXME: Any unwanted side-effects with this?

  • as.double(advector): Makes as.numeric() work.

  • Complex(advector): Complex operations are redirected to adcomplex.

  • Summary(advector): Unimplemented Summary operations (currently all any range) will throw an error.

  • diff(advector): Equivalent of diff

  • print(advector): Print method

  • ifelse(test = num, yes = ad, no = ad): Equivalent of ifelse

  • ifelse(test = num, yes = num, no = num): Default method

  • sort(advector): Taped sorting of an AD vector

  • x[i: Taped subsetting of an AD vector

  • findInterval(x = advector, vec = advector): Taped interval finding of an AD vector MakeTape(function(x) findInterval(x, AD(0:10)), 1:3)

Examples

x <- advector(1:9)
a <- array(x, c(3,3))  ## as an array
outer(x, x, "+") ## Implicit via 'rep'
rev(x)           ## Implicit via '['
MakeTape(sort, numeric(3))
MakeTape(function(x) AD(rivers)[x], 1:3)

RTMB documentation built on Nov. 5, 2025, 6:19 p.m.