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.4.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a90670325c5df5f6da591c16236a560762708413353cc933c1af40d8e8fbddd5 |
|
MD5 | d7da35c7497e5aeb24459d3249f8590b |
|
BLAKE2b-256 | 09546ad3e84dada5c60752683a7495e600a64f5b29a6d04c4b81a3afa9f0ea41 |
Hashes for hyperjet-1.4.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2f358a4322f961cdae9af245f1684fcc096d07dc0f9a5ab8f0aaf004423c83a |
|
MD5 | 46640da3dd7e52e905f1d0d7a5ec8a55 |
|
BLAKE2b-256 | 664f7fe837aafe41120af25dfbe503dfa497ca9713b5f92e4335c704db14bd42 |
Hashes for hyperjet-1.4.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b081fd8a6ab3ea571f57f3f1711022b0dcca1dc8a0b1b523a8b021db84b792d8 |
|
MD5 | 6cb4ad00eaf5a9b6c58d75b292786547 |
|
BLAKE2b-256 | 34ab439567bff221b7e5a3745fe801207f1cbdeb3518b4adaa1cdba19438b22a |
Hashes for hyperjet-1.4.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbdd133854428bd77b3bafc4e9bfccf932cc3af020d0ae35573b538123a72bc6 |
|
MD5 | fa6ac4f1671f08d6ff2da6cc8a23309d |
|
BLAKE2b-256 | b7e24dace8f0c4c42ad86c54b49bc7b53e689a97d4cbd552a2ba74bc7bfd36b7 |
Hashes for hyperjet-1.4.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e3be70ccca80b5cd77501ec8cd08b06da29f5bb4d0e81b32491ae9ed5be8b53 |
|
MD5 | a1a6410134d401263a62c38e2e050a19 |
|
BLAKE2b-256 | e014737665ce2784870228ca85e2d1c0cdaaa3436345c8ec6351e75ce4cd2051 |
Hashes for hyperjet-1.4.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46117689454e4a35dc395e9e6cfd63501a62cee34c5236fdb73fb2e003055378 |
|
MD5 | 978b4aa76a3691067b27ced02c8b5335 |
|
BLAKE2b-256 | 4ba2947b16608c8f69e98314a2706357859e5c53c0a003c1247f31990904872c |
Hashes for hyperjet-1.4.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b8ce69302abb4dc0214b7ee381f939dbe6079f606c01eedb3f4b48168dd19af |
|
MD5 | 6b6bd4fe713e281a678bd568fd5b4967 |
|
BLAKE2b-256 | f7a2effe8632c83ff003d22983db20bd749eefb9d5330b42ef6d0c4872663b57 |
Hashes for hyperjet-1.4.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22605f2ca48ad7be42ed06810efdd5354e1d52f34e78c058d80d857a3ebd4cce |
|
MD5 | 7026d9a16a960c1f9472ee70b73e12b3 |
|
BLAKE2b-256 | adecff74d8658279b6ab5dacb2925f87a623edbbe90aba1d0dd408a23a419a79 |
Hashes for hyperjet-1.4.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ff7a41f27ad77add802ef65b910efc69c73fad603abfacfdfa440eb370b1d97 |
|
MD5 | f93ed2126616c96eafd54e930620ef3d |
|
BLAKE2b-256 | bbde8b8dc3bf98f721c2a7550d741e174181b258c3811c4d4c1be0a50d227365 |
Hashes for hyperjet-1.4.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b28f3242fde4a53d4c9735525f125467372e801f3a560d0a858248c916aa81c |
|
MD5 | 4ed4a1105731899d44519598cd41b7b8 |
|
BLAKE2b-256 | 0cf2bed1a13e577d86e047c12a07a0218bfe9bfe2983be2bf1f2026d3f02b07e |
Hashes for hyperjet-1.4.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37d68b115d13d1c292ece96e2951adade8acc41b349055014f1cba790e1996d1 |
|
MD5 | befda7e662d73500585bd2cb50560088 |
|
BLAKE2b-256 | f078bcf2bc06517cbc425014f8b8449a24f357a6b9bdb77d0e84a13dec8a315f |
Hashes for hyperjet-1.4.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bdc50d3c2c839a6188197e4b945e428d1579f1c661dd2c8966080e12368a3a20 |
|
MD5 | f52c182ab5affcc01b593661d05f6736 |
|
BLAKE2b-256 | d87444a609e310b872d2281ab16557719d7ffb8e16623dba1b00405f9ed02a7e |