The Hessian matrix of a differentiable, scalar-valued multivariate function is a matrix of all its second partial derivatives:

is meant to be evaluated at some point

Hessian matrix tells us the curvature of the function, which includes concavity

Example

Let

  1. Calculate regular partial derivative
  1. Compute Hessian matrix

Code

import sympy as sp
import numpy as np
 
def construct_hessian(f, variables):
    """
    Constructs the Hessian matrix for a function f with respect to given variables.
 
    Parameters:
    - f: sympy expression representing the function.
    - variables: list of sympy symbols representing the variables.
 
    Returns:
    - A sympy matrix representing the Hessian.
    - A lambdified function for evaluating the Hessian numerically.
    """
    # Symbolic Hessian matrix
    hessian_matrix = sp.Matrix([[sp.diff(f, var1, var2) for var1 in variables] for var2 in variables])
    
    # Lambdify the Hessian matrix for numerical evaluation
    hessian_lambdified = sp.lambdify(variables, hessian_matrix, 'numpy')
    
    return hessian_matrix, hessian_lambdified
 
# Example usage
n = 3  # Number of variables
variables = sp.symbols(f'x1:{n+1}')  # Symbols x1, x2, ..., xn
f = sum(x**2 for x in variables) + variables[0]*variables[1] + sp.sin(variables[2])
hessian_sympy, hessian_numeric = construct_hessian(f, variables)
 
# Display the symbolic Hessian matrix
print("Symbolic Hessian Matrix:")
sp.pprint(hessian_sympy)
 
# Evaluate the Hessian at the given point
point = np.array([1.0, 2.0, 0.5])
hessian_value = hessian_numeric(*point)
print("\nHessian evaluated at point", point, ":")
print(np.array(hessian_value, dtype=float))