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.3.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a687bd45f1023477ee2a0aa842447f4d8df87eeb8db029beb72793ff2913b29 |
|
MD5 | f66db1de407b2603f654e6691881c595 |
|
BLAKE2b-256 | 719b12eff619668ff2f98067ba0baf8274f8ef0f564c68c3d00ece3ee4e2f2ab |
Hashes for hyperjet-1.3.0-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 417b6605cafcf71743b61b8b7f4a01e4639a2d7ea3b7db6b0203a0e2cdf0cead |
|
MD5 | 05f977cbfdf4510079956feffe4ffd12 |
|
BLAKE2b-256 | 76bdc2036fa59c751e052bb0a4689c3822d92703720a29a4ed17c1d3abd5ee77 |
Hashes for hyperjet-1.3.0-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18d16cb439b12290da20c400d30e7068d7367965e4ce28c4e90a7155da98c890 |
|
MD5 | a36348e91c50e250edb00903f695a897 |
|
BLAKE2b-256 | 99956315058783cf191ebb021acb276fcbfc53b4f27a038d536e6db688c90b1c |
Hashes for hyperjet-1.3.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24a1ff3812f71760b59b442bc2bbdf033ccd463a9ae8fab6d7c20eb4df1f7174 |
|
MD5 | 11fde9d5454fd15b98b03254c6a107a5 |
|
BLAKE2b-256 | 6f5ad5a1d2d3f8e3a709b73743d5fe7f9739a4022a78956c63b549f14e9273e7 |
Hashes for hyperjet-1.3.0-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f5982d67e78a011efe7158ab06d760779589302487dbc52cf4d423caa5d2acf |
|
MD5 | 7b2216f95869c604fa740387f7436950 |
|
BLAKE2b-256 | 6fbbd0a4c6f79f2abcad17726ca7179f7b069e0dabe34bfb75b8b2528053a214 |
Hashes for hyperjet-1.3.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb685f7c51f7dea439b4ea05e190d00d49e60e0f048c4566413782cfd7b3f592 |
|
MD5 | b7a0900f85cef463422c6840fc615c25 |
|
BLAKE2b-256 | 88c7875b0df5e700aa95c90934ffa4d5332dd75b622c405ce357eccccfaab9e2 |
Hashes for hyperjet-1.3.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6104e7dfb7cf5084dd8eb3e0f5e3cb57a7ee40f269949e146bc2ddea648ada3 |
|
MD5 | a8f7a7ada1356512378c774612c1ca35 |
|
BLAKE2b-256 | 87868df900667e50f54c6b3d06843ec3fc32bc77cd6b69f2aa80e3da8e1be253 |
Hashes for hyperjet-1.3.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f1d843152557b3eb5bcd29bf7b22df5b62ec3e65e3ebe643039deca40ce9687 |
|
MD5 | f01eb77b5f83e179e22e69d9234781f3 |
|
BLAKE2b-256 | 8af117552a0b0373a7f13c8089602b5ed1c248d1670b2e6e3154fd5e8560ce05 |
Hashes for hyperjet-1.3.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b480e72fb9db065c226fad06d0b7c937bf408f6412330f003048ddbdc0ac81c9 |
|
MD5 | b37c71a486210b583cf8036d42f997da |
|
BLAKE2b-256 | 91a8e258d4c57cc055381e2cb7d7da66d15eb8073eeefafaa4ef141393c70577 |
Hashes for hyperjet-1.3.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ca954cb9cdcbbd288a45db2fafcdee0a20f40f896cd50e36149dd187cf3e9b0 |
|
MD5 | 7e98b8887716419ec44e4f6263364249 |
|
BLAKE2b-256 | f19b2c8ce89cdf695d9d352edac3462297721622d2b63b151fafd1aaeb54356c |
Hashes for hyperjet-1.3.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c0adec1d0d627f0d31fae5f8ca0aa6919f55a0a8224a46cd43920c7f1056661 |
|
MD5 | 11d44fcde31a9240e407db24ca5d89e9 |
|
BLAKE2b-256 | b059b348082efb5321537ae21bfb3ab00e7ee3a70ad5e983ba4b9b653a4dd309 |
Hashes for hyperjet-1.3.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23eb23a33ccfcf91e78826d8684e3bbd039f291c4996797f379179e09fe48af6 |
|
MD5 | b91fef99078db2679d7995f7eb4b3d41 |
|
BLAKE2b-256 | 21f3f208f4c57c41118f5fcbf17db05a8d3f835d79da0118fad48b67313577b9 |