dualFun1 {salad}R Documentation

Defining in-house derivatives

Description

Defining the differential of a univariate function

Usage

dualFun1(f, df)

Arguments

f

a function with a unique argument

df

the differential of f

Details

This function returns a new function that can be applied to a dual object. This allows to extend the package by defining functions it is currenlty unable to derive. It can also gain some time for intensively used functions (see examples below).

Value

Returns a function.

Examples

# using salad do compute the differential of a quadratic function
f <- function(x) x**2 + x + 1
x <- dual(4)
f(x)
d(f(x))

# using `dualFun1` to define the differential of f saves time
f1 <- dualFun1(f, \(x) 2*x + 1)
f1(x)
d(f1(x))
system.time( for(i in 1:500) f(x) )
system.time( for(i in 1:500) f1(x) )



[Package salad version 1.0 Index]