Learn R Programming

dual: an R package for dual numbers

Authors

Luca Sartore

Maintainer: Luca Sartore

Features of the package

Dual numbers are mainly used to implement automatic differentiation. The dual package provides mathematical functions that are able to handle computations with dual numbers. The package is useful to calculate exact derivatives in R without providing self-coded functions.

For a complete list of exported functions, use library(help = "dual") once the dual package is installed (see the inst/INSTALL.md file for a detailed description of the setup process).

Example

library(dual)
x <- dual(f = 1.5, grad = c(1:0, 0))
y <- dual(f = 0.5, grad = c(0:1, 0))
z <- dual(f = 1.0, grad = c(0, 0:1))
exp(z - x) * sin(x)^y / x

a <- dual(f = 1.1, grad = c(1.2, 2.3, 3.4, 4.5, 5.6))
0.5 * a^2 - 0.1

lambertW <- function(x) {
  w0 <- 1
  w1 <- w0 - (w0*exp(w0)-x)/((w0+1)*exp(w0)-(w0+2)*(w0*exp(w0)-x)/(2*w0+2))
  while(abs(w1-w0) > 1e-15) {
    w0 <- w1
    w1 <- w0 - (w0*exp(w0)-x)/((w0+1)*exp(w0)-(w0+2)*(w0*exp(w0)-x)/(2*w0+2))
  }
  return(w1)
}
lambertW(dual(1, 1))

References

Baydin, A. G., Pearlmutter, B. A., Radul, A. A., & Siskind, J. M. (2018). Automatic differentiation in machine learning: a survey. Journal of Machine Learning Research, 18, 1-43.

Cheng, H. H. (1994). Programming with dual numbers and its applications in mechanisms design. Engineering with Computers, 10(4), 212-229.

Kisil, V. V. (2007). Erlangen program at large-2: inventing a wheel. The parabolic one. arXiv: 0707.4020.

Copy Link

Version

Install

install.packages('dual')

Monthly Downloads

333

Version

0.0.5

License

GPL-3

Maintainer

Luca Sartore

Last Published

October 3rd, 2023

Functions in dual (0.0.5)

log

Logarithms and Exponentials
Trig

Trigonometric Functions
Hyperbolic

Hyperbolic Functions
Special

Special Functions of Mathematics
Logic

Logic Operators for Comparing Dual Numbers
Arithmetic

Arithmetic Operators
dual-class

Dual object class An S4 Class for dual numbers
Error

Special Functions of Mathematics
dual-package

tools:::Rd_package_title("dual")
MathFun

Miscellaneous Mathematical Functions