Skip to main content

Automatic differentiation with dual numbers

Project description

HyperJet

HyperJet — Algorithmic Differentiation with Hyper-Dual numbers for Python and C++


A header-only library for algorithmic differentiation with hyper-dual numbers. Written in C++17 with an extensive Python interface.

Build Status PyPI PyPI - License PyPI - Python Version PyPI - Format

Installation

pip install hyperjet

Quickstart

Import the module:

import hyperjet as hj

Create a set of variables e.g. x=3 and y=6:

x, y = hj.DDScalar.variables([3, 6])

x and y are hyper-dual numbers. This is indicated by the postfix hj:

x
>>> 3hj

Get the value as a simple float:

x.f
>>> 3

The hyper-dual number stores the derivatives as a numpy array.

Get the first order derivatives (Gradient) of a hyper-dual number:

x.g  # = [dx/dx, dx/dy]
>>> array([1., 0.])

Get the second order derivatives (Hessian matrix):

x.hm()  # = [[d^2 x/ dx**2 , d^2 x/(dx*dy)],
        #    [d^2 x/(dx*dy), d^2 x/ dy**2 ]]
>>> array([[0., 0.],
           [0., 0.]])

For a simple variable these derivatives are trivial.

Now do some computations:

f = (x * y) / (x - y)
f
>>> -6hj

The result is again a hyper-dual number.

Get the first order derivatives of f with respect to x and y:

f.g  # = [df/dx, df/dy]
>>> array([-4.,  1.])

Get the second order derivatives of f:

f.hm()  # = [[d^2 f/ dx**2 , d^2 f/(dx*dy)],
        #    [d^2 f/(dx*dy), d^2 f/ dy**2 ]]
>>> array([[-2.66666667,  1.33333333],
           [ 1.33333333, -0.66666667]])

You can use numpy to perform vector and matrix operations.

Compute the nomalized cross product of two vectors u = [1, 2, 2] and v = [4, 1, -1] with hyper-dual numbers:

import numpy as np

variables = hj.DDScalar.variables([1, 2,  2,
                                   4, 1, -1])

u = np.array(variables[:3])  # = [1, 2, 2]
v = np.array(variables[3:])  # = [4, 1, -1]

normal = np.cross(u, v)
normal /= np.linalg.norm(normal)
normal
>>> array([-0.331042hj, 0.744845hj, -0.579324hj], dtype=object)

The result is a three-dimensional numpy array containing hyper-dual numbers.

Get the value and derivatives of the x-component:

normal[0].f
>>> -0.3310423554409472

normal[0].g
>>> array([ 0.00453483, -0.01020336,  0.00793595,  0.07255723, -0.16325376, 0.12697515])

normal[0].hm()
>>> array([[ 0.00434846, -0.01091775,  0.00647611, -0.0029818 , -0.01143025, -0.02335746],
           [-0.01091775,  0.02711578, -0.01655522,  0.00444165,  0.03081974, 0.04858632],
           [ 0.00647611, -0.01655522,  0.0093492 , -0.00295074, -0.02510461, -0.03690759],
           [-0.0029818 ,  0.00444165, -0.00295074, -0.02956956,  0.03025289, -0.01546811],
           [-0.01143025,  0.03081974, -0.02510461,  0.03025289,  0.01355789, -0.02868433],
           [-0.02335746,  0.04858632, -0.03690759, -0.01546811, -0.02868433, 0.03641839]])

Reference

If you use HyperJet, please refer to the official GitHub repository:

@misc{HyperJet,
  author = "Thomas Oberbichler",
  title = "HyperJet",
  howpublished = "\url{http://github.com/oberbichler/HyperJet}",
}

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

hyperjet-1.1.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distributions

hyperjet-1.1.1-cp39-cp39-win_amd64.whl (359.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

hyperjet-1.1.1-cp39-cp39-manylinux2010_x86_64.whl (420.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

hyperjet-1.1.1-cp39-cp39-macosx_10_15_x86_64.whl (551.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.15+ x86-64

hyperjet-1.1.1-cp38-cp38-win_amd64.whl (355.9 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

hyperjet-1.1.1-cp38-cp38-manylinux2010_x86_64.whl (420.1 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

hyperjet-1.1.1-cp38-cp38-macosx_10_15_x86_64.whl (551.5 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

hyperjet-1.1.1-cp37-cp37m-win_amd64.whl (349.4 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

hyperjet-1.1.1-cp37-cp37m-manylinux2010_x86_64.whl (448.1 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

hyperjet-1.1.1-cp37-cp37m-macosx_10_15_x86_64.whl (527.3 kB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

hyperjet-1.1.1-cp36-cp36m-win_amd64.whl (349.5 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

hyperjet-1.1.1-cp36-cp36m-manylinux2010_x86_64.whl (446.3 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

hyperjet-1.1.1-cp36-cp36m-macosx_10_15_x86_64.whl (527.3 kB view hashes)

Uploaded CPython 3.6m macOS 10.15+ x86-64

Supported by

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