Skip to main content

A Python package for forward-mode automatic differentiation using dual numbers.

Project description

dual_autodiff

dual_autodiff is a Python package that provides automatic differentiation using dual numbers. It is designed to handle derivatives and mathematical operations efficiently. The package also includes dual_autodiff_x, a Cython-optimized version for enhanced performance.


Features

  • Automatic differentiation using dual numbers.
  • Support for a wide range of mathematical funtions:
  • Cython-optimized version (dual_autodiff_x) for improved speed.

Installation

Prerequisites

  • Python 3.9 or higher.
  • pip for package installation.
  • conda (optional) for managing virtual environments.

Installing dual_autodiff

Using pip

Clone the repository and install the package:

git clone 'https://gitlab.developers.cam.ac.uk/phy/data-intensive-science-mphil/assessments/c1_coursework1/rsr45.git'
cd dual_autodiff
pip install e .

Using conda

  1. Create a virtual environment using the provided environment.yaml file:
conda env create -n dual_env -f environment.yaml
  1. Activate the environment:
conda activate dual_env
  1. Install the package within this environment:
pip install -e .

Usage

Using dual_autodiff

from dual_autodiff import Dual, sin, cos

# Create a Dual object
x = Dual(1.0, 1.0)  

# Perform operations
y = sin(x)
print(f"Value: {y.real}, Derivative: {y.dual}")

Available Functions

Dual Class Methods

The following methods are available in the Dual class:

Arithmetic Operations

  • __add__, __radd__ - Addition
  • __sub__, __rsub__ - Subtraction
  • __mul__, __rmul__ - Multiplication
  • __truediv__, __rtruediv__ - Division
  • __pow__ - Power

Trigonometric Functions

  • sin() - Sine
  • cos() - Cosine
  • tan() - Tangent
  • asin() - Arcsine
  • acos() - Arccosine
  • atan() - Arctangent

Exponential and Logarithmic Functions

  • exp() - Exponential
  • log() - Natural Logarithm
  • sqrt() - Square Root

Hyperbolic Functions

  • sinh() - Hyperbolic Sine
  • cosh() - Hyperbolic Cosine
  • tanh() - Hyperbolic Tangent

Global Functions in functions.py

These functions are global aliases for the corresponding Dual class methods, allowing them to be called directly with either float or Dual inputs:

Trigonometric Functions

  • sin
  • cos
  • tan
  • asin
  • acos
  • atan

Exponential and Logarithmic Functions

  • exp
  • log
  • sqrt

Hyperbolic Functions

  • sinh
  • cosh
  • tanh

How It Works

Dual Numbers

Dual numbers are numbers of the form:

[ a + b $\epsilon$ ]

Where:

  • a is the real part, representing the function value.
  • b is the dual part, representing the derivative.
  • $\epsilon$^2 = 0, making (\epsilon) infinitesimally small.

This structure allows for efficient computation of derivatives during operations.


Cython Optimization

The dual_autodiff_x package uses Cython to compile critical operations into C, providing a significant speed boost for computationally intensive tasks.

Installing dual_autodiff_x

For the Cython-optimized version:

cd dual_autodiff_x
pip install e .

See the README.md file in dual_autodiff_x.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rsr45_dual_autodiff-0.0.post33.tar.gz (2.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rsr45_dual_autodiff-0.0.post33-py3-none-any.whl (2.2 MB view details)

Uploaded Python 3

File details

Details for the file rsr45_dual_autodiff-0.0.post33.tar.gz.

File metadata

File hashes

Hashes for rsr45_dual_autodiff-0.0.post33.tar.gz
Algorithm Hash digest
SHA256 755a29c1b0ef7eba546ff29eed608a607af167e73d468d2e9c843cbaa814c4a9
MD5 2d38197867e8e5e8eb256bb34e7117ea
BLAKE2b-256 f80703788d6d23817c5e991fa69ab0e3e19c55a45989e9a04fa4f1cf10657c79

See more details on using hashes here.

File details

Details for the file rsr45_dual_autodiff-0.0.post33-py3-none-any.whl.

File metadata

File hashes

Hashes for rsr45_dual_autodiff-0.0.post33-py3-none-any.whl
Algorithm Hash digest
SHA256 af38c122758605fc5c981606fe50c2a8535c795af0d9401f318d8e120d75ca9e
MD5 51bb9909d0783c189e6894beb4da7557
BLAKE2b-256 a8000249a194ca9934c65383d1628de4a9cc3f5d494376078b047c9f75608a23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page