Automatic differentiation with dual numbers
Project description
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.
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]) # = [1hj, 2hj, 2hj]
v = np.array(variables[3:]) # = [4hj, 1hj, -1hj]
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for hyperjet-1.2.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2aaaec9a91369ddad6a93b2849373990d3f99bd3fe08d850a28663640d3005b2 |
|
MD5 | 0829c0067296371774b3d12f0951c302 |
|
BLAKE2b-256 | ad0d42277bedfada8b658c447a6373dbe61d00da87e5b7b193ea74f19be7d45d |
Hashes for hyperjet-1.2.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f83a189ad5a2d8b96c588619a7de82bc14b65981cf2dabc8c7276e2b003cba1 |
|
MD5 | 59b1671670859ce04c7ca98f15c097a0 |
|
BLAKE2b-256 | 45caa7c6823d3608f9999aa20624d7ae46b8be3ec8828dd0e1cf5ea43641c566 |
Hashes for hyperjet-1.2.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a50ca89ba923e9331fa2af6c31ee7e56838ab0e1fed484c8b96ad2a50ae9a86 |
|
MD5 | 4f82227abd75730f444722f14a8a5cbf |
|
BLAKE2b-256 | 8733b052b8f5c569de233d00db5f2095d9b0ff6c1a994878ca42c5dbb1ccacbe |
Hashes for hyperjet-1.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb597df0b89798ae03a66bf87b6e8a840c5bb5ea73fdc87cc78c070671efa388 |
|
MD5 | 3b04dd347ba8b96ef4bc543f0798dc68 |
|
BLAKE2b-256 | 2cabd15178c6c879d4a94bb835608d068178498fb6ec487be30772715e723e87 |
Hashes for hyperjet-1.2.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d982ba54081b97e2bf34ce8816085d648b2da0b7c1853de4d585d9574a8620b4 |
|
MD5 | fe1e0ddbf7a278b3deacb77063177a5f |
|
BLAKE2b-256 | a52c013d4a53aa3cca7b6c18beb777133921778b545740ee76a7352bb894086e |
Hashes for hyperjet-1.2.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 314bf0f399fea0c7149f01a06cc84aae0e5dcac161b42becc6fbe1d1163c3873 |
|
MD5 | aaeaa147e59fdede3444c987df8de02b |
|
BLAKE2b-256 | cd2c25e05e5c6984e387ca3c93485e02621357ebdc5dc199bac64de4df8c06da |
Hashes for hyperjet-1.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26c141e52a1069889c36e745ad42389b81ce04667e9475070b1181cd7869382e |
|
MD5 | 0d40bcaef1de41fd90d8413dbfea9e36 |
|
BLAKE2b-256 | 7b8997784537dec6fcb3dba0a1267e95728fa770da82fc43e101f004d332eae9 |
Hashes for hyperjet-1.2.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f42b1e1c4023002c121c8fc2d841ed62fafa6030fc0c348ed560a5229ee250bb |
|
MD5 | 7e448a709478558ef1cf87b1d9713a9b |
|
BLAKE2b-256 | 4012b23ae5f317ef9d3c0c1ef510533a2fb1ca0fba56eda006c53a78c33e5ce6 |
Hashes for hyperjet-1.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 595fce93eb764cf4bff3b196559dc17dbff4666fd04a89c25ec2b1dcebc109db |
|
MD5 | 91376824171060e049e19d62f300ab00 |
|
BLAKE2b-256 | 30cc16cee3e3b7a250205ed92c54ba8042c0ced52b04eafc98d40267f13fc843 |
Hashes for hyperjet-1.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b27aa6b3bcf1167dd10b5324c9485cc690f9fc609a491c0e97636e2cb24c67c5 |
|
MD5 | bddda50454aca29b67d209dd5c9088b8 |
|
BLAKE2b-256 | e9e9b3afd34d9592c89e3db104d8dba4cf8f2c6c60b75751a4b624878a588cef |
Hashes for hyperjet-1.2.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c4aa66d2699fa2fa3c35a46fc8d00e92b82c3cc9013daf28acadb9d37bc34e9f |
|
MD5 | 6b983744a7eadaa7e5d406e2e72d23c7 |
|
BLAKE2b-256 | 3e231b463b38404bf0115e008ba5bd970f143bd02dd4cce5651570e10f2a76ca |
Hashes for hyperjet-1.2.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1feda104212286d2eaec0aadd692f80b3dc3e71529276d415f5652d4544cf8fe |
|
MD5 | fe14772dc56c9084b131ee6d88a07625 |
|
BLAKE2b-256 | a095241815cd1da6a4390f521073be40715ee03384fb907559915c1c4a611f53 |