如何在给定点找到多个变量的梯度?

How to find gradient of multiple variables at a given point?

我需要在点 [2, 4] 处找到函数 (x**2+y) 的梯度。

import numdifftools as nd
import sympy as sym
from sympy import *

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

def rosen(x, y): 
    return (x**2 + y)
grad = nd.Gradient(rosen)([2, 4])
print("Gradient of is ", grad)
TypeError: rosen() missing 1 required positional argument: 'y'

传递给函数的参数 nd.Gradient 必须是一个数组。

import numdifftools as nd

def rosen(xy): 
    return (xy[0]**2 + xy[1])

grad = nd.Gradient(rosen)([2, 4])
print("Gradient of is ", grad)