This project provides a basic implementation of dual numbers in Python with an example application of dual numbers for automatic differentiation. Possibly interesting for educational purposes.
The dual numbers system was introduced 1873 by the English mathematician William Clifford.
Dual numbers are of the form
The arithmetic operations for dual numbers are defined as follows.
| Operation | |
|---|---|
| Addition | |
| Subtraction | |
| Multiplication | |
| Division |
Any real function can be extended to the dual numbers.To see this, we can employ the Taylor series given by:
We are interested in the behavior of a real function extendend by dual numbers. Therefore, we set
By the definition of
This property is especially interesting for automatic differentiation.
We can use this expression to extend functions such as hyperbolic or power functions to the dual numbers.
We can also extend activation functions such as
and thus it follows for dual numbers
We ca use the expressoin
For this to work we use the Taylor series for two variables up to the first order:
Again we won't consider higher order terms as these become zero since
Now we have everything to extend
Thus, for the actual implementation in Python, the following cases for exponentiation involving dual numbers are of interest.
As you can see, this expression can also be used to compute quantities where
from dualnumber import Dual
d1 = Dual(1, 2)
d2 = Dual(3, 4)
d3 = d1 + d2
d4 = d3 - d2
d5 = d4 * d3
d6 = d5 / d4
d7 = d6.sin()
d8 = d7.cos()
d9 = d9.tanh()
d10 = d9.ln()
d11 = d10.exp()
d12 = d11**d10
d12 = d11.relu()Dual numbers are great for automatic differentiation. Here is a toy example showing gradient descent for
Run the example
cd PyDualNumber
python -m examples.gradient_descentRun the following command to install this package in your environment:
cd PyDualNumber
pip install .Install required packages to run the example, tests, and to facilitate development according to clean code principles:
cd PyDualNumber
pip install -r requirements.txtRun the tests by executing:
cd PyDualNumber
pytest dual_numberSome good practices for software developement in Python.
Install static type checker mypy:
pip install mypyIgnore false positives by adding a marker as a comment:
a = "bla" # type: ignoreCheck type consistency by running:
mypy dual_numberInstall pylint to check for generic code structure:
pip install pylintCheck code structure by running:
pylint dual_numberOr run pylint with a customiced pylintrc file:
pylint --rcfile dual_number/pylintrc dual_numberThis project uses flake8 and black for automatic formatting.
Install flake8 for full flexibility and configurability and black for uncompromising and deterministic code formatting:
pip install flake8
pip install blackRun tools for automatic formatting:
flake8 dual_number
black --check dual_numberRemove the --check flag to perform automatic formatting changes.
Instead of running all checks manually we can make use of Makefiles to run them all automatically. Run the following command in the root folder:
cd dual_number
make checkThis check will automatically fail if at least one check fails. To run all checks ignoring errors by running:
cd dual_number
make --ignore-errors check # or: make -i checkIf you find this content useful, please cite the following:
@misc{KaiFischer2022pdn,
author = {Fischer, Kai},
title = {PyDualNumber},
year = {2022},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/kaifishr/PyDualNumber}},
}MIT
