This is a Common Lisp library for working with dual numbers. Dual numbers are a simple extension to the real numbers, of the form a + bε, where a and b are real numbers and ε is a nilpotent element such that ε² = 0.
This library provides a dual-number class and implements various arithmetic and transcendental functions for it. The primary application of this library is automatic differentiation.
- Basic arithmetic operations:
+,-,*,/ - Trigonometric functions:
sin,cos,tan - Hyperbolic functions:
sinh,cosh,tanh - Transcendental functions:
exp,log,sqrt - Automatic differentiation via the
derivativefunction.
This library depends on:
alexandriageneric-arithmetic
These dependencies are managed by ASDF.
Clone the repository into a directory known to ASDF (e.g., ~/common-lisp/). You can then load the system using Quicklisp:
(ql:quickload :dual-numbers)You can create a dual number using the make-dual function:
(use-package :dual-numbers)
(make-dual 2 3)
;; => #<DUAL-NUMBER 2 + 3ε>
;; If the infinitesimal part is zero, a regular number is returned
(make-dual 5 0)
;; => 5The library uses generic-arithmetic to overload the standard arithmetic functions.
(let ((d1 (make-dual 2 1))
(d2 (make-dual 3 4)))
(+ d1 d2))
;; => #<DUAL-NUMBER 5 + 5ε>
(* d1 d2)
;; => #<DUAL-NUMBER 6 + 11ε>The core feature of this library is automatic differentiation. The derivative function is a curried function that takes a function and returns a new function that computes the derivative.
(use-package :dual-numbers)
;; The derivative of x^2 at x=3 is 2*3=6
(funcall (derivative (lambda (x) (* x x))) 3)
;; => 6
;; The derivative of sin(x) at x=pi is cos(pi)=-1
(funcall (derivative #'sin) pi)
;; => -1.0
;; The derivative of exp(x) at x=0 is exp(0)=1
(funcall (derivative #'exp) 0)
;; => 1.0-
make-dual standard infinitesimalCreates a dual number. -
standard-part numberReturns the standard (real) part of a dual number. -
infinitesimal-part numberReturns the infinitesimal (dual) part of a dual number. -
derivative functionReturns a new function that computes the derivative offunction.
The tests can be run by loading the dual-numbers-tests.lisp file in a Common Lisp implementation.
(load "dual-numbers-tests.lisp")This will run the test suite and print "All tests passed!" if successful.
This library is released under the MIT License.