def grad(f, basis, for_numerical=True):
"""
Compute the symbolic gradient of a vector-valued function with respect to a
basis.
Parameters
----------
f : 1D array_like of sympy Expressions
The vector-valued function to compute the gradient of.
basis : 1D array_like of sympy symbols
The basis symbols to compute the gradient with respect to.
for_numerical : bool, optional
A placeholder for the option of numerically computing the gradient.
Returns
-------
grad : 2D array_like of sympy Expressions
The symbolic gradient.
"""
if hasattr(f, '__len__'): # as of version 1.1.1, Array isn't supported
f = sp.Matrix(f)
return f.__class__([
[
sp.diff(f[x], basis[y])
if not for_numerical or not f[x].has(sp.sign(basis[y])) else 0
for y in range(len(basis))
] for x in range(len(f))
])
评论列表
文章目录