Skip to content
DeepForgeby svx
All problems
HardCalculusca-005

Hessian Matrix

Problem

Compute the Hessian matrix of f(x) = sum(x_i^2) at a given point using second-order central differences (h = 1e-3).

H is n x n where: H[i][i] = (f(x + h*e_i) - 2*f(x) + f(x - h*e_i)) / h^2 H[i][j] = (f(x+h*e_i+h*e_j) - f(x+h*e_i-h*e_j) - f(x-h*e_i+h*e_j) + f(x-h*e_i-h*e_j)) / (4 h^2)

Hint

For diagonal: second difference. For off-diagonal: 4-point cross stencil.

Test cases

case 1
in: [[1,2,3]]
expected: [[2,0,0],[0,2,0],[0,0,2]]
case 2
in: [[0,0]]
expected: [[2,0],[0,2]]
case 3
in: [[5]]
expected: [[2]]
Starter codePython
def hessian(point, h=1e-3):
    # f(x) = sum(x_i ** 2)
    # Returns n x n Hessian
    # Your code here
    pass

3 test cases · run your code against them in the editor