How to compute the gradient and Hessian of a vector field symbolically with sympy.

32 views
Skip to first unread message

Isaque Soares

unread,
Jul 3, 2020, 7:24:44 PM7/3/20
to sympy
Is there a way to compute the gradient and hessian matrices of a vector field like u = (x) i + (2yz) j + (3xy) k.

Faisal Riyaz

unread,
Jul 4, 2020, 3:35:24 AM7/4/20
to sy...@googlegroups.com
Hi Isaque,

The vector module has a gradient function but it works only for scalar fields. There is an implementation given
in this issue. Unfortunately, it has not been merged.

The hessian function in the matrices module also works for scalars only. You will have to iterate over each component
separately.

Thanks
Faisal

On Sat, Jul 4, 2020 at 4:54 AM Isaque Soares <isaque...@gmail.com> wrote:
Is there a way to compute the gradient and hessian matrices of a vector field like u = (x) i + (2yz) j + (3xy) k.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/ba1b7af0-fd4b-4b67-879a-59a0b3ffbc15o%40googlegroups.com.

Kasper Peeters

unread,
Jul 4, 2020, 9:24:52 AM7/4/20
to sympy

Is there a way to compute the gradient and hessian matrices of a vector field like u = (x) i + (2yz) j + (3xy) k.

I know you didn't ask for this, but just in case it is of any help, here's how you do it in Cadabra (https://cadabra.science).
Cadabra uses Sympy under the hood, so it's at least a partial Sympy answer.

Set-up with:

   {x,y,z}::Coordinate;
   {i,j,k,l}::Indices(values={x,y,z});
   \partial{#}::PartialDerivative;
   rl:= { u_{x} = x, u_{y} = 2 y z, u_{z} = 3 x y };

The gradient can then be computed using:

   grad:= g_{i j} = \partial_{i}{ u_{j} };
   evaluate(grad, rl, rhsonly=True);

and the Hessian with:

   H:= H_{i j k} = \partial_{i j}{ u_{k} };
   evaluate(H, rl, rhsonly=True);

Hope this helps.

Kasper

Oscar Benjamin

unread,
Jul 4, 2020, 9:35:39 AM7/4/20
to sympy
Also Faisal just opened a pull request implementing this:

https://github.com/sympy/sympy/pull/19703

Does that look like what you expected? Feel free to comment on the
pull request if you have any suggestions.

Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/6f06c7ac-46d7-499f-af4d-392e9399be24o%40googlegroups.com.

Nicolas Guarin

unread,
Jul 4, 2020, 2:35:33 PM7/4/20
to sympy
You could also use a package that I wrote for that purpose

https://continuum-mechanics.readthedocs.io/en/latest/

If you are interested in Cartesian coordinates only you could just do the following

u = Matrix([x, 2*y*z, 3*x*y])
J = u.jacobian([x, y, z])
H = Array([J[:, k].jacobian([x, y, z]) for k in range(3)], (3, 3, 3))

to get

[[0  0  0]  [0  0  0]  [0  0  0]]
[[       ]  [       ]  [       ]]
[[0  0  0]  [0  0  2]  [0  2  0]]
[[       ]  [       ]  [       ]]
[[0  3  0]  [3  0  0]  [0  0  0]]

Reply all
Reply to author
Forward
0 new messages