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.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.1-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88c83b862f59dd54bcd8773aa9d98a7f7e71e9e71cb4f41cdb6b871d1505546d |
|
MD5 | efbe3e9e5d3e719acb0ae06cff0616fd |
|
BLAKE2b-256 | f2b63d18d6d894163b04f42f3d5d2b5bb4fa0790bb82adffd58a966b32cde3ef |
Hashes for hyperjet-1.4.1-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12af3aaa2b230b39dc313af4492f0a0c4ff505fd8952370a2f9a24796e7028f8 |
|
MD5 | 0cb94c4bbec97e2f2a1bc7c47a4803db |
|
BLAKE2b-256 | 317249aa589cb2f6763e2eebdf0de9c325fd7cf59c9123b10d5e95e53d86556b |
Hashes for hyperjet-1.4.1-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86b4a4a45d591d061dbcf233dd454f22e223a064054b85f4256a0775be4986f1 |
|
MD5 | a9632ebb43e137bd51460d66af0619e0 |
|
BLAKE2b-256 | ebaf4e6480ecc677301d98961fdd83e0920571d4e1c847769f48b3bf1239adfa |
Hashes for hyperjet-1.4.1-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9702871955856a52f2a40aa869dbd5215d2d0b97db040234d059294cb003ce64 |
|
MD5 | 7313f50f7d5cbf5f63e727e59b8e8bda |
|
BLAKE2b-256 | b83f3000624f9cd858f665bd96e11cac2ede9790d34ac5eb2e11f7673f51b987 |
Hashes for hyperjet-1.4.1-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc087b5c9059b639ac5cacdfbcf040f0f8094e5775105f4a1b2d9e556ca1ea7f |
|
MD5 | 46f7733a6c79bf780dafd7806ae97b69 |
|
BLAKE2b-256 | bd6d73393581fd166552aeb7da2d91bcc603040e457bcaa48734fe37e40fc3b5 |
Hashes for hyperjet-1.4.1-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5318f5feaf370c3c407023b7cdbedfae513034b6b5823b6a20809475c780712 |
|
MD5 | b2c41a3783968fcf632b78e28066bdea |
|
BLAKE2b-256 | 346c85416289e48b8841f388b515ef4bd284d5a4f6ef9ca85ef4e7cf94c82ae8 |
Hashes for hyperjet-1.4.1-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec5425b1f0289cfb9fdc4dd55e790baf1842a302dbb2956781fc3fdf14b35e44 |
|
MD5 | bf7cbcc8c72de2744a2c8c374535875a |
|
BLAKE2b-256 | 0e8a0e298a7c83535f40208ecdb11f43fc04c6e81aa051213f4082657c5ef9e3 |
Hashes for hyperjet-1.4.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad907643bafa59caaa8002e54786dc6f226f0d98db5b064b696dc83ededc097e |
|
MD5 | 96eae77cc6345cf43682ef4ef5421d39 |
|
BLAKE2b-256 | 164a88cd2d72f82572a5be3b479eb481c287618d71213c908d4a89b5ff1301c9 |
Hashes for hyperjet-1.4.1-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7d3d534f4009f47d411fabf140dff1979381f3b8ea192af39547817521b7871 |
|
MD5 | 7543d2ac2fb45d2216d9de5ee97bfe1c |
|
BLAKE2b-256 | 81d59d235dd76452a7b089a0474a02dcf9ae3cfb2b2125203775a644a4324700 |
Hashes for hyperjet-1.4.1-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80cb38564773531f8705c5e9a5c4771a2d7fde1b3e889cb7eca875833dd7738c |
|
MD5 | a59680a5b01284d0f9f1c77ef6eef7dc |
|
BLAKE2b-256 | e036b61e5e9c2e1ba35d735e12ef00283110c17c662206b68e0d12ca5a427ea0 |
Hashes for hyperjet-1.4.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33e55de800d7a58d89174507260cf6ef69b754a9c2842fc4d6e455ffa7a64ec0 |
|
MD5 | ebec76ac5cb072118acdc7e308610b85 |
|
BLAKE2b-256 | 4a57fdd873dba3b331786c879a20c8dc24cd40a0ec93efb263f01943646e9151 |
Hashes for hyperjet-1.4.1-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 532999b08bff9d2cfdf65e3357a69c675129d0acfd7567721c83747557aa606e |
|
MD5 | c112b399dd160717109396056b2a50d8 |
|
BLAKE2b-256 | ecba5ccdfbc42bd15597ac5fd36ed4b7be015dd188e579de3c8fe004beb25786 |