GitHub - auto-differentiation/xad-py: High-Performance Automatic Differentiation for Python (original) (raw)

Python

XAD is a library designed forautomatic differentiation, aimed at both beginners and advanced users. It is intended for use in production environments, emphasizing performance and ease of use. The library facilitates the computation of derivatives within computer programs, making the process efficient and straightforward for a wide range of mathematical functions, from simple arithmetic to complex calculations, ensuring accurate and automatic derivative computations.

The Python bindings for XAD offer the following features:

For more details and to integrate XAD into your projects, consult the comprehensive documentation.

Application Areas

Automatic differentiation has many application areas, for example:

Getting Started

Install:

Calculate first-order derivatives in adjoint mode:

import xad.adj_1st as xadj

set independent variables

x0_ad = xadj.Real(1.0) x1_ad = xadj.Real(1.5) x2_ad = xadj.Real(1.3) x3_ad = xadj.Real(1.2)

with xadj.Tape() as tape: # and register them tape.registerInput(x0_ad) tape.registerInput(x1_ad) tape.registerInput(x2_ad) tape.registerInput(x3_ad)

# start recording derivatives
tape.newRecording()

# calculate the output
y = x0_ad + x1_ad - x2_ad * x3_ad

# register and seed adjoint of output
tape.registerOutput(y)
y.derivative = 1.0

# compute all other adjoints
tape.computeAdjoints()

# output results
print(f"y = {y}")
print(f"first order derivatives:\n")
print(f"dy/dx0 = {x0_ad.derivative}")
print(f"dy/dx1 = {x1_ad.derivative}")
print(f"dy/dx2 = {x2_ad.derivative}")
print(f"dy/dx3 = {x3_ad.derivative}")

For more information, see the Documentation.