Learn R Programming

salad (version 1.2)

dual: Dual objects

Description

Create a dual object

Usage

dual(x, varnames, dx, constant = FALSE)

Value

an object of class 'dual'

Arguments

x

a numeric object (vector, matrix, or array)

varnames

(optional) the name of the variables in x

dx

(optional) a list of derivatives for the elements of x

constant

if 'TRUE', then a constant is returned.

Details

The basic usage is dual(x) which will create an object of class 'dual' with unit derivatives in each of its components. The variable names will be derived from the names of x, or generated in the form x1, x2, etc.

Another possible usage is dual(x, varnames = c('x1', 'x2'), constant = TRUE) which returns an object with null derivatives in x1 and x2.

Finally, a list of derivatives can be defined using option dx.

Examples

Run this code
# simple usage
x <- dual( c(1,2) )
x
d(x)
x <- dual(matrix(c(1,2,3,4), 2, 2))
x
d(x, "x1.1")

# using an object with names
x <- dual( c(a = 1, b = 2) )
x
d(x)

# generate a constant 
x <- dual(1, varnames = c("x1", "x2"), constant = TRUE)

# specify dx
x <- dual(c(1,2), dx = list(x1 = c(1,1)))
x
d(x)
# this is equivalent to :
x <- dual(1)
x <- c(x, x + 1)
x
d(x)

Run the code above in your browser using DataLab