Snippet: Symbolic Gradient of a function using sympy

sympy does not have a method to calculate the gradient, but as the gradient is the jacobian of one function, we can calculate the gradient of a function f with

from sympy import *

x, y = symbols('x, y')

f = 8 * x**2 + 4 * y ** 2 - 9

Df = Matrix([f]).jacobian(Matrix(list(f.free_symbols)))