0.0
No commit activity in last 3 years
No release in over 3 years
A Ruby implementation of dual numbers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 3.4
 Project Readme

dual_number

This library provides a Ruby implementation of dual numbers, which are useful for forward mode automatic differentiation.

It is intended to accompany the article “Automatic differentiation in Ruby”.

That article explains the details, but here’s a brief demonstration:

$ irb -Ilib
>> require 'dual_number'
=> true

>> x = DualNumber(1, 2)
=> (1+2ε)

>> y = DualNumber(3, 4)
=> (3+4ε)

>> x + y
=> (4+6ε)

>> x * y
=> (3+10ε)

>> (x + 3) * 4
=> (16+8ε)

>> 3 + (4 * x)
=> (7+8ε)

One application of dual numbers is to use the second (“dual”) component to represent the derivative of the first (“real”) component. This lets us find the derivative of a function at a particular value by just passing in a dual number instead of a normal number:

>> def distance(time:)
     time * Math.sin(time * time) + 1
   end
=> :distance

>> value_and_derivative = distance(time: DualNumber(3, 1))
=> (2.2363554557252696-15.988226228682427ε)

>> value_and_derivative.real
=> 2.2363554557252696

>> value_and_derivative.dual
=> -15.988226228682427

If you have any questions, please get in touch via Twitter or email. If you find any bugs or other problems with the code, please open an issue.