Learn R Programming

salad (version 1.2)

dualFun1: Defining in-house derivatives

Description

Defining the differential of a univariate function

Usage

dualFun1(f, df)

Value

Returns a function.

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).

Examples

Run this code
# 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) )


Run the code above in your browser using DataLab