Numdifftools

0.9.42 · active · verified Sat Apr 11

The numdifftools library is a suite of tools written in Python to solve automatic numerical differentiation problems in one or more variables. It employs finite differences in an adaptive manner, coupled with a Richardson extrapolation methodology, to provide maximally accurate results. The library is currently at version 0.9.42 and sees active, though not rapid, development, with the documentation for 0.9.41 updated in late 2025 and PyPI showing an update for 0.9.42 in early 2022.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compute the first and second derivatives of a scalar function, and the gradient of a multivariate function using `numdifftools.Derivative` and `numdifftools.Gradient`.

import numpy as np
import numdifftools as nd

def f(x):
    return x**3 + x**2

# Compute the 1st derivative of f(x) at x=1
df = nd.Derivative(f)
result_df = df(1)
print(f"First derivative of f(x) at x=1: {result_df}")

# Compute the 2nd derivative of f(x) at x=1
ddf = nd.Derivative(f, n=2)
result_ddf = ddf(1)
print(f"Second derivative of f(x) at x=1: {result_ddf}")

# Compute the gradient of a multivariate function
def rosen(x):
    return (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2

grad = nd.Gradient(rosen)
result_grad = grad([1, 1]) # At the minimum of Rosenbrock function
print(f"Gradient of Rosenbrock at (1,1): {result_grad}")

view raw JSON →