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]) # = [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
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.1.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cff8ceb77c14eaed6937092b97cabfca7c3f46ca528512c3131317b507dc5802 |
|
MD5 | ab6d6e7fe36bc8c77e3527d4005118f7 |
|
BLAKE2b-256 | b84ecc0573b34e736625ea1bdbed630ff8b8992e566e827284db3aef7e29c157 |
Hashes for hyperjet-1.1.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3bedef4697b3cd64d855884bbfdae1e2d7bc6037692609a7ce2d826acafa942 |
|
MD5 | 3c36634a30b0a157e7a418ab5506bfb0 |
|
BLAKE2b-256 | 34b872d230651818bf88fe20e922614dd2073d5b65ac5ff3abc0aa9afda44af0 |
Hashes for hyperjet-1.1.1-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 180bbc9f914cf44979cf3ccbca45ebde84a7e4e39b65b3a14537281f347ecf1d |
|
MD5 | 6beaa920408d10febd8183f3ecd58358 |
|
BLAKE2b-256 | d645d4542c9cf5a8d28e549d487175a2de82b0b27119933c93d4b4892edb5589 |
Hashes for hyperjet-1.1.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed4c9697ab589a3958c8021993d75875828937b2094506d0af60643fceb42e2b |
|
MD5 | 99f5eb4207a252bd657cc1b265244417 |
|
BLAKE2b-256 | 52cf085c39bb7b8478cb3e0fedbf5b98f5a16b1a345f229ede3d4cf70750496f |
Hashes for hyperjet-1.1.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ee9b646496b234aa1847de7f0d00437477bb161894f3860d95ad2b9e0920bb4 |
|
MD5 | 9399dcc4dc18c2bc7a9a8649ab2e643a |
|
BLAKE2b-256 | 5c8cd402f84de1506af664264211676f06c8206c0d1c9bdc402c22fc15df7713 |
Hashes for hyperjet-1.1.1-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3733aa209ea81245d89a7c1f9fdaff85eca803a7a1cd9ca365fdaa6a8574169 |
|
MD5 | 5b769ea397167f003fc1d350467d0cb0 |
|
BLAKE2b-256 | 5faca5c166e62306f16b6dc772fbd334eb6138330a5d658ea261888f3d464883 |
Hashes for hyperjet-1.1.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa5f32f2874c345bf07b3356916a67a76996f46c81be1ff2aa9aef97ac99cd77 |
|
MD5 | 4fc1f6776bced53ad1a0a40b4fa37c0f |
|
BLAKE2b-256 | 3ed6c58e6fdc9d05e727f9a1695d7839d18c42f9e97d92d9abee2ea0163df86b |
Hashes for hyperjet-1.1.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cbc4d33f3c337f3a83dc9d9ede08b8d4850285124d59a6477c9c8bb97baf38f |
|
MD5 | 240908c568c92ec8fb36445a4e71cd18 |
|
BLAKE2b-256 | 6ef19cb737b2348de35128ae14bfcbc4d05dee482763a162331c7f800e5e3911 |
Hashes for hyperjet-1.1.1-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bd398fc6e45188a58b7aec59d630d0c2fc94ac8a400aeb5bbc6a4039c6f2322 |
|
MD5 | 955868e15388a5f5065ea7f497fd43a7 |
|
BLAKE2b-256 | 642a5e92a0ac33329707e6082a33b77b5c9aeb0b602a1d87efed8ff7b138151b |
Hashes for hyperjet-1.1.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb85f2270945e46078ac9977a804dadd898872da38884dc4a9b3184391039c6b |
|
MD5 | b283830b09b9904064c5be8c4c667daa |
|
BLAKE2b-256 | c1cc26642f691c3e3cc6c846578db79334aff486918311063123d40b1516d7e2 |
Hashes for hyperjet-1.1.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db2770f700b74075a77d6d33045fe6c573dea2a61199996a59795a3161722f97 |
|
MD5 | 0bf4d004c22dac4cd84e56a2deaba758 |
|
BLAKE2b-256 | 0c3567e765e992a2f212dd9dd2c43fffa845c10882ec96240e4857d8e235ee16 |
Hashes for hyperjet-1.1.1-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f9a17277453ff28c8bf9491b8cbab0b538de345835f9bf244e43a89f4dc5bd7 |
|
MD5 | 8e56cf018cd6955b6ef8ef0b52385086 |
|
BLAKE2b-256 | 43b2fea8f04fa128b690d582185168c6656e2b42554f66ba4dfab520b0139dfd |